Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

77 actualización packagesjson #86

Merged
merged 21 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@
"type": "module",
"dependencies": {
"@lucia-auth/adapter-sqlite": "^3.0.2",
"@prisma/adapter-d1": "^5.18.0",
"@prisma/client": "5.18.0",
"@prisma/adapter-d1": "^5.19.1",
"@prisma/client": "^5.19.1",
"arctic": "^1.9.2",
"cf-webpush": "^1.1.4",
"lucia": "^3.2.0",
Expand Down
23 changes: 23 additions & 0 deletions sonar-project.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Nombre del proyecto
sonar.projectKey=donde-esta-tu-local
sonar.projectName=donde-esta-tu-local
sonar.projectVersion=1.0

# Ruta a la fuente del proyecto
sonar.sources=src

# Extensiones de archivo a analizar
sonar.inclusions=**/*.svelte,**/*.js,**/*.ts,**/*.html

# Configuración de TypeScript (si usas TypeScript)
sonar.ts.node.maxSpaceSize=4096

# Configuración de la codificación de caracteres
sonar.sourceEncoding=UTF-8

# Opcional: Configuración de pruebas
sonar.tests=tests
sonar.test.inclusions=**/*.spec.js,**/*.spec.ts

# Opcional: Configuración de exclusiones
sonar.exclusions=**/*.test.js,**/*.test.ts
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
NewNotificationForAdmins,
type Payload,
type NotificationExtraData
} from '$lib/utils/notification/notifications';
} from '$lib/notification/notifications';

export async function AddGang(
prisma: PrismaClient,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
NewNotificationForUsers,
type Payload,
type NotificationExtraData
} from '$lib/utils/notification/notifications';
} from '$lib/notification/notifications';

