@forklaunch/implementation-billing-base 0.2.3 → 0.3.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.
Files changed (81) hide show
  1. package/lib/schemas/index.d.mts +402 -0
  2. package/lib/schemas/index.d.ts +402 -6
  3. package/lib/schemas/index.js +498 -5
  4. package/lib/schemas/index.mjs +547 -0
  5. package/lib/services/index.d.mts +255 -0
  6. package/lib/services/index.d.ts +255 -6
  7. package/lib/services/index.js +610 -5
  8. package/lib/services/index.mjs +599 -0
  9. package/package.json +6 -5
  10. package/lib/__test__/schemaEquality.test.d.ts +0 -2
  11. package/lib/__test__/schemaEquality.test.d.ts.map +0 -1
  12. package/lib/__test__/schemaEquality.test.js +0 -430
  13. package/lib/jest.config.d.ts +0 -4
  14. package/lib/jest.config.d.ts.map +0 -1
  15. package/lib/jest.config.js +0 -19
  16. package/lib/schemas/billingPortal.schema.d.ts +0 -119
  17. package/lib/schemas/billingPortal.schema.d.ts.map +0 -1
  18. package/lib/schemas/billingPortal.schema.js +0 -7
  19. package/lib/schemas/checkoutSession.schema.d.ts +0 -265
  20. package/lib/schemas/checkoutSession.schema.d.ts.map +0 -1
  21. package/lib/schemas/checkoutSession.schema.js +0 -7
  22. package/lib/schemas/index.d.ts.map +0 -1
  23. package/lib/schemas/paymentLink.schema.d.ts +0 -319
  24. package/lib/schemas/paymentLink.schema.d.ts.map +0 -1
  25. package/lib/schemas/paymentLink.schema.js +0 -7
  26. package/lib/schemas/plan.schema.d.ts +0 -338
  27. package/lib/schemas/plan.schema.d.ts.map +0 -1
  28. package/lib/schemas/plan.schema.js +0 -7
  29. package/lib/schemas/subscription.schema.d.ts +0 -366
  30. package/lib/schemas/subscription.schema.d.ts.map +0 -1
  31. package/lib/schemas/subscription.schema.js +0 -7
  32. package/lib/schemas/typebox/billingPortal.schema.d.ts +0 -161
  33. package/lib/schemas/typebox/billingPortal.schema.d.ts.map +0 -1
  34. package/lib/schemas/typebox/billingPortal.schema.js +0 -31
  35. package/lib/schemas/typebox/checkoutSession.schema.d.ts +0 -316
  36. package/lib/schemas/typebox/checkoutSession.schema.d.ts.map +0 -1
  37. package/lib/schemas/typebox/checkoutSession.schema.js +0 -50
  38. package/lib/schemas/typebox/paymentLink.schema.d.ts +0 -406
  39. package/lib/schemas/typebox/paymentLink.schema.d.ts.map +0 -1
  40. package/lib/schemas/typebox/paymentLink.schema.js +0 -55
  41. package/lib/schemas/typebox/plan.schema.d.ts +0 -408
  42. package/lib/schemas/typebox/plan.schema.d.ts.map +0 -1
  43. package/lib/schemas/typebox/plan.schema.js +0 -57
  44. package/lib/schemas/typebox/subscription.schema.d.ts +0 -456
  45. package/lib/schemas/typebox/subscription.schema.d.ts.map +0 -1
  46. package/lib/schemas/typebox/subscription.schema.js +0 -61
  47. package/lib/schemas/zod/billingPortal.schema.d.ts +0 -63
  48. package/lib/schemas/zod/billingPortal.schema.d.ts.map +0 -1
  49. package/lib/schemas/zod/billingPortal.schema.js +0 -31
  50. package/lib/schemas/zod/checkoutSession.schema.d.ts +0 -176
  51. package/lib/schemas/zod/checkoutSession.schema.d.ts.map +0 -1
  52. package/lib/schemas/zod/checkoutSession.schema.js +0 -50
  53. package/lib/schemas/zod/paymentLink.schema.d.ts +0 -172
  54. package/lib/schemas/zod/paymentLink.schema.d.ts.map +0 -1
  55. package/lib/schemas/zod/paymentLink.schema.js +0 -55
  56. package/lib/schemas/zod/plan.schema.d.ts +0 -196
  57. package/lib/schemas/zod/plan.schema.d.ts.map +0 -1
  58. package/lib/schemas/zod/plan.schema.js +0 -57
  59. package/lib/schemas/zod/subscription.schema.d.ts +0 -204
  60. package/lib/schemas/zod/subscription.schema.d.ts.map +0 -1
  61. package/lib/schemas/zod/subscription.schema.js +0 -61
  62. package/lib/services/billingPortal.service.d.ts +0 -112
  63. package/lib/services/billingPortal.service.d.ts.map +0 -1
  64. package/lib/services/billingPortal.service.js +0 -128
  65. package/lib/services/checkoutSession.service.d.ts +0 -137
  66. package/lib/services/checkoutSession.service.d.ts.map +0 -1
  67. package/lib/services/checkoutSession.service.js +0 -111
  68. package/lib/services/index.d.ts.map +0 -1
  69. package/lib/services/paymentLink.service.d.ts +0 -118
  70. package/lib/services/paymentLink.service.d.ts.map +0 -1
  71. package/lib/services/paymentLink.service.js +0 -154
  72. package/lib/services/plan.service.d.ts +0 -102
  73. package/lib/services/plan.service.d.ts.map +0 -1
  74. package/lib/services/plan.service.js +0 -80
  75. package/lib/services/subscription.service.d.ts +0 -159
  76. package/lib/services/subscription.service.d.ts.map +0 -1
  77. package/lib/services/subscription.service.js +0 -169
  78. package/lib/tsconfig.tsbuildinfo +0 -1
  79. package/lib/vitest.config.d.ts +0 -3
  80. package/lib/vitest.config.d.ts.map +0 -1
  81. package/lib/vitest.config.js +0 -7
