pecunia-core 0.2.0 → 0.2.1

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.
package/dist/db/index.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  import "../errors-vpD21Iku.mjs";
2
- import { a as productSchema, c as customerSchema, d as initGetModelName, f as initGetFieldAttributes, h as initGetDefaultModelName, i as subscriptionSchema, l as sharedCoreSchema, m as initGetDefaultFieldName, n as deepmerge, o as priceSchema, p as initGetIdField, r as withApplyDefault, s as discountSchema, t as getPaymentTables, u as sharedDeletableSchema } from "../get-payment-tables-CxhHAsKo.mjs";
2
+ import { a as productSchema, c as customerSchema, d as initGetModelName, f as initGetFieldAttributes, h as initGetDefaultModelName, i as subscriptionSchema, l as sharedCoreSchema, m as initGetDefaultFieldName, n as deepmerge, o as priceSchema, p as initGetIdField, r as withApplyDefault, s as discountSchema, t as getPaymentTables, u as sharedDeletableSchema } from "../get-payment-tables-6NA4nccG.mjs";
3
3
  import "../generate-id-D3ZvbbXn.mjs";
4
4
 
5
5
  //#region src/db/attributes/get-field-name.ts
@@ -1,4 +1,4 @@
1
- import { A as UsageAggregation, D as TransactionStatus, E as SubscriptionStatus, M as WebHookEventStatus, O as TransactionType, S as ProrationBehavior, T as ScheduledTaskType, _ as PaymentIntentStatus, b as PricingModel, c as BillingInterval, d as CollectionMethod, f as Currency, h as InvoiceStatus, j as WebHookDeliveryTrigger, l as CheckoutSessionMode, n as PecuniaError, o as Actor, p as DiscountType, s as AuditAction, u as CheckoutSessionStatus, v as PaymentProviders, w as ScheduledTaskStatus, x as ProductType, y as PriceType } from "./errors-vpD21Iku.mjs";
1
+ import { A as UsageAggregation, D as TransactionStatus, E as SubscriptionStatus, M as WebHookEventStatus, O as TransactionType, S as ProrationBehavior, T as ScheduledTaskType, _ as PaymentIntentStatus, b as PricingModel, c as BillingInterval, d as CollectionMethod, f as Currency, h as InvoiceStatus, j as WebHookDeliveryTrigger, k as UsageAction, l as CheckoutSessionMode, n as PecuniaError, o as Actor, p as DiscountType, s as AuditAction, u as CheckoutSessionStatus, v as PaymentProviders, w as ScheduledTaskStatus, x as ProductType, y as PriceType } from "./errors-vpD21Iku.mjs";
2
2
  import { t as generateId } from "./generate-id-D3ZvbbXn.mjs";
3
3
  import z$1, { z } from "zod";
4
4
 
@@ -533,6 +533,369 @@ const getPaymentTables = (options) => {
533
533
  ]
534
534
  }
535
535
  },