export async function RequestNewMember(prisma: PrismaClient, gangId: number, userId: string) {
const user = await prisma.user.findUnique({
Expand Down
81 changes: 81 additions & 0 deletions src/lib/notification/get-user-notifications.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { describe, it, expect, beforeAll, afterAll } from 'vitest';
import { getPrismaClient } from '$lib/tests/clientForTest';
import { GetUserNotifications, type UserNotifications } from './get-user-notifications';

const prisma = await getPrismaClient();

describe('get user notification fn', () => {
const USER_LOCAL = 'user|local';
const GANG1 = 'Gang 1';
let userNotifications: UserNotifications;

beforeAll(async () => {
// Create a PEDING notification for USER_LOCAL
await prisma.notification.create({
data: {
title: 'Test notification',
body: 'Test notification body',
status: 'PENDING',
users: {
connect: {
id: USER_LOCAL
}
}
}
});
// Create a VALIDATED notification for USER_LOCAL
await prisma.notification.create({
data: {
title: 'Test notification',
body: 'Test notification body',
status: 'VALIDATED',
users: {
connect: {
id: USER_LOCAL
}
}
}
});
// Create a REFUSED notification for USER_LOCAL
await prisma.notification.create({
data: {
title: 'Test notification',
body: 'Test notification body',
status: 'REFUSED',
users: {
connect: {
id: USER_LOCAL
}
}
}
});

userNotifications = await GetUserNotifications(prisma, USER_LOCAL);
});

afterAll(async () => {
// Remove notification from user|local
const notificationsToDelete = await prisma.notification.findMany({
where: { users: { some: { id: USER_LOCAL } } }
});
if (notificationsToDelete) {
for (const notificationToDelete of notificationsToDelete) {
await prisma.notification.delete({
where: { id: notificationToDelete.id }
});
}
}
await prisma.$disconnect();
});

it('user has gang', async () => {
expect(userNotifications.user).toHaveProperty('gang');
expect(userNotifications.user?.gang).toHaveProperty('name');
expect(userNotifications.user?.gang.name).toBe(GANG1);
});

it('user has notifications', async () => {
expect(userNotifications).toHaveProperty('notifications');
expect(userNotifications.notifications).toHaveLength(3);
});
});
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
import type { D1Database } from '@cloudflare/workers-types';
import { initializePrisma } from '$lib/server/db';
import { logger } from '$lib/server/logger';

import type { User, Gang, Notification, PrismaClient } from '@prisma/client';
import type { User, Notification, PrismaClient } from '@prisma/client';

interface UserNotifications {
user?: User;
user: User | null;
notifications: Notification[];
notificationsCount: number;
}

export { type UserNotifications };
export { type UserNotifications, GetUserNotifications };

export async function getUserNotifications(
userId: string,
db: D1Database
async function GetUserNotifications(
prisma: PrismaClient,
userId: string
): Promise<UserNotifications> {
const prisma = initializePrisma(db);

const user = await prisma.user.findUnique({
where: {
id: userId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ async function SubscribeUser(
});
}
if (sub && userIsLogged) {
const res = await fetch(Routes.notification_subscribe.url, {
const url = Routes.notification_subscribe.url as string;
await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
Expand All @@ -28,13 +29,13 @@ async function SubscribeUser(
// as value for applicationServerKey in pushManager.subscribe yet
// https://bugs.chromium.org/p/chromium/issues/detail?id=802280
function urlBase64ToUint8Array(base64String: string) {
var padding = '='.repeat((4 - (base64String.length % 4)) % 4);
var base64 = (base64String + padding).replace(/\-/g, '+').replace(/_/g, '/');
const padding = '='.repeat((4 - (base64String.length % 4)) % 4);
const base64 = (base64String + padding).replace(/-/g, '+').replace(/_/g, '/');

var rawData = window.atob(base64);
var outputArray = new Uint8Array(rawData.length);
const rawData = window.atob(base64);
const outputArray = new Uint8Array(rawData.length);

for (var i = 0; i < rawData.length; ++i) {
for (const i = 0; i < rawData.length; ++i) {
outputArray[i] = rawData.charCodeAt(i);
}
return outputArray;
Expand Down
66 changes: 66 additions & 0 deletions src/lib/notification/notifications.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { describe, it, expect, beforeAll, afterAll } from 'vitest';
import { getPrismaClient } from '$lib/tests/clientForTest';
import {
NewNotificationForAll,
NewNotificationForAdmins,
NewNotificationForUsers,
type Payload,
type NotificationExtraData
} from './notifications';
import type { User } from '@prisma/client';

const prisma = await getPrismaClient();

describe('new notifications', () => {
const USER_LOCAL = 'user|local';
const USER_ADMIN = 'admin|local';
const USER_NO_GANG = 'user|no-gang';

beforeAll(async () => {
const payload: Payload = {
title: 'Test notification',
body: 'Test notification body'
};

const extraData: NotificationExtraData = {
type: 'test-notification',
status: 'PENDING'
};
const user_admin = (await prisma.user.findUnique({
where: { id: USER_ADMIN }
})) as User;
const user_no_gang = (await prisma.user.findUnique({
where: { id: USER_NO_GANG }
})) as User;

await NewNotificationForAll(payload, extraData, USER_LOCAL, prisma);
await NewNotificationForAdmins(payload, extraData, prisma);
await NewNotificationForUsers(payload, extraData, [user_admin, user_no_gang], prisma);
});

afterAll(async () => {
await prisma.notification.deleteMany({
where: { title: 'Test notification' }
});
await prisma.$disconnect();
});

it('USER_ADMIN has 3 notifications', async () => {
const userNotifications = await prisma.notification.findMany({
where: {
title: 'Test notification',
users: { some: { id: USER_ADMIN } }
}
});
expect(userNotifications.length).toBe(3);
});
it('USER_NO_GANG has 2 notifications', async () => {
const userNotifications = await prisma.notification.findMany({
where: {
title: 'Test notification',
users: { some: { id: USER_NO_GANG } }
}
});
expect(userNotifications.length).toBe(2);
});
});
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { JWK } from '$env/static/private';
import { buildRequest, type PushSubscription } from 'cf-webpush';
import { logger } from '$lib/server/logger';
import type { User, Gang, Notification } from '@prisma/client';
import type { PrismaClient } from '@prisma/client';
import type { User, Gang, Notification, PrismaClient } from '@prisma/client';

export interface Payload {
title: string;
Expand Down
Loading