63 lines
1.4 KiB
TypeScript
63 lines
1.4 KiB
TypeScript
import { prisma } from './prisma';
|
|
|
|
export interface CreateNotificationParams {
|
|
userId: string;
|
|
type: 'message' | 'trajet_created' | 'trajet_cancelled' | 'trajet_completed';
|
|
title: string;
|
|
message: string;
|
|
link?: string;
|
|
}
|
|
|
|
/**
|
|
* Crée une notification pour un utilisateur
|
|
*/
|
|
export async function createNotification(params: CreateNotificationParams) {
|
|
try {
|
|
await prisma.notification.create({
|
|
data: {
|
|
userId: params.userId,
|
|
type: params.type,
|
|
title: params.title,
|
|
message: params.message,
|
|
link: params.link || null,
|
|
},
|
|
});
|
|
} catch (error) {
|
|
console.error('Erreur lors de la création de la notification:', error);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Crée des notifications pour tous les utilisateurs sauf celui qui a déclenché l'action
|
|
*/
|
|
export async function createNotificationForAllUsers(
|
|
params: Omit<CreateNotificationParams, 'userId'>,
|
|
excludeUserId?: string
|
|
) {
|
|
try {
|
|
const users = await prisma.user.findMany({
|
|
where: excludeUserId
|
|
? {
|
|
id: {
|
|
not: excludeUserId,
|
|
},
|
|
}
|
|
: undefined,
|
|
select: {
|
|
id: true,
|
|
},
|
|
});
|
|
|
|
await Promise.all(
|
|
users.map((user) =>
|
|
createNotification({
|
|
...params,
|
|
userId: user.id,
|
|
})
|
|
)
|
|
);
|
|
} catch (error) {
|
|
console.error('Erreur lors de la création des notifications:', error);
|
|
}
|
|
}
|