Skip to content

Commit

Permalink
fix(notification-service): notification user creation along with noti…
Browse files Browse the repository at this point in the history
…fication fix (#87)
  • Loading branch information
mayank-SFIN571 authored Oct 27, 2020
1 parent 5f51cb7 commit cc16e9c
Showing 1 changed file with 32 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,19 @@ import {
param,
post,
requestBody,
HttpErrors,
} from '@loopback/rest';
import {CONTENT_TYPE, STATUS_CODE} from '@sourceloop/core';
import {authenticate, STRATEGY} from 'loopback4-authentication';
import {authenticate, STRATEGY, AuthErrorKeys} from 'loopback4-authentication';
import {authorize} from 'loopback4-authorization';
import {INotification, NotificationBindings} from 'loopback4-notifications';

import {PermissionKey} from '../enums/permission-key.enum';
import {Notification} from '../models';
import {NotificationRepository} from '../repositories';
import {Notification, NotificationUser} from '../models';
import {
NotificationRepository,
NotificationUserRepository,
} from '../repositories';

const maxBodyLen = 1000;
export class NotificationController {
Expand All @@ -31,6 +35,8 @@ export class NotificationController {
public notificationRepository: NotificationRepository,
@inject.getter(NotificationBindings.NotificationProvider)
private readonly notifProvider: Getter<INotification>,
@repository(NotificationUserRepository)
public notificationUserRepository: NotificationUserRepository,
) {}

@authenticate(STRATEGY.BEARER)
Expand Down Expand Up @@ -60,7 +66,16 @@ export class NotificationController {
if (notification.body.length > maxBodyLen) {
notification.body = notification.body.substring(0, maxBodyLen - 1);
}
return this.notificationRepository.create(notification);
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);
return notif;
}

@authenticate(STRATEGY.BEARER)
Expand Down Expand Up @@ -93,7 +108,19 @@ export class NotificationController {
notification.body = notification.body.substring(0, maxBodyLen - 1);
}
});
return this.notificationRepository.createAll(notifications);
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);
});
await this.notificationUserRepository.createAll(notifUsers);
return notifs;
}

@authenticate(STRATEGY.BEARER)
Expand Down

0 comments on commit cc16e9c

Please sign in to comment.