Skip to content

Commit 8c9971f

Browse files
committed
chore(condo): DOMA-12992 small review fix and rebase with new adapter
1 parent 4e42cec commit 8c9971f

File tree

3 files changed

+28
-13
lines changed

3 files changed

+28
-13
lines changed

apps/condo/domains/notification/adapters/oneSignalAdapter.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -148,14 +148,14 @@ class OneSignalAdapter {
148148
* Prepares notification for either/both sending to FireBase and/or emulation if FAKE tokens present
149149
* Converts single notification to notifications array (for multiple tokens provided) for batch request
150150
* @param notificationRaw
151-
* @param data
151+
* @param dataByToken
152152
* @param tokens
153153
* @param pushTypes
154154
* @param isVoIP
155155
* @param appIds
156156
* @returns {[import('@onesignal/node-onesignal').Notification[], import('@onesignal/node-onesignal').Notification[], {}]}
157157
*/
158-
static prepareBatchData (notificationRaw, data, tokens = [], pushTypes = {}, isVoIP = false, appIds = {}) {
158+
static prepareBatchData (notificationRaw, dataByToken, tokens = [], pushTypes = {}, isVoIP = false, appIds = {}) {
159159
const notification = OneSignalAdapter.validateAndPrepareNotification(notificationRaw)
160160
const notifications = []
161161
const fakeNotifications = []
@@ -165,9 +165,11 @@ class OneSignalAdapter {
165165
const isFakeToken = pushToken.startsWith(PUSH_FAKE_TOKEN_SUCCESS) || pushToken.startsWith(PUSH_FAKE_TOKEN_FAIL)
166166
const target = isFakeToken ? fakeNotifications : notifications
167167
const pushType = pushTypes[pushToken] || PUSH_TYPE_DEFAULT
168-
const preparedData = OneSignalAdapter.prepareData(data)
169168
const appId = appIds[pushToken]
170169

170+
const preparedData = dataByToken[pushToken]
171+
if (!preparedData) return
172+
171173
const pushData = pushType === PUSH_TYPE_SILENT_DATA
172174
? {
173175
token: pushToken,
@@ -252,19 +254,19 @@ class OneSignalAdapter {
252254
* Sends notifications in parallel through provider for each appId + unique notification body
253255
* @param notification
254256
* @param tokens
255-
* @param data
257+
* @param dataByToken
256258
* @param pushTypes
257259
* @param appIds - Object mapping pushToken to appId for routing notifications to the correct OneSignal app
258260
* @param metaByToken
259261
* @param isVoIP
260262
* @returns {Promise<null|(boolean|T|{state: string, error: *})[]>}
261263
*/
262-
async sendNotification ({ notification, data, tokens, pushTypes, appIds, metaByToken } = {}, isVoIP = false) {
264+
async sendNotification ({ notification, dataByToken, tokens, pushTypes, appIds, metaByToken } = {}, isVoIP = false) {
263265
if (!tokens || isEmpty(tokens)) return [false, { error: 'No pushTokens available.' }]
264266

265267
const [notifications, fakeNotifications, pushContext] = OneSignalAdapter.prepareBatchData(
266268
notification,
267-
data,
269+
dataByToken,
268270
tokens,
269271
pushTypes,
270272
isVoIP,

apps/condo/domains/notification/utils/serverSchema/push/encryption.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@
1111
*/
1212

1313
/**
14-
* @typedef Encryptor
15-
* @type {(data: Record<string, unknown>, options?: EncryptorOptions) => EncryptorResult}
14+
* @func Encryptor
15+
* @param data {Record<string, unknown>}
16+
* @param options {EncryptorOptions | undefined}
17+
* @returns EncryptorResult
1618
*/
1719

18-
/** @type {Encryptor} */
19-
const v1 = (data, options) => {
20+
function v1 (data, options) {
2021
if (!options?.appId?.length) return null
22+
if (!data) return null
2123
const buf = Buffer.from(JSON.stringify(data), 'utf8')
2224
const keyBuf = Buffer.from(options.appId, 'utf8')
2325
const encryptedData = Buffer.from(buf.map((b, i) => b ^ keyBuf[i % keyBuf.length])).toString('base64')
@@ -38,7 +40,11 @@ const VERSIONS = {
3840
*/
3941
function encryptPushData (version, data, options = {}) {
4042
if (!VERSIONS[version]) return null
41-
return VERSIONS[version](data, options)
43+
try {
44+
return VERSIONS[version](data, options)
45+
} catch {
46+
return null
47+
}
4248
}
4349

4450
module.exports = {

apps/condo/domains/notification/utils/serverSchema/push/helpers.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,14 @@ const isEmpty = require('lodash/isEmpty')
22

33
const { find } = require('@open-condo/keystone/schema')
44

5-
const { PUSH_TRANSPORT_FIREBASE, PUSH_TRANSPORT_REDSTORE, PUSH_TRANSPORT_HUAWEI, PUSH_TRANSPORT_APPLE,
6-
PUSH_TRANSPORT_WEBHOOK, PUSH_TRANSPORT_TYPES,
5+
const {
6+
PUSH_TRANSPORT_FIREBASE,
7+
PUSH_TRANSPORT_REDSTORE,
8+
PUSH_TRANSPORT_HUAWEI,
9+
PUSH_TRANSPORT_APPLE,
10+
PUSH_TRANSPORT_WEBHOOK,
11+
PUSH_TRANSPORT_ONESIGNAL,
12+
PUSH_TRANSPORT_TYPES,
713
} = require('@condo/domains/notification/constants/constants')
814

915
/**
@@ -50,6 +56,7 @@ async function getTokens (ownerId, remoteClientId, isVoIP = false) {
5056
[PUSH_TRANSPORT_HUAWEI]: [],
5157
[PUSH_TRANSPORT_APPLE]: [],
5258
[PUSH_TRANSPORT_WEBHOOK]: [],
59+
[PUSH_TRANSPORT_ONESIGNAL]: [],
5360
}
5461
const pushTypes = {}
5562
const appIds = {}

0 commit comments

Comments
 (0)