Skip to content

Commit

Permalink
feat(user-notifications): Send notificationId in push notifications (#…
Browse files Browse the repository at this point in the history
…15962)

So the notification can be easily marked as read/seen when the user taps on the push notification.

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
eirikurn and kodiakhq[bot] authored Sep 16, 2024
1 parent 46a5b69 commit 02e8a3c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ export class NotificationDispatchService {
notification,
nationalId,
messageId,
notificationId,
}: {
notification: Notification
nationalId: string
messageId: string
notificationId?: number | null
}): Promise<void> {
const tokens = await this.getDeviceTokens(nationalId, messageId)

Expand All @@ -42,7 +44,12 @@ export class NotificationDispatchService {

for (const token of tokens) {
try {
await this.sendNotificationToToken(notification, token, messageId)
await this.sendNotificationToToken(
notification,
token,
messageId,
notificationId,
)
} catch (error) {
await this.handleSendError(error, nationalId, token, messageId)
}
Expand Down Expand Up @@ -82,6 +89,7 @@ export class NotificationDispatchService {
notification: Notification,
token: string,
messageId: string,
notificationId?: number | null,
): Promise<void> {
const message = {
token,
Expand All @@ -92,6 +100,7 @@ export class NotificationDispatchService {
data: {
messageId,
clickActionUrl: notification.clickActionUrl,
...(notificationId && { notificationId: String(notificationId) }),
},
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,18 +227,23 @@ describe('NotificationsWorkerService', () => {

expect(emailService.sendEmail).toHaveBeenCalledTimes(2)

// should write the messages to db
const messages = await notificationModel.findAll()
const recipientMessage = messages.find(
(message) => message.recipient === userWithDelegations.nationalId,
)
expect(messages).toHaveLength(2)
expect(recipientMessage).toBeDefined()

// should only send push notification for primary recipient
expect(notificationDispatch.sendPushNotification).toHaveBeenCalledTimes(1)
expect(notificationDispatch.sendPushNotification).toHaveBeenCalledWith(
expect.objectContaining({
nationalId: userWithDelegations.nationalId,
notificationId: recipientMessage.id,
}),
)

// should write the messages to db
const messages = await notificationModel.findAll()
expect(messages).toHaveLength(2)

// should have gotten user profile for primary recipient
expect(
userProfileApi.userProfileControllerFindUserProfile,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type HandleNotification = {
emailNotifications: boolean
locale?: string
}
notificationId?: number | null
messageId: string
message: CreateHnippNotificationDto
}
Expand Down Expand Up @@ -93,6 +94,7 @@ export class NotificationsWorkerService implements OnApplicationBootstrap {
async handleDocumentNotification({
profile,
messageId,
notificationId,
message,
}: HandleNotification) {
// don't send message unless user wants this type of notification and national id is a person.
Expand Down Expand Up @@ -126,6 +128,7 @@ export class NotificationsWorkerService implements OnApplicationBootstrap {
nationalId: profile.nationalId,
notification,
messageId,
notificationId,
})
}

Expand Down Expand Up @@ -342,21 +345,22 @@ export class NotificationsWorkerService implements OnApplicationBootstrap {
await this.sleepOutsideWorkingHours(messageId)

const notification = { messageId, ...message }
const messageIdExists = await this.notificationModel.count({
let dbNotification = await this.notificationModel.findOne({
where: { messageId },
attributes: ['id'],
})

if (messageIdExists > 0) {
// messageId exists do nothing
if (dbNotification) {
// messageId exists in db, do nothing
this.logger.info('notification with messageId already exists in db', {
messageId,
})
} else {
// messageId does not exist
// write to db
try {
const res = await this.notificationModel.create(notification)
if (res) {
dbNotification = await this.notificationModel.create(notification)
if (dbNotification) {
this.logger.info('notification written to db', {
notification,
messageId,
Expand Down Expand Up @@ -398,6 +402,7 @@ export class NotificationsWorkerService implements OnApplicationBootstrap {
const handleNotificationArgs: HandleNotification = {
profile: { ...profile, nationalId: message.recipient },
messageId,
notificationId: dbNotification?.id,
message,
}

Expand Down

0 comments on commit 02e8a3c

Please sign in to comment.