UNPKG

@eristack/qups

Version:

Quantity / unit price / subtotal (QUPS) with 2-of-3 sources of truth, plus modifiers and tax — business line pricing on @eristack/money

216 lines (210 loc) • 7.56 kB
import { calculateLine, createQups, patchLine } from "./chunk-UP24CFOV.js"; // src/backseat/collections.ts var QUPS_COLLECTIONS = { profiles: "qups.profiles", lines: "qups.lines" }; // src/backseat/stores.ts import { asDate } from "@eristack/backseat/adapters"; function profileFromDoc(doc) { return { id: String(doc.id), entityKey: String(doc.entityKey), defaultTruth: String(doc.defaultTruth), defaultCurrencyCode: String(doc.defaultCurrencyCode), fields: Array.isArray(doc.fields) ? doc.fields : [], defaultTaxRatePercent: doc.defaultTaxRatePercent == null ? void 0 : String(doc.defaultTaxRatePercent), defaultTaxMode: doc.defaultTaxMode == null ? void 0 : String(doc.defaultTaxMode), allowedModifierKinds: Array.isArray(doc.allowedModifierKinds) ? doc.allowedModifierKinds : void 0, active: Boolean(doc.active), createdAt: asDate(doc.createdAt), updatedAt: asDate(doc.updatedAt) }; } function profileToDoc(record) { return { id: record.id, entityKey: record.entityKey, defaultTruth: record.defaultTruth, defaultCurrencyCode: record.defaultCurrencyCode, fields: record.fields, defaultTaxRatePercent: record.defaultTaxRatePercent ?? null, defaultTaxMode: record.defaultTaxMode ?? null, allowedModifierKinds: record.allowedModifierKinds ?? null, active: record.active, createdAt: record.createdAt.toISOString(), updatedAt: record.updatedAt.toISOString() }; } function createBackseatPricingProfileStore(store, options = {}) { const collection = options.collection ?? QUPS_COLLECTIONS.profiles; async function allProfiles() { const docs = await store.list(collection); return docs.map(profileFromDoc); } return { async save(record) { const doc = profileToDoc(record); const existing = await store.get(collection, record.id); if (existing) { await store.update(collection, record.id, doc); return; } await store.create(collection, doc); }, async findById(id) { const doc = await store.get(collection, id); return doc ? profileFromDoc(doc) : null; }, async findActiveByEntityKey(entityKey) { const matches = (await allProfiles()).filter((row) => row.entityKey === entityKey && row.active).sort((a, b) => b.updatedAt.getTime() - a.updatedAt.getTime()); return matches[0] ?? null; }, async listByEntityKey(entityKey) { return (await allProfiles()).filter((row) => row.entityKey === entityKey).sort((a, b) => b.updatedAt.getTime() - a.updatedAt.getTime()); } }; } function lineFromDoc(doc) { return { id: String(doc.id), ownerKey: String(doc.ownerKey), profileId: doc.profileId == null ? void 0 : String(doc.profileId), truth: String(doc.truth), quantity: String(doc.quantity), quantityRatioNumerator: doc.quantityRatioNumerator == null ? void 0 : String(doc.quantityRatioNumerator), quantityRatioDenominator: doc.quantityRatioDenominator == null ? void 0 : String(doc.quantityRatioDenominator), currency: String(doc.currency), unitPriceAmount: String(doc.unitPriceAmount), subtotalAmount: String(doc.subtotalAmount), taxRatePercent: doc.taxRatePercent == null ? void 0 : String(doc.taxRatePercent), taxMode: doc.taxMode == null ? void 0 : String(doc.taxMode), taxAmount: doc.taxAmount == null ? void 0 : String(doc.taxAmount), netAmount: doc.netAmount == null ? void 0 : String(doc.netAmount), grossAmount: doc.grossAmount == null ? void 0 : String(doc.grossAmount), modifiers: Array.isArray(doc.modifiers) ? doc.modifiers : [], fieldValues: Array.isArray(doc.fieldValues) ? doc.fieldValues : [], rowExtras: doc.rowExtras && typeof doc.rowExtras === "object" ? doc.rowExtras : void 0, position: doc.position == null ? void 0 : Number(doc.position), createdAt: asDate(doc.createdAt), updatedAt: asDate(doc.updatedAt) }; } function lineToDoc(record) { return { id: record.id, ownerKey: record.ownerKey, profileId: record.profileId ?? null, truth: record.truth, quantity: record.quantity, quantityRatioNumerator: record.quantityRatioNumerator ?? null, quantityRatioDenominator: record.quantityRatioDenominator ?? null, currency: record.currency, unitPriceAmount: record.unitPriceAmount, subtotalAmount: record.subtotalAmount, taxRatePercent: record.taxRatePercent ?? null, taxMode: record.taxMode ?? null, taxAmount: record.taxAmount ?? null, netAmount: record.netAmount ?? null, grossAmount: record.grossAmount ?? null, modifiers: record.modifiers, fieldValues: record.fieldValues, rowExtras: record.rowExtras ?? null, position: record.position ?? null, createdAt: record.createdAt.toISOString(), updatedAt: record.updatedAt.toISOString() }; } function createBackseatPricingLineStore(store, options = {}) { const collection = options.collection ?? QUPS_COLLECTIONS.lines; async function allLines() { const docs = await store.list(collection); return docs.map(lineFromDoc); } return { async save(record) { const doc = lineToDoc(record); const existing = await store.get(collection, record.id); if (existing) { await store.update(collection, record.id, doc); return; } await store.create(collection, doc); }, async findById(id) { const doc = await store.get(collection, id); return doc ? lineFromDoc(doc) : null; }, async listByOwnerKey(ownerKey) { return (await allLines()).filter((row) => row.ownerKey === ownerKey); }, async delete(id) { const existing = await store.get(collection, id); if (existing) await store.delete(collection, id); } }; } // src/backseat/register.ts import { registerMountedRoutes } from "@eristack/backseat/adapters"; // src/backseat/qups-routes.ts import { requireParam } from "@eristack/backseat/adapters"; function createQupsRoutes(qups) { return [ { method: "POST", segment: "/calculate-line", name: "qups.calculate-line", handler: async (ctx) => ({ status: 200, body: calculateLine(ctx.json()) }) }, { method: "POST", segment: "/patch-line", name: "qups.patch-line", handler: async (ctx) => { const body = ctx.json(); return { status: 200, body: patchLine(body.current, body.patch) }; } }, { method: "GET", segment: "/lines/:ownerKey", name: "qups.list-lines", handler: async (ctx) => { const ownerKey = requireParam(ctx.params.ownerKey, "ownerKey required"); if (typeof ownerKey !== "string") return ownerKey; return { status: 200, body: await qups.listLines(ownerKey) }; } } ]; } // src/backseat/register.ts function registerQupsBackseat(api, options = {}) { const profiles = options.profiles ?? createBackseatPricingProfileStore(api.store); const lines = options.lines ?? createBackseatPricingLineStore(api.store); const qups = options.qups ?? createQups({ profiles, lines, clock: options.clock, idFactory: options.idFactory }); registerMountedRoutes(api, options.basePath ?? "/qups", createQupsRoutes(qups)); api.registerAction( "qups.calculateLine", async ({ input }) => calculateLine(input) ); return qups; } export { QUPS_COLLECTIONS, createBackseatPricingProfileStore, createBackseatPricingLineStore, registerQupsBackseat }; //# sourceMappingURL=chunk-CJS77QP5.js.map