@forklaunch/implementation-billing-base 0.1.2 → 0.1.4

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.
Files changed (101) hide show
  1. package/lib/__test__/schemaEquality.test.d.ts +2 -0
  2. package/lib/__test__/schemaEquality.test.d.ts.map +1 -0
  3. package/lib/__test__/schemaEquality.test.js +226 -0
  4. package/{schemas/typebox → lib/eject/schemas/zod}/billingPortal.schema.ts +1 -1
  5. package/{schemas/typebox → lib/eject/schemas/zod}/checkoutSession.schema.ts +1 -2
  6. package/{schemas/typebox → lib/eject/schemas/zod}/paymentLink.schema.ts +1 -1
  7. package/{schemas/typebox → lib/eject/schemas/zod}/plan.schema.ts +1 -1
  8. package/{schemas/typebox → lib/eject/schemas/zod}/subscription.schema.ts +1 -1
  9. package/lib/eject/services/services/billingPortal.service.ts +148 -0
  10. package/lib/eject/services/services/checkoutSession.service.ts +135 -0
  11. package/lib/eject/services/services/paymentLink.service.ts +173 -0
  12. package/lib/eject/services/services/plan.service.ts +135 -0
  13. package/lib/eject/services/subscription.service.ts +239 -0
  14. package/lib/index.d.ts +11 -0
  15. package/lib/index.d.ts.map +1 -0
  16. package/lib/jest.config.d.ts +4 -0
  17. package/lib/jest.config.d.ts.map +1 -0
  18. package/lib/jest.config.js +19 -0
  19. package/lib/schemas/billingPortal.schema.d.ts +42 -0
  20. package/lib/schemas/billingPortal.schema.d.ts.map +1 -0
  21. package/lib/schemas/billingPortal.schema.js +14 -0
  22. package/lib/schemas/checkoutSession.schema.d.ts +55 -0
  23. package/lib/schemas/checkoutSession.schema.d.ts.map +1 -0
  24. package/lib/schemas/checkoutSession.schema.js +14 -0
  25. package/lib/schemas/paymentLink.schema.d.ts +66 -0
  26. package/lib/schemas/paymentLink.schema.d.ts.map +1 -0
  27. package/lib/schemas/paymentLink.schema.js +14 -0
  28. package/lib/schemas/plan.schema.d.ts +78 -0
  29. package/lib/schemas/plan.schema.d.ts.map +1 -0
  30. package/lib/schemas/plan.schema.js +14 -0
  31. package/lib/schemas/subscription.schema.d.ts +90 -0
  32. package/lib/schemas/subscription.schema.d.ts.map +1 -0
  33. package/lib/schemas/subscription.schema.js +14 -0
  34. package/lib/schemas/typebox/billingPortal.schema.d.ts +20 -0
  35. package/lib/schemas/typebox/billingPortal.schema.d.ts.map +1 -0
  36. package/lib/schemas/typebox/billingPortal.schema.js +20 -0
  37. package/lib/schemas/typebox/checkoutSession.schema.d.ts +28 -0
  38. package/lib/schemas/typebox/checkoutSession.schema.d.ts.map +1 -0
  39. package/lib/schemas/typebox/checkoutSession.schema.js +27 -0
  40. package/lib/schemas/typebox/paymentLink.schema.d.ts +33 -0
  41. package/lib/schemas/typebox/paymentLink.schema.d.ts.map +1 -0
  42. package/lib/schemas/typebox/paymentLink.schema.js +32 -0
  43. package/lib/schemas/typebox/plan.schema.d.ts +39 -0
  44. package/lib/schemas/typebox/plan.schema.d.ts.map +1 -0
  45. package/lib/schemas/typebox/plan.schema.js +38 -0
  46. package/lib/schemas/typebox/subscription.schema.d.ts +45 -0
  47. package/lib/schemas/typebox/subscription.schema.d.ts.map +1 -0
  48. package/lib/schemas/typebox/subscription.schema.js +44 -0
  49. package/lib/schemas/zod/billingPortal.schema.d.ts +20 -0
  50. package/lib/schemas/zod/billingPortal.schema.d.ts.map +1 -0
  51. package/lib/schemas/zod/billingPortal.schema.js +20 -0
  52. package/lib/schemas/zod/checkoutSession.schema.d.ts +27 -0
  53. package/lib/schemas/zod/checkoutSession.schema.d.ts.map +1 -0
  54. package/lib/schemas/zod/checkoutSession.schema.js +26 -0
  55. package/lib/schemas/zod/paymentLink.schema.d.ts +33 -0
  56. package/lib/schemas/zod/paymentLink.schema.d.ts.map +1 -0
  57. package/lib/schemas/zod/paymentLink.schema.js +32 -0
  58. package/lib/schemas/zod/plan.schema.d.ts +39 -0
  59. package/lib/schemas/zod/plan.schema.d.ts.map +1 -0
  60. package/lib/schemas/zod/plan.schema.js +38 -0
  61. package/lib/schemas/zod/subscription.schema.d.ts +45 -0
  62. package/lib/schemas/zod/subscription.schema.d.ts.map +1 -0
  63. package/lib/schemas/zod/subscription.schema.js +44 -0
  64. package/lib/services/billingPortal.service.d.ts +44 -0
  65. package/lib/services/billingPortal.service.d.ts.map +1 -0
  66. package/lib/services/billingPortal.service.js +51 -0
  67. package/lib/services/checkoutSession.service.d.ts +45 -0
  68. package/lib/services/checkoutSession.service.d.ts.map +1 -0
  69. package/lib/services/checkoutSession.service.js +49 -0
  70. package/lib/services/paymentLink.service.d.ts +48 -0
  71. package/lib/services/paymentLink.service.d.ts.map +1 -0
  72. package/lib/services/paymentLink.service.js +72 -0
  73. package/lib/services/plan.service.d.ts +42 -0
  74. package/lib/services/plan.service.d.ts.map +1 -0
  75. package/lib/services/plan.service.js +43 -0
  76. package/lib/services/subscription.service.d.ts +52 -0
  77. package/lib/services/subscription.service.d.ts.map +1 -0
  78. package/lib/services/subscription.service.js +94 -0
  79. package/lib/tsconfig.tsbuildinfo +1 -0
  80. package/lib/vitest.config.d.ts +3 -0
  81. package/lib/vitest.config.d.ts.map +1 -0
  82. package/lib/vitest.config.js +7 -0
  83. package/package.json +6 -4
  84. package/__test__/schemaEquality.test.ts +0 -411
  85. package/schemas/billingPortal.schema.ts +0 -28
  86. package/schemas/checkoutSession.schema.ts +0 -28
  87. package/schemas/paymentLink.schema.ts +0 -28
  88. package/schemas/plan.schema.ts +0 -28
  89. package/schemas/subscription.schema.ts +0 -28
  90. package/tsconfig.json +0 -7
  91. /package/{schemas/zod → lib/eject/schemas}/billingPortal.schema.ts +0 -0
  92. /package/{schemas/zod → lib/eject/schemas}/checkoutSession.schema.ts +0 -0
  93. /package/{schemas/zod → lib/eject/schemas}/paymentLink.schema.ts +0 -0
  94. /package/{schemas/zod → lib/eject/schemas}/plan.schema.ts +0 -0
  95. /package/{schemas/zod → lib/eject/schemas}/subscription.schema.ts +0 -0
  96. /package/{services → lib/eject/services}/billingPortal.service.ts +0 -0
  97. /package/{services → lib/eject/services}/checkoutSession.service.ts +0 -0
  98. /package/{services → lib/eject/services}/paymentLink.service.ts +0 -0
  99. /package/{services → lib/eject/services}/plan.service.ts +0 -0
  100. /package/{services → lib/eject/services/services}/subscription.service.ts +0 -0
  101. /package/{index.ts → lib/index.js} +0 -0
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=schemaEquality.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"schemaEquality.test.d.ts","sourceRoot":"","sources":["../../__test__/schemaEquality.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,226 @@
1
+ import { isTrue } from '@forklaunch/common';
2
+ import { DummyEnum, testSchemaEquality } from '@forklaunch/core/test';
3
+ import { BillingPortalSchema as TypeboxBillingPortalSchema, CreateBillingPortalSchema as TypeboxCreateBillingPortalSchema, UpdateBillingPortalSchema as TypeboxUpdateBillingPortalSchema } from '../schemas/typebox/billingPortal.schema';
4
+ import { CheckoutSessionSchema as TypeboxCheckoutSessionSchema, CreateCheckoutSessionSchema as TypeboxCreateCheckoutSessionSchema, UpdateCheckoutSessionSchema as TypeboxUpdateCheckoutSessionSchema } from '../schemas/typebox/checkoutSession.schema';
5
+ import { CreatePaymentLinkSchema as TypeboxCreatePaymentLinkSchema, PaymentLinkSchema as TypeboxPaymentLinkSchema, UpdatePaymentLinkSchema as TypeboxUpdatePaymentLinkSchema } from '../schemas/typebox/paymentLink.schema';
6
+ import { CreatePlanSchema as TypeboxCreatePlanSchema, PlanSchema as TypeboxPlanSchema, UpdatePlanSchema as TypeboxUpdatePlanSchema } from '../schemas/typebox/plan.schema';
7
+ import { CreateSubscriptionSchema as TypeboxCreateSubscriptionSchema, SubscriptionSchema as TypeboxSubscriptionSchema, UpdateSubscriptionSchema as TypeboxUpdateSubscriptionSchema } from '../schemas/typebox/subscription.schema';
8
+ import { BillingPortalSchema as ZodBillingPortalSchema, CreateBillingPortalSchema as ZodCreateBillingPortalSchema, UpdateBillingPortalSchema as ZodUpdateBillingPortalSchema } from '../schemas/zod/billingPortal.schema';
9
+ import { CheckoutSessionSchema as ZodCheckoutSessionSchema, CreateCheckoutSessionSchema as ZodCreateCheckoutSessionSchema, UpdateCheckoutSessionSchema as ZodUpdateCheckoutSessionSchema } from '../schemas/zod/checkoutSession.schema';
10
+ import { CreatePaymentLinkSchema as ZodCreatePaymentLinkSchema, PaymentLinkSchema as ZodPaymentLinkSchema, UpdatePaymentLinkSchema as ZodUpdatePaymentLinkSchema } from '../schemas/zod/paymentLink.schema';
11
+ import { CreatePlanSchema as ZodCreatePlanSchema, PlanSchema as ZodPlanSchema, UpdatePlanSchema as ZodUpdatePlanSchema } from '../schemas/zod/plan.schema';
12
+ import { CreateSubscriptionSchema as ZodCreateSubscriptionSchema, SubscriptionSchema as ZodSubscriptionSchema, UpdateSubscriptionSchema as ZodUpdateSubscriptionSchema } from '../schemas/zod/subscription.schema';
13
+ const zodUpdateBillingPortalSchema = ZodUpdateBillingPortalSchema(false);
14
+ const typeboxUpdateBillingPortalSchema = TypeboxUpdateBillingPortalSchema(false);
15
+ const zodBillingPortalSchema = ZodBillingPortalSchema(false);
16
+ const typeboxBillingPortalSchema = TypeboxBillingPortalSchema(false);
17
+ const zodCreateCheckoutSessionSchema = ZodCreateCheckoutSessionSchema(DummyEnum);
18
+ const typeboxCreateCheckoutSessionSchema = TypeboxCreateCheckoutSessionSchema(DummyEnum);
19
+ const zodUpdateCheckoutSessionSchema = ZodUpdateCheckoutSessionSchema(false)(DummyEnum);
20
+ const typeboxUpdateCheckoutSessionSchema = TypeboxUpdateCheckoutSessionSchema(false)(DummyEnum);
21
+ const zodCheckoutSessionSchema = ZodCheckoutSessionSchema(false)(DummyEnum);
22
+ const typeboxCheckoutSessionSchema = TypeboxCheckoutSessionSchema(false)(DummyEnum);
23
+ const zodCreatePaymentLinkSchema = ZodCreatePaymentLinkSchema(DummyEnum);
24
+ const typeboxCreatePaymentLinkSchema = TypeboxCreatePaymentLinkSchema(DummyEnum);
25
+ const zodUpdatePaymentLinkSchema = ZodUpdatePaymentLinkSchema(false)(DummyEnum);
26
+ const typeboxUpdatePaymentLinkSchema = TypeboxUpdatePaymentLinkSchema(false)(DummyEnum);
27
+ const zodPaymentLinkSchema = ZodPaymentLinkSchema(false)(DummyEnum);
28
+ const typeboxPaymentLinkSchema = TypeboxPaymentLinkSchema(false)(DummyEnum);
29
+ const zodCreatePlanSchema = ZodCreatePlanSchema(DummyEnum, DummyEnum);
30
+ const typeboxCreatePlanSchema = TypeboxCreatePlanSchema(DummyEnum, DummyEnum);
31
+ const zodUpdatePlanSchema = ZodUpdatePlanSchema(false)(DummyEnum, DummyEnum);
32
+ const typeboxUpdatePlanSchema = TypeboxUpdatePlanSchema(false)(DummyEnum, DummyEnum);
33
+ const zodPlanSchema = ZodPlanSchema(false)(DummyEnum, DummyEnum);
34
+ const typeboxPlanSchema = TypeboxPlanSchema(false)(DummyEnum, DummyEnum);
35
+ const zodCreateSubscriptionSchema = ZodCreateSubscriptionSchema(DummyEnum, DummyEnum);
36
+ const typeboxCreateSubscriptionSchema = TypeboxCreateSubscriptionSchema(DummyEnum, DummyEnum);
37
+ const zodUpdateSubscriptionSchema = ZodUpdateSubscriptionSchema(false)(DummyEnum, DummyEnum);
38
+ const typeboxUpdateSubscriptionSchema = TypeboxUpdateSubscriptionSchema(false)(DummyEnum, DummyEnum);
39
+ const zodSubscriptionSchema = ZodSubscriptionSchema(false)(DummyEnum, DummyEnum);
40
+ const typeboxSubscriptionSchema = TypeboxSubscriptionSchema(false)(DummyEnum, DummyEnum);
41
+ describe('schema equality', () => {
42
+ it('should be equal for billing portal', () => {
43
+ expect(isTrue(testSchemaEquality(ZodCreateBillingPortalSchema, TypeboxCreateBillingPortalSchema, {
44
+ customerId: 'test',
45
+ uri: 'https://example.com',
46
+ expiresAt: new Date()
47
+ }))).toBeTruthy();
48
+ expect(isTrue(testSchemaEquality(zodUpdateBillingPortalSchema, typeboxUpdateBillingPortalSchema, {
49
+ id: 'test',
50
+ uri: 'https://example.com',
51
+ expiresAt: new Date()
52
+ }))).toBeTruthy();
53
+ expect(isTrue(testSchemaEquality(zodBillingPortalSchema, typeboxBillingPortalSchema, {
54
+ id: 'test',
55
+ customerId: 'test',
56
+ uri: 'https://example.com',
57
+ expiresAt: new Date(),
58
+ extraFields: {
59
+ test: 'test'
60
+ }
61
+ }))).toBeTruthy();
62
+ });
63
+ it('should be equal for checkout session', () => {
64
+ expect(isTrue(testSchemaEquality(zodCreateCheckoutSessionSchema, typeboxCreateCheckoutSessionSchema, {
65
+ customerId: 'test',
66
+ paymentMethods: [DummyEnum.A, DummyEnum.B],
67
+ successRedirectUri: 'https://example.com',
68
+ cancelRedirectUri: 'https://example.com',
69
+ extraFields: {
70
+ test: 'test'
71
+ }
72
+ }))).toBeTruthy();
73
+ expect(isTrue(testSchemaEquality(zodUpdateCheckoutSessionSchema, typeboxUpdateCheckoutSessionSchema, {
74
+ id: 'test',
75
+ customerId: 'test',
76
+ paymentMethods: [DummyEnum.A, DummyEnum.B],
77
+ successRedirectUri: 'https://example.com',
78
+ cancelRedirectUri: 'https://example.com',
79
+ extraFields: {
80
+ test: 'test'
81
+ }
82
+ }))).toBeTruthy();
83
+ expect(isTrue(testSchemaEquality(zodCheckoutSessionSchema, typeboxCheckoutSessionSchema, {
84
+ id: 'test',
85
+ customerId: 'test',
86
+ paymentMethods: [DummyEnum.A, DummyEnum.B],
87
+ successRedirectUri: 'https://example.com',
88
+ cancelRedirectUri: 'https://example.com',
89
+ extraFields: {
90
+ test: 'test'
91
+ }
92
+ }))).toBeTruthy();
93
+ });
94
+ it('should be equal for payment link', () => {
95
+ expect(isTrue(testSchemaEquality(zodCreatePaymentLinkSchema, typeboxCreatePaymentLinkSchema, {
96
+ amount: 100,
97
+ currency: DummyEnum.A,
98
+ successRedirectUri: 'https://example.com',
99
+ cancelRedirectUri: 'https://example.com',
100
+ description: 'test',
101
+ metadata: {
102
+ test: 'test'
103
+ },
104
+ extraFields: {
105
+ test: 'test'
106
+ }
107
+ }))).toBeTruthy();
108
+ expect(isTrue(testSchemaEquality(zodUpdatePaymentLinkSchema, typeboxUpdatePaymentLinkSchema, {
109
+ id: 'test',
110
+ amount: 100,
111
+ currency: DummyEnum.A,
112
+ successRedirectUri: 'https://example.com',
113
+ cancelRedirectUri: 'https://example.com',
114
+ description: 'test',
115
+ metadata: {
116
+ test: 'test'
117
+ },
118
+ extraFields: {
119
+ test: 'test'
120
+ }
121
+ })));
122
+ expect(isTrue(testSchemaEquality(zodPaymentLinkSchema, typeboxPaymentLinkSchema, {
123
+ id: 'test',
124
+ amount: 100,
125
+ currency: DummyEnum.A,
126
+ successRedirectUri: 'https://example.com',
127
+ cancelRedirectUri: 'https://example.com',
128
+ description: 'test',
129
+ metadata: {
130
+ test: 'test'
131
+ },
132
+ extraFields: {
133
+ test: 'test'
134
+ }
135
+ }))).toBeTruthy();
136
+ });
137
+ it('should be equal for plan', () => {
138
+ expect(isTrue(testSchemaEquality(zodCreatePlanSchema, typeboxCreatePlanSchema, {
139
+ name: 'test',
140
+ price: 100,
141
+ cadence: DummyEnum.A,
142
+ features: [DummyEnum.A, DummyEnum.B],
143
+ externalId: 'test',
144
+ active: true,
145
+ description: 'test',
146
+ extraFields: {
147
+ test: 'test'
148
+ }
149
+ }))).toBeTruthy();
150
+ expect(isTrue(testSchemaEquality(zodUpdatePlanSchema, typeboxUpdatePlanSchema, {
151
+ id: 'test',
152
+ name: 'test',
153
+ price: 100,
154
+ cadence: DummyEnum.A,
155
+ features: [DummyEnum.A, DummyEnum.B],
156
+ externalId: 'test',
157
+ active: true,
158
+ description: 'test',
159
+ extraFields: {
160
+ test: 'test'
161
+ }
162
+ }))).toBeTruthy();
163
+ expect(isTrue(testSchemaEquality(zodPlanSchema, typeboxPlanSchema, {
164
+ id: 'test',
165
+ name: 'test',
166
+ price: 100,
167
+ cadence: DummyEnum.A,
168
+ features: [DummyEnum.A, DummyEnum.B],
169
+ externalId: 'test',
170
+ active: true,
171
+ description: 'test',
172
+ extraFields: {
173
+ test: 'test'
174
+ }
175
+ }))).toBeTruthy();
176
+ });
177
+ it('should be equal for subscription', () => {
178
+ expect(isTrue(testSchemaEquality(zodCreateSubscriptionSchema, typeboxCreateSubscriptionSchema, {
179
+ partyId: 'test',
180
+ partyType: DummyEnum.A,
181
+ productId: 'test',
182
+ status: DummyEnum.A,
183
+ active: true,
184
+ externalId: 'test',
185
+ startDate: new Date(),
186
+ endDate: new Date(),
187
+ description: 'test',
188
+ extraFields: {
189
+ test: 'test'
190
+ },
191
+ billingProvider: DummyEnum.A
192
+ }))).toBeTruthy();
193
+ expect(isTrue(testSchemaEquality(zodUpdateSubscriptionSchema, typeboxUpdateSubscriptionSchema, {
194
+ id: 'test',
195
+ partyId: 'test',
196
+ partyType: DummyEnum.A,
197
+ productId: 'test',
198
+ status: DummyEnum.A,
199
+ active: true,
200
+ externalId: 'test',
201
+ startDate: new Date(),
202
+ endDate: new Date(),
203
+ description: 'test',
204
+ extraFields: {
205
+ test: 'test'
206
+ },
207
+ billingProvider: DummyEnum.A
208
+ }))).toBeTruthy();
209
+ expect(isTrue(testSchemaEquality(zodSubscriptionSchema, typeboxSubscriptionSchema, {
210
+ id: 'test',
211
+ partyId: 'test',
212
+ partyType: DummyEnum.A,
213
+ productId: 'test',
214
+ status: DummyEnum.A,
215
+ active: true,
216
+ externalId: 'test',
217
+ startDate: new Date(),
218
+ endDate: new Date(),
219
+ description: 'test',
220
+ extraFields: {
221
+ test: 'test'
222
+ },
223
+ billingProvider: DummyEnum.A
224
+ }))).toBeTruthy();
225
+ });
226
+ });
@@ -4,7 +4,7 @@ import {
4
4
  string,
5
5
  unknown,
6
6
  uuid
7
- } from '@forklaunch/validator/typebox';
7
+ } from '@forklaunch/validator/zod';
8
8
 
