Skip to content

Commit

Permalink
refactoring: notifications (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
shoom3301 authored Jun 20, 2024
1 parent dff026d commit 290f79b
Show file tree
Hide file tree
Showing 9 changed files with 227 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,36 @@
"description": ""
},
"options": {
"draftAndPublish": true
"draftAndPublish": false
},
"pluginOptions": {},
"attributes": {
"title": {
"type": "string"
"type": "string",
"required": true
},
"description": {
"type": "richtext"
"type": "richtext",
"required": true
},
"url": {
"type": "string"
},
"push": {
"type": "boolean"
"type": "boolean",
"default": false,
"required": true
},
"dueDate": {
"type": "datetime"
},
"thumbnail": {
"type": "media",
"multiple": false,
"required": false,
"allowedTypes": [
"images"
],
"type": "media",
"multiple": false
]
}
}
}
82 changes: 82 additions & 0 deletions src/api/notification/documentation/1.0.0/notification.json
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,88 @@
"operationId": "get/notification-list/{account}"
}
},
"/accounts/{account}/notifications": {
"get": {
"responses": {
"200": {
"description": "OK",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/NotificationResponse"
}
}
}
},
"400": {
"description": "Bad Request",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"401": {
"description": "Unauthorized",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"403": {
"description": "Forbidden",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"404": {
"description": "Not Found",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"500": {
"description": "Internal Server Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
}
},
"tags": [
"Notification"
],
"parameters": [
{
"name": "account",
"in": "path",
"description": "",
"deprecated": false,
"required": true,
"schema": {
"type": "number"
}
}
],
"operationId": "get/accounts/{account}/notifications"
}
},
"/push-notifications": {
"get": {
"responses": {
Expand Down
10 changes: 10 additions & 0 deletions src/api/notification/routes/notification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const customRouter = (innerRouter, extraRoutes = []) => {
};

const myExtraRoutes = [
// TODO: For backward-compatibility, remove after August 2024
{
method: 'GET',
path: '/notification-list/:account',
Expand All @@ -30,6 +31,15 @@ const myExtraRoutes = [
middlewares: [],
},
},
{
method: 'GET',
path: '/accounts/:account/notifications',
handler: 'notification.getNotificationList',
config: {
policies: [],
middlewares: [],
},
},
{
method: 'GET',
path: '/push-notifications',
Expand Down
13 changes: 7 additions & 6 deletions src/api/notification/services/notification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
import { factories } from '@strapi/strapi';

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

const NOTIFICATIONS_POPULATE = {
notification_template: {
Expand Down Expand Up @@ -39,7 +40,7 @@ export default factories.createCoreService(MODULE_ID, ({ strapi }) => {
MODULE_ID,
{
start: 0,
limit: 50,
limit: NOTIFICATIONS_LIMIT,
filters: {
account: { $null: true },
notification_template: notificationsTemplateFilter(push)
Expand All @@ -55,7 +56,7 @@ export default factories.createCoreService(MODULE_ID, ({ strapi }) => {
MODULE_ID,
{
start: 0,
limit: 50,
limit: NOTIFICATIONS_LIMIT,
filters: {
$or: [
{ account, notification_template: templateFilter },
Expand All @@ -80,11 +81,11 @@ export default factories.createCoreService(MODULE_ID, ({ strapi }) => {
},
async getPushNotifications() {
const push = true
const global = await strapi.entityService.findOne(GLOBAL_MODULE_ID, SINGLETON_ID, {
const notificationsConsumer = await strapi.entityService.findOne(NOTIFICATIONS_CONSUMER_MODULE_ID, SINGLETON_ID, {
populate: ['id', 'lastConsumedNotificationDate']
})

const lastConsumedNotificationDate = global?.lastConsumedNotificationDate
const lastConsumedNotificationDate = notificationsConsumer?.lastConsumedNotificationDate

return strapi.entityService.findMany(
MODULE_ID,
Expand All @@ -102,7 +103,7 @@ export default factories.createCoreService(MODULE_ID, ({ strapi }) => {
},
updateLastConsumedNotificationDate() {
return strapi.entityService.update(
GLOBAL_MODULE_ID,
NOTIFICATIONS_CONSUMER_MODULE_ID,
SINGLETON_ID,
{
data: { lastConsumedNotificationDate: new Date() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"description": ""
},
"options": {
"draftAndPublish": true
"draftAndPublish": false
},
"pluginOptions": {},
"attributes": {
Expand All @@ -17,19 +17,19 @@
"required": true,
"unique": false
},
"auth_date": {
"authDate": {
"type": "biginteger"
},
"first_name": {
"firstName": {
"type": "string"
},
"hash": {
"type": "string"
},
"chat_id": {
"chatId": {
"type": "biginteger"
},
"photo_url": {
"photoUrl": {
"type": "string"
},
"username": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default factories.createCoreController(MODULE_ID, ({strapi}) => {

const service = strapi.service(MODULE_ID)

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

if (existing.length > 0) return true

Expand Down
4 changes: 2 additions & 2 deletions src/api/telegram-subscription/routes/telegram-subscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const customRouter = (innerRouter, extraRoutes = []) => {
const myExtraRoutes = [
{
method: 'GET',
path: '/tg-subscriptions',
path: '/subscriptions/telegram',
handler: 'telegram-subscription.getSubscriptions',
config: {
policies: [],
Expand All @@ -31,7 +31,7 @@ const myExtraRoutes = [
},
{
method: 'GET',
path: '/tg-subscriptions/:account',
path: '/accounts/:account/subscriptions/telegram',
handler: 'telegram-subscription.getAccountSubscriptions',
config: {
policies: [],
Expand Down
14 changes: 7 additions & 7 deletions src/api/telegram-subscription/services/telegram-subscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ export default factories.createCoreService(MODULE_ID, ({strapi}) => {
{
data: {
account,
auth_date: data.auth_date,
first_name: data.first_name,
authDate: data.auth_date,
firstName: data.first_name,
hash: data.hash,
chat_id: data.id,
photo_url: data.photo_url,
chatId: data.id,
photoUrl: data.photo_url,
username: data.username,
}
})
Expand All @@ -52,7 +52,7 @@ export default factories.createCoreService(MODULE_ID, ({strapi}) => {
$in: accounts
}
},
fields: ['id', 'account', 'chat_id']
fields: ['id', 'account', 'chatId']
}
)
},
Expand All @@ -63,7 +63,7 @@ export default factories.createCoreService(MODULE_ID, ({strapi}) => {
filters: {
account
},
fields: ['id', 'account', 'chat_id']
fields: ['id', 'account', 'chatId']
}
)
},
Expand All @@ -83,7 +83,7 @@ export default factories.createCoreService(MODULE_ID, ({strapi}) => {
'Content-Type': 'application/json'
},
body: JSON.stringify({
chat_id: subscription.chat_id,
chat_id: subscription.chatId,
text: templateNotification(notification.notification_template.description, notification.data)
})
})
Expand Down
Loading

0 comments on commit 290f79b

Please sign in to comment.