Skip to content

Commit

Permalink
Supervisor: fix Supervised Entity Not Deleted Until Service Restart (#…
Browse files Browse the repository at this point in the history
…6170)

Signed-off-by: Giovanni Ferrari <[email protected]>
  • Loading branch information
quinarygio authored and freddidierRTE committed Mar 27, 2024
1 parent a22ae0a commit 9756ed6
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,10 @@ export default class ConfigService {
this.save();
}

public async deleteSupervisedEntity(entityId: any): Promise<void> {
public async deleteSupervisedEntity(entityId: string): Promise<void> {
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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -65,7 +66,7 @@ export default class SupervisorDatabaseService {
return await this.mongoDB.collection('supervisedEntities').find().toArray();
}

public async saveSupervisedEntity(supervisedEntity: any): Promise<void> {
public async saveSupervisedEntity(supervisedEntity: EntityToSupervise): Promise<void> {
try {
const query = {entityId: supervisedEntity.entityId};
const update = {
Expand All @@ -79,15 +80,15 @@ export default class SupervisorDatabaseService {
}
}

public async getSupervisedEntity(id: any): Promise<any> {
public async getSupervisedEntity(id: string): Promise<any> {
try {
return await this.mongoDB.collection('supervisedEntities').findOne({entityId: id});
} catch (error) {
this.logger.error('Mongo error in find supervised Entity' + JSON.stringify(error));
}
}

public async deleteSupervisedEntity(id: any): Promise<void> {
public async deleteSupervisedEntity(id: string): Promise<void> {
try {
await this.mongoDB.collection('supervisedEntities').deleteOne({entityId: id});
} catch (error) {
Expand Down
29 changes: 29 additions & 0 deletions node-services/supervisor/src/tests/configService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ class SupervisorDatabaseServiceStub extends SupervisorDatabaseService {
}
this.supervisedEntities.push(supervisedEntity);
}

public async deleteSupervisedEntity(id: string): Promise<void> {
const index = this.supervisedEntities.findIndex((entity) => entity.entityId === id);
if (index >= 0) {
this.supervisedEntities.splice(index);
}
}
}

function getDefaultConfig(): ConfigDTO {
Expand Down Expand Up @@ -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']}
]);
});
});

0 comments on commit 9756ed6

Please sign in to comment.