536
+ price: {
537
+ modelName: "prices",
538
+ order: 1,
539
+ fields: {
540
+ id: {
541
+ type: "string",
542
+ fieldName: "id",
543
+ required: true,
544
+ input: false,
545
+ returned: true,
546
+ unique: true,
547
+ validator: { output: z.uuid() }
548
+ },
549
+ productId: {
550
+ type: "string",
551
+ fieldName: "product_id",
552
+ required: false,
553
+ references: {
554
+ model: "products",
555
+ field: "id",
556
+ onDelete: "cascade"
557
+ },
558
+ validator: {
559
+ input: z.uuid().optional(),
560
+ output: z.uuid().optional()
561
+ },
562
+ index: true
563
+ },
564
+ unitAmount: {
565
+ type: "number",
566
+ fieldName: "unit_amount",
567
+ required: false,
568
+ defaultValue: 0,
569
+ validator: {
570
+ input: z.number().int().min(0).optional(),
571
+ output: z.number().int()
572
+ }
573
+ },
574
+ currency: {
575
+ type: "string",
576
+ fieldName: "currency",
577
+ required: false,
578
+ defaultValue: Currency.KES,
579
+ validator: {
580
+ input: z.enum(Currency).optional(),
581
+ output: z.enum(Currency)
582
+ },
583
+ sortable: true
584
+ },
585
+ type: {
586
+ type: "string",
587
+ fieldName: "price_type",
588
+ required: false,
589
+ defaultValue: PriceType.ONE_TIME,
590
+ validator: {
591
+ input: z.enum(PriceType).optional(),
592
+ output: z.enum(PriceType)
593
+ },
594
+ sortable: true
595
+ },
596
+ pricingModel: {
597
+ type: "string",
598
+ fieldName: "pricing_model",
599
+ required: false,
600
+ defaultValue: PricingModel.FLAT,
601
+ validator: {
602
+ input: z.enum(PricingModel).optional(),
603
+ output: z.enum(PricingModel)
604
+ },
605
+ sortable: true
606
+ },
607
+ billingInterval: {
608
+ type: "string",
609
+ fieldName: "billing_interval",
610
+ required: false,
611
+ defaultValue: BillingInterval.MONTH,
612
+ validator: {
613
+ input: z.enum(BillingInterval).optional(),
614
+ output: z.enum(BillingInterval)
615
+ },
616
+ sortable: true
617
+ },
618
+ billingIntervalCount: {
619
+ type: "number",
620
+ fieldName: "billing_interval_count",
621
+ required: false,
622
+ defaultValue: 1,
623
+ validator: {
624
+ input: z.number().int().min(1).optional(),
625
+ output: z.number().int()
626
+ }
627
+ },
628
+ trialPeriodDays: {
629
+ type: "number",
630
+ fieldName: "trial_period_days",
631
+ required: false,
632
+ defaultValue: 0,
633
+ validator: {
634
+ input: z.number().int().min(0).optional(),
635
+ output: z.number().int()
636
+ }
637
+ },
638
+ usageAggregation: {
639
+ type: "string",
640
+ fieldName: "usage_aggregation",
641
+ required: false,
642
+ validator: {
643
+ input: z.string().optional(),
644
+ output: z.string().optional()
645
+ },
646
+ sortable: true
647
+ },
648
+ active: {
649
+ type: "boolean",
650
+ fieldName: "active",
651
+ required: false,
652
+ defaultValue: true,
653
+ validator: {
654
+ input: z.boolean().optional(),
655
+ output: z.boolean()
656
+ }
657
+ },
658
+ metadata: {
659
+ type: "json",
660
+ fieldName: "metadata",
661
+ required: false,
662
+ validator: {
663
+ input: z.unknown().optional(),
664
+ output: z.unknown().optional()
665
+ }
666
+ },
667
+ deleted: {
668
+ type: "boolean",
669
+ fieldName: "deleted",
670
+ required: true,
671
+ defaultValue: false,
672
+ input: false,
673
+ returned: true,
674
+ validator: { output: z.boolean() },
675
+ index: true
676
+ },
677
+ deletedAt: {
678
+ type: "date",
679
+ fieldName: "deleted_at",
680
+ required: false,
681
+ input: false,
682
+ returned: true,
683
+ validator: { output: z.date().optional() }
684
+ },
685
+ createdAt: {
686
+ type: "date",
687
+ fieldName: "created_at",
688
+ required: true,
689
+ input: false,
690
+ returned: true,
691
+ defaultValue: () => /* @__PURE__ */ new Date(),
692
+ validator: { output: z.date() }
693
+ },
694
+ updatedAt: {
695
+ type: "date",
696
+ fieldName: "updated_at",
697
+ required: true,
698
+ input: false,
699
+ returned: true,
700
+ defaultValue: () => /* @__PURE__ */ new Date(),
701
+ onUpdate: () => /* @__PURE__ */ new Date(),
702
+ validator: { output: z.date() }
703
+ }
704
+ },
705
+ constraints: { indexes: [
706
+ {
707
+ name: "price_product_active_idx",
708
+ fields: ["productId", "active"]
709
+ },
710
+ {
711
+ name: "price_type_active_idx",
712
+ fields: ["type", "active"]
713
+ },
714
+ {
715
+ name: "price_deleted_idx",
716
+ fields: ["deleted"]
717
+ }
718
+ ] },
719
+ checks: [{
720
+ name: "billing_interval_countx",
721
+ kind: "gt",
722
+ field: "billingIntervalCount",
723
+ value: 0
724
+ }],
725
+ relations: {
726
+ product: {
727
+ kind: "one",
728
+ model: "products",
729
+ foreignKey: "productId"
730
+ },
731
+ subscriptionItems: {
732
+ kind: "many",
733
+ model: "subscription_item",
734
+ foreignKey: "priceId"
735
+ },
736
+ invoiceItems: {
737
+ kind: "many",
738
+ model: "invoice_item",
739
+ foreignKey: "priceId"
740
+ }
741
+ }
742
+ },
743
+ idempotency_key: {
744
+ modelName: "idempotency_key",
745
+ order: 1,
746
+ fields: {
747
+ id: {
748
+ type: "string",
749
+ fieldName: "id",
750
+ required: true,
751
+ input: false,
752
+ returned: true,
753
+ validator: { output: z.uuid() }
754
+ },
755
+ customerId: {
756
+ type: "string",
757
+ fieldName: "customer_id",
758
+ required: true,
759
+ references: {
760
+ model: "customer",
761
+ field: "id",
762
+ onDelete: "cascade"
763
+ },
764
+ validator: {
765
+ input: z.uuid(),
766
+ output: z.uuid()
767
+ },
768
+ index: true
769
+ },
770
+ key: {
771
+ type: "string",
772
+ fieldName: "key",
773
+ required: true,
774
+ sortable: true,
775
+ validator: {
776
+ input: z.string().max(255),
777
+ output: z.string().max(255)
778
+ },
779
+ index: true
780
+ },
781
+ requestPath: {
782
+ type: "string",
783
+ fieldName: "request_path",
784
+ required: false,
785
+ validator: {
786
+ input: z.string().max(150).optional(),
787
+ output: z.string().max(150).optional()
788
+ },
789
+ index: true
790
+ },
791
+ requestMethod: {
792
+ type: "string",
793
+ fieldName: "request_method",
794
+ required: false,
795
+ validator: {
796
+ input: z.string().max(10).optional(),
797
+ output: z.string().max(10).optional()
798
+ },
799
+ index: true
800
+ },
801
+ requestBody: {
802
+ type: "json",
803
+ fieldName: "request_body",
804
+ required: false,
805
+ validator: {
806
+ input: z.unknown().optional(),
807
+ output: z.unknown().optional()
808
+ }
809
+ },
810
+ responseStatus: {
811
+ type: "number",
812
+ fieldName: "response_status",
813
+ required: false,
814
+ validator: {
815
+ input: z.number().int().optional(),
816
+ output: z.number().int().optional()
817
+ },
818
+ index: true
819
+ },
820
+ responseBody: {
821
+ type: "json",
822
+ fieldName: "response_body",
823
+ required: false,
824
+ validator: {
825
+ input: z.unknown().optional(),
826
+ output: z.unknown().optional()
827
+ }
828
+ },
829
+ objectType: {
830
+ type: "string",
831
+ fieldName: "object_type",
832
+ required: false,
833
+ validator: {
834
+ input: z.string().max(50).optional(),
835
+ output: z.string().max(50).optional()
836
+ },
837
+ index: true
838
+ },
839
+ objectId: {
840
+ type: "string",
841
+ fieldName: "object_id",
842
+ required: false,
843
+ validator: {
844
+ input: z.string().max(255).optional(),
845
+ output: z.string().max(255).optional()
846
+ },
847
+ index: true
848
+ },
849
+ expiresAt: {
850
+ type: "date",
851
+ fieldName: "expires_at",
852
+ required: false,
853
+ validator: {
854
+ input: z.coerce.date().optional(),
855
+ output: z.date().optional()
856
+ },
857
+ index: true
858
+ },
859
+ createdAt: {
860
+ type: "date",
861
+ fieldName: "created_at",
862
+ required: true,
863
+ input: false,
864
+ returned: true,
865
+ defaultValue: () => /* @__PURE__ */ new Date(),
866
+ validator: { output: z.date() },
867
+ index: true
868
+ },
869
+ updatedAt: {
870
+ type: "date",
871
+ fieldName: "updated_at",
872
+ required: true,
873
+ input: false,
874
+ returned: true,
875
+ defaultValue: () => /* @__PURE__ */ new Date(),
876
+ onUpdate: () => /* @__PURE__ */ new Date(),
877
+ validator: { output: z.date() }
878
+ }
879
+ },
880
+ constraints: {
881
+ unique: [{
882
+ name: "idempotency_key_customer_key_uidx",
883
+ fields: ["customerId", "key"]
884
+ }],
885
+ indexes: [{
886
+ name: "idempotency_key_customer_expires_at_idx",
887
+ fields: ["customerId", "expiresAt"]
888
+ }, {
889
+ name: "idempotency_key_object_idx",
890
+ fields: ["objectType", "objectId"]
891
+ }]
892
+ },
893
+ relations: { customer: {
894
+ kind: "one",
895
+ model: "customer",
896
+ foreignKey: "customerId"
897
+ } }
898
+ },
536
899
  discount: {
537
900
  modelName: "discount",
538
901
  fields: {
@@ -1354,7 +1717,107 @@ const getPaymentTables = (options) => {
1354
1717
  model: "payment_method",
1355
1718
  foreignKey: "defaultPaymentMethodId"
1356
1719
  }
1357
- }
1720
+ }
1721
+ },
1722
+ usageRecord: {
1723
+ modelName: "usage_record",
1724
+ order: 1,
1725
+ fields: {
1726
+ id: {
1727
+ type: "string",
1728
+ fieldName: "id",
1729
+ required: true,
1730
+ input: false,
1731
+ returned: true,
1732
+ unique: true,
1733
+ validator: { output: z.uuid() }
1734
+ },
1735
+ subscriptionItemId: {
1736
+ type: "string",
1737
+ fieldName: "subscription_item_id",
1738
+ required: false,
1739
+ references: {
1740
+ model: "subscription_item",
1741
+ field: "id",
1742
+ onDelete: "cascade"
1743
+ },
1744
+ validator: {
1745
+ input: z.uuid().optional(),
1746
+ output: z.uuid().optional()
1747
+ },
1748
+ index: true
1749
+ },
1750
+ quantity: {
1751
+ type: "number",
1752
+ fieldName: "quantity",
1753
+ required: true,
1754
+ validator: {
1755
+ input: z.number().int(),
1756
+ output: z.number().int()
1757
+ }
1758
+ },
1759
+ timestamp: {
1760
+ type: "date",
1761
+ fieldName: "timestamp",
1762
+ required: false,
1763
+ defaultValue: () => /* @__PURE__ */ new Date(),
1764
+ validator: { output: z.date() },
1765
+ index: true
1766
+ },
1767
+ action: {
1768
+ type: "string",
1769
+ fieldName: "usage_action",
1770
+ required: false,
1771
+ defaultValue: UsageAction.INCREMENT,
1772
+ sortable: true,
1773
+ validator: {
1774
+ input: z.enum(UsageAction).optional(),
1775
+ output: z.enum(UsageAction)
1776
+ }
1777
+ },
1778
+ idempotencyKey: {
1779
+ type: "string",
1780
+ fieldName: "idempotency_key",
1781
+ required: false,
1782
+ unique: true,
1783
+ sortable: true,
1784
+ validator: {
1785
+ input: z.string().max(255).optional(),
1786
+ output: z.string().max(255).optional()
1787
+ },
1788
+ index: true
1789
+ },
1790
+ metadata: {
1791
+ type: "json",
1792
+ fieldName: "metadata",
1793
+ required: false,
1794
+ validator: {
1795
+ input: z.unknown().optional(),
1796
+ output: z.unknown().optional()
1797
+ }
1798
+ },
1799
+ createdAt: {
1800
+ type: "date",
1801
+ fieldName: "created_at",
1802
+ required: true,
1803
+ input: false,
1804
+ returned: true,
1805
+ defaultValue: () => /* @__PURE__ */ new Date(),
1806
+ validator: { output: z.date() }
1807
+ }
1808
+ },
1809
+ constraints: { indexes: [{
1810
+ name: "usage_record_subscription_item_id_timestamp_idx",
1811
+ fields: ["subscriptionItemId", "timestamp"]
1812
+ }, {
1813
+ name: "usage_record_timestamp_idx",
1814
+ fields: ["timestamp"]
1815
+ }] },
1816
+ relations: { subscriptionItem: {
1817
+ kind: "one",
1818
+ model: "subscription_item",
1819
+ foreignKey: "subscriptionItemId"
1820
+ } }
1358
1821
  },
