Skip to content

Commit

Permalink
fix(notification-service): update method to fetch user id for notific…
Browse files Browse the repository at this point in the history
…ation user (#111)

BREAKING CHANGE
  • Loading branch information
mayank-SFIN571 authored Dec 30, 2020
1 parent e98d2b8 commit 26246d7
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 18 deletions.
8 changes: 5 additions & 3 deletions services/notification-service/src/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,17 @@ import {
SesProvider,
SnsProvider,
} from 'loopback4-notifications';

import {
NotificationController,
NotificationUserController,
PubnubNotificationController,
NotificationNotificationUserController,
NotificationUserController,
NotificationUserNotificationController,
PubnubNotificationController,
} from './controllers';
import {NotifServiceBindings} from './keys';
import {Notification, NotificationAccess, NotificationUser} from './models';
import {ChannelManagerProvider} from './providers';
import {NotificationUserProvider} from './providers/notification-user.service';
import {
NotificationAccessRepository,
NotificationRepository,
Expand Down Expand Up @@ -110,6 +110,8 @@ export class NotificationServiceComponent implements Component {

this.providers = {
[NotifServiceBindings.ChannelManager.key]: ChannelManagerProvider,
[NotifServiceBindings.NotificationUserManager
.key]: NotificationUserProvider,
};

this.controllers = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,24 @@ import {
getFilterSchemaFor,
getModelSchemaRef,
getWhereSchemaFor,
HttpErrors,
param,
post,
requestBody,
HttpErrors,
} from '@loopback/rest';
import {CONTENT_TYPE, STATUS_CODE} from '@sourceloop/core';
import {authenticate, STRATEGY, AuthErrorKeys} from 'loopback4-authentication';
import {authenticate, AuthErrorKeys, STRATEGY} from 'loopback4-authentication';
import {authorize} from 'loopback4-authorization';
import {INotification, NotificationBindings} from 'loopback4-notifications';

import {ErrorKeys} from '../enums/error-keys.enum';
import {PermissionKey} from '../enums/permission-key.enum';
import {NotifServiceBindings} from '../keys';
import {Notification, NotificationUser} from '../models';
import {
NotificationRepository,
NotificationUserRepository,
} from '../repositories';
import {ErrorKeys} from '../enums/error-keys.enum';
import {INotificationUserManager} from '../types';

const maxBodyLen = 1000;
export class NotificationController {
Expand All @@ -38,6 +39,8 @@ export class NotificationController {
private readonly notifProvider: Getter<INotification>,
@repository(NotificationUserRepository)
public notificationUserRepository: NotificationUserRepository,
@inject(NotifServiceBindings.NotificationUserManager)
private readonly notifUserService: INotificationUserManager,
) {}

@authenticate(STRATEGY.BEARER)
Expand Down Expand Up @@ -72,7 +75,7 @@ export class NotificationController {
throw new HttpErrors.UnprocessableEntity(AuthErrorKeys.UnknownError);
}

const receiversToCreate = this.createNotifUsers(notif);
const receiversToCreate = await this.createNotifUsers(notif);
await this.notificationUserRepository.createAll(receiversToCreate);
return notif;
}
Expand Down Expand Up @@ -115,14 +118,14 @@ export class NotificationController {
});
const notifs = await this.notificationRepository.createAll(notifications);
const notifUsers: NotificationUser[] = [];
notifs.forEach(notif => {
for (const notif of notifs) {
if (!notif || !notif.id) {
throw new HttpErrors.UnprocessableEntity(AuthErrorKeys.UnknownError);
}

const receiversToCreate = this.createNotifUsers(notif);
const receiversToCreate = await this.createNotifUsers(notif);
notifUsers.push(...receiversToCreate);
});
}
await this.notificationUserRepository.createAll(notifUsers);
return notifs;
}
Expand Down Expand Up @@ -185,11 +188,7 @@ export class NotificationController {
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;
});

return this.notifUserService.getNotifUsers(notif);
}
}
9 changes: 8 additions & 1 deletion services/notification-service/src/keys.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import {BindingKey} from '@loopback/core';
import {INotifServiceConfig, IChannelManager} from './types';
import {BINDING_PREFIX} from '@sourceloop/core';
import {
IChannelManager,
INotificationUserManager,
INotifServiceConfig,
} from './types';

export namespace NotifServiceBindings {
export const Config = BindingKey.create<INotifServiceConfig | null>(
Expand All @@ -9,4 +13,7 @@ export namespace NotifServiceBindings {
export const ChannelManager = BindingKey.create<IChannelManager | null>(
`${BINDING_PREFIX}.notification.channnelMgr`,
);
export const NotificationUserManager = BindingKey.create<INotificationUserManager | null>(
`${BINDING_PREFIX}.notification.notifUserMgr`,
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import {bind, /* inject, */ BindingScope, Provider} from '@loopback/core';
import {Notification, NotificationUser} from '../models';
import {INotificationUserManager} from '../types';

@bind({scope: BindingScope.TRANSIENT})
export class NotificationUserProvider
implements Provider<INotificationUserManager> {
constructor() {}

value() {
return {
getNotifUsers: async (notif: Notification) => {
return Promise.resolve(
notif.receiver.to.map(to => {
const notifUser = new NotificationUser();
notifUser.notificationId = notif.id ?? '';
notifUser.userId = to.id;
return notifUser;
}),
);
},
};
}
}
5 changes: 5 additions & 0 deletions services/notification-service/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {IAuthUserWithPermissions, IServiceConfig} from '@sourceloop/core';
import {Config} from 'loopback4-notifications';
import {Notification, NotificationUser} from './models';

export interface IChannelManager {
isChannelAccessAllowed(
Expand All @@ -8,6 +9,10 @@ export interface IChannelManager {
): boolean;
}

export interface INotificationUserManager {
getNotifUsers(notif: Notification): Promise<NotificationUser[]>;
}

export interface INotifServiceConfig extends IServiceConfig {
useCustomEmailProvider: boolean;
useCustomSMSProvider: boolean;
Expand Down

0 comments on commit 26246d7

Please sign in to comment.