From be90edc8dbbe700600a565fcb14aba9c4158fdb1 Mon Sep 17 00:00:00 2001 From: Chinmoy Date: Thu, 18 Jul 2024 12:21:45 +0530 Subject: [PATCH] Added feature to modify notification title and description. (#245) --- src/modules/bot/bot.controller.ts | 12 +++++++ src/modules/bot/bot.service.spec.ts | 23 ++++++++++++ src/modules/bot/bot.service.ts | 50 +++++++++++++++++++++++++-- src/modules/bot/dto/update-bot.dto.ts | 5 +++ 4 files changed, 87 insertions(+), 3 deletions(-) diff --git a/src/modules/bot/bot.controller.ts b/src/modules/bot/bot.controller.ts index b1ba7dc..5a6d2d7 100644 --- a/src/modules/bot/bot.controller.ts +++ b/src/modules/bot/bot.controller.ts @@ -31,6 +31,7 @@ import { Request } from 'express'; import { extname } from 'path'; import fs from 'fs'; import { DeleteBotsDTO } from './dto/delete-bot-dto'; +import { ModifyNotificationDTO } from './dto/update-bot.dto'; const editFileName = (req: Request, file: Express.Multer.File, callback) => { @@ -380,4 +381,15 @@ export class BotController { } return await this.botService.getBroadcastReport(botId, limit, nextPage); } + + @Post('/modifyNotification/:botId') + @UseInterceptors( + AddResponseObjectInterceptor, + AddAdminHeaderInterceptor, + AddOwnerInfoInterceptor, + AddROToResponseInterceptor, + ) + async modifyNotification(@Param('botId') botId: string, @Body() body: ModifyNotificationDTO) { + await this.botService.modifyNotification(botId, body.title, body.description); + } } diff --git a/src/modules/bot/bot.service.spec.ts b/src/modules/bot/bot.service.spec.ts index e69b59d..ea0a0b7 100644 --- a/src/modules/bot/bot.service.spec.ts +++ b/src/modules/bot/bot.service.spec.ts @@ -117,6 +117,7 @@ class MockConfigService { case 'CAFFINE_INVALIDATE_ENDPOINT': return '/testcaffineendpoint'; case 'AUTHORIZATION_KEY_TRANSACTION_LAYER': return 'testAuthToken'; case 'BROADCAST_BOT_REPORT_ENDPOINT': return 'testBotReportEndpoint'; + case 'ORCHESTRATOR_BASE_URL': return 'http://orchestrator_url'; default: return ''; } } @@ -418,6 +419,10 @@ describe('BotService', () => { configService = module.get(ConfigService); }); + afterEach(async () => { + fetchMock.restore(); + }); + it('create bot test', async () => { fetchMock.postOnce(`${configService.get('MINIO_MEDIA_UPLOAD_URL')}`, { fileName: 'testFileName' @@ -609,6 +614,9 @@ describe('BotService', () => { fetchMock.deleteOnce(`${configService.get('UCI_CORE_BASE_URL')}${configService.get('CAFFINE_INVALIDATE_ENDPOINT')}`, true ); + fetchMock.deleteOnce(`${configService.get('ORCHESTRATOR_BASE_URL')}${configService.get('CAFFINE_INVALIDATE_ENDPOINT')}`, + true + ); await botService.update('testBotIdExisting', { 'status': 'DISABLED' }); @@ -623,6 +631,9 @@ describe('BotService', () => { fetchMock.deleteOnce(`${configService.get('UCI_CORE_BASE_URL')}${configService.get('CAFFINE_INVALIDATE_ENDPOINT')}`, true ); + fetchMock.deleteOnce(`${configService.get('ORCHESTRATOR_BASE_URL')}${configService.get('CAFFINE_INVALIDATE_ENDPOINT')}`, + true + ); await expect(botService.update('testBotIdExisting', { 'endDate': '1129-299-092' })) @@ -653,6 +664,9 @@ describe('BotService', () => { fetchMock.deleteOnce(`${configService.get('UCI_CORE_BASE_URL')}${configService.get('CAFFINE_INVALIDATE_ENDPOINT')}`, () => { throw new InternalServerErrorException(); }); + fetchMock.deleteOnce(`${configService.get('ORCHESTRATOR_BASE_URL')}${configService.get('CAFFINE_INVALIDATE_ENDPOINT')}`, + true + ); await expect(botService.update('testBotIdExisting', { 'endDate': '2023-10-12' })) @@ -695,6 +709,9 @@ describe('BotService', () => { fetchMock.delete(`${configService.get('UCI_CORE_BASE_URL')}${configService.get('CAFFINE_INVALIDATE_ENDPOINT')}`, true ); + fetchMock.delete(`${configService.get('ORCHESTRATOR_BASE_URL')}${configService.get('CAFFINE_INVALIDATE_ENDPOINT')}`, + true + ); await botService.remove({ids: ['testId'], endDate: null}); expect(deletedIds).toEqual( [ @@ -728,6 +745,9 @@ describe('BotService', () => { fetchMock.delete(`${configService.get('UCI_CORE_BASE_URL')}${configService.get('CAFFINE_INVALIDATE_ENDPOINT')}`, true ); + fetchMock.delete(`${configService.get('ORCHESTRATOR_BASE_URL')}${configService.get('CAFFINE_INVALIDATE_ENDPOINT')}`, + true + ); await botService.remove({ids: null, endDate: '2025-12-01'}); expect(deletedIds).toEqual( [ @@ -761,6 +781,9 @@ describe('BotService', () => { fetchMock.delete(`${configService.get('UCI_CORE_BASE_URL')}${configService.get('CAFFINE_INVALIDATE_ENDPOINT')}`, true ); + fetchMock.deleteOnce(`${configService.get('ORCHESTRATOR_BASE_URL')}${configService.get('CAFFINE_INVALIDATE_ENDPOINT')}`, + true + ); const response = await botService.remove({ids: ['testId'], endDate: null}); const expectedBotIds = ['testId']; expect(response).toEqual(expectedBotIds); diff --git a/src/modules/bot/bot.service.ts b/src/modules/bot/bot.service.ts index e7d4613..21b757b 100644 --- a/src/modules/bot/bot.service.ts +++ b/src/modules/bot/bot.service.ts @@ -619,14 +619,16 @@ export class BotService { async invalidateTransactionLayerCache() { const inbound_base = this.configService.get('UCI_CORE_BASE_URL'); + const orchestrator_base = this.configService.get('ORCHESTRATOR_BASE_URL'); const caffine_invalidate_endpoint = this.configService.get('CAFFINE_INVALIDATE_ENDPOINT'); const transaction_layer_auth_token = this.configService.get('AUTHORIZATION_KEY_TRANSACTION_LAYER'); if (!inbound_base || !caffine_invalidate_endpoint || !transaction_layer_auth_token) { this.logger.error(`Missing configuration: inbound endpoint: ${inbound_base}, caffine reset endpoint: ${caffine_invalidate_endpoint} or transaction layer auth token.`); throw new InternalServerErrorException(); } - const caffine_reset_url = `${inbound_base}${caffine_invalidate_endpoint}`; - return fetch(caffine_reset_url, {method: 'DELETE', headers: {'Authorization': transaction_layer_auth_token}}) + const caffine_reset_url_inbound = `${inbound_base}${caffine_invalidate_endpoint}`; + const caffine_reset_url_orchestrator = `${orchestrator_base}${caffine_invalidate_endpoint}`; + await fetch(caffine_reset_url_inbound, {method: 'DELETE', headers: {'Authorization': transaction_layer_auth_token}}) .then((resp) => { if (resp.ok) { return resp.json(); @@ -637,9 +639,23 @@ export class BotService { }) .then() .catch((err) => { - this.logger.error(`Got failure response from inbound on cache invalidation endpoint ${caffine_reset_url}. Error: ${err}`); + this.logger.error(`Got failure response from inbound on cache invalidation endpoint ${caffine_reset_url_inbound}. Error: ${err}`); throw new ServiceUnavailableException('Could not invalidate cache after update!'); }); + await fetch(caffine_reset_url_orchestrator, {method: 'DELETE', headers: {'Authorization': transaction_layer_auth_token}}) + .then((resp) => { + if (resp.ok) { + return resp.json(); + } + else { + throw new ServiceUnavailableException(resp); + } + }) + .then() + .catch((err) => { + this.logger.error(`Got failure response from inbound on cache invalidation endpoint ${caffine_reset_url_orchestrator}. Error: ${err}`); + throw new ServiceUnavailableException('Could not invalidate cache after update!'); + }) } async remove(deleteBotsDTO: DeleteBotsDTO) { @@ -775,4 +791,32 @@ export class BotService { throw new ServiceUnavailableException('Could not pull data from database!'); }); } + + async modifyNotification(botId: string, title?: string, description?: string) { + const requiredBot = await this.findOne(botId); + if (!botId) { + throw new BadRequestException(`Bot with id: ${botId} does not exist!`); + } + const requiredTransformer = requiredBot?.logicIDs?.[0]?.transformers?.[0]; + if (!requiredTransformer) { + throw new BadRequestException(`Bad configuration! Bot ${botId} does not contain transformer config.`); + } + const meta = requiredTransformer.meta!; + if (title) { + meta['title'] = title; + } + if (description) { + meta['body'] = description; + } + await this.prisma.transformerConfig.update({ + where: { + id: requiredTransformer.id, + }, + data: { + meta: meta, + }, + }); + await this.cacheManager.reset(); + await this.invalidateTransactionLayerCache(); + } } diff --git a/src/modules/bot/dto/update-bot.dto.ts b/src/modules/bot/dto/update-bot.dto.ts index f79f175..54f2e92 100644 --- a/src/modules/bot/dto/update-bot.dto.ts +++ b/src/modules/bot/dto/update-bot.dto.ts @@ -2,3 +2,8 @@ import { PartialType } from '@nestjs/swagger'; import { CreateBotDto } from './create-bot.dto'; export class UpdateBotDto extends PartialType(CreateBotDto) {} + +export type ModifyNotificationDTO = { + title?: string, + description?: string, +}