1359
1822
  subscription_item: {
1360
1823
  modelName: "subscription_item",
@@ -1472,32 +1935,313 @@ const getPaymentTables = (options) => {
1472
1935
  }
1473
1936
  ]
1474
1937
  },
1475
- checks: [{
1476
- name: "subscription_item_quantity_gte_1",
1477
- kind: "gte",
1478
- field: "quantity",
1479
- value: 1
1480
- }],
1938
+ checks: [{
1939
+ name: "subscription_item_quantity_gte_1",
1940
+ kind: "gte",
1941
+ field: "quantity",
1942
+ value: 1
1943
+ }],
1944
+ relations: {
1945
+ subscription: {
1946
+ kind: "one",
1947
+ model: "subscription",
1948
+ foreignKey: "subscriptionId"
1949
+ },
1950
+ price: {
1951
+ kind: "one",
1952
+ model: "prices",
1953
+ foreignKey: "priceId"
1954
+ },
1955
+ usageRecords: {
1956
+ kind: "many",
1957
+ model: "usage_record",
1958
+ foreignKey: "subscriptionItemId"
1959
+ }
1960
+ }
1961
+ },
1962
+ subscription_schedule: {
1963
+ modelName: "subscription_schedule",
1964
+ order: 1,
1965
+ fields: {
1966
+ id: {
1967
+ type: "string",
1968
+ fieldName: "id",
1969
+ required: true,
1970
+ input: false,
1971
+ returned: true,
1972
+ validator: { output: z.uuid() }
1973
+ },
1974
+ subscriptionId: {
1975
+ type: "string",
1976
+ fieldName: "subscription_id",
1977
+ required: true,
1978
+ references: {
1979
+ model: "subscription",
1980
+ field: "id",
1981
+ onDelete: "restrict"
1982
+ },
1983
+ validator: {
1984
+ input: z.uuid(),
1985
+ output: z.uuid()
1986
+ },
1987
+ index: true
1988
+ },
1989
+ phases: {
1990
+ type: "json",
1991
+ fieldName: "phases",
1992
+ required: false
1993
+ },
1994
+ status: {
1995
+ type: [
1996
+ "ACTIVE",
1997
+ "COMPLETED",
1998
+ "RELEASED",
1999
+ "CANCELED"
2000
+ ],
2001
+ fieldName: "schedule_status",
2002
+ required: true,
2003
+ defaultValue: "ACTIVE",
2004
+ sortable: true,
2005
+ validator: {
2006
+ input: z.enum([
2007
+ "ACTIVE",
2008
+ "COMPLETED",
2009
+ "RELEASED",
2010
+ "CANCELED"
2011
+ ]).default("ACTIVE"),
2012
+ output: z.enum([
2013
+ "ACTIVE",
2014
+ "COMPLETED",
2015
+ "RELEASED",
2016
+ "CANCELED"
2017
+ ])
2018
+ },
2019
+ index: true
2020
+ },
2021
+ startDate: {
2022
+ type: "date",
2023
+ fieldName: "start_date",
2024
+ required: false,
2025
+ index: true
2026
+ },
2027
+ endDate: {
2028
+ type: "date",
2029
+ fieldName: "end_date",
2030
+ required: false
2031
+ },
2032
+ completedAt: {
2033
+ type: "date",
2034
+ fieldName: "completed_at",
2035
+ required: false
2036
+ },
2037
+ releasedAt: {
2038
+ type: "date",
2039
+ fieldName: "released_at",
2040
+ required: false
2041
+ },
2042
+ metadata: {
2043
+ type: "json",
2044
+ fieldName: "metadata",
2045
+ required: false
2046
+ },
2047
+ createdAt: {
2048
+ type: "date",
2049
+ fieldName: "created_at",
2050
+ required: true,
2051
+ input: false,
2052
+ returned: true,
2053
+ defaultValue: () => /* @__PURE__ */ new Date()
2054
+ },
2055
+ updatedAt: {
2056
+ type: "date",
2057
+ fieldName: "updated_at",
2058
+ required: true,
2059
+ input: false,
2060
+ returned: true,
2061
+ defaultValue: () => /* @__PURE__ */ new Date(),
2062
+ onUpdate: () => /* @__PURE__ */ new Date()
2063
+ }
2064
+ },
2065
+ constraints: { indexes: [
2066
+ {
2067
+ name: "subscription_schedule_subscription_id_status_idx",
2068
+ fields: ["subscriptionId", "status"]
2069
+ },
2070
+ {
2071
+ name: "subscription_schedule_start_date_idx",
2072
+ fields: ["startDate"]
2073
+ },
2074
+ {
2075
+ name: "subscription_schedule_status_idx",
2076
+ fields: ["status"]
2077
+ }
2078
+ ] },
2079
+ relations: { subscription: {
2080
+ kind: "one",
2081
+ model: "subscription",
2082
+ foreignKey: "subscriptionId"
2083
+ } }
2084
+ },
2085
+ product: {
2086
+ modelName: "products",
2087
+ order: 1,
2088
+ fields: {
2089
+ id: {
2090
+ type: "string",
2091
+ fieldName: "id",
2092
+ required: true,
2093
+ input: false,
2094
+ returned: true,
2095
+ unique: true,
2096
+ validator: { output: z.uuid() }
2097
+ },
2098
+ type: {
2099
+ type: "string",
2100
+ fieldName: "product_type",
2101
+ required: false
2102
+ },
2103
+ name: {
2104
+ type: "string",
2105
+ fieldName: "product_name",
2106
+ required: true,
2107
+ sortable: true,
2108
+ validator: {
2109
+ input: z.string().max(255),
2110
+ output: z.string()
2111
+ },
2112
+ index: true
2113
+ },
2114
+ description: {
2115
+ type: "string",
2116
+ fieldName: "product_description",
2117
+ required: false
2118
+ },
2119
+ images: {
2120
+ type: "string[]",
2121
+ fieldName: "product_images",
2122
+ required: true,
2123
+ defaultValue: () => [],
2124
+ validator: {
2125
+ input: z.array(z.string()).default([]),
2126
+ output: z.array(z.string())
2127
+ }
2128
+ },
2129
+ defaultPriceId: {
2130
+ type: "string",
2131
+ fieldName: "default_price_id",
2132
+ required: false,
2133
+ references: {
2134
+ model: "prices",
2135
+ field: "id",
2136
+ onDelete: "set null"
2137
+ },
2138
+ validator: {
2139
+ input: z.uuid().optional(),
2140
+ output: z.uuid().optional()
2141
+ },
2142
+ index: true
2143
+ },
2144
+ marketingFeatures: {
2145
+ type: "json",
2146
+ fieldName: "marketing_features",
2147
+ required: false,
2148
+ validator: {
2149
+ input: z.array(z.object({ name: z.string() })).optional(),
2150
+ output: z.array(z.object({ name: z.string() })).optional()
2151
+ }
2152
+ },
2153
+ packageDimensions: {
2154
+ type: "json",
2155
+ fieldName: "package_dimensions",
2156
+ required: false,
2157
+ validator: {
2158
+ input: z.object({
2159
+ height: z.number(),
2160
+ length: z.number(),
2161
+ weight: z.number(),
2162
+ width: z.number()
2163
+ }).optional(),
2164
+ output: z.object({
2165
+ height: z.number(),
2166
+ length: z.number(),
2167
+ weight: z.number(),
2168
+ width: z.number()
2169
+ }).optional()
2170
+ }
2171
+ },
2172
+ shippable: {
2173
+ type: "boolean",
2174
+ fieldName: "shippable",
2175
+ required: false
2176
+ },
2177
+ url: {
2178
+ type: "string",
2179
+ fieldName: "url",
2180
+ required: false
2181
+ },
2182
+ active: {
2183
+ type: "boolean",
2184
+ fieldName: "active",
2185
+ required: false,
2186
+ defaultValue: true
2187
+ },
2188
+ metadata: {
2189
+ type: "json",
2190
+ fieldName: "metadata",
2191
+ required: false
2192
+ },
2193
+ deleted: {
2194
+ type: "boolean",
2195
+ fieldName: "deleted",
2196
+ required: true,
2197
+ defaultValue: false,
2198
+ input: false,
2199
+ returned: true
2200
+ },
2201
+ deletedAt: {
2202
+ type: "date",
2203
+ fieldName: "deleted_at",
2204
+ required: false,
2205
+ input: false,
2206
+ returned: true
2207
+ },
2208
+ createdAt: {
2209
+ type: "date",
2210
+ fieldName: "created_at",
2211
+ required: true,
2212
+ input: false,
2213
+ returned: true,
2214
+ defaultValue: () => /* @__PURE__ */ new Date()
2215
+ },
2216
+ updatedAt: {
2217
+ type: "date",
2218
+ fieldName: "updated_at",
2219
+ required: true,
2220
+ input: false,
2221
+ returned: true,
2222
+ defaultValue: () => /* @__PURE__ */ new Date(),
2223
+ onUpdate: () => /* @__PURE__ */ new Date()
2224
+ }
2225
+ },
2226
+ constraints: { indexes: [{
2227
+ name: "product_name_idx",
2228
+ fields: ["name"]
2229
+ }] },
1481
2230
  relations: {
1482
- subscription: {
1483
- kind: "one",
1484
- model: "subscription",
1485
- foreignKey: "subscriptionId"
2231
+ prices: {
2232
+ kind: "many",
2233
+ model: "prices",
2234
+ foreignKey: "productId"
1486
2235
  },
1487
- price: {
2236
+ defaultPrice: {
1488
2237
  kind: "one",
1489
2238
  model: "prices",
1490
- foreignKey: "priceId"
1491
- },
1492
- usageRecords: {
1493
- kind: "many",
1494
- model: "usage_record",
1495
- foreignKey: "subscriptionItemId"
2239
+ foreignKey: "defaultPriceId"
1496
2240
  }
1497
2241
  }
1498
2242
  },
1499
- subscription_schedule: {
1500
- modelName: "subscription_schedule",
2243
+ subscription: {
2244
+ modelName: "subscription",
1501
2245
  order: 1,
1502
2246
  fields: {
1503
2247
  id: {
@@ -1506,16 +2250,17 @@ const getPaymentTables = (options) => {
1506
2250
  required: true,
1507
2251
  input: false,
1508
2252
  returned: true,
2253
+ unique: true,
1509
2254
  validator: { output: z.uuid() }
1510
2255
  },
1511
- subscriptionId: {
2256
+ customerId: {
1512
2257
  type: "string",
1513
- fieldName: "subscription_id",
2258
+ fieldName: "customer_id",
1514
2259
  required: true,
1515
2260
  references: {
1516
- model: "subscription",
2261
+ model: "customer",
1517
2262
  field: "id",
1518
- onDelete: "restrict"
2263
+ onDelete: "cascade"
1519
2264
  },
1520
2265
  validator: {
1521
2266
  input: z.uuid(),
@@ -1523,63 +2268,157 @@ const getPaymentTables = (options) => {
1523
2268
  },
1524
2269
  index: true
1525
2270
  },
1526
- phases: {
1527
- type: "json",
1528
- fieldName: "phases",
1529
- required: false
2271
+ paymentMethodId: {
2272
+ type: "string",
2273
+ fieldName: "payment_method_id",
2274
+ required: false,
2275
+ references: {
2276
+ model: "payment_methods",
2277
+ field: "id",
2278
+ onDelete: "set null"
2279
+ },
2280
+ validator: {
2281
+ input: z.uuid().optional(),
2282
+ output: z.uuid().optional()
2283
+ },
2284
+ index: true
1530
2285
  },
1531
2286
  status: {
1532
- type: [
1533
- "ACTIVE",
1534
- "COMPLETED",
1535
- "RELEASED",
1536
- "CANCELED"
1537
- ],
1538
- fieldName: "schedule_status",
1539
- required: true,
1540
- defaultValue: "ACTIVE",
2287
+ type: "string",
2288
+ fieldName: "subscription_status",
2289
+ required: false,
2290
+ defaultValue: SubscriptionStatus.INCOMPLETE,
1541
2291
  sortable: true,
1542
2292
  validator: {
1543
- input: z.enum([
1544
- "ACTIVE",
1545
- "COMPLETED",
1546
- "RELEASED",
1547
- "CANCELED"
1548
- ]).default("ACTIVE"),
1549
- output: z.enum([
1550
- "ACTIVE",
1551
- "COMPLETED",
1552
- "RELEASED",
1553
- "CANCELED"
1554
- ])
2293
+ input: z.enum(SubscriptionStatus).optional(),
2294
+ output: z.enum(SubscriptionStatus)
1555
2295
  },
1556
2296
  index: true
1557
2297
  },
1558
- startDate: {
2298
+ currentBillingPeriodStart: {
1559
2299
  type: "date",
1560
- fieldName: "start_date",
1561
- required: false,
2300
+ fieldName: "current_billing_period_start",
2301
+ required: true,
2302
+ validator: {
2303
+ input: z.date(),
2304
+ output: z.date()
2305
+ }
2306
+ },
2307
+ currentBillingPeriodEnd: {
2308
+ type: "date",
2309
+ fieldName: "current_billing_period_end",
2310
+ required: true,
2311
+ validator: {
2312
+ input: z.date(),
2313
+ output: z.date()
2314
+ },
1562
2315
  index: true
1563
2316
  },
1564
- endDate: {
2317
+ currentTrialStart: {
1565
2318
  type: "date",
1566
- fieldName: "end_date",
1567
- required: false
2319
+ fieldName: "current_trial_start",
2320
+ required: false,
2321
+ validator: {
2322
+ input: z.date().optional(),
2323
+ output: z.date().optional()
2324
+ }
1568
2325
  },
1569
- completedAt: {
2326
+ currentTrialEnd: {
1570
2327
  type: "date",
1571
- fieldName: "completed_at",
1572
- required: false
2328
+ fieldName: "current_trial_end",
2329
+ required: false,
2330
+ validator: {
2331
+ input: z.date().optional(),
2332
+ output: z.date().optional()
2333
+ },
2334
+ index: true
1573
2335
  },
1574
- releasedAt: {
2336
+ cancelAtPeriodEnd: {
2337
+ type: "boolean",
2338
+ fieldName: "cancel_at_period_end",
2339
+ required: true,
2340
+ defaultValue: false,
2341
+ validator: {
2342
+ input: z.boolean().optional(),
2343
+ output: z.boolean()
2344
+ }
2345
+ },
2346
+ canceledAt: {
1575
2347
  type: "date",
1576
- fieldName: "released_at",
1577
- required: false
2348
+ fieldName: "canceled_at",
2349
+ required: false,
2350
+ validator: {
2351
+ input: z.date().optional(),
2352
+ output: z.date().optional()
2353
+ }
2354
+ },
2355
+ cancelAt: {
2356
+ type: "date",
2357
+ fieldName: "cancel_at",
2358
+ required: false,
2359
+ validator: {
2360
+ input: z.date().optional(),
2361
+ output: z.date().optional()
2362
+ }
2363
+ },
2364
+ daysUntilDue: {
2365
+ type: "number",
2366
+ fieldName: "days_until_due",
2367
+ required: true,
2368
+ defaultValue: 0,
2369
+ validator: {
2370
+ input: z.number().int().min(0).optional(),
2371
+ output: z.number().int()
2372
+ }
2373
+ },
2374
+ proration: {
2375
+ type: "string",
2376
+ fieldName: "proration",
2377
+ required: true,
2378
+ defaultValue: ProrationBehavior.CREATE_PRORATIONS,
2379
+ sortable: true,
2380
+ validator: {
2381
+ input: z.enum(ProrationBehavior).optional(),
2382
+ output: z.enum(ProrationBehavior)
2383
+ }
2384
+ },
2385
+ collectionMethod: {
2386
+ type: "string",
2387
+ fieldName: "collection_method",
2388
+ required: true,
2389
+ defaultValue: CollectionMethod.SEND_INVOICE,
2390
+ sortable: true,
2391
+ validator: {
2392
+ input: z.enum(CollectionMethod).optional(),
2393
+ output: z.enum(CollectionMethod)
2394
+ }
1578
2395
  },
1579
2396
  metadata: {
1580
2397
  type: "json",
1581
2398
  fieldName: "metadata",
1582
- required: false
2399
+ required: false,
2400
+ validator: {
2401
+ input: z.unknown().optional(),
2402
+ output: z.unknown().optional()
2403
+ }
2404
+ },
2405
+ deleted: {
2406
+ type: "boolean",
2407
+ fieldName: "deleted",
2408
+ required: true,
2409
+ defaultValue: false,
2410
+ input: false,
2411
+ returned: true,
2412
+ validator: { output: z.boolean() },
2413
+ index: true
2414
+ },
2415
+ deletedAt: {
2416
+ type: "date",
2417
+ fieldName: "deleted_at",
2418
+ required: false,
2419
+ input: false,
2420
+ returned: true,
2421
+ validator: { output: z.date().optional() }
1583
2422
  },
1584
2423
  createdAt: {
1585
2424
  type: "date",
@@ -1587,7 +2426,8 @@ const getPaymentTables = (options) => {
1587
2426
  required: true,
1588
2427
  input: false,
1589
2428
  returned: true,
1590
- defaultValue: () => /* @__PURE__ */ new Date()
2429
+ defaultValue: () => /* @__PURE__ */ new Date(),
2430
+ validator: { output: z.date() }
1591
2431
  },
1592
2432
  updatedAt: {
1593
2433
  type: "date",
@@ -1596,28 +2436,93 @@ const getPaymentTables = (options) => {
1596
2436
  input: false,
1597
2437
  returned: true,
1598
2438
  defaultValue: () => /* @__PURE__ */ new Date(),
1599
- onUpdate: () => /* @__PURE__ */ new Date()
2439
+ onUpdate: () => /* @__PURE__ */ new Date(),
2440
+ validator: { output: z.date() }
2441
+ },
2442
+ subscriptionStartedAt: {
2443
+ type: "date",
2444
+ fieldName: "subscription_started_at",
2445
+ required: true,
2446
+ validator: {
2447
+ input: z.date(),
2448
+ output: z.date()
2449
+ }
2450
+ },
2451
+ subscriptionEndedAt: {
2452
+ type: "date",
2453
+ fieldName: "subscription_ended_at",
2454
+ required: false,
2455
+ validator: {
2456
+ input: z.date().optional(),
2457
+ output: z.date().optional()
2458
+ }
1600
2459
  }
1601
2460
  },
1602
2461
  constraints: { indexes: [
1603
2462
  {
1604
- name: "subscription_schedule_subscription_id_status_idx",
1605
- fields: ["subscriptionId", "status"]
2463
+ name: "subscription_customer_id_status_idx",
2464
+ fields: ["customerId", "status"]
1606
2465
  },
1607
2466
  {
1608
- name: "subscription_schedule_start_date_idx",
1609
- fields: ["startDate"]
2467
+ name: "subscription_current_billing_period_end_status_idx",
2468
+ fields: ["currentBillingPeriodEnd", "status"]
1610
2469
  },
1611
2470
  {
1612
- name: "subscription_schedule_status_idx",
1613
- fields: ["status"]
2471
+ name: "subscription_current_trial_end_status_idx",
2472
+ fields: ["currentTrialEnd", "status"]
2473
+ },
2474
+ {
2475
+ name: "subscription_payment_method_id_idx",
2476
+ fields: ["paymentMethodId"]
2477
+ },
2478
+ {
2479
+ name: "subscription_deleted_idx",
2480
+ fields: ["deleted"]
1614
2481
  }
1615
2482
  ] },
1616
- relations: { subscription: {
1617
- kind: "one",
1618
- model: "subscription",
1619
- foreignKey: "subscriptionId"
1620
- } }
2483
+ checks: [{
2484
+ name: "days_until_due_gte_0",
2485
+ kind: "gte",
2486
+ field: "daysUntilDue",
2487
+ value: 0
2488
+ }],
2489
+ relations: {
2490
+ items: {
2491
+ kind: "many",
2492
+ model: "subscription_item",
2493
+ foreignKey: "subscriptionId"
2494
+ },
2495
+ invoices: {
2496
+ kind: "many",
2497
+ model: "invoice",
2498
+ foreignKey: "subscriptionId"
2499
+ },
2500
+ paymentIntents: {
2501
+ kind: "many",
2502
+ model: "payment_intent",
2503
+ foreignKey: "subscriptionId"
2504
+ },
2505
+ schedules: {
2506
+ kind: "many",
2507
+ model: "subscription_schedule",
2508
+ foreignKey: "subscriptionId"
2509
+ },
2510
+ checkoutSession: {
2511
+ kind: "one",
2512
+ model: "checkout_sessions",
2513
+ foreignKey: "id"
2514
+ },
2515
+ customer: {
2516
+ kind: "one",
2517
+ model: "customer",
2518
+ foreignKey: "customerId"
2519
+ },
2520
+ paymentMethod: {
2521
+ kind: "one",
2522
+ model: "payment_methods",
2523
+ foreignKey: "paymentMethodId"
2524
+ }
2525
+ }
1621
2526
  },
1622
2527
  invoice: {
1623
2528
  modelName: "invoice",
package/dist/index.mjs CHANGED
@@ -2,7 +2,7 @@ import { a as defineRequestState, c as hasRequestState, d as getCurrentPaymentCo
2
2
  import { t as getAsyncLocalStorage } from "./async_hooks-D8vqDJIk.mjs";
3
3
  import { i as useMiddleware, n as createPecuniaMiddleware, r as optionsMiddleware, t as createPaymentEndpoint } from "./api-BlJV5ZQo.mjs";
4
4
  import { A as UsageAggregation, C as ScheduleStatus, D as TransactionStatus, E as SubscriptionStatus, M as WebHookEventStatus, N as statusCodes, O as TransactionType, P as ErrorType, S as ProrationBehavior, T as ScheduledTaskType, _ as PaymentIntentStatus, a as AccountType, b as PricingModel, c as BillingInterval, d as CollectionMethod, f as Currency, g as LedgerEntryType, h as InvoiceStatus, i as createProviderError, j as WebHookDeliveryTrigger, k as UsageAction, l as CheckoutSessionMode, m as ERROR_REGISTRY, n as PecuniaError, o as Actor, p as DiscountType, r as ValidationError, s as AuditAction, t as ApiError, u as CheckoutSessionStatus, v as PaymentProviders, w as ScheduledTaskStatus, x as ProductType, y as PriceType } from "./errors-vpD21Iku.mjs";
5
- import { a as productSchema, c as customerSchema, d as initGetModelName, f as initGetFieldAttributes, h as initGetDefaultModelName, i as subscriptionSchema, l as sharedCoreSchema, m as initGetDefaultFieldName, n as deepmerge, o as priceSchema, p as initGetIdField, r as withApplyDefault, s as discountSchema, t as getPaymentTables, u as sharedDeletableSchema } from "./get-payment-tables-CxhHAsKo.mjs";
5
+ import { a as productSchema, c as customerSchema, d as initGetModelName, f as initGetFieldAttributes, h as initGetDefaultModelName, i as subscriptionSchema, l as sharedCoreSchema, m as initGetDefaultFieldName, n as deepmerge, o as priceSchema, p as initGetIdField, r as withApplyDefault, s as discountSchema, t as getPaymentTables, u as sharedDeletableSchema } from "./get-payment-tables-6NA4nccG.mjs";
6
6
  import { t as generateId } from "./generate-id-D3ZvbbXn.mjs";
7
7
  import { initGetFieldName } from "./db/index.mjs";
8
8
  import { a as shouldPublishLog, c as env, d as isDevelopment, f as isProduction, i as logger, l as getBooleanEnvVar, m as nodeENV, n as createLogger, o as getColorDepth, p as isTest, r as levels, s as ENV, t as TTY_COLORS, u as getEnvVar } from "./env-ChlcfGdT.mjs";
@@ -2,7 +2,7 @@ import { f as runWithEndpointContext } from "../context-COLduJme.mjs";
2
2
  import "../async_hooks-D8vqDJIk.mjs";
3
3
  import { i as useMiddleware } from "../api-BlJV5ZQo.mjs";
4
4
  import { P as ErrorType, n as PecuniaError } from "../errors-vpD21Iku.mjs";
5
- import { d as initGetModelName, f as initGetFieldAttributes, h as initGetDefaultModelName, m as initGetDefaultFieldName, p as initGetIdField, r as withApplyDefault, t as getPaymentTables } from "../get-payment-tables-CxhHAsKo.mjs";
5
+ import { d as initGetModelName, f as initGetFieldAttributes, h as initGetDefaultModelName, m as initGetDefaultFieldName, p as initGetIdField, r as withApplyDefault, t as getPaymentTables } from "../get-payment-tables-6NA4nccG.mjs";
6
6
  import "../generate-id-D3ZvbbXn.mjs";
7
7
  import "../env-ChlcfGdT.mjs";
8
8
  import { n as safeJSONParse } from "../utils-BtfYqMRX.mjs";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pecunia-core",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "type": "module",
5
5
  "main": "./dist/index.mjs",
6
6
  "module": "./dist/index.mjs",