Skip to content

Commit

Permalink
Merge pull request #31 from bancodobrasil/fix/menu-meta-validation
Browse files Browse the repository at this point in the history
Fix/menu meta validation
  • Loading branch information
eduardaguterres authored Oct 2, 2023
2 parents 4868cdc + 688a983 commit 2473ee8
Showing 1 changed file with 22 additions and 82 deletions.
104 changes: 22 additions & 82 deletions src/menus/entities/subscribers/menu.subscriber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,17 +121,30 @@ export class MenuSubscriber implements EntitySubscriberInterface<Menu> {
meta: WithAction<MenuMeta>[],
dbMeta?: MenuMeta[],
): void {
const metaWithIndex = meta.map((m, index) => ({ ...m, index }));
const hydrateMeta = (meta: WithAction<MenuMeta>[]) => {
return meta.map((m) => {
if (m.action === InputAction.CREATE) {
return m;
}
const dbMetaItem = dbMeta?.find((m2) => m2.id === m.id) || {};
if (dbMetaItem) {
return { ...dbMetaItem, ...m };
}
return m;
});
};
const entity = hydrateMeta(meta);
const entityWithIndex = entity.map((m, index) => ({ ...m, index }));
const errors = {};
const {
IS_UNIQUE,
META_TYPE_CANNOT_BE_CHANGED,
META_DEFAULT_VALUE_REQUIRED,
} = FieldValidationError.constraints;
// Check if ids are unique
metaWithIndex
entityWithIndex
.filter((m) => {
return metaWithIndex.find(
return entityWithIndex.find(
(m2) => m2.id === m.id && m2.index !== m.index,
);
})
Expand All @@ -147,9 +160,9 @@ export class MenuSubscriber implements EntitySubscriberInterface<Menu> {
};
});
// Check if names are unique
metaWithIndex
entityWithIndex
.filter((m) => {
return metaWithIndex.find(
return entityWithIndex.find(
(m2) => !!m.name && m2.name === m.name && m2.index !== m.index,
);
})
Expand All @@ -165,9 +178,9 @@ export class MenuSubscriber implements EntitySubscriberInterface<Menu> {
};
});
// Check if orders are unique
metaWithIndex
entityWithIndex
.filter((m) => {
return metaWithIndex.find(
return entityWithIndex.find(
(m2) => !!m.order && m2.order === m.order && m2.index !== m.index,
);
})
Expand All @@ -184,81 +197,8 @@ export class MenuSubscriber implements EntitySubscriberInterface<Menu> {
});
if (dbMeta) {
// Is an update
// Check if ids are unique
metaWithIndex
.filter((m) => {
return (
m.action === InputAction.CREATE &&
dbMeta.find((m2) => m2.id === m.id)
);
})
.forEach((m) => {
errors[`meta[${m.index}]`] = {
...errors[`meta[${m.index}]`],
id: {
errors: [
`Menu meta ids must be unique. Found repeated id: ${m.id}`,
],
constraints: [IS_UNIQUE],
},
};
});
// Check if names are unique
metaWithIndex
.filter((m) => {
const existing = dbMeta.find(
(m2) => m2.name === m.name && m2.id !== m.id,
);
return (
!!m.name &&
existing &&
!metaWithIndex.find(
(m2) =>
m2.id === existing.id &&
(m2.name !== m.name || m2.action === InputAction.DELETE),
)
);
})
.forEach((m) => {
errors[`meta[${m.index}]`] = {
...errors[`meta[${m.index}]`],
name: {
errors: [
`Menu meta names must be unique. Found repeated name: ${m.name}`,
],
constraints: [IS_UNIQUE],
},
};
});
// Check if orders are unique
metaWithIndex
.filter((m) => {
const existing = dbMeta.find(
(m2) => m2.order === m.order && m2.id !== m.id,
);
return (
!!m.order &&
existing &&
!metaWithIndex.find(
(m2) =>
m2.id === existing.id &&
(m2.order !== m.order || m2.action === InputAction.DELETE),
)
);
})
.forEach((m) => {
errors[`meta[${m.index}]`] = {
...errors[`meta[${m.index}]`],
order: {
errors: [
`Menu meta orders must be unique. Found repeated order: ${m.order}`,
],
constraints: [IS_UNIQUE],
},
};
});
// Check if types have changed
metaWithIndex
entityWithIndex
.filter((m) => {
const dbMetaItem = dbMeta.find((m2) => m2.id === m.id);
return dbMetaItem && m.type && dbMetaItem.type !== m.type;
Expand All @@ -275,7 +215,7 @@ export class MenuSubscriber implements EntitySubscriberInterface<Menu> {
};
});
// Check if default value is set when required
metaWithIndex
entityWithIndex
.filter((m) => {
const dbMetaItem = dbMeta.find((m2) => m2.id === m.id);
const required =
Expand Down

0 comments on commit 2473ee8

Please sign in to comment.