From 9dd8d79dc2db2fbc408f99771e38f7671c1f5acd Mon Sep 17 00:00:00 2001 From: mayank-SFIN571 <51310882+mayank-SFIN571@users.noreply.github.com> Date: Tue, 3 Nov 2020 19:46:43 +0530 Subject: [PATCH] fix(notification-service): fix for notification user entry (#90) RFIT-568 --- .../controllers/notification.controller.ts | 28 +++++++++++++------ .../src/enums/error-keys.enum.ts | 3 ++ 2 files changed, 22 insertions(+), 9 deletions(-) create mode 100644 services/notification-service/src/enums/error-keys.enum.ts diff --git a/services/notification-service/src/controllers/notification.controller.ts b/services/notification-service/src/controllers/notification.controller.ts index 7694467d79..e634f5f486 100644 --- a/services/notification-service/src/controllers/notification.controller.ts +++ b/services/notification-service/src/controllers/notification.controller.ts @@ -27,6 +27,7 @@ import { NotificationRepository, NotificationUserRepository, } from '../repositories'; +import {ErrorKeys} from '../enums/error-keys.enum'; const maxBodyLen = 1000; export class NotificationController { @@ -67,14 +68,12 @@ export class NotificationController { notification.body = notification.body.substring(0, maxBodyLen - 1); } const notif = await this.notificationRepository.create(notification); - - const notifUser = new NotificationUser(); if (!notif || !notif.id) { throw new HttpErrors.UnprocessableEntity(AuthErrorKeys.UnknownError); } - notifUser.notificationId = notif.id; - notifUser.isRead = false; - await this.notificationUserRepository.create(notifUser); + + const receiversToCreate = this.createNotifUsers(notif); + await this.notificationUserRepository.createAll(receiversToCreate); return notif; } @@ -117,13 +116,12 @@ export class NotificationController { const notifs = await this.notificationRepository.createAll(notifications); const notifUsers: NotificationUser[] = []; notifs.forEach(notif => { - const notifUser = new NotificationUser(); if (!notif || !notif.id) { throw new HttpErrors.UnprocessableEntity(AuthErrorKeys.UnknownError); } - notifUser.notificationId = notif.id; - notifUser.isRead = false; - notifUsers.push(notifUser); + + const receiversToCreate = this.createNotifUsers(notif); + notifUsers.push(...receiversToCreate); }); await this.notificationUserRepository.createAll(notifUsers); return notifs; @@ -182,4 +180,16 @@ export class NotificationController { async findById(@param.path.string('id') id: string): Promise { return this.notificationRepository.findById(id); } + + createNotifUsers(notif: Notification) { + if (!notif.receiver || !notif.receiver.to) { + throw new HttpErrors.UnprocessableEntity(ErrorKeys.ReceiverNotFound); + } + return notif.receiver.to.map(to => { + const notifUser = new NotificationUser(); + notifUser.notificationId = notif.id ?? ''; + notifUser.userId = to.id; + return notifUser; + }); + } } diff --git a/services/notification-service/src/enums/error-keys.enum.ts b/services/notification-service/src/enums/error-keys.enum.ts new file mode 100644 index 0000000000..fc01ba1f05 --- /dev/null +++ b/services/notification-service/src/enums/error-keys.enum.ts @@ -0,0 +1,3 @@ +export const enum ErrorKeys { + ReceiverNotFound = 'ReceiverNotFound', +}