This commit is contained in:
+77
-17
@@ -1,6 +1,7 @@
|
||||
import { createHash } from 'node:crypto'
|
||||
import { Prisma } from '@prisma/client'
|
||||
import { prisma } from './db.js'
|
||||
import { lockCurrencyPrice, resolveKycPricingContext } from './pricing-context.js'
|
||||
|
||||
interface DemoWorkspaceAccess {
|
||||
teamUuid: string
|
||||
@@ -111,12 +112,22 @@ function stableUuid(teamUuid: string, entity: string, index: number): string {
|
||||
}
|
||||
|
||||
export async function ensureDemoWorkspaceData(access: DemoWorkspaceAccess): Promise<void> {
|
||||
const [quotationCount, orderCount] = await Promise.all([
|
||||
const [quotationCount, orderCount, demoQuotationPricingCount, demoOrderPricingCount] = await Promise.all([
|
||||
prisma.quotation.count({ where: { teamUuid: access.teamUuid } }),
|
||||
prisma.order.count({ where: { teamUuid: access.teamUuid } }),
|
||||
prisma.quotation.count({
|
||||
where: { teamUuid: access.teamUuid, notes: demoNote, sourceAmount: null },
|
||||
}),
|
||||
prisma.order.count({
|
||||
where: { teamUuid: access.teamUuid, notes: demoNote, sourceAmount: null },
|
||||
}),
|
||||
])
|
||||
const needsCalculations = quotationCount === 0
|
||||
const needsOrders = orderCount === 0
|
||||
const needsPricingBackfill = demoQuotationPricingCount > 0 || demoOrderPricingCount > 0
|
||||
const kyc = needsCalculations || needsOrders || needsPricingBackfill
|
||||
? await resolveKycPricingContext(access.userId)
|
||||
: null
|
||||
|
||||
for (const [index, route] of demoRoutes.entries()) {
|
||||
const createsOrder = index < 2
|
||||
@@ -132,11 +143,11 @@ export async function ensureDemoWorkspaceData(access: DemoWorkspaceAccess): Prom
|
||||
status: 'active',
|
||||
sourceCountryCode: route.sourceCountryCode,
|
||||
destinationCountryCode: route.destinationCountryCode,
|
||||
amountUsd: new Prisma.Decimal(route.amountUsd),
|
||||
minPriceUsd: new Prisma.Decimal(route.amountUsd),
|
||||
sourceAmount: new Prisma.Decimal(route.amountUsd),
|
||||
minSourcePrice: new Prisma.Decimal(route.amountUsd),
|
||||
etaDays: route.etaDays,
|
||||
priority: 50 + index,
|
||||
currency: 'USD',
|
||||
sourceCurrency: 'USD',
|
||||
notes: demoNote,
|
||||
},
|
||||
update: {
|
||||
@@ -150,16 +161,25 @@ export async function ensureDemoWorkspaceData(access: DemoWorkspaceAccess): Prom
|
||||
tareTypeCode: null,
|
||||
sourceCountryCode: route.sourceCountryCode,
|
||||
destinationCountryCode: route.destinationCountryCode,
|
||||
amountUsd: new Prisma.Decimal(route.amountUsd),
|
||||
minPriceUsd: new Prisma.Decimal(route.amountUsd),
|
||||
sourceAmount: new Prisma.Decimal(route.amountUsd),
|
||||
minSourcePrice: new Prisma.Decimal(route.amountUsd),
|
||||
etaDays: route.etaDays,
|
||||
priority: 50 + index,
|
||||
currency: 'USD',
|
||||
sourceCurrency: 'USD',
|
||||
notes: demoNote,
|
||||
},
|
||||
})
|
||||
|
||||
if (!needsCalculations && !(needsOrders && createsOrder)) continue
|
||||
if (!needsCalculations && !(needsOrders && createsOrder) && !needsPricingBackfill) continue
|
||||
if (kyc === null) throw new Error('KYC pricing context was not resolved for demo data')
|
||||
const price = await lockCurrencyPrice({
|
||||
sourceAmount: route.amountUsd,
|
||||
sourceCurrency: 'USD',
|
||||
pricingCountryCode: kyc.countryCode,
|
||||
})
|
||||
const pricingEffectiveDate = new Date(`${price.rateEffectiveDate}T00:00:00.000Z`)
|
||||
const pricingQuotedAt = new Date(price.rateQuotedAt)
|
||||
const pricingLockedAt = new Date(price.rateLockedAt)
|
||||
|
||||
const quotationUuid = stableUuid(access.teamUuid, 'demo-quotation', index)
|
||||
const quotation = await prisma.quotation.upsert({
|
||||
@@ -188,8 +208,18 @@ export async function ensureDemoWorkspaceData(access: DemoWorkspaceAccess): Prom
|
||||
routeDistanceKm: route.routeDistanceKm,
|
||||
selectedTariffId: tariff.id,
|
||||
tariffMatchSummary: `Demo tariff: ${route.title}`,
|
||||
totalAmount: new Prisma.Decimal(route.amountUsd),
|
||||
currency: 'USD',
|
||||
sourceAmount: new Prisma.Decimal(price.sourceAmount),
|
||||
sourceCurrency: price.sourceCurrency,
|
||||
clientAmount: new Prisma.Decimal(price.clientAmount),
|
||||
clientCurrency: price.clientCurrency,
|
||||
exchangeRate: new Prisma.Decimal(price.exchangeRate),
|
||||
exchangeRateSnapshotUuid: price.rateSnapshotUuid,
|
||||
exchangeRateProvider: price.provider,
|
||||
exchangeRateEffectiveDate: pricingEffectiveDate,
|
||||
exchangeRateQuotedAt: pricingQuotedAt,
|
||||
exchangeRateLockedAt: pricingLockedAt,
|
||||
pricingCountryCode: price.pricingCountryCode,
|
||||
pricingKycProfileUuid: kyc.profileUuid,
|
||||
etaDays: route.etaDays,
|
||||
notes: demoNote,
|
||||
},
|
||||
@@ -197,14 +227,24 @@ export async function ensureDemoWorkspaceData(access: DemoWorkspaceAccess): Prom
|
||||
teamUuid: access.teamUuid,
|
||||
createdByUserId: access.userId,
|
||||
selectedTariffId: tariff.id,
|
||||
totalAmount: new Prisma.Decimal(route.amountUsd),
|
||||
currency: 'USD',
|
||||
sourceAmount: new Prisma.Decimal(price.sourceAmount),
|
||||
sourceCurrency: price.sourceCurrency,
|
||||
clientAmount: new Prisma.Decimal(price.clientAmount),
|
||||
clientCurrency: price.clientCurrency,
|
||||
exchangeRate: new Prisma.Decimal(price.exchangeRate),
|
||||
exchangeRateSnapshotUuid: price.rateSnapshotUuid,
|
||||
exchangeRateProvider: price.provider,
|
||||
exchangeRateEffectiveDate: pricingEffectiveDate,
|
||||
exchangeRateQuotedAt: pricingQuotedAt,
|
||||
exchangeRateLockedAt: pricingLockedAt,
|
||||
pricingCountryCode: price.pricingCountryCode,
|
||||
pricingKycProfileUuid: kyc.profileUuid,
|
||||
etaDays: route.etaDays,
|
||||
notes: demoNote,
|
||||
},
|
||||
})
|
||||
|
||||
if (!needsOrders || !createsOrder) continue
|
||||
if ((!needsOrders && !needsPricingBackfill) || !createsOrder) continue
|
||||
await prisma.order.upsert({
|
||||
where: { uuid: stableUuid(access.teamUuid, 'demo-order', index) },
|
||||
create: {
|
||||
@@ -214,8 +254,18 @@ export async function ensureDemoWorkspaceData(access: DemoWorkspaceAccess): Prom
|
||||
createdByUserId: access.userId,
|
||||
name: route.title,
|
||||
status: index === 0 ? 'in_transit' : 'processing',
|
||||
totalAmount: new Prisma.Decimal(route.amountUsd),
|
||||
currency: 'USD',
|
||||
sourceAmount: new Prisma.Decimal(price.sourceAmount),
|
||||
sourceCurrency: price.sourceCurrency,
|
||||
clientAmount: new Prisma.Decimal(price.clientAmount),
|
||||
clientCurrency: price.clientCurrency,
|
||||
exchangeRate: new Prisma.Decimal(price.exchangeRate),
|
||||
exchangeRateSnapshotUuid: price.rateSnapshotUuid,
|
||||
exchangeRateProvider: price.provider,
|
||||
exchangeRateEffectiveDate: pricingEffectiveDate,
|
||||
exchangeRateQuotedAt: pricingQuotedAt,
|
||||
exchangeRateLockedAt: pricingLockedAt,
|
||||
pricingCountryCode: price.pricingCountryCode,
|
||||
pricingKycProfileUuid: kyc.profileUuid,
|
||||
sourceLocationName: route.sourceLocationName,
|
||||
sourceCountryCode: route.sourceCountryCode,
|
||||
sourceLatitude: route.sourceLatitude,
|
||||
@@ -231,8 +281,18 @@ export async function ensureDemoWorkspaceData(access: DemoWorkspaceAccess): Prom
|
||||
teamUuid: access.teamUuid,
|
||||
createdByUserId: access.userId,
|
||||
status: index === 0 ? 'in_transit' : 'processing',
|
||||
totalAmount: new Prisma.Decimal(route.amountUsd),
|
||||
currency: 'USD',
|
||||
sourceAmount: new Prisma.Decimal(price.sourceAmount),
|
||||
sourceCurrency: price.sourceCurrency,
|
||||
clientAmount: new Prisma.Decimal(price.clientAmount),
|
||||
clientCurrency: price.clientCurrency,
|
||||
exchangeRate: new Prisma.Decimal(price.exchangeRate),
|
||||
exchangeRateSnapshotUuid: price.rateSnapshotUuid,
|
||||
exchangeRateProvider: price.provider,
|
||||
exchangeRateEffectiveDate: pricingEffectiveDate,
|
||||
exchangeRateQuotedAt: pricingQuotedAt,
|
||||
exchangeRateLockedAt: pricingLockedAt,
|
||||
pricingCountryCode: price.pricingCountryCode,
|
||||
pricingKycProfileUuid: kyc.profileUuid,
|
||||
notes: demoNote,
|
||||
},
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user