@forklaunch/implementation-billing-base 0.1.13 → 0.1.15
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/__test__/schemaEquality.test.d.ts +1 -1
- package/lib/__test__/schemaEquality.test.js +305 -146
- package/lib/jest.config.d.ts +1 -1
- package/lib/jest.config.js +16 -16
- package/lib/schemas/billingPortal.schema.d.ts +103 -34
- package/lib/schemas/billingPortal.schema.js +4 -1
- package/lib/schemas/checkoutSession.schema.d.ts +169 -54
- package/lib/schemas/checkoutSession.schema.js +4 -1
- package/lib/schemas/index.d.ts +1 -1
- package/lib/schemas/paymentLink.schema.d.ts +220 -64
- package/lib/schemas/paymentLink.schema.js +4 -1
- package/lib/schemas/plan.schema.d.ts +328 -76
- package/lib/schemas/plan.schema.js +4 -1
- package/lib/schemas/subscription.schema.d.ts +356 -88
- package/lib/schemas/subscription.schema.js +4 -1
- package/lib/schemas/typebox/billingPortal.schema.d.ts +152 -38
- package/lib/schemas/typebox/billingPortal.schema.js +23 -17
- package/lib/schemas/typebox/checkoutSession.schema.d.ts +189 -55
- package/lib/schemas/typebox/checkoutSession.schema.js +25 -13
- package/lib/schemas/typebox/paymentLink.schema.d.ts +267 -65
- package/lib/schemas/typebox/paymentLink.schema.js +27 -15
- package/lib/schemas/typebox/plan.schema.d.ts +401 -77
- package/lib/schemas/typebox/plan.schema.js +31 -17
- package/lib/schemas/typebox/subscription.schema.d.ts +449 -89
- package/lib/schemas/typebox/subscription.schema.js +31 -19
- package/lib/schemas/zod/billingPortal.schema.d.ts +54 -38
- package/lib/schemas/zod/billingPortal.schema.js +23 -17
- package/lib/schemas/zod/checkoutSession.schema.d.ts +115 -55
- package/lib/schemas/zod/checkoutSession.schema.js +25 -13
- package/lib/schemas/zod/paymentLink.schema.d.ts +111 -65
- package/lib/schemas/zod/paymentLink.schema.js +27 -15
- package/lib/schemas/zod/plan.schema.d.ts +189 -77
- package/lib/schemas/zod/plan.schema.js +31 -17
- package/lib/schemas/zod/subscription.schema.d.ts +197 -89
- package/lib/schemas/zod/subscription.schema.js +31 -19
- package/lib/services/billingPortal.service.d.ts +77 -28
- package/lib/services/billingPortal.service.js +64 -45
- package/lib/services/checkoutSession.service.d.ts +77 -29
- package/lib/services/checkoutSession.service.js +54 -43
- package/lib/services/index.d.ts +1 -1
- package/lib/services/paymentLink.service.d.ts +82 -32
- package/lib/services/paymentLink.service.js +84 -66
- package/lib/services/plan.service.d.ts +73 -26
- package/lib/services/plan.service.js +45 -40
- package/lib/services/subscription.service.d.ts +136 -44
- package/lib/services/subscription.service.js +111 -88
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/lib/vitest.config.d.ts +2 -2
- package/lib/vitest.config.js +4 -4
- package/package.json +8 -8
|
@@ -1,46 +1,94 @@
|
|
|
1
1
|
import { IdDto } from '@forklaunch/common';
|
|
2
2
|
import { TtlCache } from '@forklaunch/core/cache';
|
|
3
|
-
import {
|
|
4
|
-
|
|
3
|
+
import {
|
|
4
|
+
RequestDtoMapperConstructor,
|
|
5
|
+
ResponseDtoMapperConstructor
|
|
6
|
+
} from '@forklaunch/core/mappers';
|
|
7
|
+
import {
|
|
8
|
+
MetricsDefinition,
|
|
9
|
+
OpenTelemetryCollector
|
|
10
|
+
} from '@forklaunch/core/http';
|
|
5
11
|
import { CheckoutSessionService } from '@forklaunch/interfaces-billing/interfaces';
|
|
6
|
-
import {
|
|
12
|
+
import {
|
|
13
|
+
CheckoutSessionDto,
|
|
14
|
+
CreateCheckoutSessionDto,
|
|
15
|
+
UpdateCheckoutSessionDto
|
|
16
|
+
} from '@forklaunch/interfaces-billing/types';
|
|
7
17
|
import { AnySchemaValidator } from '@forklaunch/validator';
|
|
8
|
-
export declare class BaseCheckoutSessionService<
|
|
18
|
+
export declare class BaseCheckoutSessionService<
|
|
19
|
+
SchemaValidator extends AnySchemaValidator,
|
|
20
|
+
PaymentMethodEnum,
|
|
21
|
+
Metrics extends MetricsDefinition = MetricsDefinition,
|
|
22
|
+
Dto extends {
|
|
9
23
|
CheckoutSessionDtoMapper: CheckoutSessionDto<PaymentMethodEnum>;
|
|
10
24
|
CreateCheckoutSessionDtoMapper: CreateCheckoutSessionDto<PaymentMethodEnum>;
|
|
11
25
|
UpdateCheckoutSessionDtoMapper: UpdateCheckoutSessionDto<PaymentMethodEnum>;
|
|
12
|
-
} = {
|
|
26
|
+
} = {
|
|
13
27
|
CheckoutSessionDtoMapper: CheckoutSessionDto<PaymentMethodEnum>;
|
|
14
28
|
CreateCheckoutSessionDtoMapper: CreateCheckoutSessionDto<PaymentMethodEnum>;
|
|
15
29
|
UpdateCheckoutSessionDtoMapper: UpdateCheckoutSessionDto<PaymentMethodEnum>;
|
|
16
|
-
},
|
|
30
|
+
},
|
|
31
|
+
Entities extends {
|
|
17
32
|
CheckoutSessionDtoMapper: CheckoutSessionDto<PaymentMethodEnum>;
|
|
18
33
|
CreateCheckoutSessionDtoMapper: CheckoutSessionDto<PaymentMethodEnum>;
|
|
19
34
|
UpdateCheckoutSessionDtoMapper: CheckoutSessionDto<PaymentMethodEnum>;
|
|
20
|
-
} = {
|
|
35
|
+
} = {
|
|
21
36
|
CheckoutSessionDtoMapper: CheckoutSessionDto<PaymentMethodEnum>;
|
|
22
37
|
CreateCheckoutSessionDtoMapper: CheckoutSessionDto<PaymentMethodEnum>;
|
|
23
38
|
UpdateCheckoutSessionDtoMapper: CheckoutSessionDto<PaymentMethodEnum>;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
39
|
+
}
|
|
40
|
+
> implements CheckoutSessionService<PaymentMethodEnum>
|
|
41
|
+
{
|
|
42
|
+
#private;
|
|
43
|
+
protected readonly cache: TtlCache;
|
|
44
|
+
protected readonly openTelemetryCollector: OpenTelemetryCollector<Metrics>;
|
|
45
|
+
protected readonly schemaValidator: SchemaValidator;
|
|
46
|
+
protected readonly mapperss: {
|
|
47
|
+
CheckoutSessionDtoMapper: ResponseDtoMapperConstructor<
|
|
48
|
+
SchemaValidator,
|
|
49
|
+
Dto['CheckoutSessionDtoMapper'],
|
|
50
|
+
Entities['CheckoutSessionDtoMapper']
|
|
51
|
+
>;
|
|
52
|
+
CreateCheckoutSessionDtoMapper: RequestDtoMapperConstructor<
|
|
53
|
+
SchemaValidator,
|
|
54
|
+
Dto['CreateCheckoutSessionDtoMapper'],
|
|
55
|
+
Entities['CreateCheckoutSessionDtoMapper']
|
|
56
|
+
>;
|
|
57
|
+
UpdateCheckoutSessionDtoMapper: RequestDtoMapperConstructor<
|
|
58
|
+
SchemaValidator,
|
|
59
|
+
Dto['UpdateCheckoutSessionDtoMapper'],
|
|
60
|
+
Entities['UpdateCheckoutSessionDtoMapper']
|
|
61
|
+
>;
|
|
62
|
+
};
|
|
63
|
+
constructor(
|
|
64
|
+
cache: TtlCache,
|
|
65
|
+
openTelemetryCollector: OpenTelemetryCollector<Metrics>,
|
|
66
|
+
schemaValidator: SchemaValidator,
|
|
67
|
+
mapperss: {
|
|
68
|
+
CheckoutSessionDtoMapper: ResponseDtoMapperConstructor<
|
|
69
|
+
SchemaValidator,
|
|
70
|
+
Dto['CheckoutSessionDtoMapper'],
|
|
71
|
+
Entities['CheckoutSessionDtoMapper']
|
|
72
|
+
>;
|
|
73
|
+
CreateCheckoutSessionDtoMapper: RequestDtoMapperConstructor<
|
|
74
|
+
SchemaValidator,
|
|
75
|
+
Dto['CreateCheckoutSessionDtoMapper'],
|
|
76
|
+
Entities['CreateCheckoutSessionDtoMapper']
|
|
77
|
+
>;
|
|
78
|
+
UpdateCheckoutSessionDtoMapper: RequestDtoMapperConstructor<
|
|
79
|
+
SchemaValidator,
|
|
80
|
+
Dto['UpdateCheckoutSessionDtoMapper'],
|
|
81
|
+
Entities['UpdateCheckoutSessionDtoMapper']
|
|
82
|
+
>;
|
|
83
|
+
}
|
|
84
|
+
);
|
|
85
|
+
protected createCacheKey: (id: string) => string;
|
|
86
|
+
createCheckoutSession(
|
|
87
|
+
checkoutSessionDto: Dto['CreateCheckoutSessionDtoMapper']
|
|
88
|
+
): Promise<Dto['CheckoutSessionDtoMapper']>;
|
|
89
|
+
getCheckoutSession({ id }: IdDto): Promise<Dto['CheckoutSessionDtoMapper']>;
|
|
90
|
+
expireCheckoutSession({ id }: IdDto): Promise<void>;
|
|
91
|
+
handleCheckoutSuccess({ id }: IdDto): Promise<void>;
|
|
92
|
+
handleCheckoutFailure({ id }: IdDto): Promise<void>;
|
|
45
93
|
}
|
|
46
|
-
//# sourceMappingURL=checkoutSession.service.d.ts.map
|
|
94
|
+
//# sourceMappingURL=checkoutSession.service.d.ts.map
|
|
@@ -1,49 +1,60 @@
|
|
|
1
1
|
import { createCacheKey } from '@forklaunch/core/cache';
|
|
2
2
|
import { transformIntoInternalDtoMapper } from '@forklaunch/core/mappers';
|
|
3
3
|
export class BaseCheckoutSessionService {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
4
|
+
cache;
|
|
5
|
+
openTelemetryCollector;
|
|
6
|
+
schemaValidator;
|
|
7
|
+
mapperss;
|
|
8
|
+
#mapperss;
|
|
9
|
+
constructor(cache, openTelemetryCollector, schemaValidator, mapperss) {
|
|
10
|
+
this.cache = cache;
|
|
11
|
+
this.openTelemetryCollector = openTelemetryCollector;
|
|
12
|
+
this.schemaValidator = schemaValidator;
|
|
13
|
+
this.mapperss = mapperss;
|
|
14
|
+
this.#mapperss = transformIntoInternalDtoMapper(mapperss, schemaValidator);
|
|
15
|
+
}
|
|
16
|
+
createCacheKey = createCacheKey('checkout_session');
|
|
17
|
+
async createCheckoutSession(checkoutSessionDto) {
|
|
18
|
+
const checkoutSession =
|
|
19
|
+
this.#mapperss.CreateCheckoutSessionDtoMapper.deserializeDtoToEntity(
|
|
20
|
+
checkoutSessionDto
|
|
21
|
+
);
|
|
22
|
+
// Store the checkoutSession details in the cache
|
|
23
|
+
await this.cache.putRecord({
|
|
24
|
+
key: this.createCacheKey(checkoutSession.id),
|
|
25
|
+
value: checkoutSession,
|
|
26
|
+
ttlMilliseconds: this.cache.getTtlMilliseconds()
|
|
27
|
+
});
|
|
28
|
+
return this.#mapperss.CheckoutSessionDtoMapper.serializeEntityToDto(
|
|
29
|
+
checkoutSession
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
async getCheckoutSession({ id }) {
|
|
33
|
+
const checkoutSessionDetails = await this.cache.readRecord(
|
|
34
|
+
this.createCacheKey(id)
|
|
35
|
+
);
|
|
36
|
+
if (!checkoutSessionDetails) {
|
|
37
|
+
throw new Error('Session not found');
|
|
15
38
|
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
}
|
|
27
|
-
async getCheckoutSession({ id }) {
|
|
28
|
-
const checkoutSessionDetails = await this.cache.readRecord(this.createCacheKey(id));
|
|
29
|
-
if (!checkoutSessionDetails) {
|
|
30
|
-
throw new Error('Session not found');
|
|
31
|
-
}
|
|
32
|
-
return this.#mapperss.CheckoutSessionDtoMapper.serializeEntityToDto(checkoutSessionDetails.value);
|
|
33
|
-
}
|
|
34
|
-
async expireCheckoutSession({ id }) {
|
|
35
|
-
const checkoutSessionDetails = await this.cache.readRecord(this.createCacheKey(id));
|
|
36
|
-
if (!checkoutSessionDetails) {
|
|
37
|
-
throw new Error('Session not found');
|
|
38
|
-
}
|
|
39
|
-
await this.cache.deleteRecord(this.createCacheKey(id));
|
|
40
|
-
}
|
|
41
|
-
async handleCheckoutSuccess({ id }) {
|
|
42
|
-
this.openTelemetryCollector.info('Checkout success', { id });
|
|
43
|
-
await this.cache.deleteRecord(this.createCacheKey(id));
|
|
44
|
-
}
|
|
45
|
-
async handleCheckoutFailure({ id }) {
|
|
46
|
-
this.openTelemetryCollector.info('Checkout failure', { id });
|
|
47
|
-
await this.cache.deleteRecord(this.createCacheKey(id));
|
|
39
|
+
return this.#mapperss.CheckoutSessionDtoMapper.serializeEntityToDto(
|
|
40
|
+
checkoutSessionDetails.value
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
async expireCheckoutSession({ id }) {
|
|
44
|
+
const checkoutSessionDetails = await this.cache.readRecord(
|
|
45
|
+
this.createCacheKey(id)
|
|
46
|
+
);
|
|
47
|
+
if (!checkoutSessionDetails) {
|
|
48
|
+
throw new Error('Session not found');
|
|
48
49
|
}
|
|
50
|
+
await this.cache.deleteRecord(this.createCacheKey(id));
|
|
51
|
+
}
|
|
52
|
+
async handleCheckoutSuccess({ id }) {
|
|
53
|
+
this.openTelemetryCollector.info('Checkout success', { id });
|
|
54
|
+
await this.cache.deleteRecord(this.createCacheKey(id));
|
|
55
|
+
}
|
|
56
|
+
async handleCheckoutFailure({ id }) {
|
|
57
|
+
this.openTelemetryCollector.info('Checkout failure', { id });
|
|
58
|
+
await this.cache.deleteRecord(this.createCacheKey(id));
|
|
59
|
+
}
|
|
49
60
|
}
|
package/lib/services/index.d.ts
CHANGED
|
@@ -1,49 +1,99 @@
|
|
|
1
1
|
import { IdDto, IdsDto } from '@forklaunch/common';
|
|
2
2
|
import { TtlCache } from '@forklaunch/core/cache';
|
|
3
|
-
import {
|
|
4
|
-
|
|
3
|
+
import {
|
|
4
|
+
RequestDtoMapperConstructor,
|
|
5
|
+
ResponseDtoMapperConstructor
|
|
6
|
+
} from '@forklaunch/core/mappers';
|
|
7
|
+
import {
|
|
8
|
+
MetricsDefinition,
|
|
9
|
+
OpenTelemetryCollector
|
|
10
|
+
} from '@forklaunch/core/http';
|
|
5
11
|
import { PaymentLinkService } from '@forklaunch/interfaces-billing/interfaces';
|
|
6
|
-
import {
|
|
12
|
+
import {
|
|
13
|
+
CreatePaymentLinkDto,
|
|
14
|
+
PaymentLinkDto,
|
|
15
|
+
UpdatePaymentLinkDto
|
|
16
|
+
} from '@forklaunch/interfaces-billing/types';
|
|
7
17
|
import { AnySchemaValidator } from '@forklaunch/validator';
|
|
8
|
-
export declare class BasePaymentLinkService<
|
|
18
|
+
export declare class BasePaymentLinkService<
|
|
19
|
+
SchemaValidator extends AnySchemaValidator,
|
|
20
|
+
CurrencyEnum,
|
|
21
|
+
Metrics extends MetricsDefinition = MetricsDefinition,
|
|
22
|
+
Dto extends {
|
|
9
23
|
PaymentLinkDtoMapper: PaymentLinkDto<CurrencyEnum>;
|
|
10
24
|
CreatePaymentLinkDtoMapper: CreatePaymentLinkDto<CurrencyEnum>;
|
|
11
25
|
UpdatePaymentLinkDtoMapper: UpdatePaymentLinkDto<CurrencyEnum>;
|
|
12
|
-
} = {
|
|
26
|
+
} = {
|
|
13
27
|
PaymentLinkDtoMapper: PaymentLinkDto<CurrencyEnum>;
|
|
14
28
|
CreatePaymentLinkDtoMapper: CreatePaymentLinkDto<CurrencyEnum>;
|
|
15
29
|
UpdatePaymentLinkDtoMapper: UpdatePaymentLinkDto<CurrencyEnum>;
|
|
16
|
-
},
|
|
30
|
+
},
|
|
31
|
+
Entities extends {
|
|
17
32
|
PaymentLinkDtoMapper: PaymentLinkDto<CurrencyEnum>;
|
|
18
33
|
CreatePaymentLinkDtoMapper: PaymentLinkDto<CurrencyEnum>;
|
|
19
34
|
UpdatePaymentLinkDtoMapper: PaymentLinkDto<CurrencyEnum>;
|
|
20
|
-
} = {
|
|
35
|
+
} = {
|
|
21
36
|
PaymentLinkDtoMapper: PaymentLinkDto<CurrencyEnum>;
|
|
22
37
|
CreatePaymentLinkDtoMapper: PaymentLinkDto<CurrencyEnum>;
|
|
23
38
|
UpdatePaymentLinkDtoMapper: PaymentLinkDto<CurrencyEnum>;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
39
|
+
}
|
|
40
|
+
> implements PaymentLinkService<CurrencyEnum>
|
|
41
|
+
{
|
|
42
|
+
#private;
|
|
43
|
+
protected readonly cache: TtlCache;
|
|
44
|
+
protected readonly openTelemetryCollector: OpenTelemetryCollector<Metrics>;
|
|
45
|
+
protected readonly schemaValidator: SchemaValidator;
|
|
46
|
+
protected readonly mapperss: {
|
|
47
|
+
PaymentLinkDtoMapper: ResponseDtoMapperConstructor<
|
|
48
|
+
SchemaValidator,
|
|
49
|
+
Dto['PaymentLinkDtoMapper'],
|
|
50
|
+
Entities['PaymentLinkDtoMapper']
|
|
51
|
+
>;
|
|
52
|
+
CreatePaymentLinkDtoMapper: RequestDtoMapperConstructor<
|
|
53
|
+
SchemaValidator,
|
|
54
|
+
Dto['CreatePaymentLinkDtoMapper'],
|
|
55
|
+
Entities['CreatePaymentLinkDtoMapper']
|
|
56
|
+
>;
|
|
57
|
+
UpdatePaymentLinkDtoMapper: RequestDtoMapperConstructor<
|
|
58
|
+
SchemaValidator,
|
|
59
|
+
Dto['UpdatePaymentLinkDtoMapper'],
|
|
60
|
+
Entities['UpdatePaymentLinkDtoMapper']
|
|
61
|
+
>;
|
|
62
|
+
};
|
|
63
|
+
constructor(
|
|
64
|
+
cache: TtlCache,
|
|
65
|
+
openTelemetryCollector: OpenTelemetryCollector<Metrics>,
|
|
66
|
+
schemaValidator: SchemaValidator,
|
|
67
|
+
mapperss: {
|
|
68
|
+
PaymentLinkDtoMapper: ResponseDtoMapperConstructor<
|
|
69
|
+
SchemaValidator,
|
|
70
|
+
Dto['PaymentLinkDtoMapper'],
|
|
71
|
+
Entities['PaymentLinkDtoMapper']
|
|
72
|
+
>;
|
|
73
|
+
CreatePaymentLinkDtoMapper: RequestDtoMapperConstructor<
|
|
74
|
+
SchemaValidator,
|
|
75
|
+
Dto['CreatePaymentLinkDtoMapper'],
|
|
76
|
+
Entities['CreatePaymentLinkDtoMapper']
|
|
77
|
+
>;
|
|
78
|
+
UpdatePaymentLinkDtoMapper: RequestDtoMapperConstructor<
|
|
79
|
+
SchemaValidator,
|
|
80
|
+
Dto['UpdatePaymentLinkDtoMapper'],
|
|
81
|
+
Entities['UpdatePaymentLinkDtoMapper']
|
|
82
|
+
>;
|
|
83
|
+
}
|
|
84
|
+
);
|
|
85
|
+
protected cacheKeyPrefix: string;
|
|
86
|
+
protected createCacheKey: (id: string) => string;
|
|
87
|
+
createPaymentLink(
|
|
88
|
+
paymentLinkDto: Dto['CreatePaymentLinkDtoMapper']
|
|
89
|
+
): Promise<Dto['PaymentLinkDtoMapper']>;
|
|
90
|
+
updatePaymentLink(
|
|
91
|
+
paymentLinkDto: Dto['UpdatePaymentLinkDtoMapper']
|
|
92
|
+
): Promise<Dto['PaymentLinkDtoMapper']>;
|
|
93
|
+
getPaymentLink({ id }: IdDto): Promise<Dto['PaymentLinkDtoMapper']>;
|
|
94
|
+
expirePaymentLink({ id }: IdDto): Promise<void>;
|
|
95
|
+
handlePaymentSuccess({ id }: IdDto): Promise<void>;
|
|
96
|
+
handlePaymentFailure({ id }: IdDto): Promise<void>;
|
|
97
|
+
listPaymentLinks(idsDto?: IdsDto): Promise<Dto['PaymentLinkDtoMapper'][]>;
|
|
48
98
|
}
|
|
49
|
-
//# sourceMappingURL=paymentLink.service.d.ts.map
|
|
99
|
+
//# sourceMappingURL=paymentLink.service.d.ts.map
|
|
@@ -1,72 +1,90 @@
|
|
|
1
1
|
import { createCacheKey } from '@forklaunch/core/cache';
|
|
2
2
|
import { transformIntoInternalDtoMapper } from '@forklaunch/core/mappers';
|
|
3
3
|
export class BasePaymentLinkService {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
4
|
+
cache;
|
|
5
|
+
openTelemetryCollector;
|
|
6
|
+
schemaValidator;
|
|
7
|
+
mapperss;
|
|
8
|
+
#mapperss;
|
|
9
|
+
constructor(cache, openTelemetryCollector, schemaValidator, mapperss) {
|
|
10
|
+
this.cache = cache;
|
|
11
|
+
this.openTelemetryCollector = openTelemetryCollector;
|
|
12
|
+
this.schemaValidator = schemaValidator;
|
|
13
|
+
this.mapperss = mapperss;
|
|
14
|
+
this.#mapperss = transformIntoInternalDtoMapper(mapperss, schemaValidator);
|
|
15
|
+
}
|
|
16
|
+
cacheKeyPrefix = 'payment_link';
|
|
17
|
+
createCacheKey = createCacheKey(this.cacheKeyPrefix);
|
|
18
|
+
async createPaymentLink(paymentLinkDto) {
|
|
19
|
+
// TODO: Perform permission checks here
|
|
20
|
+
const paymentLink =
|
|
21
|
+
this.#mapperss.CreatePaymentLinkDtoMapper.deserializeDtoToEntity(
|
|
22
|
+
paymentLinkDto
|
|
23
|
+
);
|
|
24
|
+
await this.cache.putRecord({
|
|
25
|
+
key: this.createCacheKey(paymentLink.id),
|
|
26
|
+
value: paymentLink,
|
|
27
|
+
ttlMilliseconds: this.cache.getTtlMilliseconds()
|
|
28
|
+
});
|
|
29
|
+
return this.#mapperss.PaymentLinkDtoMapper.serializeEntityToDto(
|
|
30
|
+
paymentLink
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
async updatePaymentLink(paymentLinkDto) {
|
|
34
|
+
const cacheKey = this.createCacheKey(paymentLinkDto.id);
|
|
35
|
+
const existingLink = await this.cache.readRecord(cacheKey);
|
|
36
|
+
if (!existingLink) {
|
|
37
|
+
throw new Error('Payment link not found');
|
|
15
38
|
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
const updatedLink = { ...existingLink, ...paymentLink };
|
|
36
|
-
await this.cache.putRecord({
|
|
37
|
-
key: cacheKey,
|
|
38
|
-
value: updatedLink,
|
|
39
|
-
ttlMilliseconds: this.cache.getTtlMilliseconds()
|
|
40
|
-
});
|
|
41
|
-
return this.#mapperss.PaymentLinkDtoMapper.serializeEntityToDto(updatedLink);
|
|
42
|
-
}
|
|
43
|
-
async getPaymentLink({ id }) {
|
|
44
|
-
const cacheKey = this.createCacheKey(id);
|
|
45
|
-
const paymentLink = await this.cache.readRecord(cacheKey);
|
|
46
|
-
if (!paymentLink) {
|
|
47
|
-
throw new Error('Payment link not found');
|
|
48
|
-
}
|
|
49
|
-
return this.#mapperss.PaymentLinkDtoMapper.serializeEntityToDto(paymentLink.value);
|
|
50
|
-
}
|
|
51
|
-
async expirePaymentLink({ id }) {
|
|
52
|
-
this.openTelemetryCollector.info('Payment link expired', { id });
|
|
53
|
-
await this.cache.deleteRecord(this.createCacheKey(id));
|
|
54
|
-
}
|
|
55
|
-
async handlePaymentSuccess({ id }) {
|
|
56
|
-
this.openTelemetryCollector.info('Payment link success', { id });
|
|
57
|
-
await this.cache.deleteRecord(this.createCacheKey(id));
|
|
58
|
-
}
|
|
59
|
-
async handlePaymentFailure({ id }) {
|
|
60
|
-
this.openTelemetryCollector.info('Payment link failure', { id });
|
|
61
|
-
await this.cache.deleteRecord(this.createCacheKey(id));
|
|
62
|
-
}
|
|
63
|
-
async listPaymentLinks(idsDto) {
|
|
64
|
-
const keys = idsDto?.ids.map((id) => this.createCacheKey(id)) ??
|
|
65
|
-
(await this.cache.listKeys(this.cacheKeyPrefix));
|
|
66
|
-
return await Promise.all(keys.map(async (key) => {
|
|
67
|
-
const paymentLink = await this.cache.readRecord(key);
|
|
68
|
-
const paymentLinkDto = this.#mapperss.PaymentLinkDtoMapper.serializeEntityToDto(paymentLink.value);
|
|
69
|
-
return paymentLinkDto;
|
|
70
|
-
}));
|
|
39
|
+
const paymentLink =
|
|
40
|
+
this.#mapperss.UpdatePaymentLinkDtoMapper.deserializeDtoToEntity(
|
|
41
|
+
paymentLinkDto
|
|
42
|
+
);
|
|
43
|
+
const updatedLink = { ...existingLink, ...paymentLink };
|
|
44
|
+
await this.cache.putRecord({
|
|
45
|
+
key: cacheKey,
|
|
46
|
+
value: updatedLink,
|
|
47
|
+
ttlMilliseconds: this.cache.getTtlMilliseconds()
|
|
48
|
+
});
|
|
49
|
+
return this.#mapperss.PaymentLinkDtoMapper.serializeEntityToDto(
|
|
50
|
+
updatedLink
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
async getPaymentLink({ id }) {
|
|
54
|
+
const cacheKey = this.createCacheKey(id);
|
|
55
|
+
const paymentLink = await this.cache.readRecord(cacheKey);
|
|
56
|
+
if (!paymentLink) {
|
|
57
|
+
throw new Error('Payment link not found');
|
|
71
58
|
}
|
|
59
|
+
return this.#mapperss.PaymentLinkDtoMapper.serializeEntityToDto(
|
|
60
|
+
paymentLink.value
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
async expirePaymentLink({ id }) {
|
|
64
|
+
this.openTelemetryCollector.info('Payment link expired', { id });
|
|
65
|
+
await this.cache.deleteRecord(this.createCacheKey(id));
|
|
66
|
+
}
|
|
67
|
+
async handlePaymentSuccess({ id }) {
|
|
68
|
+
this.openTelemetryCollector.info('Payment link success', { id });
|
|
69
|
+
await this.cache.deleteRecord(this.createCacheKey(id));
|
|
70
|
+
}
|
|
71
|
+
async handlePaymentFailure({ id }) {
|
|
72
|
+
this.openTelemetryCollector.info('Payment link failure', { id });
|
|
73
|
+
await this.cache.deleteRecord(this.createCacheKey(id));
|
|
74
|
+
}
|
|
75
|
+
async listPaymentLinks(idsDto) {
|
|
76
|
+
const keys =
|
|
77
|
+
idsDto?.ids.map((id) => this.createCacheKey(id)) ??
|
|
78
|
+
(await this.cache.listKeys(this.cacheKeyPrefix));
|
|
79
|
+
return await Promise.all(
|
|
80
|
+
keys.map(async (key) => {
|
|
81
|
+
const paymentLink = await this.cache.readRecord(key);
|
|
82
|
+
const paymentLinkDto =
|
|
83
|
+
this.#mapperss.PaymentLinkDtoMapper.serializeEntityToDto(
|
|
84
|
+
paymentLink.value
|
|
85
|
+
);
|
|
86
|
+
return paymentLinkDto;
|
|
87
|
+
})
|
|
88
|
+
);
|
|
89
|
+
}
|
|
72
90
|
}
|
|
@@ -1,43 +1,90 @@
|
|
|
1
1
|
import { IdDto, IdsDto } from '@forklaunch/common';
|
|
2
|
-
import {
|
|
3
|
-
|
|
2
|
+
import {
|
|
3
|
+
RequestDtoMapperConstructor,
|
|
4
|
+
ResponseDtoMapperConstructor
|
|
5
|
+
} from '@forklaunch/core/mappers';
|
|
6
|
+
import {
|
|
7
|
+
MetricsDefinition,
|
|
8
|
+
OpenTelemetryCollector
|
|
9
|
+
} from '@forklaunch/core/http';
|
|
4
10
|
import { PlanService } from '@forklaunch/interfaces-billing/interfaces';
|
|
5
|
-
import {
|
|
11
|
+
import {
|
|
12
|
+
CreatePlanDto,
|
|
13
|
+
PlanDto,
|
|
14
|
+
UpdatePlanDto
|
|
15
|
+
} from '@forklaunch/interfaces-billing/types';
|
|
6
16
|
import { AnySchemaValidator } from '@forklaunch/validator';
|
|
7
17
|
import { EntityManager } from '@mikro-orm/core';
|
|
8
|
-
export declare class BasePlanService<
|
|
18
|
+
export declare class BasePlanService<
|
|
19
|
+
SchemaValidator extends AnySchemaValidator,
|
|
20
|
+
PlanCadenceEnum,
|
|
21
|
+
BillingProviderEnum,
|
|
22
|
+
Metrics extends MetricsDefinition = MetricsDefinition,
|
|
23
|
+
Dto extends {
|
|
9
24
|
PlanDtoMapper: PlanDto<PlanCadenceEnum, BillingProviderEnum>;
|
|
10
25
|
CreatePlanDtoMapper: CreatePlanDto<PlanCadenceEnum, BillingProviderEnum>;
|
|
11
26
|
UpdatePlanDtoMapper: UpdatePlanDto<PlanCadenceEnum, BillingProviderEnum>;
|
|
12
|
-
} = {
|
|
27
|
+
} = {
|
|
13
28
|
PlanDtoMapper: PlanDto<PlanCadenceEnum, BillingProviderEnum>;
|
|
14
29
|
CreatePlanDtoMapper: CreatePlanDto<PlanCadenceEnum, BillingProviderEnum>;
|
|
15
30
|
UpdatePlanDtoMapper: UpdatePlanDto<PlanCadenceEnum, BillingProviderEnum>;
|
|
16
|
-
},
|
|
31
|
+
},
|
|
32
|
+
Entities extends {
|
|
17
33
|
PlanDtoMapper: PlanDto<PlanCadenceEnum, BillingProviderEnum>;
|
|
18
34
|
CreatePlanDtoMapper: PlanDto<PlanCadenceEnum, BillingProviderEnum>;
|
|
19
35
|
UpdatePlanDtoMapper: PlanDto<PlanCadenceEnum, BillingProviderEnum>;
|
|
20
|
-
} = {
|
|
36
|
+
} = {
|
|
21
37
|
PlanDtoMapper: PlanDto<PlanCadenceEnum, BillingProviderEnum>;
|
|
22
38
|
CreatePlanDtoMapper: PlanDto<PlanCadenceEnum, BillingProviderEnum>;
|
|
23
39
|
UpdatePlanDtoMapper: PlanDto<PlanCadenceEnum, BillingProviderEnum>;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
40
|
+
}
|
|
41
|
+
> implements PlanService<PlanCadenceEnum, BillingProviderEnum>
|
|
42
|
+
{
|
|
43
|
+
#private;
|
|
44
|
+
private em;
|
|
45
|
+
private readonly openTelemetryCollector;
|
|
46
|
+
private readonly schemaValidator;
|
|
47
|
+
private readonly mapperss;
|
|
48
|
+
constructor(
|
|
49
|
+
em: EntityManager,
|
|
50
|
+
openTelemetryCollector: OpenTelemetryCollector<Metrics>,
|
|
51
|
+
schemaValidator: SchemaValidator,
|
|
52
|
+
mapperss: {
|
|
53
|
+
PlanDtoMapper: ResponseDtoMapperConstructor<
|
|
54
|
+
SchemaValidator,
|
|
55
|
+
Dto['PlanDtoMapper'],
|
|
56
|
+
Entities['PlanDtoMapper']
|
|
57
|
+
>;
|
|
58
|
+
CreatePlanDtoMapper: RequestDtoMapperConstructor<
|
|
59
|
+
SchemaValidator,
|
|
60
|
+
Dto['CreatePlanDtoMapper'],
|
|
61
|
+
Entities['CreatePlanDtoMapper']
|
|
62
|
+
>;
|
|
63
|
+
UpdatePlanDtoMapper: RequestDtoMapperConstructor<
|
|
64
|
+
SchemaValidator,
|
|
65
|
+
Dto['UpdatePlanDtoMapper'],
|
|
66
|
+
Entities['UpdatePlanDtoMapper']
|
|
67
|
+
>;
|
|
68
|
+
}
|
|
69
|
+
);
|
|
70
|
+
listPlans(
|
|
71
|
+
idsDto?: IdsDto,
|
|
72
|
+
em?: EntityManager
|
|
73
|
+
): Promise<Dto['PlanDtoMapper'][]>;
|
|
74
|
+
createPlan(
|
|
75
|
+
planDto: Dto['CreatePlanDtoMapper'],
|
|
76
|
+
em?: EntityManager
|
|
77
|
+
): Promise<Dto['PlanDtoMapper']>;
|
|
78
|
+
getPlan(idDto: IdDto, em?: EntityManager): Promise<Dto['PlanDtoMapper']>;
|
|
79
|
+
updatePlan(
|
|
80
|
+
planDto: Dto['UpdatePlanDtoMapper'],
|
|
81
|
+
em?: EntityManager
|
|
82
|
+
): Promise<Dto['PlanDtoMapper']>;
|
|
83
|
+
deletePlan(
|
|
84
|
+
idDto: {
|
|
85
|
+
id: string;
|
|
86
|
+
},
|
|
87
|
+
em?: EntityManager
|
|
88
|
+
): Promise<void>;
|
|
42
89
|
}
|
|
43
|
-
//# sourceMappingURL=plan.service.d.ts.map
|
|
90
|
+
//# sourceMappingURL=plan.service.d.ts.map
|