From 152fcde98a624e7abb052676b4cca4efc82b0d3a Mon Sep 17 00:00:00 2001 From: Gunnar K Vilbergsson Date: Tue, 12 Nov 2024 13:33:57 +0000 Subject: [PATCH 1/2] chore(application-system): Add logging to notification service and make it awaitable --- .../children-residence-change.service.ts | 92 ++++++++++--------- .../lib/notification/notifications.module.ts | 3 +- .../lib/notification/notifications.service.ts | 39 ++++++-- 3 files changed, 84 insertions(+), 50 deletions(-) diff --git a/libs/application/template-api-modules/src/lib/modules/templates/children-residence-change-v2/children-residence-change.service.ts b/libs/application/template-api-modules/src/lib/modules/templates/children-residence-change-v2/children-residence-change.service.ts index d039efb1bd56..e24df17dc74d 100644 --- a/libs/application/template-api-modules/src/lib/modules/templates/children-residence-change-v2/children-residence-change.service.ts +++ b/libs/application/template-api-modules/src/lib/modules/templates/children-residence-change-v2/children-residence-change.service.ts @@ -1,4 +1,4 @@ -import { Inject, Injectable } from '@nestjs/common' +import { Injectable } from '@nestjs/common' import { SharedModuleConfig, TemplateApiModuleActionProps, @@ -182,12 +182,13 @@ export class ChildrenResidenceChangeServiceV2 extends BaseTemplateApiService { const contractLink = await this.getApplicationLink(application) if (otherParent) { - this.notificationsService.sendNotification({ + await this.notificationsService.sendNotification({ type: NotificationType.AssignCounterParty, messageParties: { recipient: otherParent.nationalId, sender: applicant.nationalId, }, + applicationId: application.id, args: { applicantName: applicant.fullName, contractLink, @@ -220,26 +221,30 @@ export class ChildrenResidenceChangeServiceV2 extends BaseTemplateApiService { throw new Error('Other parent was undefined') } - this.notificationsService.sendNotification({ - type: NotificationType.ChildrenResidenceChangeApprovedByOrg, - messageParties: { - recipient: applicant.nationalId, - }, - args: { - applicationLink, - caseNumber: caseNumber || '', - }, - }) - this.notificationsService.sendNotification({ - type: NotificationType.ChildrenResidenceChangeApprovedByOrg, - messageParties: { - recipient: otherParent.nationalId, - }, - args: { - applicationLink, - caseNumber: caseNumber || '', - }, - }) + await Promise.all([ + this.notificationsService.sendNotification({ + type: NotificationType.ChildrenResidenceChangeApprovedByOrg, + messageParties: { + recipient: applicant.nationalId, + }, + applicationId: application.id, + args: { + applicationLink, + caseNumber: caseNumber || '', + }, + }), + this.notificationsService.sendNotification({ + type: NotificationType.ChildrenResidenceChangeApprovedByOrg, + messageParties: { + recipient: otherParent.nationalId, + }, + applicationId: application.id, + args: { + applicationLink, + caseNumber: caseNumber || '', + }, + }), + ]) } async rejectedByCounterParty({ application }: Props) { @@ -255,11 +260,12 @@ export class ChildrenResidenceChangeServiceV2 extends BaseTemplateApiService { const applicant = nationalRegistry.data const otherParent = childrenCustodyInformation.data[0].otherParent - this.notificationsService.sendNotification({ + await this.notificationsService.sendNotification({ type: NotificationType.RejectedByCounterParty, messageParties: { recipient: applicant.nationalId, }, + applicationId: application.id, args: { counterPartyName: otherParent?.fullName || '', }, @@ -292,24 +298,28 @@ export class ChildrenResidenceChangeServiceV2 extends BaseTemplateApiService { throw new Error('Other parent was undefined') } - this.notificationsService.sendNotification({ - type: NotificationType.RejectedByOrganization, - messageParties: { - recipient: applicant.nationalId, - }, - args: { - orgName: syslumennName, - }, - }) - this.notificationsService.sendNotification({ - type: NotificationType.RejectedByOrganization, - messageParties: { - recipient: otherParent.nationalId, - }, - args: { - orgName: syslumennName, - }, - }) + await Promise.all([ + this.notificationsService.sendNotification({ + type: NotificationType.RejectedByOrganization, + messageParties: { + recipient: applicant.nationalId, + }, + applicationId: application.id, + args: { + orgName: syslumennName, + }, + }), + this.notificationsService.sendNotification({ + type: NotificationType.RejectedByOrganization, + messageParties: { + recipient: otherParent.nationalId, + }, + applicationId: application.id, + args: { + orgName: syslumennName, + }, + }), + ]) } private async getApplicationLink(application: CRCApplication) { diff --git a/libs/application/template-api-modules/src/lib/notification/notifications.module.ts b/libs/application/template-api-modules/src/lib/notification/notifications.module.ts index 7f65661987d2..f1c0a60b4108 100644 --- a/libs/application/template-api-modules/src/lib/notification/notifications.module.ts +++ b/libs/application/template-api-modules/src/lib/notification/notifications.module.ts @@ -2,9 +2,10 @@ import { Module } from '@nestjs/common' import { UserNotificationClientModule } from '@island.is/clients/user-notification' import { NotificationsService } from './notifications.service' +import { LoggingModule } from '@island.is/logging' @Module({ - imports: [UserNotificationClientModule], + imports: [UserNotificationClientModule, LoggingModule], providers: [NotificationsService], exports: [NotificationsService], }) diff --git a/libs/application/template-api-modules/src/lib/notification/notifications.service.ts b/libs/application/template-api-modules/src/lib/notification/notifications.service.ts index 81bdc9c883b2..712a250118cf 100644 --- a/libs/application/template-api-modules/src/lib/notification/notifications.service.ts +++ b/libs/application/template-api-modules/src/lib/notification/notifications.service.ts @@ -1,11 +1,21 @@ -import { NotificationsApi } from '@island.is/clients/user-notification' -import { Injectable } from '@nestjs/common' +import { + CreateNotificationResponse, + NotificationsApi, +} from '@island.is/clients/user-notification' +import type { Logger } from '@island.is/logging' +import { LOGGER_PROVIDER } from '@island.is/logging' +import { Inject, Injectable } from '@nestjs/common' import { NotificationArgs } from './notificationTypes' import { NotificationConfig, NotificationType } from './notificationsTemplates' @Injectable() export class NotificationsService { - constructor(private readonly notificationApi: NotificationsApi) {} + constructor( + private readonly notificationApi: NotificationsApi, + @Inject(LOGGER_PROVIDER) private logger: Logger, + ) { + this.logger = logger.child({ context: 'NotificationsService' }) + } /** * Sends a notification using the specified type and arguments. @@ -14,6 +24,7 @@ export class NotificationsService { * @param data.type - The type of notification (User, Order, or System) * @param data.messageParties.recipient - The recipient nationalId of the notification * @param data.messageParties.sender - Optional. The sender nationalId of the notification + * @param data.applicationId - Optional. The applicationId of the notification * @param data.args - The arguments specific to the notification type * @@ -24,6 +35,7 @@ export class NotificationsService { * messageParties: { * recipient: 'user@example.com', * }, + * applicationId: '12345', * args: { * username: 'johndoe', * email: 'johndoe@example.com' @@ -37,6 +49,7 @@ export class NotificationsService { * messageParties: { * recipient: 'customer@example.com', * }, + * applicationId: '12345', * args: { * orderId: '12345', * orderStatus: 'Shipped' @@ -44,14 +57,15 @@ export class NotificationsService { * sender: 'orders@company.com' * }); */ - public sendNotification(data: { + public async sendNotification(data: { type: T messageParties: { recipient: string sender?: string } + applicationId?: string args?: NotificationArgs - }) { + }): Promise { const templateId = NotificationConfig[data.type].templateId const notification = { @@ -64,8 +78,17 @@ export class NotificationsService { })), } - this.notificationApi.notificationsControllerCreateHnippNotification({ - createHnippNotificationDto: notification, - }) + const response = + await this.notificationApi.notificationsControllerCreateHnippNotification( + { + createHnippNotificationDto: notification, + }, + ) + + this.logger.info( + `Notification with id ${response.id} sent for application ${data.applicationId}`, + ) + + return response } } From ffed2e00e3579b32613afc583dc7013162f5911c Mon Sep 17 00:00:00 2001 From: Gunnar K Vilbergsson Date: Tue, 12 Nov 2024 13:40:19 +0000 Subject: [PATCH 2/2] Add templateId for clarity --- .../src/lib/notification/notifications.service.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/application/template-api-modules/src/lib/notification/notifications.service.ts b/libs/application/template-api-modules/src/lib/notification/notifications.service.ts index 712a250118cf..159adfa6aecc 100644 --- a/libs/application/template-api-modules/src/lib/notification/notifications.service.ts +++ b/libs/application/template-api-modules/src/lib/notification/notifications.service.ts @@ -86,7 +86,7 @@ export class NotificationsService { ) this.logger.info( - `Notification with id ${response.id} sent for application ${data.applicationId}`, + `Notification with templateId ${templateId} and messageId ${response.id} sent for application ${data.applicationId}`, ) return response