forked from beyondessential/tupaia
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MAUI-1267: Allow deleting entities via the Admin Panel (beyondessenti…
- Loading branch information
Showing
8 changed files
with
95 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
packages/central-server/src/apiV2/entities/DeleteEntity.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/** | ||
* Tupaia | ||
* Copyright (c) 2017 - 2020 Beyond Essential Systems Pty Ltd | ||
*/ | ||
|
||
import { BESAdminDeleteHandler } from '../DeleteHandler'; | ||
|
||
const UNDELETEABLE_ENTITY_TYPES = ['project', 'country']; | ||
|
||
export class DeleteEntity extends BESAdminDeleteHandler { | ||
async deleteRecord() { | ||
const entity = await this.resourceModel.findById(this.recordId); | ||
if (UNDELETEABLE_ENTITY_TYPES.includes(entity.type)) { | ||
throw new Error(`Cannot delete an entity of type: ${entity.type}`); | ||
} | ||
|
||
// Check for existing survey responses (they should be cleaned up first) | ||
const surveyResponsesForEntity = await this.models.surveyResponse.find({ | ||
entity_id: entity.id, | ||
}); | ||
if (surveyResponsesForEntity.length > 0) { | ||
throw new Error( | ||
`There are still survey responses for this entity, please clean them up before deleting this entity`, | ||
); | ||
} | ||
|
||
// Check for children (they should be given a new parent first) | ||
const hierarchies = await this.models.entityHierarchy.all(); | ||
const hierarchiesWhereEntityHasChildren = []; | ||
for (let i = 0; i < hierarchies.length; i++) { | ||
const hierarchy = hierarchies[i]; | ||
const children = await entity.getChildren(hierarchy.id); | ||
if (children.length > 0) { | ||
hierarchiesWhereEntityHasChildren.push(hierarchy); | ||
} | ||
} | ||
|
||
if (hierarchiesWhereEntityHasChildren.length > 0) { | ||
throw new Error( | ||
`This entity still has children in the following hierarchies [${hierarchiesWhereEntityHasChildren.map( | ||
hierarchy => hierarchy.name, | ||
)}], please delete them or re-import them with a new parent before deleting this entity`, | ||
); | ||
} | ||
|
||
// Delete any entity_relations where this entity is the leaf node, as they prevent deleting the entity otherwise | ||
await this.models.entityRelation.delete({ child_id: this.recordId }); | ||
|
||
await super.deleteRecord(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
packages/central-server/src/apiV2/entities/assertEntityPermissions.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/** | ||
* Tupaia | ||
* Copyright (c) 2017 - 2020 Beyond Essential Systems Pty Ltd | ||
*/ | ||
|
||
export const assertEntityPermissions = async (accessPolicy, models, entityId) => { | ||
const entity = await models.entity.findById(entityId); | ||
if (!entity) { | ||
throw new Error(`No entity exists with id ${entityId}`); | ||
} | ||
if (!accessPolicy.allows(entity.country_code)) { | ||
throw new Error('You do not have permissions for this entity'); | ||
} | ||
return true; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters