fix(catalog): moar clean up

This commit is contained in:
Nikita Nek Dudnik
2020-06-11 11:55:48 +02:00
parent 94a23357f2
commit 62b1d6a3ed
@@ -81,30 +81,28 @@ export class DatabaseEntitiesCatalog implements EntitiesCatalog {
async removeEntityByUid(uid: string): Promise<void> {
return await this.database.transaction(async tx => {
const currentEntity = await this.database.entityByUid(tx, uid);
if (!currentEntity) {
const entityResponse = await this.database.entityByUid(tx, uid);
if (!entityResponse) {
throw new NotFoundError(`Entity with ID ${uid} was not found`);
}
const colocatedEntities = currentEntity?.entity.metadata.annotations?.[
LOCATION_ANNOTATION
]
const location =
entityResponse.entity.metadata.annotations?.[LOCATION_ANNOTATION];
const colocatedEntities = location
? await this.database.entities(tx, [
{
key: LOCATION_ANNOTATION,
values: [
currentEntity.entity.metadata.annotations[LOCATION_ANNOTATION],
],
values: [location],
},
])
: [currentEntity];
: [entityResponse];
for (const dbResponse of colocatedEntities) {
await this.database.removeEntity(tx, dbResponse?.entity.metadata.uid!);
}
if (currentEntity?.locationId) {
await this.database.removeLocation(tx, currentEntity?.locationId!);
if (entityResponse.locationId) {
await this.database.removeLocation(tx, entityResponse?.locationId!);
}
return Promise.resolve();
return undefined;
});
}