Skip to content

Commit

Permalink
Invalidates cache on transaction layer on bot update. (#204)
Browse files Browse the repository at this point in the history
  • Loading branch information
chinmoy12c authored Aug 3, 2023
1 parent bbcfe22 commit 0298a47
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/modules/bot/bot.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ class MockConfigService {
case 'ODK_BASE_URL': return 'http://odk_form_upload_url';
case 'TRANSFORMER_BASE_URL': return 'http://transformer_base_url';
case 'UCI_CORE_BASE_URL': return 'http://uci_core_base_url';
case 'CAFFINE_INVALIDATE_ENDPOINT': return '/testcaffineendpoint';
case 'AUTHORIZATION_KEY_TRANSACTION_LAYER': return 'testAuthToken';
default: return '';
}
}
Expand Down Expand Up @@ -495,17 +497,24 @@ describe('BotService', () => {
});

it('bot update throws NotFoundException when non existent bot is updated',async () => {
fetchMock.getOnce(`${configService.get<string>('UCI_CORE_BASE_URL')}${configService.get<string>('CAFFINE_INVALIDATE_ENDPOINT')}`,
true
);
expect(botService.update('testBotIdNotExisting', {
'status': 'DISABLED'
}))
.rejects
.toThrowError(new NotFoundException('Bot does not exist!'));
fetchMock.restore();
})

it('bot update calls prisma update', async () => {
fetchMock.getOnce(`${configService.get<string>('MINIO_GET_SIGNED_FILE_URL')}/?fileName=testImageFile`,
'testImageUrl'
);
fetchMock.deleteOnce(`${configService.get<string>('UCI_CORE_BASE_URL')}${configService.get<string>('CAFFINE_INVALIDATE_ENDPOINT')}`,
true
);
await botService.update('testBotIdExisting', {
'status': 'DISABLED'
});
Expand All @@ -517,6 +526,9 @@ describe('BotService', () => {
fetchMock.getOnce(`${configService.get<string>('MINIO_GET_SIGNED_FILE_URL')}/?fileName=testImageFile`,
'testImageUrl'
);
fetchMock.deleteOnce(`${configService.get<string>('UCI_CORE_BASE_URL')}${configService.get<string>('CAFFINE_INVALIDATE_ENDPOINT')}`,
true
);
await expect(botService.update('testBotIdExisting', {
'endDate': '1129-299-092'
}))
Expand All @@ -539,4 +551,19 @@ describe('BotService', () => {
);
expect(resp).toEqual({"data": "sortedBots", "totalCount": 10});
})

it('bot update throws on inbound cache invalidate error',async () => {
fetchMock.getOnce(`${configService.get<string>('MINIO_GET_SIGNED_FILE_URL')}/?fileName=testImageFile`,
'testImageUrl'
);
fetchMock.deleteOnce(`${configService.get<string>('UCI_CORE_BASE_URL')}${configService.get<string>('CAFFINE_INVALIDATE_ENDPOINT')}`, () => {
throw new InternalServerErrorException();
});
await expect(botService.update('testBotIdExisting', {
'endDate': '2023-10-12'
}))
.rejects
.toThrowError(new ServiceUnavailableException('Could not invalidate cache after update!'));
fetchMock.restore();
})
});
22 changes: 22 additions & 0 deletions src/modules/bot/bot.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,14 @@ export class BotService {
}

async update(id: string, updateBotDto: any) {
const inbound_base = this.configService.get<string>('UCI_CORE_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 endpoint: ${caffine_invalidate_endpoint} or transaction layer auth token.`);
throw new InternalServerErrorException();
}
const caffine_reset_url = `${inbound_base}${caffine_invalidate_endpoint}`;
const existingBot = await this.findOne(id);
if (!existingBot) {
throw new NotFoundException("Bot does not exist!")
Expand Down Expand Up @@ -457,6 +465,20 @@ export class BotService {
data: updateBotDto,
});
await this.cacheManager.reset();
await fetch(caffine_reset_url, {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}. Error: ${err}`);
throw new ServiceUnavailableException('Could not invalidate cache after update!');
});
return updatedBot;
}

Expand Down

0 comments on commit 0298a47

Please sign in to comment.