52 lines
1.1 KiB
TypeScript
52 lines
1.1 KiB
TypeScript
interface NotificationPayload {
|
|
title: string;
|
|
message: string;
|
|
type?: "info" | "success" | "warning" | "error";
|
|
target_user?: number;
|
|
source?: string;
|
|
payload?: Record<string, any>;
|
|
}
|
|
|
|
interface NotificationEntity {
|
|
id: number;
|
|
title: string;
|
|
message: string;
|
|
type: string;
|
|
target_user?: number;
|
|
source?: string;
|
|
payload?: Record<string, any>;
|
|
read: boolean;
|
|
createdAt: string;
|
|
updatedAt: string;
|
|
}
|
|
|
|
/**
|
|
* notification service
|
|
*/
|
|
import { factories } from "@strapi/strapi";
|
|
|
|
export default factories.createCoreService(
|
|
"api::notification.notification",
|
|
({ strapi }: { strapi }) => ({
|
|
//Custom service for add notification.
|
|
async createNotification({
|
|
title,
|
|
message,
|
|
type = "info",
|
|
target_user,
|
|
source,
|
|
payload,
|
|
}: NotificationPayload): Promise<NotificationEntity> {
|
|
const data = { title, message, type, target_user, source, payload };
|
|
|
|
const notification = await strapi.entityService.create(
|
|
"api::notification.notification",
|
|
{ data }
|
|
);
|
|
|
|
strapi.log.info(`🔔 Notification créée: ${title}`);
|
|
return notification;
|
|
},
|
|
})
|
|
);
|