@@ -1,5 +1,610 @@
1
- export * from './billingPortal.service';
2
- export * from './checkoutSession.service';
3
- export * from './paymentLink.service';
4
- export * from './plan.service';
5
- export * from './subscription.service';
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // services/index.ts
21
+ var services_exports = {};
22
+ __export(services_exports, {
23
+ BaseBillingPortalService: () => BaseBillingPortalService,
24
+ BaseCheckoutSessionService: () => BaseCheckoutSessionService,
25
+ BasePaymentLinkService: () => BasePaymentLinkService,
26
+ BasePlanService: () => BasePlanService,
27
+ BaseSubscriptionService: () => BaseSubscriptionService
28
+ });
29
+ module.exports = __toCommonJS(services_exports);
30
+
31
+ // services/billingPortal.service.ts
32
+ var import_cache = require("@forklaunch/core/cache");
33
+ var import_http = require("@forklaunch/core/http");
34
+ var import_mappers = require("@forklaunch/core/mappers");
35
+ var BaseBillingPortalService = class {
36
+ constructor(em, cache, openTelemetryCollector, schemaValidator, mappers, options) {
37
+ this.em = em;
38
+ this.cache = cache;
39
+ this.openTelemetryCollector = openTelemetryCollector;
40
+ this.schemaValidator = schemaValidator;
41
+ this.mappers = mappers;
42
+ this.options = options;
43
+ this.#mappers = (0, import_mappers.transformIntoInternalDtoMapper)(mappers, schemaValidator);
44
+ this.enableDatabaseBackup = options?.enableDatabaseBackup ?? false;
45
+ this.evaluatedTelemetryOptions = options?.telemetry ? (0, import_http.evaluateTelemetryOptions)(options.telemetry).enabled : {
46
+ logging: false,
47
+ metrics: false,
48
+ tracing: false
49
+ };
50
+ }
51
+ #mappers;
52
+ evaluatedTelemetryOptions;
53
+ enableDatabaseBackup;
54
+ createCacheKey = (0, import_cache.createCacheKey)("billing_portal_session");
55
+ async createBillingPortalSession(billingPortalDto) {
56
+ if (this.evaluatedTelemetryOptions.logging) {
57
+ this.openTelemetryCollector.info(
58
+ "Creating billing portal session",
59
+ billingPortalDto
60
+ );
61
+ }
62
+ const billingPortal = await this.#mappers.CreateBillingPortalDtoMapper.deserializeDtoToEntity(
63
+ billingPortalDto,
64
+ this.em
65
+ );
66
+ if (this.enableDatabaseBackup) {
67
+ await this.em.persistAndFlush(billingPortal);
68
+ }
69
+ const createdBillingPortalDto = await this.#mappers.BillingPortalDtoMapper.serializeEntityToDto(
70
+ billingPortal
71
+ );
72
+ await this.cache.putRecord({
73
+ key: this.createCacheKey(createdBillingPortalDto.id),
74
+ value: createdBillingPortalDto,
75
+ ttlMilliseconds: this.cache.getTtlMilliseconds()
76
+ });
77
+ return createdBillingPortalDto;
78
+ }
79
+ async getBillingPortalSession(idDto) {
80
+ if (this.evaluatedTelemetryOptions.logging) {
81
+ this.openTelemetryCollector.info("Getting billing portal session", idDto);
82
+ }
83
+ const billingPortalDetails = await this.cache.readRecord(this.createCacheKey(idDto.id));
84
+ if (!billingPortalDetails) {
85
+ throw new Error("Session not found");
86
+ }
87
+ return billingPortalDetails.value;
88
+ }
89
+ async updateBillingPortalSession(billingPortalDto) {
90
+ if (this.evaluatedTelemetryOptions.logging) {
91
+ this.openTelemetryCollector.info(
92
+ "Updating billing portal session",
93
+ billingPortalDto
94
+ );
95
+ }
96
+ const existingBillingPortal = (await this.cache.readRecord(
97
+ this.createCacheKey(billingPortalDto.id)
98
+ ))?.value;
99
+ if (!existingBillingPortal) {
100
+ throw new Error("Session not found");
101
+ }
102
+ const billingPortal = await this.#mappers.UpdateBillingPortalDtoMapper.deserializeDtoToEntity(
103
+ billingPortalDto,
104
+ this.em
105
+ );
106
+ if (this.enableDatabaseBackup) {
107
+ await this.em.persistAndFlush({
108
+ billingPortal
109
+ });
110
+ }
111
+ const updatedBillingPortalDto = {
112
+ ...existingBillingPortal,
113
+ ...await this.#mappers.BillingPortalDtoMapper.serializeEntityToDto(
114
+ billingPortal
115
+ )
116
+ };
117
+ await this.cache.putRecord({
118
+ key: this.createCacheKey(updatedBillingPortalDto.id),
119
+ value: updatedBillingPortalDto,
120
+ ttlMilliseconds: this.cache.getTtlMilliseconds()
121
+ });
122
+ return updatedBillingPortalDto;
123
+ }
124
+ async expireBillingPortalSession(idDto) {
125
+ if (this.evaluatedTelemetryOptions.logging) {
126
+ this.openTelemetryCollector.info(
127
+ "Expiring billing portal session",
128
+ idDto
129
+ );
130
+ }
131
+ const sessionExists = await this.cache.readRecord(
132
+ this.createCacheKey(idDto.id)
133
+ );
134
+ if (!sessionExists) {
135
+ throw new Error("Session not found");
136
+ }
137
+ await this.cache.deleteRecord(this.createCacheKey(idDto.id));
138
+ }
139
+ };
140
+
141
+ // services/checkoutSession.service.ts
142
+ var import_cache2 = require("@forklaunch/core/cache");
143
+ var import_http2 = require("@forklaunch/core/http");
144
+ var import_mappers2 = require("@forklaunch/core/mappers");
145
+ var BaseCheckoutSessionService = class {
146
+ constructor(em, cache, openTelemetryCollector, schemaValidator, mappers, options) {
147
+ this.em = em;
148
+ this.cache = cache;
149
+ this.openTelemetryCollector = openTelemetryCollector;
150
+ this.schemaValidator = schemaValidator;
151
+ this.mappers = mappers;
152
+ this.options = options;
153
+ this.#mappers = (0, import_mappers2.transformIntoInternalDtoMapper)(mappers, schemaValidator);
154
+ this.enableDatabaseBackup = options?.enableDatabaseBackup ?? false;
155
+ this.evaluatedTelemetryOptions = options?.telemetry ? (0, import_http2.evaluateTelemetryOptions)(options.telemetry).enabled : {
156
+ logging: false,
157
+ metrics: false,
158
+ tracing: false
159
+ };
160
+ }
161
+ #mappers;
162
+ evaluatedTelemetryOptions;
163
+ enableDatabaseBackup;
164
+ createCacheKey = (0, import_cache2.createCacheKey)("checkout_session");
165
+ async createCheckoutSession(checkoutSessionDto) {
166
+ if (this.evaluatedTelemetryOptions.logging) {
167
+ this.openTelemetryCollector.info(
168
+ "Creating checkout session",
169
+ checkoutSessionDto
170
+ );
171
+ }
172
+ const checkoutSession = await this.#mappers.CreateCheckoutSessionDtoMapper.deserializeDtoToEntity(
173
+ checkoutSessionDto,
174
+ this.em
175
+ );
176
+ const createdCheckoutSessionDto = await this.#mappers.CheckoutSessionDtoMapper.serializeEntityToDto(
177
+ checkoutSession
178
+ );
179
+ if (this.enableDatabaseBackup) {
180
+ await this.em.persistAndFlush(checkoutSession);
181
+ }
182
+ await this.cache.putRecord({
183
+ key: this.createCacheKey(createdCheckoutSessionDto.id),
184
+ value: createdCheckoutSessionDto,
185
+ ttlMilliseconds: this.cache.getTtlMilliseconds()
186
+ });
187
+ return createdCheckoutSessionDto;
188
+ }
189
+ async getCheckoutSession({
190
+ id
191
+ }) {
192
+ const checkoutSessionDetails = await this.cache.readRecord(this.createCacheKey(id));
193
+ if (!checkoutSessionDetails) {
194
+ throw new Error("Session not found");
195
+ }
196
+ return this.#mappers.CheckoutSessionDtoMapper.serializeEntityToDto(
197
+ checkoutSessionDetails.value
198
+ );
199
+ }
200
+ async expireCheckoutSession({ id }) {
201
+ const checkoutSessionDetails = await this.cache.readRecord(
202
+ this.createCacheKey(id)
203
+ );
204
+ if (!checkoutSessionDetails) {
205
+ throw new Error("Session not found");
206
+ }
207
+ await this.cache.deleteRecord(this.createCacheKey(id));
208
+ }
209
+ async handleCheckoutSuccess({ id }) {
210
+ if (this.evaluatedTelemetryOptions.logging) {
211
+ this.openTelemetryCollector.info("Checkout success", { id });
212
+ }
213
+ if (this.enableDatabaseBackup) {
214
+ const checkoutSession = await this.em.upsert("CheckoutSession", {
215
+ id,
216
+ status: "SUCCESS"
217
+ });
218
+ await this.em.persistAndFlush(checkoutSession);
219
+ }
220
+ await this.cache.deleteRecord(this.createCacheKey(id));
221
+ }
222
+ async handleCheckoutFailure({ id }) {
223
+ if (this.evaluatedTelemetryOptions.logging) {
224
+ this.openTelemetryCollector.info("Checkout failure", { id });
225
+ }
226
+ if (this.enableDatabaseBackup) {
227
+ const checkoutSession = await this.em.upsert("CheckoutSession", {
228
+ id,
229
+ status: "FAILED"
230
+ });
231
+ await this.em.persistAndFlush(checkoutSession);
232
+ }
233
+ await this.cache.deleteRecord(this.createCacheKey(id));
234
+ }
235
+ };
236
+
237
+ // services/paymentLink.service.ts
238
+ var import_cache3 = require("@forklaunch/core/cache");
239
+ var import_http3 = require("@forklaunch/core/http");
240
+ var import_mappers3 = require("@forklaunch/core/mappers");
241
+ var BasePaymentLinkService = class {
242
+ constructor(em, cache, openTelemetryCollector, schemaValidator, mappers, options) {
243
+ this.em = em;
244
+ this.cache = cache;
245
+ this.openTelemetryCollector = openTelemetryCollector;
246
+ this.schemaValidator = schemaValidator;
247
+ this.mappers = mappers;
248
+ this.options = options;
249
+ this.#mappers = (0, import_mappers3.transformIntoInternalDtoMapper)(mappers, schemaValidator);
250
+ this.enableDatabaseBackup = options?.enableDatabaseBackup ?? false;
251
+ this.evaluatedTelemetryOptions = options?.telemetry ? (0, import_http3.evaluateTelemetryOptions)(options.telemetry).enabled : {
252
+ logging: false,
253
+ metrics: false,
254
+ tracing: false
255
+ };
256
+ }
257
+ #mappers;
258
+ evaluatedTelemetryOptions;
259
+ enableDatabaseBackup;
260
+ cacheKeyPrefix = "payment_link";
261
+ createCacheKey = (0, import_cache3.createCacheKey)(this.cacheKeyPrefix);
262
+ async createPaymentLink(paymentLinkDto) {
263
+ if (this.evaluatedTelemetryOptions.logging) {
264
+ this.openTelemetryCollector.info("Creating payment link", paymentLinkDto);
265
+ }
266
+ const paymentLink = await this.#mappers.CreatePaymentLinkDtoMapper.deserializeDtoToEntity(
267
+ paymentLinkDto,
268
+ this.em
269
+ );
270
+ if (this.enableDatabaseBackup) {
271
+ await this.em.persistAndFlush(paymentLink);
272
+ }
273
+ const createdPaymentLinkDto = await this.#mappers.PaymentLinkDtoMapper.serializeEntityToDto(
274
+ paymentLink
275
+ );
276
+ await this.cache.putRecord({
277
+ key: this.createCacheKey(createdPaymentLinkDto.id),
278
+ value: createdPaymentLinkDto,
279
+ ttlMilliseconds: this.cache.getTtlMilliseconds()
280
+ });
281
+ return createdPaymentLinkDto;
282
+ }
283
+ async updatePaymentLink(paymentLinkDto) {
284
+ if (this.evaluatedTelemetryOptions.logging) {
285
+ this.openTelemetryCollector.info("Updating payment link", paymentLinkDto);
286
+ }
287
+ const cacheKey = this.createCacheKey(paymentLinkDto.id);
288
+ const existingLink = (await this.cache.readRecord(cacheKey))?.value;
289
+ if (!existingLink) {
290
+ throw new Error("Payment link not found");
291
+ }
292
+ const paymentLink = await this.#mappers.UpdatePaymentLinkDtoMapper.deserializeDtoToEntity(
293
+ paymentLinkDto,
294
+ this.em
295
+ );
296
+ if (this.enableDatabaseBackup) {
297
+ await this.em.persistAndFlush(paymentLink);
298
+ }
299
+ const updatedLinkDto = {
300
+ ...existingLink,
301
+ ...await this.#mappers.PaymentLinkDtoMapper.serializeEntityToDto(
302
+ paymentLink
303
+ )
304
+ };
305
+ await this.cache.putRecord({
306
+ key: cacheKey,
307
+ value: updatedLinkDto,
308
+ ttlMilliseconds: this.cache.getTtlMilliseconds()
309
+ });
310
+ return updatedLinkDto;
311
+ }
312
+ async getPaymentLink({ id }) {
313
+ if (this.evaluatedTelemetryOptions.logging) {
314
+ this.openTelemetryCollector.info("Getting payment link", { id });
315
+ }
316
+ const cacheKey = this.createCacheKey(id);
317
+ const paymentLink = await this.cache.readRecord(cacheKey);
318
+ if (!paymentLink) {
319
+ throw new Error("Payment link not found");
320
+ }
321
+ return this.#mappers.PaymentLinkDtoMapper.serializeEntityToDto(
322
+ paymentLink.value
323
+ );
324
+ }
325
+ async expirePaymentLink({ id }) {
326
+ this.openTelemetryCollector.info("Payment link expired", { id });
327
+ if (this.enableDatabaseBackup) {
328
+ const paymentLink = await this.em.upsert("PaymentLink", {
329
+ id,
330
+ status: "EXPIRED"
331
+ });
332
+ await this.em.removeAndFlush(paymentLink);
333
+ }
334
+ await this.cache.deleteRecord(this.createCacheKey(id));
335
+ }
336
+ async handlePaymentSuccess({ id }) {
337
+ this.openTelemetryCollector.info("Payment link success", { id });
338
+ if (this.enableDatabaseBackup) {
339
+ const paymentLink = await this.em.upsert("PaymentLink", {
340
+ id,
341
+ status: "COMPLETED"
342
+ });
343
+ await this.em.removeAndFlush(paymentLink);
344
+ }
345
+ await this.cache.deleteRecord(this.createCacheKey(id));
346
+ }
347
+ async handlePaymentFailure({ id }) {
348
+ this.openTelemetryCollector.info("Payment link failure", { id });
349
+ if (this.enableDatabaseBackup) {
350
+ const paymentLink = await this.em.upsert("PaymentLink", {
351
+ id,
352
+ status: "FAILED"
353
+ });
354
+ await this.em.removeAndFlush(paymentLink);
355
+ }
356
+ await this.cache.deleteRecord(this.createCacheKey(id));
357
+ }
358
+ async listPaymentLinks(idsDto) {
359
+ const keys = idsDto?.ids.map((id) => this.createCacheKey(id)) ?? await this.cache.listKeys(this.cacheKeyPrefix);
360
+ return Promise.all(
361
+ keys.map(async (key) => {
362
+ const paymentLink = await this.cache.readRecord(key);
363
+ const paymentLinkDto = this.#mappers.PaymentLinkDtoMapper.serializeEntityToDto(
364
+ paymentLink.value
365
+ );
366
+ return paymentLinkDto;
367
+ })
368
+ );
369
+ }
370
+ };
371
+
372
+ // services/plan.service.ts
373
+ var import_http4 = require("@forklaunch/core/http");
374
+ var import_mappers4 = require("@forklaunch/core/mappers");
375
+ var BasePlanService = class {
376
+ constructor(em, openTelemetryCollector, schemaValidator, mappers, options) {
377
+ this.em = em;
378
+ this.openTelemetryCollector = openTelemetryCollector;
379
+ this.schemaValidator = schemaValidator;
380
+ this.mappers = mappers;
381
+ this.options = options;
382
+ this.#mappers = (0, import_mappers4.transformIntoInternalDtoMapper)(mappers, schemaValidator);
383
+ this.evaluatedTelemetryOptions = options?.telemetry ? (0, import_http4.evaluateTelemetryOptions)(options.telemetry).enabled : {
384
+ logging: false,
385
+ metrics: false,
386
+ tracing: false
387
+ };
388
+ }
389
+ #mappers;
390
+ evaluatedTelemetryOptions;
391
+ async listPlans(idsDto, em) {
392
+ if (this.evaluatedTelemetryOptions.logging) {
393
+ this.openTelemetryCollector.info("Listing plans", idsDto);
394
+ }
395
+ return Promise.all(
396
+ (await (em ?? this.em).findAll("Plan", {
397
+ filters: idsDto?.ids ? { id: { $in: idsDto.ids } } : void 0
398
+ })).map(
399
+ (plan) => this.#mappers.PlanDtoMapper.serializeEntityToDto(
400
+ plan
401
+ )
402
+ )
403
+ );
404
+ }
405
+ async createPlan(planDto, em) {
406
+ if (this.evaluatedTelemetryOptions.logging) {
407
+ this.openTelemetryCollector.info("Creating plan", planDto);
408
+ }
409
+ const plan = await this.#mappers.CreatePlanDtoMapper.deserializeDtoToEntity(
410
+ planDto,
411
+ em ?? this.em
412
+ );
413
+ await (em ?? this.em).transactional(async (innerEm) => {
414
+ await innerEm.persist(plan);
415
+ });
416
+ return this.#mappers.PlanDtoMapper.serializeEntityToDto(plan);
417
+ }
418
+ async getPlan(idDto, em) {
419
+ if (this.evaluatedTelemetryOptions.logging) {
420
+ this.openTelemetryCollector.info("Getting plan", idDto);
421
+ }
422
+ const plan = await (em ?? this.em).findOneOrFail("Plan", idDto);
423
+ return this.#mappers.PlanDtoMapper.serializeEntityToDto(
424
+ plan
425
+ );
426
+ }
427
+ async updatePlan(planDto, em) {
428
+ if (this.evaluatedTelemetryOptions.logging) {
429
+ this.openTelemetryCollector.info("Updating plan", planDto);
430
+ }
431
+ const plan = await this.#mappers.UpdatePlanDtoMapper.deserializeDtoToEntity(
432
+ planDto,
433
+ em ?? this.em
434
+ );
435
+ const updatedPlan = await (em ?? this.em).upsert(plan);
436
+ await (em ?? this.em).transactional(async (innerEm) => {
437
+ await innerEm.persist(plan);
438
+ });
439
+ const updatedPlanDto = await this.#mappers.PlanDtoMapper.serializeEntityToDto(updatedPlan);
440
+ return updatedPlanDto;
441
+ }
442
+ async deletePlan(idDto, em) {
443
+ if (this.evaluatedTelemetryOptions.logging) {
444
+ this.openTelemetryCollector.info("Deleting plan", idDto);
445
+ }
446
+ await (em ?? this.em).nativeDelete("Plan", idDto);
447
+ }
448
+ };
449
+
450
+ // services/subscription.service.ts
451
+ var import_http5 = require("@forklaunch/core/http");
452
+ var import_mappers5 = require("@forklaunch/core/mappers");
453
+ var BaseSubscriptionService = class {
454
+ constructor(em, openTelemetryCollector, schemaValidator, mappers, options) {
455
+ this.em = em;
456
+ this.openTelemetryCollector = openTelemetryCollector;
457
+ this.schemaValidator = schemaValidator;
458
+ this.mappers = mappers;
459
+ this.options = options;
460
+ this.#mappers = (0, import_mappers5.transformIntoInternalDtoMapper)(mappers, this.schemaValidator);
461
+ this.evaluatedTelemetryOptions = options?.telemetry ? (0, import_http5.evaluateTelemetryOptions)(options.telemetry).enabled : {
462
+ logging: false,
463
+ metrics: false,
464
+ tracing: false
465
+ };
466
+ }
467
+ #mappers;
468
+ evaluatedTelemetryOptions;
469
+ async createSubscription(subscriptionDto, em) {
470
+ if (this.evaluatedTelemetryOptions.logging) {
471
+ this.openTelemetryCollector.info(
472
+ "Creating subscription",
473
+ subscriptionDto
474
+ );
475
+ }
476
+ const subscription = await this.#mappers.CreateSubscriptionDtoMapper.deserializeDtoToEntity(
477
+ subscriptionDto,
478
+ em ?? this.em
479
+ );
480
+ await (em ?? this.em).transactional(async (innerEm) => {
481
+ await innerEm.persist(subscription);
482
+ });
483
+ const createdSubscriptionDto = await this.#mappers.SubscriptionDtoMapper.serializeEntityToDto(
484
+ subscription
485
+ );
486
+ return createdSubscriptionDto;
487
+ }
488
+ async getSubscription(idDto, em) {
489
+ if (this.evaluatedTelemetryOptions.logging) {
490
+ this.openTelemetryCollector.info("Getting subscription", idDto);
491
+ }
492
+ const subscription = await (em ?? this.em).findOneOrFail(
493
+ "Subscription",
494
+ idDto
495
+ );
496
+ return this.#mappers.SubscriptionDtoMapper.serializeEntityToDto(
497
+ subscription
498
+ );
499
+ }
500
+ async getUserSubscription({ id }, em) {
501
+ if (this.evaluatedTelemetryOptions.logging) {
502
+ this.openTelemetryCollector.info("Getting user subscription", id);
503
+ }
504
+ const subscription = await (em ?? this.em).findOneOrFail("Subscription", {
505
+ partyId: id,
506
+ partyType: "USER",
507
+ active: true
508
+ });
509
+ return this.#mappers.SubscriptionDtoMapper.serializeEntityToDto(
510
+ subscription
511
+ );
512
+ }
513
+ async getOrganizationSubscription({ id }, em) {
514
+ if (this.evaluatedTelemetryOptions.logging) {
515
+ this.openTelemetryCollector.info("Getting organization subscription", id);
516
+ }
517
+ const subscription = await (em ?? this.em).findOneOrFail("Subscription", {
518
+ partyId: id,
519
+ partyType: "ORGANIZATION",
520
+ active: true
521
+ });
522
+ return this.#mappers.SubscriptionDtoMapper.serializeEntityToDto(
523
+ subscription
524
+ );
525
+ }
526
+ async updateSubscription(subscriptionDto, em) {
527
+ if (this.evaluatedTelemetryOptions.logging) {
528
+ this.openTelemetryCollector.info(
529
+ "Updating subscription",
530
+ subscriptionDto
531
+ );
532
+ }
533
+ const subscription = this.#mappers.UpdateSubscriptionDtoMapper.deserializeDtoToEntity(
534
+ subscriptionDto,
535
+ em ?? this.em
536
+ );
537
+ const updatedSubscription = await (em ?? this.em).upsert(subscription);
538
+ await (em ?? this.em).transactional(async (innerEm) => {
539
+ await innerEm.persist(updatedSubscription);
540
+ });
541
+ const updatedSubscriptionDto = await this.#mappers.SubscriptionDtoMapper.serializeEntityToDto(
542
+ updatedSubscription
543
+ );
544
+ return updatedSubscriptionDto;
545
+ }
546
+ async deleteSubscription(idDto, em) {
547
+ if (this.evaluatedTelemetryOptions.logging) {
548
+ this.openTelemetryCollector.info("Deleting subscription", idDto);
549
+ }
550
+ const subscription = await (em ?? this.em).findOne("Subscription", idDto);
551
+ if (!subscription) {
552
+ throw new Error("Subscription not found");
553
+ }
554
+ await (em ?? this.em).removeAndFlush(subscription);
555
+ }
556
+ async listSubscriptions(idsDto, em) {
557
+ if (this.evaluatedTelemetryOptions.logging) {
558
+ this.openTelemetryCollector.info("Listing subscriptions", idsDto);
559
+ }
560
+ const subscriptions = await (em ?? this.em).findAll("Subscription", {
561
+ where: idsDto.ids ? {
562
+ id: {
563
+ $in: idsDto.ids
564
+ }
565
+ } : void 0
566
+ });
567
+ return Promise.all(
568
+ subscriptions.map((subscription) => {
569
+ const subscriptionDto = this.#mappers.SubscriptionDtoMapper.serializeEntityToDto(
570
+ subscription
571
+ );
572
+ return subscriptionDto;
573
+ })
574
+ );
575
+ }
576
+ async cancelSubscription(idDto, em) {
577
+ if (this.evaluatedTelemetryOptions.logging) {
578
+ this.openTelemetryCollector.info("Canceling subscription", idDto);
579
+ }
580
+ const subscription = await (em ?? this.em).findOne("Subscription", idDto);
581
+ if (!subscription) {
582
+ throw new Error("Subscription not found");
583
+ }
584
+ subscription.active = false;
585
+ await (em ?? this.em).transactional(async (innerEm) => {
586
+ await innerEm.persist(subscription);
587
+ });
588
+ }
589
+ async resumeSubscription(idDto, em) {
590
+ if (this.evaluatedTelemetryOptions.logging) {
591
+ this.openTelemetryCollector.info("Resuming subscription", idDto);
592
+ }
593
+ const subscription = await (em ?? this.em).findOne("Subscription", idDto);
594
+ if (!subscription) {
595
+ throw new Error("Subscription not found");
596
+ }
597
+ subscription.active = true;
598
+ await (em ?? this.em).transactional(async (innerEm) => {
599
+ await innerEm.persist(subscription);
600
+ });
601
+ }
602
+ };
603
+ // Annotate the CommonJS export names for ESM import in node:
604
+ 0 && (module.exports = {
605
+ BaseBillingPortalService,
606
+ BaseCheckoutSessionService,
607
+ BasePaymentLinkService,
608
+ BasePlanService,
609
+ BaseSubscriptionService
610
+ });