Skip to content

Commit

Permalink
feat: remember last consumed notification (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
shoom3301 committed May 8, 2024
1 parent 6d88a04 commit 7869179
Show file tree
Hide file tree
Showing 10 changed files with 3,249 additions and 2,160 deletions.
47 changes: 43 additions & 4 deletions src/api/notification/controllers/notification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,58 @@
import { factories } from '@strapi/strapi'

const MODULE_ID = 'api::notification.notification'
const GLOBAL_MODULE_ID = 'api::notifications-consumer.notifications-consumer'
const SINGLETON_ID = 1

export default factories.createCoreController(MODULE_ID, ({ strapi }) => {
return {
async getNotificationList(context) {
const account = context.params.account

return strapi.service(MODULE_ID).getNotificationList(account, false)
return strapi.service(MODULE_ID).getNotificationList(account)
},

async getPushNotifications(context) {
const account = context.params.account
async getPushNotifications() {
const global = await strapi.entityService.findOne(GLOBAL_MODULE_ID, SINGLETON_ID, {
populate: ['id', 'lastConsumedNotificationDate']
})

const lastConsumedNotificationDate = global?.lastConsumedNotificationDate

const notifications = await strapi.entityService.findMany(
MODULE_ID,
{
limit: 200,
filters: {
notification_template: { push: true },
...(lastConsumedNotificationDate ? {
createdAt: {$gt: lastConsumedNotificationDate}
} : undefined)
},
populate: {
notification_template: {
fields: ['id', 'title', 'description', 'url', 'push'],
populate: {
thumbnail: {
fields: ['url']
}
}
}
}
}
)

if (notifications.length) {
await strapi.entityService.update(
GLOBAL_MODULE_ID,
SINGLETON_ID,
{
data: { lastConsumedNotificationDate: new Date() }
}
)
}

return strapi.service(MODULE_ID).getNotificationList(account, true)
return notifications
},
}
});
17 changes: 3 additions & 14 deletions src/api/notification/documentation/1.0.0/notification.json
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@
"operationId": "get/notification-list/{account}"
}
},
"/push-notifications/{account}": {
"/push-notifications": {
"get": {
"responses": {
"200": {
Expand Down Expand Up @@ -653,19 +653,8 @@
"tags": [
"Notification"
],
"parameters": [
{
"name": "account",
"in": "path",
"description": "",
"deprecated": false,
"required": true,
"schema": {
"type": "number"
}
}
],
"operationId": "get/push-notifications/{account}"
"parameters": [],
"operationId": "get/push-notifications"
}
}
}
2 changes: 1 addition & 1 deletion src/api/notification/routes/notification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const myExtraRoutes = [
},
{
method: 'GET',
path: '/push-notifications/:account',
path: '/push-notifications',
handler: 'notification.getPushNotifications',
config: {
policies: [],
Expand Down
4 changes: 2 additions & 2 deletions src/api/notification/services/notification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ const MODULE_ID = 'api::notification.notification'

export default factories.createCoreService(MODULE_ID, ({ strapi }) => {
return {
async getNotificationList(account: string, push: boolean) {
async getNotificationList(account: string) {
const notifications = await strapi.entityService.findMany(
MODULE_ID,
{
start: 0,
limit: 50,
filters: {
account,
notification_template: { push }
notification_template: { push: false }
},
populate: {
notification_template: {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"kind": "singleType",
"collectionName": "notifications_consumers",
"info": {
"singularName": "notifications-consumer",
"pluralName": "notifications-consumers",
"displayName": "NotificationsConsumer"
},
"options": {
"draftAndPublish": false
},
"pluginOptions": {},
"attributes": {
"lastConsumedNotificationDate": {
"type": "datetime"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* notifications-consumer controller
*/

import { factories } from '@strapi/strapi'

export default factories.createCoreController('api::notifications-consumer.notifications-consumer');
Loading

0 comments on commit 7869179

Please sign in to comment.