diff --git a/src/demo-workspace.ts b/src/demo-workspace.ts index a3bcaf2..68480ab 100644 --- a/src/demo-workspace.ts +++ b/src/demo-workspace.ts @@ -169,8 +169,10 @@ export async function ensureDemoWorkspaceData(access: DemoWorkspaceAccess): Prom orderCount, demoQuotationPricingCount, demoOrderPricingCount, - demoQuotationWithoutRouteCount, - demoOrderWithoutRouteCount, + demoQuotationCount, + demoOrderCount, + demoQuotationRouteStageCount, + demoOrderRouteStageCount, ] = await Promise.all([ prisma.quotation.count({ where: { teamUuid: access.teamUuid } }), prisma.order.count({ where: { teamUuid: access.teamUuid } }), @@ -181,16 +183,31 @@ export async function ensureDemoWorkspaceData(access: DemoWorkspaceAccess): Prom where: { teamUuid: access.teamUuid, notes: demoNote, sourceAmount: null }, }), prisma.quotation.count({ - where: { teamUuid: access.teamUuid, notes: demoNote, routeStages: { none: {} } }, + where: { teamUuid: access.teamUuid, notes: demoNote }, }), prisma.order.count({ - where: { teamUuid: access.teamUuid, notes: demoNote, routeStages: { none: {} } }, + where: { teamUuid: access.teamUuid, notes: demoNote }, + }), + prisma.quotationRouteStage.count({ + where: { quotation: { teamUuid: access.teamUuid, notes: demoNote } }, + }), + prisma.orderRouteStage.count({ + where: { order: { teamUuid: access.teamUuid, notes: demoNote } }, }), ]) const needsCalculations = quotationCount === 0 const needsOrders = orderCount === 0 const needsPricingBackfill = demoQuotationPricingCount > 0 || demoOrderPricingCount > 0 - const needsRouteBackfill = demoQuotationWithoutRouteCount > 0 || demoOrderWithoutRouteCount > 0 + const expectedQuotationRouteStageCount = demoRoutes.reduce( + (total, route) => total + route.stages.length, + 0, + ) + const expectedOrderRouteStageCount = demoRoutes + .slice(0, 2) + .reduce((total, route) => total + route.stages.length, 0) + const needsRouteBackfill = + (demoQuotationCount > 0 && demoQuotationRouteStageCount < expectedQuotationRouteStageCount) || + (demoOrderCount > 0 && demoOrderRouteStageCount < expectedOrderRouteStageCount) const kyc = needsCalculations || needsOrders || needsPricingBackfill || needsRouteBackfill ? await ensureDemoKycPricingContext({ teamUuid: access.teamUuid,