From aa1b07d2383d6467d6bd4db4b512da29b1a258ac Mon Sep 17 00:00:00 2001 From: Samarpan Date: Tue, 3 Nov 2020 11:24:38 +0530 Subject: [PATCH] feat(notification-service): add relation for notification user SFO-0 --- commitlint.config.js | 2 +- .../src/controllers/index.ts | 2 + ...tification-notification-user.controller.ts | 135 ++++++++++++++++++ ...tification-user-notification.controller.ts | 38 +++++ .../src/enums/permission-key.enum.ts | 2 + .../src/models/notification-user.model.ts | 23 ++- .../src/models/notification.model.ts | 12 +- .../notification-user.repository.ts | 22 ++- .../repositories/notification.repository.ts | 29 +++- 9 files changed, 251 insertions(+), 14 deletions(-) create mode 100644 services/notification-service/src/controllers/notification-notification-user.controller.ts create mode 100644 services/notification-service/src/controllers/notification-user-notification.controller.ts diff --git a/commitlint.config.js b/commitlint.config.js index 2339f2b25d..48824c6ecd 100644 --- a/commitlint.config.js +++ b/commitlint.config.js @@ -11,7 +11,7 @@ module.exports = { }, parserPreset: { parserOpts: { - issuePrefixes: ['SFO-'], + issuePrefixes: ['SFO-', '#', 'RD-', 'RPMS-', 'RFIT-'], }, }, }; diff --git a/services/notification-service/src/controllers/index.ts b/services/notification-service/src/controllers/index.ts index 581423e4fa..c1fabe86ac 100644 --- a/services/notification-service/src/controllers/index.ts +++ b/services/notification-service/src/controllers/index.ts @@ -1,3 +1,5 @@ export * from './pubnub-notification.controller'; export * from './notification-user.controller'; export * from './notification.controller'; +export * from './notification-user-notification.controller'; +export * from './notification-notification-user.controller'; diff --git a/services/notification-service/src/controllers/notification-notification-user.controller.ts b/services/notification-service/src/controllers/notification-notification-user.controller.ts new file mode 100644 index 0000000000..b9348d2f14 --- /dev/null +++ b/services/notification-service/src/controllers/notification-notification-user.controller.ts @@ -0,0 +1,135 @@ +import { + Count, + CountSchema, + Filter, + repository, + Where, +} from '@loopback/repository'; +import { + del, + get, + getModelSchemaRef, + getWhereSchemaFor, + param, + patch, + post, + requestBody, +} from '@loopback/rest'; +import {authenticate, STRATEGY} from 'loopback4-authentication'; +import {authorize} from 'loopback4-authorization'; +import {PermissionKey} from '../enums/permission-key.enum'; +import {Notification, NotificationUser} from '../models'; +import {NotificationRepository} from '../repositories'; + +const basePath = '/notifications/{id}/notification-users'; + +export class NotificationNotificationUserController { + constructor( + @repository(NotificationRepository) + protected notificationRepository: NotificationRepository, + ) {} + + @authenticate(STRATEGY.BEARER) + @authorize([PermissionKey.ViewNotification]) + @get(basePath, { + responses: { + '200': { + description: 'Array of Notification has many NotificationUser', + content: { + 'application/json': { + schema: { + type: 'array', + items: getModelSchemaRef(NotificationUser), + }, + }, + }, + }, + }, + }) + async find( + @param.path.string('id') id: string, + @param.query.object('filter') filter?: Filter, + ): Promise { + return this.notificationRepository.notificationUsers(id).find(filter); + } + + @authenticate(STRATEGY.BEARER) + @authorize([PermissionKey.CreateNotification]) + @post(basePath, { + responses: { + '200': { + description: 'Notification model instance', + content: { + 'application/json': { + schema: getModelSchemaRef(NotificationUser), + }, + }, + }, + }, + }) + async create( + @param.path.string('id') id: typeof Notification.prototype.id, + @requestBody({ + content: { + 'application/json': { + schema: getModelSchemaRef(NotificationUser, { + title: 'NewNotificationUserInNotification', + exclude: ['id'], + optional: ['notificationId'], + }), + }, + }, + }) + notificationUser: Omit, + ): Promise { + return this.notificationRepository + .notificationUsers(id) + .create(notificationUser); + } + + @authenticate(STRATEGY.BEARER) + @authorize([PermissionKey.UpdateNotification]) + @patch(basePath, { + responses: { + '200': { + description: 'Notification.NotificationUser PATCH success count', + content: {'application/json': {schema: CountSchema}}, + }, + }, + }) + async patch( + @param.path.string('id') id: string, + @requestBody({ + content: { + 'application/json': { + schema: getModelSchemaRef(NotificationUser, {partial: true}), + }, + }, + }) + notificationUser: Partial, + @param.query.object('where', getWhereSchemaFor(NotificationUser)) + where?: Where, + ): Promise { + return this.notificationRepository + .notificationUsers(id) + .patch(notificationUser, where); + } + + @authenticate(STRATEGY.BEARER) + @authorize([PermissionKey.DeleteNotification]) + @del(basePath, { + responses: { + '200': { + description: 'Notification.NotificationUser DELETE success count', + content: {'application/json': {schema: CountSchema}}, + }, + }, + }) + async delete( + @param.path.string('id') id: string, + @param.query.object('where', getWhereSchemaFor(NotificationUser)) + where?: Where, + ): Promise { + return this.notificationRepository.notificationUsers(id).delete(where); + } +} diff --git a/services/notification-service/src/controllers/notification-user-notification.controller.ts b/services/notification-service/src/controllers/notification-user-notification.controller.ts new file mode 100644 index 0000000000..46385d3c64 --- /dev/null +++ b/services/notification-service/src/controllers/notification-user-notification.controller.ts @@ -0,0 +1,38 @@ +import {repository} from '@loopback/repository'; +import {get, getModelSchemaRef, param} from '@loopback/rest'; +import {authenticate, STRATEGY} from 'loopback4-authentication'; +import {authorize} from 'loopback4-authorization'; + +import {PermissionKey} from '../enums/permission-key.enum'; +import {Notification, NotificationUser} from '../models'; +import {NotificationUserRepository} from '../repositories'; + +export class NotificationUserNotificationController { + constructor( + @repository(NotificationUserRepository) + public notificationUserRepository: NotificationUserRepository, + ) {} + + @authenticate(STRATEGY.BEARER) + @authorize([PermissionKey.ViewNotification]) + @get('/notification-users/{id}/notification', { + responses: { + '200': { + description: 'Notification belonging to NotificationUser', + content: { + 'application/json': { + schema: { + type: 'array', + items: getModelSchemaRef(Notification), + }, + }, + }, + }, + }, + }) + async getNotification( + @param.path.string('id') id: typeof NotificationUser.prototype.id, + ): Promise { + return this.notificationUserRepository.notification(id); + } +} diff --git a/services/notification-service/src/enums/permission-key.enum.ts b/services/notification-service/src/enums/permission-key.enum.ts index e203936267..37e4630a7f 100644 --- a/services/notification-service/src/enums/permission-key.enum.ts +++ b/services/notification-service/src/enums/permission-key.enum.ts @@ -1,5 +1,7 @@ export const enum PermissionKey { ViewNotification = 'ViewNotification', CreateNotification = 'CreateNotification', + UpdateNotification = 'UpdateNotification', + DeleteNotification = 'DeleteNotification', CanGetNotificationAccess = 'CanGetNotificationAccess', } diff --git a/services/notification-service/src/models/notification-user.model.ts b/services/notification-service/src/models/notification-user.model.ts index 449bc7d734..55dafbf47b 100644 --- a/services/notification-service/src/models/notification-user.model.ts +++ b/services/notification-service/src/models/notification-user.model.ts @@ -1,5 +1,6 @@ -import {model, property} from '@loopback/repository'; +import {model, property, belongsTo} from '@loopback/repository'; import {BaseEntity} from '@sourceloop/core'; +import {Notification} from './notification.model'; @model({ name: 'notification_users', @@ -11,11 +12,14 @@ export class NotificationUser extends BaseEntity { }) id?: string; - @property({ - type: 'string', - required: true, - name: 'notification_id', - }) + @belongsTo( + () => Notification, + {name: 'notification'}, + { + name: 'notification_id', + required: true, + }, + ) notificationId: string; @property({ @@ -36,3 +40,10 @@ export class NotificationUser extends BaseEntity { super(data); } } + +export interface NotificationUserRelations { + notificaTION: Notification; +} + +export type NotificationUserWithRelations = NotificationUser & + NotificationUserRelations; diff --git a/services/notification-service/src/models/notification.model.ts b/services/notification-service/src/models/notification.model.ts index f4ac1c4863..29367e8440 100644 --- a/services/notification-service/src/models/notification.model.ts +++ b/services/notification-service/src/models/notification.model.ts @@ -1,10 +1,11 @@ -import {Entity, model, property} from '@loopback/repository'; +import {Entity, model, property, hasMany} from '@loopback/repository'; import { Message, Receiver, MessageType, MessageOptions, } from 'loopback4-notifications'; +import {NotificationUser} from './notification-user.model'; @model({ name: 'notifications', @@ -53,7 +54,16 @@ export class Notification extends Entity implements Message { }) options?: MessageOptions; + @hasMany(() => NotificationUser, {keyTo: 'notificationId'}) + notificationUsers: NotificationUser[]; + constructor(data?: Partial) { super(data); } } + +export interface NotificationRelations { + notificationUsers: NotificationUser[]; +} + +export type NotificationWithRelations = Notification & NotificationRelations; diff --git a/services/notification-service/src/repositories/notification-user.repository.ts b/services/notification-service/src/repositories/notification-user.repository.ts index 3194273ea7..e7a694b410 100644 --- a/services/notification-service/src/repositories/notification-user.repository.ts +++ b/services/notification-service/src/repositories/notification-user.repository.ts @@ -1,18 +1,34 @@ -import {inject} from '@loopback/core'; -import {juggler} from '@loopback/repository'; +import {Getter, inject} from '@loopback/core'; +import {BelongsToAccessor, juggler, repository} from '@loopback/repository'; import {DefaultSoftCrudRepository} from '@sourceloop/core'; -import {NotificationUser} from '../models'; +import {Notification, NotificationUser} from '../models'; import {NotifDbSourceName} from '../types'; +import {NotificationRepository} from './notification.repository'; export class NotificationUserRepository extends DefaultSoftCrudRepository< NotificationUser, typeof NotificationUser.prototype.id > { + public readonly notification: BelongsToAccessor< + Notification, + typeof NotificationUser.prototype.id + >; + constructor( @inject(`datasources.${NotifDbSourceName}`) dataSource: juggler.DataSource, + @repository.getter('NotificationRepository') + protected notificationRepositoryGetter: Getter, ) { super(NotificationUser, dataSource); + this.notification = this.createBelongsToAccessorFor( + 'notification', + notificationRepositoryGetter, + ); + this.registerInclusionResolver( + 'notification', + this.notification.inclusionResolver, + ); } } diff --git a/services/notification-service/src/repositories/notification.repository.ts b/services/notification-service/src/repositories/notification.repository.ts index b5ac28e56d..63474d6c58 100644 --- a/services/notification-service/src/repositories/notification.repository.ts +++ b/services/notification-service/src/repositories/notification.repository.ts @@ -1,17 +1,40 @@ -import {inject} from '@loopback/core'; -import {DefaultCrudRepository, juggler} from '@loopback/repository'; +import {Getter, inject} from '@loopback/core'; +import { + DefaultCrudRepository, + HasManyRepositoryFactory, + juggler, + repository, +} from '@loopback/repository'; -import {Notification} from '../models'; +import {Notification, NotificationUser} from '../models'; import {NotifDbSourceName} from '../types'; +import {NotificationUserRepository} from './notification-user.repository'; export class NotificationRepository extends DefaultCrudRepository< Notification, typeof Notification.prototype.id > { + public readonly notificationUsers: HasManyRepositoryFactory< + NotificationUser, + typeof Notification.prototype.id + >; + constructor( @inject(`datasources.${NotifDbSourceName}`) dataSource: juggler.DataSource, + @repository.getter('NotificationUserRepository') + protected notificationUserRepositoryGetter: Getter< + NotificationUserRepository + >, ) { super(Notification, dataSource); + this.notificationUsers = this.createHasManyRepositoryFactoryFor( + 'notificationUsers', + notificationUserRepositoryGetter, + ); + this.registerInclusionResolver( + 'notificationUsers', + this.notificationUsers.inclusionResolver, + ); } }