Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(application-system): Add logging to notification service and make it awaitable #16821

Merged
merged 4 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Inject, Injectable } from '@nestjs/common'
import { Injectable } from '@nestjs/common'
import {
SharedModuleConfig,
TemplateApiModuleActionProps,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand All @@ -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 || '',
},
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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],
})
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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

*
Expand All @@ -24,6 +35,7 @@ export class NotificationsService {
* messageParties: {
* recipient: '[email protected]',
* },
* applicationId: '12345',
* args: {
* username: 'johndoe',
* email: '[email protected]'
Expand All @@ -37,21 +49,23 @@ export class NotificationsService {
* messageParties: {
* recipient: '[email protected]',
* },
* applicationId: '12345',
* args: {
* orderId: '12345',
* orderStatus: 'Shipped'
* },
* sender: '[email protected]'
* });
*/
public sendNotification<T extends NotificationType>(data: {
public async sendNotification<T extends NotificationType>(data: {
type: T
messageParties: {
recipient: string
sender?: string
}
applicationId?: string
args?: NotificationArgs<T>
}) {
}): Promise<CreateNotificationResponse> {
const templateId = NotificationConfig[data.type].templateId

const notification = {
Expand All @@ -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 templateId ${templateId} and messageId ${response.id} sent for application ${data.applicationId}`,
)

return response
}
norda-gunni marked this conversation as resolved.
Show resolved Hide resolved
}
Loading