From e18a3fa0923a412873d6870302c04e4aababaa3a Mon Sep 17 00:00:00 2001 From: julien vdb Date: Mon, 19 Jan 2026 00:07:21 +0100 Subject: [PATCH] 0.12.19 : better notifications --- package.json | 2 +- .../chat-message/controllers/chat-message.ts | 66 ++++++++++++++- .../content-types/notification/schema.json | 29 ++++--- src/api/notification/services/notification.ts | 8 +- .../1.0.0/full_documentation.json | 80 ++++++++++++------- types/generated/contentTypes.d.ts | 9 ++- 6 files changed, 144 insertions(+), 50 deletions(-) diff --git a/package.json b/package.json index a246eaf..181a23d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "harmony-back", - "version": "0.12.18", + "version": "0.12.19", "private": true, "description": "A Strapi application", "scripts": { diff --git a/src/api/chat-message/controllers/chat-message.ts b/src/api/chat-message/controllers/chat-message.ts index 0fd4c03..ed25636 100644 --- a/src/api/chat-message/controllers/chat-message.ts +++ b/src/api/chat-message/controllers/chat-message.ts @@ -75,10 +75,74 @@ export default factories.createCoreController( } const message = await strapi .service("api::chat-message.chat-message") - .createMessageInConversation(finalConversationId, userId, content, mediaId); + .createMessageInConversation( + finalConversationId, + userId, + content, + mediaId + ); + + const currentUser = await strapi.entityService.findOne( + "plugin::users-permissions.user", + userId, + { populate: { avatar: true } } + ); + + const conversation = await strapi.entityService.findOne( + "api::chat-conversation.chat-conversation", + finalConversationId, + { populate: { users: true } } + ); + + const conversationMembers = (conversation as any)?.users || []; + const today = new Date(); + today.setHours(0, 0, 0, 0); + + for (const member of conversationMembers) { + if (member.id === userId) continue; + + const existingNotifications = await strapi.entityService.findMany( + "api::notification.notification", + { + filters: { + $and: [ + { type: "messageSent" }, + { target_user: member.id }, + { floodId: finalConversationId }, + ], + }, + sort: { createdAt: "desc" }, + } + ); + + const hasNotificationToday = existingNotifications?.some( + (notification: any) => { + const notificationDate = new Date(notification.createdAt); + notificationDate.setHours(0, 0, 0, 0); + return notificationDate.getTime() === today.getTime(); + } + ); + + if (!hasNotificationToday) { + await strapi + .service("api::notification.notification") + ?.createNotification({ + message: `Tu as reçu un message de ${currentUser?.name}`, + type: "messageSent", + target_user: member.id, + source: "user", + floodId: finalConversationId, + payload: { + avatar: (currentUser as any)?.avatar?.url, + url: `/app/chat/${finalConversationId}`, + }, + }); + } + } ctx.body = { data: message, + conversationId: finalConversationId, }; } catch (error: any) { return ctx.badRequest(`Failed to create message: ${error.message}`); diff --git a/src/api/notification/content-types/notification/schema.json b/src/api/notification/content-types/notification/schema.json index 836ed7b..6c3d21b 100644 --- a/src/api/notification/content-types/notification/schema.json +++ b/src/api/notification/content-types/notification/schema.json @@ -4,26 +4,24 @@ "info": { "singularName": "notification", "pluralName": "notifications", - "displayName": "Notification" + "displayName": "Notification", + "description": "" }, "options": { "draftAndPublish": false }, "pluginOptions": {}, "attributes": { - "title": { - "type": "string" - }, "message": { "type": "string" }, "type": { "type": "enumeration", "enum": [ - "info", - "success", - "warning", - "error" + "accountCreation", + "friendRequest", + "messageSent", + "postCreated" ] }, "target_user": { @@ -35,11 +33,20 @@ "type": "boolean", "default": false }, - "source": { - "type": "string" - }, "payload": { "type": "json" + }, + "source": { + "type": "enumeration", + "enum": [ + "system", + "user", + "group", + "choral" + ] + }, + "floodId": { + "type": "integer" } } } diff --git a/src/api/notification/services/notification.ts b/src/api/notification/services/notification.ts index 6776b3b..94d50a3 100644 --- a/src/api/notification/services/notification.ts +++ b/src/api/notification/services/notification.ts @@ -4,6 +4,7 @@ interface NotificationPayload { type?: "info" | "success" | "warning" | "error"; target_user?: number; source?: string; + floodId?: number; payload?: Record; } @@ -14,6 +15,7 @@ interface NotificationEntity { type: string; target_user?: number; source?: string; + floodId?: number; payload?: Record; read: boolean; createdAt: string; @@ -38,21 +40,21 @@ export default factories.createCoreService( ({ strapi }: { strapi }) => ({ //Custom service for add notification. async createNotification({ - title, message, type = "info", target_user, source, + floodId, payload, }: NotificationPayload): Promise { - const data = { title, message, type, target_user, source, payload }; + const data = { message, type, target_user, source, floodId, payload }; const notification = await strapi.entityService.create( "api::notification.notification", { data } ); - strapi.log.info(`🔔 Notification créée: ${title}`); + strapi.log.info(`🔔 Notification créée: ${message}`); return notification; }, async addActivity({ diff --git a/src/extensions/documentation/documentation/1.0.0/full_documentation.json b/src/extensions/documentation/documentation/1.0.0/full_documentation.json index 5d3d4a8..40aaacf 100644 --- a/src/extensions/documentation/documentation/1.0.0/full_documentation.json +++ b/src/extensions/documentation/documentation/1.0.0/full_documentation.json @@ -14,7 +14,7 @@ "name": "Apache 2.0", "url": "https://www.apache.org/licenses/LICENSE-2.0.html" }, - "x-generation-date": "2026-01-16T10:01:51.447Z" + "x-generation-date": "2026-01-18T23:07:12.280Z" }, "x-strapi-config": { "plugins": [ @@ -108357,19 +108357,16 @@ "data": { "type": "object", "properties": { - "title": { - "type": "string" - }, "message": { "type": "string" }, "type": { "type": "string", "enum": [ - "info", - "success", - "warning", - "error" + "accountCreation", + "friendRequest", + "messageSent", + "postCreated" ] }, "target_user": { @@ -108386,10 +108383,19 @@ "read": { "type": "boolean" }, - "source": { - "type": "string" - }, "payload": {}, + "source": { + "type": "string", + "enum": [ + "system", + "user", + "group", + "choral" + ] + }, + "floodId": { + "type": "integer" + }, "locale": { "type": "string" }, @@ -108455,19 +108461,16 @@ "documentId": { "type": "string" }, - "title": { - "type": "string" - }, "message": { "type": "string" }, "type": { "type": "string", "enum": [ - "info", - "success", - "warning", - "error" + "accountCreation", + "friendRequest", + "messageSent", + "postCreated" ] }, "target_user": { @@ -112116,10 +112119,19 @@ "read": { "type": "boolean" }, - "source": { - "type": "string" - }, "payload": {}, + "source": { + "type": "string", + "enum": [ + "system", + "user", + "group", + "choral" + ] + }, + "floodId": { + "type": "integer" + }, "createdAt": { "type": "string", "format": "date-time" @@ -112168,19 +112180,16 @@ "documentId": { "type": "string" }, - "title": { - "type": "string" - }, "message": { "type": "string" }, "type": { "type": "string", "enum": [ - "info", - "success", - "warning", - "error" + "accountCreation", + "friendRequest", + "messageSent", + "postCreated" ] }, "target_user": { @@ -112197,10 +112206,19 @@ "read": { "type": "boolean" }, - "source": { - "type": "string" - }, "payload": {}, + "source": { + "type": "string", + "enum": [ + "system", + "user", + "group", + "choral" + ] + }, + "floodId": { + "type": "integer" + }, "createdAt": { "type": "string", "format": "date-time" diff --git a/types/generated/contentTypes.d.ts b/types/generated/contentTypes.d.ts index 8c0bb10..fbec076 100644 --- a/types/generated/contentTypes.d.ts +++ b/types/generated/contentTypes.d.ts @@ -1333,6 +1333,7 @@ export interface ApiNotificationNotification extends Struct.CollectionTypeSchema { collectionName: 'notifications'; info: { + description: ''; displayName: 'Notification'; pluralName: 'notifications'; singularName: 'notification'; @@ -1344,6 +1345,7 @@ export interface ApiNotificationNotification createdAt: Schema.Attribute.DateTime; createdBy: Schema.Attribute.Relation<'oneToOne', 'admin::user'> & Schema.Attribute.Private; + floodId: Schema.Attribute.Integer; locale: Schema.Attribute.String & Schema.Attribute.Private; localizations: Schema.Attribute.Relation< 'oneToMany', @@ -1354,13 +1356,14 @@ export interface ApiNotificationNotification payload: Schema.Attribute.JSON; publishedAt: Schema.Attribute.DateTime; read: Schema.Attribute.Boolean & Schema.Attribute.DefaultTo; - source: Schema.Attribute.String; + source: Schema.Attribute.Enumeration<['system', 'user', 'group', 'choral']>; target_user: Schema.Attribute.Relation< 'oneToOne', 'plugin::users-permissions.user' >; - title: Schema.Attribute.String; - type: Schema.Attribute.Enumeration<['info', 'success', 'warning', 'error']>; + type: Schema.Attribute.Enumeration< + ['accountCreation', 'friendRequest', 'messageSent', 'postCreated'] + >; updatedAt: Schema.Attribute.DateTime; updatedBy: Schema.Attribute.Relation<'oneToOne', 'admin::user'> & Schema.Attribute.Private;