9
9
  export const CreateBillingPortalSchema = {
10
10
  customerId: string,
@@ -7,7 +7,7 @@ import {
7
7
  string,
8
8
  unknown,
9
9
  uuid
10
- } from '@forklaunch/validator/typebox';
10
+ } from '@forklaunch/validator/zod';
11
11
 
12
12
  export const CreateCheckoutSessionSchema = <
13
13
  T extends Record<string, LiteralSchema>
@@ -37,7 +37,6 @@ export const CheckoutSessionSchema =
37
37
  <T extends Record<string, LiteralSchema>>(PaymentMethodEnum: T) => ({
38
38
  id: uuidId ? uuid : string,
39
39
  customerId: string,
40
- metadata: optional(unknown),
41
40
  paymentMethods: array(enum_(PaymentMethodEnum)),
42
41
  successRedirectUri: string,
43
42
  cancelRedirectUri: string,
@@ -7,7 +7,7 @@ import {
7
7
  string,
8
8
  unknown,
9
9
  uuid
10
- } from '@forklaunch/validator/typebox';
10
+ } from '@forklaunch/validator/zod';
11
11
 
12
12
  export const CreatePaymentLinkSchema = <
13
13
  T extends Record<string, LiteralSchema>
@@ -9,7 +9,7 @@ import {
9
9
  string,
10
10
  unknown,
11
11
  uuid
12
- } from '@forklaunch/validator/typebox';
12
+ } from '@forklaunch/validator/zod';
13
13
 
