diff --git a/src/api/notification/content-types/notification/schema.json b/src/api/notification/content-types/notification/schema.json index 3965e1f..5842a4c 100644 --- a/src/api/notification/content-types/notification/schema.json +++ b/src/api/notification/content-types/notification/schema.json @@ -8,13 +8,13 @@ "description": "" }, "options": { - "draftAndPublish": true + "draftAndPublish": false }, "pluginOptions": {}, "attributes": { "account": { "type": "string", - "required": true + "required": false }, "data": { "type": "json" diff --git a/src/api/notification/services/notification.ts b/src/api/notification/services/notification.ts index 4534d9a..931efdb 100644 --- a/src/api/notification/services/notification.ts +++ b/src/api/notification/services/notification.ts @@ -8,9 +8,35 @@ const MODULE_ID = 'api::notification.notification' const GLOBAL_MODULE_ID = 'api::notifications-consumer.notifications-consumer' const SINGLETON_ID = 1 +const NOTIFICATIONS_POPULATE = { + notification_template: { + fields: ['id', 'title', 'description', 'url', 'push'], + populate: { + thumbnail: { + fields: ['url'] + } + } + } +} + export default factories.createCoreService(MODULE_ID, ({ strapi }) => { return { + async getNotificationsForAll(push: boolean) { + return strapi.entityService.findMany( + MODULE_ID, + { + start: 0, + limit: 50, + filters: { + account: { $null: true }, + notification_template: { push } + }, + populate: NOTIFICATIONS_POPULATE + } + ) + }, async getNotificationList(account: string) { + const push = false const notifications = await strapi.entityService.findMany( MODULE_ID, { @@ -18,60 +44,47 @@ export default factories.createCoreService(MODULE_ID, ({ strapi }) => { limit: 50, filters: { account, - notification_template: { push: false } + notification_template: { push } }, - populate: { - notification_template: { - fields: ['id', 'title', 'description', 'url', 'push'], - populate: { - thumbnail: { - fields: ['url'] - } - } - } - } + populate: NOTIFICATIONS_POPULATE } ) + const notificationsForAll = await this.getNotificationsForAll(push) - return notifications.map(notification => ({ + return [...notifications, ...notificationsForAll].map(notification => ({ id: notification.id, account: notification.account, title: notification.notification_template.title, description: templateNotification(notification.notification_template.description, notification.data), url: notification.notification_template.url, createdAt: notification.createdAt, - thumbnail: notification.notification_template.thumbnail.url + thumbnail: notification.notification_template.thumbnail?.url })) }, async getPushNotifications() { + const push = true const global = await strapi.entityService.findOne(GLOBAL_MODULE_ID, SINGLETON_ID, { populate: ['id', 'lastConsumedNotificationDate'] }) const lastConsumedNotificationDate = global?.lastConsumedNotificationDate - return strapi.entityService.findMany( + const notificationsForAll = await this.getNotificationsForAll(push) + const notifications = await strapi.entityService.findMany( MODULE_ID, { limit: 200, filters: { - notification_template: { push: true }, + notification_template: { push }, ...(lastConsumedNotificationDate ? { createdAt: {$gt: lastConsumedNotificationDate} } : undefined) }, - populate: { - notification_template: { - fields: ['id', 'title', 'description', 'url', 'push'], - populate: { - thumbnail: { - fields: ['url'] - } - } - } - } + populate: NOTIFICATIONS_POPULATE } ) + + return [notifications, notificationsForAll] }, updateLastConsumedNotificationDate() { return strapi.entityService.update( 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 c946d8c..5d44cca 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": "2024-05-09T06:57:04.271Z" + "x-generation-date": "2024-05-09T07:14:54.538Z" }, "x-strapi-config": { "path": "/documentation", @@ -11639,9 +11639,6 @@ ], "properties": { "data": { - "required": [ - "account" - ], "type": "object", "properties": { "account": { @@ -11711,9 +11708,6 @@ }, "Notification": { "type": "object", - "required": [ - "account" - ], "properties": { "account": { "type": "string" @@ -12428,10 +12422,6 @@ "type": "string", "format": "date-time" }, - "publishedAt": { - "type": "string", - "format": "date-time" - }, "createdBy": { "type": "object", "properties": {