From 2462bd6f214726224257a1382f75df470db623c1 Mon Sep 17 00:00:00 2001 From: vineet-suri <49708329+vineet-suri@users.noreply.github.com> Date: Sat, 30 Jan 2021 14:04:19 +0530 Subject: [PATCH] feat(notification-service): adds delete APIs for notification and notification users (#115) --- .../notification-user.controller.ts | 24 +++++++++++++++- .../controllers/notification.controller.ts | 28 +++++++++++++++---- 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/services/notification-service/src/controllers/notification-user.controller.ts b/services/notification-service/src/controllers/notification-user.controller.ts index c9ac4bda0c..17cbec80c3 100644 --- a/services/notification-service/src/controllers/notification-user.controller.ts +++ b/services/notification-service/src/controllers/notification-user.controller.ts @@ -274,7 +274,7 @@ export class NotificationUserController { @authenticate(STRATEGY.BEARER, { passReqToCallback: true, }) - @authorize({permissions: [PermissionKey.ViewNotification]}) + @authorize({permissions: [PermissionKey.DeleteNotification]}) @del(`${basePath}/{id}`, { responses: { '204': { @@ -291,6 +291,28 @@ export class NotificationUserController { await this.notificationUserRepository.deleteById(id); } + @authenticate(STRATEGY.BEARER, { + passReqToCallback: true, + }) + @authorize({permissions: [PermissionKey.DeleteNotification]}) + @del(basePath, { + responses: { + [STATUS_CODE.NO_CONTENT]: { + description: 'Notification DELETE success', + }, + }, + }) + async deleteAll( + @inject(AuthenticationBindings.CURRENT_USER) + currentUser: IAuthUserWithPermissions, + @param.query.object('where', getWhereSchemaFor(NotificationUser)) + where?: Where, + ): Promise { + return this.notificationUserRepository.deleteAll( + this._createWhereBuilder(currentUser, where).build(), + ); + } + private async _verifyOwned( id: string, currentUser: IAuthUserWithPermissions, diff --git a/services/notification-service/src/controllers/notification.controller.ts b/services/notification-service/src/controllers/notification.controller.ts index b822c8be43..694621f2a2 100644 --- a/services/notification-service/src/controllers/notification.controller.ts +++ b/services/notification-service/src/controllers/notification.controller.ts @@ -7,6 +7,7 @@ import { Where, } from '@loopback/repository'; import { + del, get, getFilterSchemaFor, getModelSchemaRef, @@ -29,6 +30,7 @@ import { NotificationUserRepository, } from '../repositories'; import {INotificationUserManager} from '../types'; +const basePath = '/notifications'; const maxBodyLen = 1000; export class NotificationController { @@ -45,7 +47,7 @@ export class NotificationController { @authenticate(STRATEGY.BEARER) @authorize({permissions: [PermissionKey.CreateNotification]}) - @post('/notifications', { + @post(basePath, { responses: { [STATUS_CODE.OK]: { description: 'Notification model instance', @@ -82,7 +84,7 @@ export class NotificationController { @authenticate(STRATEGY.BEARER) @authorize({permissions: [PermissionKey.CreateNotification]}) - @post('/notifications/bulk', { + @post(`${basePath}/bulk`, { responses: { [STATUS_CODE.OK]: { description: 'Array of Notifications', @@ -132,7 +134,7 @@ export class NotificationController { @authenticate(STRATEGY.BEARER) @authorize({permissions: [PermissionKey.ViewNotification]}) - @get('/notifications/count', { + @get(`${basePath}/count`, { responses: { [STATUS_CODE.OK]: { description: 'Notification model count', @@ -149,7 +151,7 @@ export class NotificationController { @authenticate(STRATEGY.BEARER) @authorize({permissions: ['*']}) - @get('/notifications', { + @get(basePath, { responses: { [STATUS_CODE.OK]: { description: 'Array of Notification model instances', @@ -170,7 +172,7 @@ export class NotificationController { @authenticate(STRATEGY.BEARER) @authorize({permissions: [PermissionKey.ViewNotification]}) - @get('/notifications/{id}', { + @get(`${basePath}/{id}`, { responses: { [STATUS_CODE.OK]: { description: 'Notification model instance', @@ -184,6 +186,22 @@ export class NotificationController { return this.notificationRepository.findById(id); } + @authenticate(STRATEGY.BEARER) + @authorize({permissions: [PermissionKey.DeleteNotification]}) + @del(basePath, { + responses: { + [STATUS_CODE.NO_CONTENT]: { + description: 'Notification DELETE success', + }, + }, + }) + async deleteAll( + @param.query.object('where', getWhereSchemaFor(Notification)) + where?: Where, + ): Promise { + return this.notificationRepository.deleteAll(where); + } + createNotifUsers(notif: Notification) { if (!notif.receiver || !notif.receiver.to) { throw new HttpErrors.UnprocessableEntity(ErrorKeys.ReceiverNotFound);