Skip to content

Commit 290f79b

Browse files
authored
refactoring: notifications (#30)
1 parent dff026d commit 290f79b

9 files changed

Lines changed: 227 additions & 49 deletions

File tree

src/api/notification-template/content-types/notification-template/schema.json

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,36 @@
88
"description": ""
99
},
1010
"options": {
11-
"draftAndPublish": true
11+
"draftAndPublish": false
1212
},
1313
"pluginOptions": {},
1414
"attributes": {
1515
"title": {
16-
"type": "string"
16+
"type": "string",
17+
"required": true
1718
},
1819
"description": {
19-
"type": "richtext"
20+
"type": "richtext",
21+
"required": true
2022
},
2123
"url": {
2224
"type": "string"
2325
},
2426
"push": {
25-
"type": "boolean"
27+
"type": "boolean",
28+
"default": false,
29+
"required": true
2630
},
2731
"dueDate": {
2832
"type": "datetime"
2933
},
3034
"thumbnail": {
35+
"type": "media",
36+
"multiple": false,
37+
"required": false,
3138
"allowedTypes": [
3239
"images"
33-
],
34-
"type": "media",
35-
"multiple": false
40+
]
3641
}
3742
}
3843
}

src/api/notification/documentation/1.0.0/notification.json

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,88 @@
586586
"operationId": "get/notification-list/{account}"
587587
}
588588
},
589+
"/accounts/{account}/notifications": {
590+
"get": {
591+
"responses": {
592+
"200": {
593+
"description": "OK",
594+
"content": {
595+
"application/json": {
596+
"schema": {
597+
"$ref": "#/components/schemas/NotificationResponse"
598+
}
599+
}
600+
}
601+
},
602+
"400": {
603+
"description": "Bad Request",
604+
"content": {
605+
"application/json": {
606+
"schema": {
607+
"$ref": "#/components/schemas/Error"
608+
}
609+
}
610+
}
611+
},
612+
"401": {
613+
"description": "Unauthorized",
614+
"content": {
615+
"application/json": {
616+
"schema": {
617+
"$ref": "#/components/schemas/Error"
618+
}
619+
}
620+
}
621+
},
622+
"403": {
623+
"description": "Forbidden",
624+
"content": {
625+
"application/json": {
626+
"schema": {
627+
"$ref": "#/components/schemas/Error"
628+
}
629+
}
630+
}
631+
},
632+
"404": {
633+
"description": "Not Found",
634+
"content": {
635+
"application/json": {
636+
"schema": {
637+
"$ref": "#/components/schemas/Error"
638+
}
639+
}
640+
}
641+
},
642+
"500": {
643+
"description": "Internal Server Error",
644+
"content": {
645+
"application/json": {
646+
"schema": {
647+
"$ref": "#/components/schemas/Error"
648+
}
649+
}
650+
}
651+
}
652+
},
653+
"tags": [
654+
"Notification"
655+
],
656+
"parameters": [
657+
{
658+
"name": "account",
659+
"in": "path",
660+
"description": "",
661+
"deprecated": false,
662+
"required": true,
663+
"schema": {
664+
"type": "number"
665+
}
666+
}
667+
],
668+
"operationId": "get/accounts/{account}/notifications"
669+
}
670+
},
589671
"/push-notifications": {
590672
"get": {
591673
"responses": {

src/api/notification/routes/notification.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const customRouter = (innerRouter, extraRoutes = []) => {
2121
};
2222

2323
const myExtraRoutes = [
24+
// TODO: For backward-compatibility, remove after August 2024
2425
{
2526
method: 'GET',
2627
path: '/notification-list/:account',
@@ -30,6 +31,15 @@ const myExtraRoutes = [
3031
middlewares: [],
3132
},
3233
},
34+
{
35+
method: 'GET',
36+
path: '/accounts/:account/notifications',
37+
handler: 'notification.getNotificationList',
38+
config: {
39+
policies: [],
40+
middlewares: [],
41+
},
42+
},
3343
{
3444
method: 'GET',
3545
path: '/push-notifications',

src/api/notification/services/notification.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
import { factories } from '@strapi/strapi';
66

77
const MODULE_ID = 'api::notification.notification'
8-
const GLOBAL_MODULE_ID = 'api::notifications-consumer.notifications-consumer'
8+
const NOTIFICATIONS_CONSUMER_MODULE_ID = 'api::notifications-consumer.notifications-consumer'
99
const SINGLETON_ID = 1
10+
const NOTIFICATIONS_LIMIT = 50
1011

1112
const NOTIFICATIONS_POPULATE = {
1213
notification_template: {
@@ -39,7 +40,7 @@ export default factories.createCoreService(MODULE_ID, ({ strapi }) => {
3940
MODULE_ID,
4041
{
4142
start: 0,
42-
limit: 50,
43+
limit: NOTIFICATIONS_LIMIT,
4344
filters: {
4445
account: { $null: true },
4546
notification_template: notificationsTemplateFilter(push)
@@ -55,7 +56,7 @@ export default factories.createCoreService(MODULE_ID, ({ strapi }) => {
5556
MODULE_ID,
5657
{
5758
start: 0,
58-
limit: 50,
59+
limit: NOTIFICATIONS_LIMIT,
5960
filters: {
6061
$or: [
6162
{ account, notification_template: templateFilter },
@@ -80,11 +81,11 @@ export default factories.createCoreService(MODULE_ID, ({ strapi }) => {
8081
},
8182
async getPushNotifications() {
8283
const push = true
83-
const global = await strapi.entityService.findOne(GLOBAL_MODULE_ID, SINGLETON_ID, {
84+
const notificationsConsumer = await strapi.entityService.findOne(NOTIFICATIONS_CONSUMER_MODULE_ID, SINGLETON_ID, {
8485
populate: ['id', 'lastConsumedNotificationDate']
8586
})
8687

87-
const lastConsumedNotificationDate = global?.lastConsumedNotificationDate
88+
const lastConsumedNotificationDate = notificationsConsumer?.lastConsumedNotificationDate
8889

8990
return strapi.entityService.findMany(
9091
MODULE_ID,
@@ -102,7 +103,7 @@ export default factories.createCoreService(MODULE_ID, ({ strapi }) => {
102103
},
103104
updateLastConsumedNotificationDate() {
104105
return strapi.entityService.update(
105-
GLOBAL_MODULE_ID,
106+
NOTIFICATIONS_CONSUMER_MODULE_ID,
106107
SINGLETON_ID,
107108
{
108109
data: { lastConsumedNotificationDate: new Date() }

src/api/telegram-subscription/content-types/telegram-subscription/schema.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"description": ""
99
},
1010
"options": {
11-
"draftAndPublish": true
11+
"draftAndPublish": false
1212
},
1313
"pluginOptions": {},
1414
"attributes": {
@@ -17,19 +17,19 @@
1717
"required": true,
1818
"unique": false
1919
},
20-
"auth_date": {
20+
"authDate": {
2121
"type": "biginteger"
2222
},
23-
"first_name": {
23+
"firstName": {
2424
"type": "string"
2525
},
2626
"hash": {
2727
"type": "string"
2828
},
29-
"chat_id": {
29+
"chatId": {
3030
"type": "biginteger"
3131
},
32-
"photo_url": {
32+
"photoUrl": {
3333
"type": "string"
3434
},
3535
"username": {

src/api/telegram-subscription/controllers/telegram-subscription.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export default factories.createCoreController(MODULE_ID, ({strapi}) => {
1515

1616
const service = strapi.service(MODULE_ID)
1717

18-
const existing = await strapi.entityService.findMany(MODULE_ID, { filters: { account, chat_id: data.id } })
18+
const existing = await strapi.entityService.findMany(MODULE_ID, { filters: { account, chatId: data.id } })
1919

2020
if (existing.length > 0) return true
2121

src/api/telegram-subscription/routes/telegram-subscription.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const customRouter = (innerRouter, extraRoutes = []) => {
2222
const myExtraRoutes = [
2323
{
2424
method: 'GET',
25-
path: '/tg-subscriptions',
25+
path: '/subscriptions/telegram',
2626
handler: 'telegram-subscription.getSubscriptions',
2727
config: {
2828
policies: [],
@@ -31,7 +31,7 @@ const myExtraRoutes = [
3131
},
3232
{
3333
method: 'GET',
34-
path: '/tg-subscriptions/:account',
34+
path: '/accounts/:account/subscriptions/telegram',
3535
handler: 'telegram-subscription.getAccountSubscriptions',
3636
config: {
3737
policies: [],

src/api/telegram-subscription/services/telegram-subscription.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ export default factories.createCoreService(MODULE_ID, ({strapi}) => {
3434
{
3535
data: {
3636
account,
37-
auth_date: data.auth_date,
38-
first_name: data.first_name,
37+
authDate: data.auth_date,
38+
firstName: data.first_name,
3939
hash: data.hash,
40-
chat_id: data.id,
41-
photo_url: data.photo_url,
40+
chatId: data.id,
41+
photoUrl: data.photo_url,
4242
username: data.username,
4343
}
4444
})
@@ -52,7 +52,7 @@ export default factories.createCoreService(MODULE_ID, ({strapi}) => {
5252
$in: accounts
5353
}
5454
},
55-
fields: ['id', 'account', 'chat_id']
55+
fields: ['id', 'account', 'chatId']
5656
}
5757
)
5858
},
@@ -63,7 +63,7 @@ export default factories.createCoreService(MODULE_ID, ({strapi}) => {
6363
filters: {
6464
account
6565
},
66-
fields: ['id', 'account', 'chat_id']
66+
fields: ['id', 'account', 'chatId']
6767
}
6868
)
6969
},
@@ -83,7 +83,7 @@ export default factories.createCoreService(MODULE_ID, ({strapi}) => {
8383
'Content-Type': 'application/json'
8484
},
8585
body: JSON.stringify({
86-
chat_id: subscription.chat_id,
86+
chat_id: subscription.chatId,
8787
text: templateNotification(notification.notification_template.description, notification.data)
8888
})
8989
})

0 commit comments

Comments
 (0)