import { NextResponse } from 'next/server' import { z } from 'zod' import type { OpenApiRouteDoc } from '@open-mercato/shared/lib/openapi' import { cpqErrorDoc } from '../../_helpers/quoteRouteOpenApi' import { cpqContextError, resolveCpqRouteContext } from '../../context' import { cpqPricePreviewSchema } from '../../../data/validators' import type { DefaultCpqPricingService } from '../../../services/cpqPricingService' import type { ResolvedCharge } from '../../../services/types' import { getBaseCurrencyCode } from '../../../services/types' import { withChargeWaterfall, withoutChargeWaterfall } from '../../../services/priceAdjustments/chargeWaterfall' export const metadata = { POST: { requireAuth: true, requireFeatures: ['cpq.quotes.view'] }, } export async function POST(req: Request) { try { const ctx = await resolveCpqRouteContext(req) const contextError = cpqContextError(ctx) if (contextError) return contextError const body = cpqPricePreviewSchema.parse(await req.json()) const scope = { organizationId: ctx.organizationId, tenantId: ctx.tenantId } const currencyCode = body.currencyCode ?? await getBaseCurrencyCode(ctx.em, scope) const pricingService = ctx.container.resolve('cpqPricingService') as DefaultCpqPricingService let primaryCharges: ResolvedCharge[] = [] if (body.primaryItem) { primaryCharges = await pricingService.resolveProductCharges({ offeringId: body.primaryItem.offeringId ?? undefined, productId: body.primaryItem.productId ?? undefined, configuration: body.primaryItem.configuration, currencyCode, ...scope, }) } const childItems: Array<{ offeringId: string | null; productId: string | null; charges: ResolvedCharge[] }> = [] for (const child of body.childItems) { const charges = await pricingService.resolveProductCharges({ offeringId: child.offeringId ?? undefined, productId: child.productId ?? undefined, configuration: child.configuration, currencyCode, ...scope, }) childItems.push({ offeringId: child.offeringId ?? null, productId: child.productId ?? null, charges, }) } const allCharges = [...primaryCharges, ...childItems.flatMap((c) => c.charges)] const nrcTotal = allCharges .filter((c) => c.chargeType === 'nrc' && c.totalPrice != null) .reduce((s, c) => s + (c.totalPrice ?? 0), 0) const mrcTotal = allCharges .filter((c) => c.chargeType === 'mrc' && c.totalPrice != null) .reduce((s, c) => s + (c.totalPrice ?? 0), 0) const usageCharges = allCharges .filter((c) => c.chargeType === 'usage') .map((c) => ({ chargeCode: c.chargeCode, chargeName: c.chargeName, unitPrice: c.unitPrice, note: c.note ?? 'Usage-based charge — billed on actual consumption.', })) const presentCharge = body.includeWaterfall ? withChargeWaterfall : withoutChargeWaterfall return NextResponse.json({ primaryCharges: primaryCharges.map(presentCharge), childItems: childItems.map((item) => ({ ...item, charges: item.charges.map(presentCharge) })), totals: { nrcTotal, mrcTotal, usageCharges }, currencyCode, }) } catch (err) { if (err instanceof z.ZodError) { return NextResponse.json({ error: 'Validation error', details: err.issues }, { status: 400 }) } console.error('[cpq/quotes/price.POST]', err) return NextResponse.json({ error: 'Internal error' }, { status: 500 }) } } const chargeDoc = z.object({ chargeCode: z.string(), chargeName: z.string(), chargeType: z.enum(['nrc', 'mrc', 'usage']), pricingMethod: z.enum(['flat', 'per_unit', 'volume', 'tiered']), currencyCode: z.string(), unitPrice: z.number(), quantity: z.number().nullable(), totalPrice: z.number().nullable(), listUnitPrice: z.number().optional(), listTotalPrice: z.number().nullable().optional(), standardUnitPrice: z.number().optional(), standardTotalPrice: z.number().nullable().optional(), adjustments: z.array(z.object({ origin: z.literal('rule'), ruleCode: z.string(), ruleName: z.string(), ruleType: z.string(), value: z.number(), unitPriceBefore: z.number(), unitPriceAfter: z.number(), delta: z.number() }).passthrough()).nullish(), }).passthrough() export const openApi: OpenApiRouteDoc = { tag: 'CPQ Pricing', methods: { POST: { summary: 'Calculate catalog prices and price rules without saving', description: 'Manual quote adjustments are never applied. Set includeWaterfall=true to return list and standard prices; these fields are omitted by default.', requestBody: { schema: cpqPricePreviewSchema }, responses: [{ status: 200, description: 'Resolved rule prices and optional waterfall', schema: z.object({ primaryCharges: z.array(chargeDoc), childItems: z.array(z.object({ offeringId: z.string().nullable(), productId: z.string().nullable(), charges: z.array(chargeDoc) })), totals: z.object({ nrcTotal: z.number(), mrcTotal: z.number(), usageCharges: z.array(z.unknown()) }), currencyCode: z.string(), }) }], errors: [cpqErrorDoc(400, 'Invalid pricing request'), cpqErrorDoc(500, 'Price calculation failed')], } } }