14
14
  export const CreatePlanSchema = <
15
15
  T extends Record<string, LiteralSchema>,
@@ -7,7 +7,7 @@ import {
7
7
  string,
8
8
  unknown,
9
9
  uuid
10
- } from '@forklaunch/validator/typebox';
10
+ } from '@forklaunch/validator/zod';
11
11
 
12
12
  export const CreateSubscriptionSchema = <
13
13
  T extends Record<string, LiteralSchema>,
@@ -0,0 +1,148 @@
1
+ import {
2
+ BillingPortalDto,
3
+ BillingPortalService,
4
+ CreateBillingPortalDto,
5
+ UpdateBillingPortalDto
6
+ } from '@forklaunch/interfaces-billing';
7
+ import { IdDto, InstanceTypeRecord } from '@forklaunch/common';
8
+ import { createCacheKey, TtlCache } from '@forklaunch/core/cache';
9
+ import {
10
+ InternalDtoMapper,
11
+ RequestDtoMapperConstructor,
12
+ ResponseDtoMapperConstructor,
13
+ transformIntoInternalDtoMapper
14
+ } from '@forklaunch/core/dtoMapper';
15
+ import {
16
+ MetricsDefinition,
17
+ OpenTelemetryCollector
18
+ } from '@forklaunch/core/http';
19
+ import { AnySchemaValidator } from '@forklaunch/validator';
20
+
21
+ export class BaseBillingPortalService<
22
+ SchemaValidator extends AnySchemaValidator,
23
+ Metrics extends MetricsDefinition = MetricsDefinition,
24
+ Dto extends {
25
+ BillingPortalDtoMapper: BillingPortalDto;
26
+ CreateBillingPortalDtoMapper: CreateBillingPortalDto;
27
+ UpdateBillingPortalDtoMapper: UpdateBillingPortalDto;
28
+ } = {
29
+ BillingPortalDtoMapper: BillingPortalDto;
30
+ CreateBillingPortalDtoMapper: CreateBillingPortalDto;
31
+ UpdateBillingPortalDtoMapper: UpdateBillingPortalDto;
32
+ },
33
+ Entities extends {
34
+ BillingPortalDtoMapper: BillingPortalDto;
35
+ CreateBillingPortalDtoMapper: BillingPortalDto;
36
+ UpdateBillingPortalDtoMapper: BillingPortalDto;
37
+ } = {
38
+ BillingPortalDtoMapper: BillingPortalDto;
39
+ CreateBillingPortalDtoMapper: BillingPortalDto;
40
+ UpdateBillingPortalDtoMapper: BillingPortalDto;
41
+ }
42
+ > implements BillingPortalService
43
+ {
44
+ #dtoMappers: InternalDtoMapper<
45
+ InstanceTypeRecord<typeof this.dtoMappers>,
46
+ Entities,
47
+ Dto
48
+ >;
49
+
50
+ constructor(
51
+ protected cache: TtlCache,
52
+ protected openTelemetryCollector: OpenTelemetryCollector<Metrics>,
53
+ protected schemaValidator: SchemaValidator,
54
+ protected dtoMappers: {
55
+ BillingPortalDtoMapper: ResponseDtoMapperConstructor<
56
+ SchemaValidator,
57
+ Dto['BillingPortalDtoMapper'],
58
+ Entities['BillingPortalDtoMapper']
59
+ >;
60
+ CreateBillingPortalDtoMapper: RequestDtoMapperConstructor<
61
+ SchemaValidator,
62
+ Dto['CreateBillingPortalDtoMapper'],
63
+ Entities['CreateBillingPortalDtoMapper']
64
+ >;
65
+ UpdateBillingPortalDtoMapper: RequestDtoMapperConstructor<
66
+ SchemaValidator,
67
+ Dto['UpdateBillingPortalDtoMapper'],
68
+ Entities['UpdateBillingPortalDtoMapper']
69
+ >;
70
+ }
71
+ ) {
72
+ this.#dtoMappers = transformIntoInternalDtoMapper(
73
+ dtoMappers,
74
+ schemaValidator
75
+ );
76
+ }
77
+
78
+ protected createCacheKey = createCacheKey('billing_portal_session');
79
+
80
+ async createBillingPortalSession(
81
+ billingPortalDto: Dto['CreateBillingPortalDtoMapper']
82
+ ): Promise<Dto['BillingPortalDtoMapper']> {
83
+ const billingPortalSession =
84
+ this.#dtoMappers.CreateBillingPortalDtoMapper.deserializeDtoToEntity(
85
+ billingPortalDto
86
+ );
87
+
88
+ this.openTelemetryCollector.debug(
89
+ 'Create billing portal session',
90
+ billingPortalSession
91
+ );
92
+
93
+ await this.cache.putRecord({
94
+ key: this.createCacheKey(billingPortalSession.id),
95
+ value: billingPortalSession,
96
+ ttlMilliseconds: this.cache.getTtlMilliseconds()
97
+ });
98
+
99
+ return this.#dtoMappers.BillingPortalDtoMapper.serializeEntityToDto(
100
+ billingPortalSession
101
+ );
102
+ }
103
+
104
+ async getBillingPortalSession(
105
+ idDto: IdDto
106
+ ): Promise<Dto['BillingPortalDtoMapper']> {
107
+ const billingPortalSessionDetails = await this.cache.readRecord<
108
+ Entities['BillingPortalDtoMapper']
109
+ >(this.createCacheKey(idDto.id));
110
+ if (!billingPortalSessionDetails) {
111
+ throw new Error('Session not found');
112
+ }
113
+
114
+ return this.#dtoMappers.BillingPortalDtoMapper.serializeEntityToDto(
115
+ billingPortalSessionDetails.value
116
+ );
117
+ }
118
+
119
+ async updateBillingPortalSession(
120
+ billingPortalDto: UpdateBillingPortalDto
121
+ ): Promise<Dto['BillingPortalDtoMapper']> {
122
+ const billingPortalSession =
123
+ this.#dtoMappers.UpdateBillingPortalDtoMapper.deserializeDtoToEntity(
124
+ billingPortalDto
125
+ );
126
+ // Save the updated session to your database or external service
127
+ await this.cache.putRecord({
128
+ key: this.createCacheKey(billingPortalSession.id),
129
+ value: billingPortalSession,
130
+ ttlMilliseconds: this.cache.getTtlMilliseconds()
131
+ });
132
+
133
+ return this.#dtoMappers.BillingPortalDtoMapper.serializeEntityToDto(
134
+ billingPortalSession
135
+ );
136
+ }
137
+
138
+ async expireBillingPortalSession(idDto: IdDto): Promise<void> {
139
+ const sessionExists = await this.cache.readRecord(
140
+ this.createCacheKey(idDto.id)
141
+ );
142
+ if (!sessionExists) {
143
+ throw new Error('Session not found');
144
+ }
145
+
146
+ await this.cache.deleteRecord(this.createCacheKey(idDto.id));
147
+ }
148
+ }
@@ -0,0 +1,135 @@
1
+ import {
2
+ CheckoutSessionDto,
3
+ CheckoutSessionService,
4
+ CreateCheckoutSessionDto,
5
+ UpdateCheckoutSessionDto
6
+ } from '@forklaunch/interfaces-billing';
7
+ import { IdDto, InstanceTypeRecord } from '@forklaunch/common';
8
+ import { createCacheKey, TtlCache } from '@forklaunch/core/cache';
9
+ import {
10
+ InternalDtoMapper,
11
+ RequestDtoMapperConstructor,
12
+ ResponseDtoMapperConstructor,
13
+ transformIntoInternalDtoMapper
14
+ } from '@forklaunch/core/dtoMapper';
15
+ import {
16
+ MetricsDefinition,
17
+ OpenTelemetryCollector
18
+ } from '@forklaunch/core/http';
19
+ import { AnySchemaValidator } from '@forklaunch/validator';
20
+
21
+ export class BaseCheckoutSessionService<
22
+ SchemaValidator extends AnySchemaValidator,
23
+ PaymentMethodEnum,
24
+ Metrics extends MetricsDefinition = MetricsDefinition,
25
+ Dto extends {
26
+ CheckoutSessionDtoMapper: CheckoutSessionDto<PaymentMethodEnum>;
27
+ CreateCheckoutSessionDtoMapper: CreateCheckoutSessionDto<PaymentMethodEnum>;
28
+ UpdateCheckoutSessionDtoMapper: UpdateCheckoutSessionDto<PaymentMethodEnum>;
29
+ } = {
30
+ CheckoutSessionDtoMapper: CheckoutSessionDto<PaymentMethodEnum>;
31
+ CreateCheckoutSessionDtoMapper: CreateCheckoutSessionDto<PaymentMethodEnum>;
32
+ UpdateCheckoutSessionDtoMapper: UpdateCheckoutSessionDto<PaymentMethodEnum>;
33
+ },
34
+ Entities extends {
35
+ CheckoutSessionDtoMapper: CheckoutSessionDto<PaymentMethodEnum>;
36
+ CreateCheckoutSessionDtoMapper: CheckoutSessionDto<PaymentMethodEnum>;
37
+ UpdateCheckoutSessionDtoMapper: CheckoutSessionDto<PaymentMethodEnum>;
38
+ } = {
39
+ CheckoutSessionDtoMapper: CheckoutSessionDto<PaymentMethodEnum>;
40
+ CreateCheckoutSessionDtoMapper: CheckoutSessionDto<PaymentMethodEnum>;
41
+ UpdateCheckoutSessionDtoMapper: CheckoutSessionDto<PaymentMethodEnum>;
42
+ }
43
+ > implements CheckoutSessionService<PaymentMethodEnum>
44
+ {
45
+ #dtoMappers: InternalDtoMapper<
46
+ InstanceTypeRecord<typeof this.dtoMappers>,
47
+ Entities,
48
+ Dto
49
+ >;
50
+
51
+ constructor(
52
+ protected readonly cache: TtlCache,
53
+ protected readonly openTelemetryCollector: OpenTelemetryCollector<Metrics>,
54
+ protected readonly schemaValidator: SchemaValidator,
55
+ protected readonly dtoMappers: {
56
+ CheckoutSessionDtoMapper: ResponseDtoMapperConstructor<
57
+ SchemaValidator,
58
+ Dto['CheckoutSessionDtoMapper'],
59
+ Entities['CheckoutSessionDtoMapper']
60
+ >;
61
+ CreateCheckoutSessionDtoMapper: RequestDtoMapperConstructor<
62
+ SchemaValidator,
63
+ Dto['CreateCheckoutSessionDtoMapper'],
64
+ Entities['CreateCheckoutSessionDtoMapper']
65
+ >;
66
+ UpdateCheckoutSessionDtoMapper: RequestDtoMapperConstructor<
67
+ SchemaValidator,
68
+ Dto['UpdateCheckoutSessionDtoMapper'],
69
+ Entities['UpdateCheckoutSessionDtoMapper']
70
+ >;
71
+ }
72
+ ) {
73
+ this.#dtoMappers = transformIntoInternalDtoMapper(
74
+ dtoMappers,
75
+ schemaValidator
76
+ );
77
+ }
78
+
79
+ protected createCacheKey = createCacheKey('checkout_session');
80
+
81
+ async createCheckoutSession(
82
+ checkoutSessionDto: Dto['CreateCheckoutSessionDtoMapper']
83
+ ): Promise<Dto['CheckoutSessionDtoMapper']> {
84
+ const checkoutSession =
85
+ this.#dtoMappers.CreateCheckoutSessionDtoMapper.deserializeDtoToEntity(
86
+ checkoutSessionDto
87
+ );
88
+
89
+ // Store the checkoutSession details in the cache
90
+ await this.cache.putRecord({
91
+ key: this.createCacheKey(checkoutSession.id),
92
+ value: checkoutSession,
93
+ ttlMilliseconds: this.cache.getTtlMilliseconds()
94
+ });
95
+
96
+ return this.#dtoMappers.CheckoutSessionDtoMapper.serializeEntityToDto(
97
+ checkoutSession
98
+ );
99
+ }
100
+
101
+ async getCheckoutSession({
102
+ id
103
+ }: IdDto): Promise<Dto['CheckoutSessionDtoMapper']> {
104
+ const checkoutSessionDetails = await this.cache.readRecord<
105
+ Entities['CheckoutSessionDtoMapper']
106
+ >(this.createCacheKey(id));
107
+ if (!checkoutSessionDetails) {
108
+ throw new Error('Session not found');
109
+ }
110
+
111
+ return this.#dtoMappers.CheckoutSessionDtoMapper.serializeEntityToDto(
112
+ checkoutSessionDetails.value
113
+ );
114
+ }
115
+
116
+ async expireCheckoutSession({ id }: IdDto): Promise<void> {
117
+ const checkoutSessionDetails = await this.cache.readRecord(
118
+ this.createCacheKey(id)
119
+ );
120
+ if (!checkoutSessionDetails) {
121
+ throw new Error('Session not found');
122
+ }
123
+ await this.cache.deleteRecord(this.createCacheKey(id));
124
+ }
125
+
126
+ async handleCheckoutSuccess({ id }: IdDto): Promise<void> {
127
+ this.openTelemetryCollector.info('Checkout success', { id });
128
+ await this.cache.deleteRecord(this.createCacheKey(id));
129
+ }
130
+
131
+ async handleCheckoutFailure({ id }: IdDto): Promise<void> {
132
+ this.openTelemetryCollector.info('Checkout failure', { id });
133
+ await this.cache.deleteRecord(this.createCacheKey(id));
134
+ }
135
+ }