Skip to content
Snippets Groups Projects
Commit ae8fa588 authored by otho's avatar otho
Browse files

fix(structure update): exclude concerned structure from search query

parent 30bb8657
No related branches found
No related tags found
No related merge requests found
...@@ -96,6 +96,7 @@ export const updateStructure = new ValidatedMethod({ ...@@ -96,6 +96,7 @@ export const updateStructure = new ValidatedMethod({
const structuresWithSameNameOnSameLevel = isAStructureWithSameNameExistWithSameParent({ const structuresWithSameNameOnSameLevel = isAStructureWithSameNameExistWithSameParent({
name, name,
parentId: structure.parentId, parentId: structure.parentId,
structureId: structure._id,
}); });
if (structuresWithSameNameOnSameLevel) { if (structuresWithSameNameOnSameLevel) {
......
...@@ -11,8 +11,22 @@ export const hasAdminRightOnStructure = ({ userId, structureId }) => { ...@@ -11,8 +11,22 @@ export const hasAdminRightOnStructure = ({ userId, structureId }) => {
return isAdmin; return isAdmin;
}; };
export const isAStructureWithSameNameExistWithSameParent = ({ name, parentId }) => { export const isAStructureWithSameNameExistWithSameParent = ({ name, parentId, structureId = undefined }) => {
const regExp = new RegExp(`^${name}$`, 'i'); const regExp = new RegExp(`^${name}$`, 'i');
const structuresWithSameNameOnSameLevel = Structures.find({ name: { $regex: regExp }, parentId }); const query = {
name: { $regex: regExp },
parentId,
};
// structureId will be undefined if we are in a create-structure scenario
// so, don't need to use it
// otherwise, we need to exclude the concerned structure from the query
// that's why we use `$ne` clause/operator
if (typeof structureId === 'string') {
query._id = { $ne: structureId };
}
const structuresWithSameNameOnSameLevel = Structures.find({
...query,
});
return structuresWithSameNameOnSameLevel.count() > 0; return structuresWithSameNameOnSameLevel.count() > 0;
}; };
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment