Skip to content

Commit

Permalink
feat: notifications without account (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
shoom3301 authored May 9, 2024
1 parent 96b0848 commit 2a9c09b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 38 deletions.
4 changes: 2 additions & 2 deletions src/api/notification/content-types/notification/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
"description": ""
},
"options": {
"draftAndPublish": true
"draftAndPublish": false
},
"pluginOptions": {},
"attributes": {
"account": {
"type": "string",
"required": true
"required": false
},
"data": {
"type": "json"
Expand Down
63 changes: 38 additions & 25 deletions src/api/notification/services/notification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,70 +8,83 @@ 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,
{
start: 0,
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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -11639,9 +11639,6 @@
],
"properties": {
"data": {
"required": [
"account"
],
"type": "object",
"properties": {
"account": {
Expand Down Expand Up @@ -11711,9 +11708,6 @@
},
"Notification": {
"type": "object",
"required": [
"account"
],
"properties": {
"account": {
"type": "string"
Expand Down Expand Up @@ -12428,10 +12422,6 @@
"type": "string",
"format": "date-time"
},
"publishedAt": {
"type": "string",
"format": "date-time"
},
"createdBy": {
"type": "object",
"properties": {
Expand Down

0 comments on commit 2a9c09b

Please sign in to comment.