Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the plan for the hardcoded 50

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would even keep it as it is.
We could add pagination and so on, but...will we really need it?
I think the main use-case of the notifications is to notify users about something new which is relevant only for the nearest feature.
As a user I would read even only the last 3-5 notifications.
But this is only my assumption, I'm open for any proposal.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, lets keep it for now them

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