From 4708e2fccf2fdbf452b54e1a12631a92798741c4 Mon Sep 17 00:00:00 2001 From: Chinmoy Chakraborty Date: Wed, 17 Jul 2024 00:03:03 +0530 Subject: [PATCH] Added feature to modify notification title and description. --- 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 eb5345f..cdc61bf 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) => { @@ -372,4 +373,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 e5d7f6f..dabb676 100644 --- a/src/modules/bot/bot.service.spec.ts +++ b/src/modules/bot/bot.service.spec.ts @@ -105,6 +105,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 ''; } } @@ -398,6 +399,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' @@ -589,6 +594,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' }); @@ -603,6 +611,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' })) @@ -633,6 +644,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' })) @@ -675,6 +689,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( [ @@ -708,6 +725,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( [ @@ -741,6 +761,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 38c3704..1dbf2b7 100644 --- a/src/modules/bot/bot.service.ts +++ b/src/modules/bot/bot.service.ts @@ -607,14 +607,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(); @@ -625,9 +627,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) { @@ -763,4 +779,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, +}