diff --git a/node-services/supervisor/src/domain/client-side/configService.ts b/node-services/supervisor/src/domain/client-side/configService.ts index 079e63f608..2ba9382267 100644 --- a/node-services/supervisor/src/domain/client-side/configService.ts +++ b/node-services/supervisor/src/domain/client-side/configService.ts @@ -94,12 +94,10 @@ export default class ConfigService { this.save(); } - public async deleteSupervisedEntity(entityId: any): Promise { + public async deleteSupervisedEntity(entityId: string): Promise { await this.supervisorDatabaseService.deleteSupervisedEntity(entityId); - const index = this.supervisorConfig.entitiesToSupervise.findIndex( - (entity) => entity.entityId === entityId.entityId - ); + const index = this.supervisorConfig.entitiesToSupervise.findIndex((entity) => entity.entityId === entityId); if (index >= 0) { this.supervisorConfig.entitiesToSupervise.splice(index); } diff --git a/node-services/supervisor/src/domain/server-side/supervisorDatabaseService.ts b/node-services/supervisor/src/domain/server-side/supervisorDatabaseService.ts index c641cfabc8..33e0d2c6a5 100644 --- a/node-services/supervisor/src/domain/server-side/supervisorDatabaseService.ts +++ b/node-services/supervisor/src/domain/server-side/supervisorDatabaseService.ts @@ -10,6 +10,7 @@ import {Db, MongoClient} from 'mongodb'; import {Logger} from 'winston'; import {MongoConfig} from './MongoConfig'; +import {EntityToSupervise} from '../application/entityToSupervise'; export default class SupervisorDatabaseService { logger: Logger; @@ -65,7 +66,7 @@ export default class SupervisorDatabaseService { return await this.mongoDB.collection('supervisedEntities').find().toArray(); } - public async saveSupervisedEntity(supervisedEntity: any): Promise { + public async saveSupervisedEntity(supervisedEntity: EntityToSupervise): Promise { try { const query = {entityId: supervisedEntity.entityId}; const update = { @@ -79,7 +80,7 @@ export default class SupervisorDatabaseService { } } - public async getSupervisedEntity(id: any): Promise { + public async getSupervisedEntity(id: string): Promise { try { return await this.mongoDB.collection('supervisedEntities').findOne({entityId: id}); } catch (error) { @@ -87,7 +88,7 @@ export default class SupervisorDatabaseService { } } - public async deleteSupervisedEntity(id: any): Promise { + public async deleteSupervisedEntity(id: string): Promise { try { await this.mongoDB.collection('supervisedEntities').deleteOne({entityId: id}); } catch (error) { diff --git a/node-services/supervisor/src/tests/configService.test.ts b/node-services/supervisor/src/tests/configService.test.ts index 443d53fb63..c230b87d40 100644 --- a/node-services/supervisor/src/tests/configService.test.ts +++ b/node-services/supervisor/src/tests/configService.test.ts @@ -27,6 +27,13 @@ class SupervisorDatabaseServiceStub extends SupervisorDatabaseService { } this.supervisedEntities.push(supervisedEntity); } + + public async deleteSupervisedEntity(id: string): Promise { + const index = this.supervisedEntities.findIndex((entity) => entity.entityId === id); + if (index >= 0) { + this.supervisedEntities.splice(index); + } + } } function getDefaultConfig(): ConfigDTO { @@ -100,4 +107,26 @@ describe('config service', function () { {entityId: 'ENTITY4', supervisors: ['ENTITY1']} ]); }); + + it('Delete supervised Entity', async function () { + const defaultConfig = getDefaultConfig(); + const supervisorDatabaseService = new SupervisorDatabaseServiceStub(); + supervisorDatabaseService.supervisedEntities = [ + {entityId: 'ENTITY4', supervisors: ['ENTITY1']}, + {entityId: 'ENTITY3', supervisors: ['ENTITY2']} + ]; + + const configService = new ConfigService(supervisorDatabaseService, defaultConfig, null, logger); + + await configService.synchronizeWithMongoDb(); + expect(configService.getSupervisorConfig().entitiesToSupervise).toEqual([ + {entityId: 'ENTITY4', supervisors: ['ENTITY1']}, + {entityId: 'ENTITY3', supervisors: ['ENTITY2']} + ]); + + await configService.deleteSupervisedEntity('ENTITY3'); + expect(configService.getSupervisorConfig().entitiesToSupervise).toEqual([ + {entityId: 'ENTITY4', supervisors: ['ENTITY1']} + ]); + }); });