Skip to content

Commit 5fcf3d3

Browse files
authored
feat(condo): DOMA-13021 move work with push tokens to new model (#7370)
* feat(condo): DOMA-13051 add RemoteClientPushToken model * feat(condo): DOMA-13021 make new model for tokens synchronization * feat(condo): DOMA-13021 tokens synchronization seems complete * feat(condo): DOMA-13021 using pushtokens to send push messages * feat(condo): DOMA-13021 some more validation for pushTokens in sync * fix(condo): DOMA-13021 more tests * feat(condo): DOMA-13021 rebases * fix(condo): DOMA-13021 after review fixes * fix(condo): DOMA-13021 fix tests * fix(condo): DOMA-13021 forgotten file * fix(condo): DOMA-13021 after review fixes * fix(condo): DOMA-13021 after review fixes * test(condo): DOMA-13021 after review fixes * fix(condo): DOMA-13021 fix some unused util * fix(condo): DOMA-13021 fix linter
1 parent 1681ec0 commit 5fcf3d3

File tree

13 files changed

+1052
-295
lines changed

13 files changed

+1052
-295
lines changed

apps/condo/domains/notification/constants/constants.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,9 @@ const DIRECTLY_AVAILABLE_TYPES = [
199199

200200
const SMS_FORBIDDEN_SYMBOLS_REGEXP = /[&#|«»]+/gim
201201

202+
const MAXIMUM_PUSH_TOKENS_COUNT_IN_SYNC_REMOTE_CLIENT = 50
203+
const MAXIMUM_RAW_PUSH_TOKENS_COUNT_IN_SYNC_REMOTE_CLIENT = 100
204+
202205
const newsItemMessageMeta = {
203206
dv: { required: true },
204207
title: { required: true },
@@ -1276,5 +1279,7 @@ module.exports = {
12761279
ONESIGNAL_CONFIG_ENV,
12771280
PUSH_TRANSPORT_ONESIGNAL,
12781281
REMOTE_CLIENT_GROUP_UNGROUPED,
1282+
MAXIMUM_PUSH_TOKENS_COUNT_IN_SYNC_REMOTE_CLIENT,
1283+
MAXIMUM_RAW_PUSH_TOKENS_COUNT_IN_SYNC_REMOTE_CLIENT,
12791284
}
12801285

apps/condo/domains/notification/constants/errors.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const EMPTY_ONESIGNAL_CONFIG_ERROR = `Valid ${ONESIGNAL_CONFIG_ENV} should be pr
2020
const UNUSABLE_TOKEN_PROVIDED = 'UNUSABLE_TOKEN_PROVIDED'
2121
const INVALID_PUSH_TOKEN = 'INVALID_PUSH_TOKEN'
2222
const TOO_MANY_TOKENS_FOR_TRANSPORT = 'TOO_MANY_TOKENS_FOR_TRANSPORT'
23+
const TOO_MANY_TOKENS = 'TOO_MANY_TOKENS'
2324
const DEVICE_KEY_VALIDATION_ERROR = 'DEVICE_KEY_VALIDATION_ERROR'
2425
const INVALID_DEVICE_KEY = 'INVALID_DEVICE_KEY'
2526

@@ -50,6 +51,7 @@ module.exports = {
5051
UNUSABLE_TOKEN_PROVIDED,
5152
INVALID_PUSH_TOKEN,
5253
TOO_MANY_TOKENS_FOR_TRANSPORT,
54+
TOO_MANY_TOKENS,
5355
DEVICE_KEY_VALIDATION_ERROR,
5456
INVALID_DEVICE_KEY,
5557
}

apps/condo/domains/notification/helpers/sendRemoteClientsUpgradeAppNotifications.js

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
*/
44

55
const dayjs = require('dayjs')
6-
const { isEmpty } = require('lodash')
6+
const isEmpty = require('lodash/isEmpty')
77

88
const conf = require('@open-condo/config')
99
const { getLogger } = require('@open-condo/keystone/logging')
1010
const { getSchemaCtx } = require('@open-condo/keystone/schema')
1111

1212
const { DATE_FORMAT } = require('@condo/domains/common/utils/date')
1313
const { RESIDENT_UPGRADE_APP_TYPE, STAFF_UPGRADE_APP_TYPE, PUSH_TRANSPORT_FIREBASE } = require('@condo/domains/notification/constants/constants')
14-
const { sendMessage, RemoteClient } = require('@condo/domains/notification/utils/serverSchema')
14+
const { sendMessage, RemoteClientPushToken } = require('@condo/domains/notification/utils/serverSchema')
1515

1616
const TODAY = dayjs().format(DATE_FORMAT)
1717
const CHUNK_SIZE = 50
@@ -50,36 +50,38 @@ const prepareAndSendNotification = async (context, remoteClient) => {
5050

5151
const sendRemoteClientsUpgradeAppNotifications = async (where = {}) => {
5252
const { keystone: context } = getSchemaCtx('RemoteClient')
53-
const remoteClientWhere = { ...where, appId: 'unknown', owner: { id_not: null }, pushToken_not: null, pushTransport: PUSH_TRANSPORT_FIREBASE }
54-
const remoteClientsCount = await RemoteClient.count(context, remoteClientWhere)
53+
const remoteClientPushTokensWhere = { remoteClient: { ...where, appId: 'unknown', owner: { id_not: null } }, isPush: true, provider: PUSH_TRANSPORT_FIREBASE, deletedAt: null }
54+
const remoteClientPushTokensCount = await RemoteClientPushToken.count(context, remoteClientPushTokensWhere)
55+
//const remoteClientWhere = { ...where, appId: 'unknown', owner: { id_not: null }, pushToken_not: null, pushTransport: PUSH_TRANSPORT_FIREBASE }
56+
//const remoteClientsCount = await RemoteClient.count(context, remoteClientWhere)
5557
let skip = 0, successCnt = 0
5658

5759
logger.info({
58-
msg: 'available obsolete remote clients',
59-
count: remoteClientsCount,
60-
data: remoteClientWhere,
60+
msg: 'available obsolete remote clients push tokens',
61+
count: remoteClientPushTokensCount,
62+
data: remoteClientPushTokensWhere,
6163
})
6264

63-
if (!remoteClientsCount) return
65+
if (!remoteClientPushTokensCount) return
6466

65-
while (skip < remoteClientsCount) {
66-
const remoteClients = await RemoteClient.getAll(context,
67-
remoteClientWhere,
68-
'id owner { id type }',
67+
while (skip < remoteClientPushTokensCount) {
68+
const remoteClientsPushTokens = await RemoteClientPushToken.getAll(context,
69+
remoteClientPushTokensWhere,
70+
'remoteClient { id owner { id type } }',
6971
{ sortBy: ['createdAt_ASC'], first: CHUNK_SIZE, skip }
7072
)
7173

72-
if (isEmpty(remoteClients)) break
74+
if (isEmpty(remoteClientsPushTokens)) break
7375

74-
skip += remoteClients.length
76+
skip += remoteClientsPushTokens.length
7577

76-
for (const remoteClient of remoteClients) {
77-
const success = await prepareAndSendNotification(context, remoteClient)
78+
for (const remoteClientPushToken of remoteClientsPushTokens) {
79+
const success = await prepareAndSendNotification(context, remoteClientPushToken.remoteClient)
7880
successCnt += success
7981
}
8082
}
8183

82-
logger.info({ msg: 'notifications sent', data: { successCnt, attempts: remoteClientsCount } })
84+
logger.info({ msg: 'notifications sent', data: { successCnt, attempts: remoteClientPushTokensCount } })
8385
}
8486

8587
module.exports = {

apps/condo/domains/notification/helpers/sendRemoteClientsUpgradeAppNotifications.spec.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ describe('sendResidentsNoAccountNotifications', () => {
4141
type: RESIDENT_UPGRADE_APP_TYPE,
4242
uniqKey: notificationKey,
4343
}
44+
const lastMessages = await Message.getAll(admin, {}, { first: 2 })
45+
console.error(JSON.stringify(lastMessages, null, 2))
4446
const message = await Message.getOne(admin, messageWhere)
4547

4648
expect(message).toBeDefined()

0 commit comments

Comments
 (0)