Skip to content

Commit f1082fb

Browse files
committed
Clean up product validation
Make product validation function more readable, similar to change in project validation.
1 parent 13fb91e commit f1082fb

File tree

1 file changed

+26
-17
lines changed
  • source/SIL.AppBuilder.Portal/common/databaseProxy

1 file changed

+26
-17
lines changed

source/SIL.AppBuilder.Portal/common/databaseProxy/Products.ts

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,9 @@ export { deleteProduct as delete };
145145
async function validateProductBase(
146146
projectId: number,
147147
productDefinitionId: number,
148+
/** If this would be `null`, it is set to `undefined` by caller */
148149
storeId?: number,
150+
/** If this would be `null`, it is set to `undefined` by caller */
149151
storeLanguageId?: number
150152
) {
151153
if (storeId === undefined) {
@@ -168,7 +170,7 @@ async function validateProductBase(
168170
const project = await prisma.projects.findUnique({
169171
where: {
170172
Id: projectId,
171-
// Project must have a WorkflowProjectUrl
173+
// Project must have a WorkflowProjectUrl (handled by query)
172174
WorkflowProjectUrl: {
173175
not: null
174176
}
@@ -216,20 +218,27 @@ async function validateProductBase(
216218
}
217219
}
218220
});
219-
// 3. The store is allowed by the organization
220-
return (
221-
(project?.Organization.OrganizationStores.length ?? 0) > 0 &&
222-
// 1. The store's type matches the Workflow's store type
223-
productDefinition?.Workflow.StoreTypeId ===
224-
project?.Organization.OrganizationStores[0].Store.StoreType.Id &&
225-
// 2. The project has a WorkflowProjectUrl
226-
// handled by query
227-
// 4. The language, if specified, is allowed by the store
228-
((storeLanguageId &&
229-
(project?.Organization.OrganizationStores[0].Store.StoreType.StoreLanguages.length ?? 0) >
230-
0) ||
231-
storeLanguageId === undefined) &&
232-
// 5. The product type is allowed by the organization
233-
(project?.Organization.OrganizationProductDefinitions.length ?? 0) > 0
234-
);
221+
222+
/** 3. The store is allowed by the organization */
223+
const storeInOrg = (project?.Organization.OrganizationStores.length ?? 0) > 0;
224+
225+
const prodDefStore = productDefinition?.Workflow.StoreTypeId;
226+
const orgStore = project?.Organization.OrganizationStores[0]?.Store.StoreType.Id;
227+
/** 1. The store's type matches the Workflow's store type
228+
*
229+
* Note: if both are undefined, this would be `true`; however, under those circumstances,
230+
* condition #3 would evaluate to `false`, rendering the whole check `false`.
231+
*/
232+
const storeMatchFlowStore = prodDefStore === orgStore;
233+
234+
const numOrgStoreLangs =
235+
project?.Organization.OrganizationStores[0]?.Store.StoreType.StoreLanguages.length;
236+
/** 4. The language, if specified, is allowed by the store */
237+
const optionalLanguageAllowed = storeLanguageId === undefined || (numOrgStoreLangs ?? 0) > 0;
238+
239+
const numOrgProdDefs = project?.Organization.OrganizationProductDefinitions.length;
240+
/** 5. The product type is allowed by the organization */
241+
const productInOrg = (numOrgProdDefs ?? 0) > 0;
242+
243+
return storeInOrg && storeMatchFlowStore && optionalLanguageAllowed && productInOrg;
235244
}

0 commit comments

Comments
 (0)