@@ -145,7 +145,9 @@ export { deleteProduct as delete };
145
145
async function validateProductBase (
146
146
projectId : number ,
147
147
productDefinitionId : number ,
148
+ /** If this would be `null`, it is set to `undefined` by caller */
148
149
storeId ?: number ,
150
+ /** If this would be `null`, it is set to `undefined` by caller */
149
151
storeLanguageId ?: number
150
152
) {
151
153
if ( storeId === undefined ) {
@@ -168,7 +170,7 @@ async function validateProductBase(
168
170
const project = await prisma . projects . findUnique ( {
169
171
where : {
170
172
Id : projectId ,
171
- // Project must have a WorkflowProjectUrl
173
+ // Project must have a WorkflowProjectUrl (handled by query)
172
174
WorkflowProjectUrl : {
173
175
not : null
174
176
}
@@ -216,20 +218,27 @@ async function validateProductBase(
216
218
}
217
219
}
218
220
} ) ;
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 ;
235
244
}
0 commit comments