Skip to content

Commit

Permalink
Added feature to modify notification title and description. (#245)
Browse files Browse the repository at this point in the history
  • Loading branch information
chinmoy12c authored Jul 18, 2024
1 parent 28f53c6 commit be90edc
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 3 deletions.
12 changes: 12 additions & 0 deletions src/modules/bot/bot.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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);
}
}
23 changes: 23 additions & 0 deletions src/modules/bot/bot.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 '';
}
}
Expand Down Expand Up @@ -418,6 +419,10 @@ describe('BotService', () => {
configService = module.get<ConfigService>(ConfigService);
});

afterEach(async () => {
fetchMock.restore();
});

it('create bot test', async () => {
fetchMock.postOnce(`${configService.get<string>('MINIO_MEDIA_UPLOAD_URL')}`, {
fileName: 'testFileName'
Expand Down Expand Up @@ -609,6 +614,9 @@ describe('BotService', () => {
fetchMock.deleteOnce(`${configService.get<string>('UCI_CORE_BASE_URL')}${configService.get<string>('CAFFINE_INVALIDATE_ENDPOINT')}`,
true
);
fetchMock.deleteOnce(`${configService.get<string>('ORCHESTRATOR_BASE_URL')}${configService.get<string>('CAFFINE_INVALIDATE_ENDPOINT')}`,
true
);
await botService.update('testBotIdExisting', {
'status': 'DISABLED'
});
Expand All @@ -623,6 +631,9 @@ describe('BotService', () => {
fetchMock.deleteOnce(`${configService.get<string>('UCI_CORE_BASE_URL')}${configService.get<string>('CAFFINE_INVALIDATE_ENDPOINT')}`,
true
);
fetchMock.deleteOnce(`${configService.get<string>('ORCHESTRATOR_BASE_URL')}${configService.get<string>('CAFFINE_INVALIDATE_ENDPOINT')}`,
true
);
await expect(botService.update('testBotIdExisting', {
'endDate': '1129-299-092'
}))
Expand Down Expand Up @@ -653,6 +664,9 @@ describe('BotService', () => {
fetchMock.deleteOnce(`${configService.get<string>('UCI_CORE_BASE_URL')}${configService.get<string>('CAFFINE_INVALIDATE_ENDPOINT')}`, () => {
throw new InternalServerErrorException();
});
fetchMock.deleteOnce(`${configService.get<string>('ORCHESTRATOR_BASE_URL')}${configService.get<string>('CAFFINE_INVALIDATE_ENDPOINT')}`,
true
);
await expect(botService.update('testBotIdExisting', {
'endDate': '2023-10-12'
}))
Expand Down Expand Up @@ -695,6 +709,9 @@ describe('BotService', () => {
fetchMock.delete(`${configService.get<string>('UCI_CORE_BASE_URL')}${configService.get<string>('CAFFINE_INVALIDATE_ENDPOINT')}`,
true
);
fetchMock.delete(`${configService.get<string>('ORCHESTRATOR_BASE_URL')}${configService.get<string>('CAFFINE_INVALIDATE_ENDPOINT')}`,
true
);
await botService.remove({ids: ['testId'], endDate: null});
expect(deletedIds).toEqual(
[
Expand Down Expand Up @@ -728,6 +745,9 @@ describe('BotService', () => {
fetchMock.delete(`${configService.get<string>('UCI_CORE_BASE_URL')}${configService.get<string>('CAFFINE_INVALIDATE_ENDPOINT')}`,
true
);
fetchMock.delete(`${configService.get<string>('ORCHESTRATOR_BASE_URL')}${configService.get<string>('CAFFINE_INVALIDATE_ENDPOINT')}`,
true
);
await botService.remove({ids: null, endDate: '2025-12-01'});
expect(deletedIds).toEqual(
[
Expand Down Expand Up @@ -761,6 +781,9 @@ describe('BotService', () => {
fetchMock.delete(`${configService.get<string>('UCI_CORE_BASE_URL')}${configService.get<string>('CAFFINE_INVALIDATE_ENDPOINT')}`,
true
);
fetchMock.deleteOnce(`${configService.get<string>('ORCHESTRATOR_BASE_URL')}${configService.get<string>('CAFFINE_INVALIDATE_ENDPOINT')}`,
true
);
const response = await botService.remove({ids: ['testId'], endDate: null});
const expectedBotIds = ['testId'];
expect(response).toEqual(expectedBotIds);
Expand Down
50 changes: 47 additions & 3 deletions src/modules/bot/bot.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -619,14 +619,16 @@ export class BotService {

async invalidateTransactionLayerCache() {
const inbound_base = this.configService.get<string>('UCI_CORE_BASE_URL');
const orchestrator_base = this.configService.get<string>('ORCHESTRATOR_BASE_URL');
const caffine_invalidate_endpoint = this.configService.get<string>('CAFFINE_INVALIDATE_ENDPOINT');
const transaction_layer_auth_token = this.configService.get<string>('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();
Expand All @@ -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) {
Expand Down Expand Up @@ -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();
}
}
5 changes: 5 additions & 0 deletions src/modules/bot/dto/update-bot.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

0 comments on commit be90edc

Please sign in to comment.