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

fix(notification-service): fix for notification user entry #90

Merged
merged 1 commit into from
Nov 3, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
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',
}