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