Skip to content

Commit

Permalink
fix(notification-service): fix for notification user entry (#90)
Browse files Browse the repository at this point in the history
RFIT-568
  • Loading branch information
mayank-SFIN571 authored Nov 3, 2020
1 parent 8300a88 commit 9dd8d79
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
NotificationRepository,
NotificationUserRepository,
} from '../repositories';
import {ErrorKeys} from '../enums/error-keys.enum';

const maxBodyLen = 1000;
export class NotificationController {
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -182,4 +180,16 @@ export class NotificationController {
async findById(@param.path.string('id') id: string): Promise<Notification> {
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;
});
}
}
3 changes: 3 additions & 0 deletions services/notification-service/src/enums/error-keys.enum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const enum ErrorKeys {
ReceiverNotFound = 'ReceiverNotFound',
}

0 comments on commit 9dd8d79

Please sign in to comment.