@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
239 lines (225 loc) • 9.07 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/backseat/store/index.ts
var store_exports = {};
__export(store_exports, {
QUPS_COLLECTIONS: () => QUPS_COLLECTIONS,
createBackseatPricingLineStore: () => createBackseatPricingLineStore,
createBackseatPricingProfileStore: () => createBackseatPricingProfileStore,
createIndexedDbQupsStores: () => createIndexedDbQupsStores
});
module.exports = __toCommonJS(store_exports);
var import_store = require("@eristack/backseat/store");
// src/backseat/stores.ts
var import_adapters = require("@eristack/backseat/adapters");
// src/backseat/collections.ts
var QUPS_COLLECTIONS = {
profiles: "qups.profiles",
lines: "qups.lines"
};
// src/backseat/stores.ts
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: (0, import_adapters.asDate)(doc.createdAt),
updatedAt: (0, import_adapters.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: (0, import_adapters.asDate)(doc.createdAt),
updatedAt: (0, import_adapters.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
var import_adapters3 = require("@eristack/backseat/adapters");
// src/core/calculate.ts
var import_money4 = require("@eristack/money");
// src/core/line.ts
var import_money3 = require("@eristack/money");
// src/core/modifier.ts
var import_money = require("@eristack/money");
// src/core/qups.ts
var import_decimal = __toESM(require("decimal.js"), 1);
var QtyDecimal = import_decimal.default.clone({
precision: 40,
rounding: import_decimal.default.ROUND_HALF_EVEN
});
// src/core/tax.ts
var import_money2 = require("@eristack/money");
// src/core/stores/serialize.ts
var import_money5 = require("@eristack/money");
// src/core/create-qups.ts
var import_entity_id = require("@eristack/entity-id");
// src/backseat/qups-routes.ts
var import_adapters2 = require("@eristack/backseat/adapters");
// src/backseat/store/index.ts
function createIndexedDbQupsStores(options = {}) {
const backseatStore = (0, import_store.createIndexedDbBackseatStore)({
dbName: options.dbName
});
return {
backseatStore,
profiles: createBackseatPricingProfileStore(backseatStore),
lines: createBackseatPricingLineStore(backseatStore)
};
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
QUPS_COLLECTIONS,
createBackseatPricingLineStore,
createBackseatPricingProfileStore,
createIndexedDbQupsStores
});
//# sourceMappingURL=index.cjs.map