@happyvertical/smrt-commerce 0.35.1 → 0.35.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,6 @@
1
1
  import { SmrtCollection } from '@happyvertical/smrt-core';
2
2
  import { InvoiceLineItem } from '../models/InvoiceLineItem.js';
3
+ import { AccountingLineItemInput } from '../types/index.js';
3
4
  export declare class InvoiceLineItemCollection extends SmrtCollection<InvoiceLineItem> {
4
5
  static readonly _itemClass: typeof InvoiceLineItem;
5
6
  /**
@@ -65,7 +66,7 @@ export declare class InvoiceLineItemCollection extends SmrtCollection<InvoiceLin
65
66
  * @param invoiceId - Invoice ID
66
67
  * @returns Array of line items in accounting format
67
68
  */
68
- toAccountingLineItems(invoiceId: string): Promise<any[]>;
69
+ toAccountingLineItems(invoiceId: string): Promise<AccountingLineItemInput[]>;
69
70
  /**
70
71
  * Find all invoice line items belonging to a specific tenant
71
72
  *
@@ -1 +1 @@
1
- {"version":3,"file":"InvoiceLineItemCollection.d.ts","sourceRoot":"","sources":["../../src/collections/InvoiceLineItemCollection.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,cAAc,EAAE,MAAM,0BAA0B,CAAC;AAE1D,OAAO,EAAE,eAAe,EAAE,MAAM,8BAA8B,CAAC;AAE/D,qBAAa,yBAA0B,SAAQ,cAAc,CAAC,eAAe,CAAC;IAC5E,MAAM,CAAC,QAAQ,CAAC,UAAU,yBAAmB;IAE7C;;;;;OAKG;IACG,aAAa,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,eAAe,EAAE,CAAC;IAOlE;;;;;;OAMG;IACG,YAAY,CAChB,UAAU,EAAE,MAAM,EAClB,QAAQ,EAAE,MAAM,GACf,OAAO,CAAC,eAAe,EAAE,CAAC;IAO7B;;;;;OAKG;IACG,gBAAgB,CAAC,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,eAAe,EAAE,CAAC;IAOtE;;;;;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,CAAC;IAOlE;;;;;;OAMG;IACG,YAAY,CAChB,UAAU,EAAE,MAAM,EAClB,QAAQ,EAAE,MAAM,GACf,OAAO,CAAC,eAAe,EAAE,CAAC;IAO7B;;;;;OAKG;IACG,gBAAgB,CAAC,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,eAAe,EAAE,CAAC;IAOtE;;;;;OAKG;IACG,kBAAkB,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAK5D;;;;;OAKG;IACG,qBAAqB,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAK/D;;;;;OAKG;IACG,gBAAgB,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAK1D;;;;;OAKG;IACG,gBAAgB,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAQ1D;;;;;OAKG;IACG,kBAAkB,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,eAAe,EAAE,CAAC;IAYvE;;;;;OAKG;IACG,qBAAqB,CACzB,SAAS,EAAE,MAAM,GAChB,OAAO,CAAC,uBAAuB,EAAE,CAAC;IASrC;;;;;OAKG;IACG,YAAY,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,eAAe,EAAE,CAAC;IAIhE;;;;;;;;OAQG;IACG,UAAU,IAAI,OAAO,CAAC,eAAe,EAAE,CAAC;IAI9C;;;;;;;;OAQG;IACG,eAAe,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,eAAe,EAAE,CAAC;CAOpE"}
package/dist/index.js CHANGED
@@ -1785,9 +1785,7 @@ let FulfillmentLineItem = class extends SmrtObject {
1785
1785
  }
1786
1786
  if (this.contractLineItemId) {
1787
1787
  const { ContractLineItemCollection: ContractLineItemCollection2 } = await Promise.resolve().then(() => ContractLineItemCollection$1);
1788
- const lineItems = await ContractLineItemCollection2.create(
1789
- this.options
1790
- );
1788
+ const lineItems = await ContractLineItemCollection2.create(this.options);
1791
1789
  const orderedLineItem = await lineItems.get(this.contractLineItemId);
1792
1790
  if (!orderedLineItem) {
1793
1791
  throw new Error(
@@ -1796,9 +1794,7 @@ let FulfillmentLineItem = class extends SmrtObject {
1796
1794
  }
1797
1795
  if (this.fulfillmentId) {
1798
1796
  const { FulfillmentCollection: FulfillmentCollection2 } = await Promise.resolve().then(() => FulfillmentCollection$1);
1799
- const fulfillments = await FulfillmentCollection2.create(
1800
- this.options
1801
- );
1797
+ const fulfillments = await FulfillmentCollection2.create(this.options);
1802
1798
  const fulfillment = await fulfillments.get(this.fulfillmentId);
1803
1799
  if (fulfillment && orderedLineItem.contractId !== fulfillment.contractId) {
1804
1800
  throw new Error(
@@ -1808,9 +1804,7 @@ let FulfillmentLineItem = class extends SmrtObject {
1808
1804
  }
1809
1805
  const ordered = Number(orderedLineItem.quantity);
1810
1806
  const { FulfillmentLineItemCollection: FulfillmentLineItemCollection2 } = await Promise.resolve().then(() => FulfillmentLineItemCollection$1);
1811
- const siblings = await FulfillmentLineItemCollection2.create(
1812
- this.options
1813
- );
1807
+ const siblings = await FulfillmentLineItemCollection2.create(this.options);
1814
1808
  const existing = await siblings.findByContractLineItem(
1815
1809
  this.contractLineItemId
1816
1810
  );
@@ -2225,7 +2219,9 @@ let Invoice = class extends SmrtObject {
2225
2219
  async recomputeAmountsForSave(persisted) {
2226
2220
  if (persisted && this.id) {
2227
2221
  const { InvoiceLineItemCollection: InvoiceLineItemCollection2 } = await Promise.resolve().then(() => InvoiceLineItemCollection$1);
2228
- const lineItemCollection = await InvoiceLineItemCollection2.create(this.options);
2222
+ const lineItemCollection = await InvoiceLineItemCollection2.create(
2223
+ this.options
2224
+ );
2229
2225
  const lineItems = await lineItemCollection.findByInvoice(this.id);
2230
2226
  if (lineItems.length > 0) {
2231
2227
  const subtotal = lineItems.reduce(
@@ -2244,7 +2240,9 @@ let Invoice = class extends SmrtObject {
2244
2240
  this.assertTotalArithmetic();
2245
2241
  }
2246
2242
  const { PaymentAllocationCollection: PaymentAllocationCollection2 } = await Promise.resolve().then(() => PaymentAllocationCollection$1);
2247
- const allocationCollection = await PaymentAllocationCollection2.create(this.options);
2243
+ const allocationCollection = await PaymentAllocationCollection2.create(
2244
+ this.options
2245
+ );
2248
2246
  const allocated = await allocationCollection.getTotalAllocatedToInvoice(
2249
2247
  this.id
2250
2248
  );
@@ -2546,9 +2544,7 @@ let Invoice = class extends SmrtObject {
2546
2544
  );
2547
2545
  }
2548
2546
  const { JournalCollection } = await import("@happyvertical/smrt-ledgers");
2549
- const journalCollection = await JournalCollection.create(
2550
- this.options
2551
- );
2547
+ const journalCollection = await JournalCollection.create(this.options);
2552
2548
  const entries = [
2553
2549
  // Debit AR (assets increase)
2554
2550
  {
@@ -2577,6 +2573,11 @@ let Invoice = class extends SmrtObject {
2577
2573
  entries
2578
2574
  });
2579
2575
  await journal.post();
2576
+ if (!journal.id) {
2577
+ throw new Error(
2578
+ "Invoice.recognizeRevenue: journal was posted but has no id"
2579
+ );
2580
+ }
2580
2581
  this.arJournalId = journal.id;
2581
2582
  try {
2582
2583
  await this.save();
@@ -2631,16 +2632,24 @@ let Invoice = class extends SmrtObject {
2631
2632
  const lineItemCollection = await InvoiceLineItemCollection2.create(
2632
2633
  this.options
2633
2634
  );
2634
- const lineItems = await lineItemCollection.findByInvoice(this.id);
2635
+ if (!this.id) {
2636
+ throw new Error(
2637
+ "Invoice.toAccountingInput requires a persisted invoice (missing id)"
2638
+ );
2639
+ }
2640
+ const invoiceId = this.id;
2641
+ const lineItems = await lineItemCollection.findByInvoice(invoiceId);
2635
2642
  return {
2636
- id: this.id,
2643
+ id: invoiceId,
2637
2644
  externalId: this.externalId || void 0,
2638
2645
  invoiceNumber: this.invoiceNumber,
2639
2646
  customerId: this.customerId,
2640
2647
  customerExternalId: this.customerExternalId || void 0,
2641
2648
  issueDate: this.issueDate,
2642
2649
  dueDate: this.dueDate,
2643
- lineItems: lineItems.map((item) => item.toAccountingLineItem()),
2650
+ lineItems: lineItems.map(
2651
+ (item) => item.toAccountingLineItem()
2652
+ ),
2644
2653
  subtotal: this.subtotal,
2645
2654
  taxAmount: this.taxAmount,
2646
2655
  totalAmount: this.totalAmount,
@@ -3966,9 +3975,7 @@ let Payment = class extends SmrtObject {
3966
3975
  throw new Error("Payment amount must be positive");
3967
3976
  }
3968
3977
  const { JournalCollection } = await import("@happyvertical/smrt-ledgers");
3969
- const journalCollection = await JournalCollection.create(
3970
- this.options
3971
- );
3978
+ const journalCollection = await JournalCollection.create(this.options);
3972
3979
  const journal = await journalCollection.create({
3973
3980
  date: /* @__PURE__ */ new Date(),
3974
3981
  description: `Payment received for contract ${this.contractId}`,
@@ -3987,6 +3994,11 @@ let Payment = class extends SmrtObject {
3987
3994
  memo: `Payment ${this.reference || this.id}`
3988
3995
  });
3989
3996
  await journal.post();
3997
+ if (!journal.id) {
3998
+ throw new Error(
3999
+ "Payment.recordPayment: journal was posted but has no id"
4000
+ );
4001
+ }
3990
4002
  this.journalId = journal.id;
3991
4003
  this.status = PaymentStatus.COMPLETED;
3992
4004
  this.paidAt = /* @__PURE__ */ new Date();
@@ -5041,7 +5053,7 @@ class PaymentIntentCollection extends SmrtCollection {
5041
5053
  if (existing) {
5042
5054
  return { intent: existing, created: false };
5043
5055
  }
5044
- return { intent: await this.create(seed), created: true };
5056
+ return { intent: await this.create({ ...seed }), created: true };
5045
5057
  }
5046
5058
  /**
5047
5059
  * Open intents — `AWAITING_PAYMENT` and not yet past the price-