@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
|
@@ -6,102 +6,71 @@ import {
|
|
|
6
6
|
OpenTelemetryCollector,
|
|
7
7
|
TelemetryOptions
|
|
8
8
|
} from '@forklaunch/core/http';
|
|
9
|
-
import {
|
|
10
|
-
InternalDtoMapper,
|
|
11
|
-
RequestDtoMapperConstructor,
|
|
12
|
-
ResponseDtoMapperConstructor,
|
|
13
|
-
transformIntoInternalDtoMapper
|
|
14
|
-
} from '@forklaunch/core/mappers';
|
|
15
9
|
import { CheckoutSessionService } from '@forklaunch/interfaces-billing/interfaces';
|
|
16
10
|
import {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
11
|
+
InternalMapper,
|
|
12
|
+
RequestMapperConstructor,
|
|
13
|
+
ResponseMapperConstructor,
|
|
14
|
+
transformIntoInternalMapper
|
|
15
|
+
} from '@forklaunch/internal';
|
|
21
16
|
import { AnySchemaValidator } from '@forklaunch/validator';
|
|
22
17
|
import { EntityManager } from '@mikro-orm/core';
|
|
18
|
+
import { BaseCheckoutSessionDtos } from '../domain/types/baseBillingDto.types';
|
|
19
|
+
import { BaseCheckoutSessionEntities } from '../domain/types/baseBillingEntity.types';
|
|
23
20
|
|
|
24
21
|
export class BaseCheckoutSessionService<
|
|
25
22
|
SchemaValidator extends AnySchemaValidator,
|
|
26
23
|
PaymentMethodEnum,
|
|
24
|
+
CurrencyEnum,
|
|
27
25
|
StatusEnum,
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
} = {
|
|
40
|
-
CheckoutSessionDtoMapper: CheckoutSessionDto<PaymentMethodEnum, StatusEnum>;
|
|
41
|
-
CreateCheckoutSessionDtoMapper: CreateCheckoutSessionDto<
|
|
42
|
-
PaymentMethodEnum,
|
|
43
|
-
StatusEnum
|
|
44
|
-
>;
|
|
45
|
-
UpdateCheckoutSessionDtoMapper: UpdateCheckoutSessionDto<
|
|
46
|
-
PaymentMethodEnum,
|
|
47
|
-
StatusEnum
|
|
48
|
-
>;
|
|
49
|
-
},
|
|
50
|
-
Entities extends {
|
|
51
|
-
CheckoutSessionDtoMapper: CheckoutSessionDto<PaymentMethodEnum, StatusEnum>;
|
|
52
|
-
CreateCheckoutSessionDtoMapper: CheckoutSessionDto<
|
|
53
|
-
PaymentMethodEnum,
|
|
54
|
-
StatusEnum
|
|
55
|
-
>;
|
|
56
|
-
UpdateCheckoutSessionDtoMapper: CheckoutSessionDto<
|
|
57
|
-
PaymentMethodEnum,
|
|
58
|
-
StatusEnum
|
|
59
|
-
>;
|
|
60
|
-
} = {
|
|
61
|
-
CheckoutSessionDtoMapper: CheckoutSessionDto<PaymentMethodEnum, StatusEnum>;
|
|
62
|
-
CreateCheckoutSessionDtoMapper: CheckoutSessionDto<
|
|
63
|
-
PaymentMethodEnum,
|
|
64
|
-
StatusEnum
|
|
65
|
-
>;
|
|
66
|
-
UpdateCheckoutSessionDtoMapper: CheckoutSessionDto<
|
|
67
|
-
PaymentMethodEnum,
|
|
68
|
-
StatusEnum
|
|
69
|
-
>;
|
|
70
|
-
}
|
|
71
|
-
> implements CheckoutSessionService<PaymentMethodEnum, StatusEnum>
|
|
26
|
+
Entities extends BaseCheckoutSessionEntities<
|
|
27
|
+
PaymentMethodEnum,
|
|
28
|
+
CurrencyEnum,
|
|
29
|
+
StatusEnum
|
|
30
|
+
>,
|
|
31
|
+
Dto extends BaseCheckoutSessionDtos<
|
|
32
|
+
PaymentMethodEnum,
|
|
33
|
+
CurrencyEnum,
|
|
34
|
+
StatusEnum
|
|
35
|
+
> = BaseCheckoutSessionDtos<PaymentMethodEnum, CurrencyEnum, StatusEnum>
|
|
36
|
+
> implements CheckoutSessionService<PaymentMethodEnum, CurrencyEnum, StatusEnum>
|
|
72
37
|
{
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
Entities,
|
|
76
|
-
Dto
|
|
77
|
-
>;
|
|
78
|
-
private evaluatedTelemetryOptions: {
|
|
38
|
+
protected _mappers: InternalMapper<InstanceTypeRecord<typeof this.mappers>>;
|
|
39
|
+
protected evaluatedTelemetryOptions: {
|
|
79
40
|
logging?: boolean;
|
|
80
41
|
metrics?: boolean;
|
|
81
42
|
tracing?: boolean;
|
|
82
43
|
};
|
|
83
|
-
|
|
44
|
+
protected enableDatabaseBackup: boolean;
|
|
84
45
|
|
|
85
46
|
constructor(
|
|
86
47
|
protected readonly em: EntityManager,
|
|
87
48
|
protected readonly cache: TtlCache,
|
|
88
|
-
protected readonly openTelemetryCollector: OpenTelemetryCollector<
|
|
49
|
+
protected readonly openTelemetryCollector: OpenTelemetryCollector<MetricsDefinition>,
|
|
89
50
|
protected readonly schemaValidator: SchemaValidator,
|
|
90
51
|
protected readonly mappers: {
|
|
91
|
-
|
|
52
|
+
CheckoutSessionMapper: ResponseMapperConstructor<
|
|
92
53
|
SchemaValidator,
|
|
93
|
-
Dto['
|
|
94
|
-
Entities['
|
|
54
|
+
Dto['CheckoutSessionMapper'],
|
|
55
|
+
Entities['CheckoutSessionMapper']
|
|
95
56
|
>;
|
|
96
|
-
|
|
57
|
+
CreateCheckoutSessionMapper: RequestMapperConstructor<
|
|
97
58
|
SchemaValidator,
|
|
98
|
-
Dto['
|
|
99
|
-
Entities['
|
|
59
|
+
Dto['CreateCheckoutSessionMapper'],
|
|
60
|
+
Entities['CreateCheckoutSessionMapper'],
|
|
61
|
+
(
|
|
62
|
+
dto: Dto['CreateCheckoutSessionMapper'],
|
|
63
|
+
em: EntityManager
|
|
64
|
+
) => Promise<Entities['CreateCheckoutSessionMapper']>
|
|
100
65
|
>;
|
|
101
|
-
|
|
66
|
+
UpdateCheckoutSessionMapper: RequestMapperConstructor<
|
|
102
67
|
SchemaValidator,
|
|
103
|
-
Dto['
|
|
104
|
-
Entities['
|
|
68
|
+
Dto['UpdateCheckoutSessionMapper'],
|
|
69
|
+
Entities['UpdateCheckoutSessionMapper'],
|
|
70
|
+
(
|
|
71
|
+
dto: Dto['UpdateCheckoutSessionMapper'],
|
|
72
|
+
em: EntityManager
|
|
73
|
+
) => Promise<Entities['UpdateCheckoutSessionMapper']>
|
|
105
74
|
>;
|
|
106
75
|
},
|
|
107
76
|
readonly options?: {
|
|
@@ -109,7 +78,7 @@ export class BaseCheckoutSessionService<
|
|
|
109
78
|
telemetry?: TelemetryOptions;
|
|
110
79
|
}
|
|
111
80
|
) {
|
|
112
|
-
this
|
|
81
|
+
this._mappers = transformIntoInternalMapper(mappers, schemaValidator);
|
|
113
82
|
this.enableDatabaseBackup = options?.enableDatabaseBackup ?? false;
|
|
114
83
|
this.evaluatedTelemetryOptions = options?.telemetry
|
|
115
84
|
? evaluateTelemetryOptions(options.telemetry).enabled
|
|
@@ -123,8 +92,8 @@ export class BaseCheckoutSessionService<
|
|
|
123
92
|
protected createCacheKey = createCacheKey('checkout_session');
|
|
124
93
|
|
|
125
94
|
async createCheckoutSession(
|
|
126
|
-
checkoutSessionDto: Dto['
|
|
127
|
-
): Promise<Dto['
|
|
95
|
+
checkoutSessionDto: Dto['CreateCheckoutSessionMapper']
|
|
96
|
+
): Promise<Dto['CheckoutSessionMapper']> {
|
|
128
97
|
if (this.evaluatedTelemetryOptions.logging) {
|
|
129
98
|
this.openTelemetryCollector.info(
|
|
130
99
|
'Creating checkout session',
|
|
@@ -133,13 +102,13 @@ export class BaseCheckoutSessionService<
|
|
|
133
102
|
}
|
|
134
103
|
|
|
135
104
|
const checkoutSession =
|
|
136
|
-
await this
|
|
105
|
+
await this._mappers.CreateCheckoutSessionMapper.deserializeDtoToEntity(
|
|
137
106
|
checkoutSessionDto,
|
|
138
107
|
this.em
|
|
139
108
|
);
|
|
140
109
|
|
|
141
110
|
const createdCheckoutSessionDto =
|
|
142
|
-
await this
|
|
111
|
+
await this._mappers.CheckoutSessionMapper.serializeEntityToDto(
|
|
143
112
|
checkoutSession
|
|
144
113
|
);
|
|
145
114
|
|
|
@@ -158,15 +127,15 @@ export class BaseCheckoutSessionService<
|
|
|
158
127
|
|
|
159
128
|
async getCheckoutSession({
|
|
160
129
|
id
|
|
161
|
-
}: IdDto): Promise<Dto['
|
|
130
|
+
}: IdDto): Promise<Dto['CheckoutSessionMapper']> {
|
|
162
131
|
const checkoutSessionDetails = await this.cache.readRecord<
|
|
163
|
-
Entities['
|
|
132
|
+
Entities['CheckoutSessionMapper']
|
|
164
133
|
>(this.createCacheKey(id));
|
|
165
134
|
if (!checkoutSessionDetails) {
|
|
166
135
|
throw new Error('Session not found');
|
|
167
136
|
}
|
|
168
137
|
|
|
169
|
-
return this
|
|
138
|
+
return this._mappers.CheckoutSessionMapper.serializeEntityToDto(
|
|
170
139
|
checkoutSessionDetails.value
|
|
171
140
|
);
|
|
172
141
|
}
|
|
@@ -6,78 +6,71 @@ import {
|
|
|
6
6
|
OpenTelemetryCollector,
|
|
7
7
|
TelemetryOptions
|
|
8
8
|
} from '@forklaunch/core/http';
|
|
9
|
-
import {
|
|
10
|
-
InternalDtoMapper,
|
|
11
|
-
RequestDtoMapperConstructor,
|
|
12
|
-
ResponseDtoMapperConstructor,
|
|
13
|
-
transformIntoInternalDtoMapper
|
|
14
|
-
} from '@forklaunch/core/mappers';
|
|
15
9
|
import { PaymentLinkService } from '@forklaunch/interfaces-billing/interfaces';
|
|
16
10
|
import {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
11
|
+
InternalMapper,
|
|
12
|
+
RequestMapperConstructor,
|
|
13
|
+
ResponseMapperConstructor,
|
|
14
|
+
transformIntoInternalMapper
|
|
15
|
+
} from '@forklaunch/internal';
|
|
21
16
|
import { AnySchemaValidator } from '@forklaunch/validator';
|
|
22
17
|
import { EntityManager } from '@mikro-orm/core';
|
|
18
|
+
import { BasePaymentLinkDtos } from '../domain/types/baseBillingDto.types';
|
|
19
|
+
import { BasePaymentLinkEntities } from '../domain/types/baseBillingEntity.types';
|
|
23
20
|
|
|
24
21
|
export class BasePaymentLinkService<
|
|
25
22
|
SchemaValidator extends AnySchemaValidator,
|
|
23
|
+
PaymentMethodEnum,
|
|
26
24
|
CurrencyEnum,
|
|
27
25
|
StatusEnum,
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
PaymentLinkDtoMapper: PaymentLinkDto<CurrencyEnum, StatusEnum>;
|
|
40
|
-
CreatePaymentLinkDtoMapper: PaymentLinkDto<CurrencyEnum, StatusEnum>;
|
|
41
|
-
UpdatePaymentLinkDtoMapper: PaymentLinkDto<CurrencyEnum, StatusEnum>;
|
|
42
|
-
} = {
|
|
43
|
-
PaymentLinkDtoMapper: PaymentLinkDto<CurrencyEnum, StatusEnum>;
|
|
44
|
-
CreatePaymentLinkDtoMapper: PaymentLinkDto<CurrencyEnum, StatusEnum>;
|
|
45
|
-
UpdatePaymentLinkDtoMapper: PaymentLinkDto<CurrencyEnum, StatusEnum>;
|
|
46
|
-
}
|
|
47
|
-
> implements PaymentLinkService<CurrencyEnum, StatusEnum>
|
|
26
|
+
Entities extends BasePaymentLinkEntities<
|
|
27
|
+
PaymentMethodEnum,
|
|
28
|
+
CurrencyEnum,
|
|
29
|
+
StatusEnum
|
|
30
|
+
>,
|
|
31
|
+
Dto extends BasePaymentLinkDtos<
|
|
32
|
+
PaymentMethodEnum,
|
|
33
|
+
CurrencyEnum,
|
|
34
|
+
StatusEnum
|
|
35
|
+
> = BasePaymentLinkDtos<PaymentMethodEnum, CurrencyEnum, StatusEnum>
|
|
36
|
+
> implements PaymentLinkService<PaymentMethodEnum, CurrencyEnum, StatusEnum>
|
|
48
37
|
{
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
Entities,
|
|
52
|
-
Dto
|
|
53
|
-
>;
|
|
54
|
-
private evaluatedTelemetryOptions: {
|
|
38
|
+
protected _mappers: InternalMapper<InstanceTypeRecord<typeof this.mappers>>;
|
|
39
|
+
protected evaluatedTelemetryOptions: {
|
|
55
40
|
logging?: boolean;
|
|
56
41
|
metrics?: boolean;
|
|
57
42
|
tracing?: boolean;
|
|
58
43
|
};
|
|
59
|
-
|
|
44
|
+
protected enableDatabaseBackup: boolean;
|
|
60
45
|
|
|
61
46
|
constructor(
|
|
62
47
|
protected readonly em: EntityManager,
|
|
63
48
|
protected readonly cache: TtlCache,
|
|
64
|
-
protected readonly openTelemetryCollector: OpenTelemetryCollector<
|
|
49
|
+
protected readonly openTelemetryCollector: OpenTelemetryCollector<MetricsDefinition>,
|
|
65
50
|
protected readonly schemaValidator: SchemaValidator,
|
|
66
51
|
protected readonly mappers: {
|
|
67
|
-
|
|
52
|
+
PaymentLinkMapper: ResponseMapperConstructor<
|
|
68
53
|
SchemaValidator,
|
|
69
|
-
Dto['
|
|
70
|
-
Entities['
|
|
54
|
+
Dto['PaymentLinkMapper'],
|
|
55
|
+
Entities['PaymentLinkMapper']
|
|
71
56
|
>;
|
|
72
|
-
|
|
57
|
+
CreatePaymentLinkMapper: RequestMapperConstructor<
|
|
73
58
|
SchemaValidator,
|
|
74
|
-
Dto['
|
|
75
|
-
Entities['
|
|
59
|
+
Dto['CreatePaymentLinkMapper'],
|
|
60
|
+
Entities['CreatePaymentLinkMapper'],
|
|
61
|
+
(
|
|
62
|
+
dto: Dto['CreatePaymentLinkMapper'],
|
|
63
|
+
em: EntityManager
|
|
64
|
+
) => Promise<Entities['CreatePaymentLinkMapper']>
|
|
76
65
|
>;
|
|
77
|
-
|
|
66
|
+
UpdatePaymentLinkMapper: RequestMapperConstructor<
|
|
78
67
|
SchemaValidator,
|
|
79
|
-
Dto['
|
|
80
|
-
Entities['
|
|
68
|
+
Dto['UpdatePaymentLinkMapper'],
|
|
69
|
+
Entities['UpdatePaymentLinkMapper'],
|
|
70
|
+
(
|
|
71
|
+
dto: Dto['UpdatePaymentLinkMapper'],
|
|
72
|
+
em: EntityManager
|
|
73
|
+
) => Promise<Entities['UpdatePaymentLinkMapper']>
|
|
81
74
|
>;
|
|
82
75
|
},
|
|
83
76
|
readonly options?: {
|
|
@@ -85,7 +78,7 @@ export class BasePaymentLinkService<
|
|
|
85
78
|
telemetry?: TelemetryOptions;
|
|
86
79
|
}
|
|
87
80
|
) {
|
|
88
|
-
this
|
|
81
|
+
this._mappers = transformIntoInternalMapper(mappers, schemaValidator);
|
|
89
82
|
this.enableDatabaseBackup = options?.enableDatabaseBackup ?? false;
|
|
90
83
|
this.evaluatedTelemetryOptions = options?.telemetry
|
|
91
84
|
? evaluateTelemetryOptions(options.telemetry).enabled
|
|
@@ -100,14 +93,14 @@ export class BasePaymentLinkService<
|
|
|
100
93
|
protected createCacheKey = createCacheKey(this.cacheKeyPrefix);
|
|
101
94
|
|
|
102
95
|
async createPaymentLink(
|
|
103
|
-
paymentLinkDto: Dto['
|
|
104
|
-
): Promise<Dto['
|
|
96
|
+
paymentLinkDto: Dto['CreatePaymentLinkMapper']
|
|
97
|
+
): Promise<Dto['PaymentLinkMapper']> {
|
|
105
98
|
if (this.evaluatedTelemetryOptions.logging) {
|
|
106
99
|
this.openTelemetryCollector.info('Creating payment link', paymentLinkDto);
|
|
107
100
|
}
|
|
108
101
|
|
|
109
102
|
const paymentLink =
|
|
110
|
-
await this
|
|
103
|
+
await this._mappers.CreatePaymentLinkMapper.deserializeDtoToEntity(
|
|
111
104
|
paymentLinkDto,
|
|
112
105
|
this.em
|
|
113
106
|
);
|
|
@@ -117,9 +110,7 @@ export class BasePaymentLinkService<
|
|
|
117
110
|
}
|
|
118
111
|
|
|
119
112
|
const createdPaymentLinkDto =
|
|
120
|
-
await this
|
|
121
|
-
paymentLink
|
|
122
|
-
);
|
|
113
|
+
await this._mappers.PaymentLinkMapper.serializeEntityToDto(paymentLink);
|
|
123
114
|
|
|
124
115
|
await this.cache.putRecord({
|
|
125
116
|
key: this.createCacheKey(createdPaymentLinkDto.id),
|
|
@@ -131,21 +122,21 @@ export class BasePaymentLinkService<
|
|
|
131
122
|
}
|
|
132
123
|
|
|
133
124
|
async updatePaymentLink(
|
|
134
|
-
paymentLinkDto: Dto['
|
|
135
|
-
): Promise<Dto['
|
|
125
|
+
paymentLinkDto: Dto['UpdatePaymentLinkMapper']
|
|
126
|
+
): Promise<Dto['PaymentLinkMapper']> {
|
|
136
127
|
if (this.evaluatedTelemetryOptions.logging) {
|
|
137
128
|
this.openTelemetryCollector.info('Updating payment link', paymentLinkDto);
|
|
138
129
|
}
|
|
139
130
|
|
|
140
131
|
const cacheKey = this.createCacheKey(paymentLinkDto.id);
|
|
141
132
|
const existingLink = (
|
|
142
|
-
await this.cache.readRecord<Entities['
|
|
133
|
+
await this.cache.readRecord<Entities['PaymentLinkMapper']>(cacheKey)
|
|
143
134
|
)?.value;
|
|
144
135
|
if (!existingLink) {
|
|
145
136
|
throw new Error('Payment link not found');
|
|
146
137
|
}
|
|
147
138
|
const paymentLink =
|
|
148
|
-
await this
|
|
139
|
+
await this._mappers.UpdatePaymentLinkMapper.deserializeDtoToEntity(
|
|
149
140
|
paymentLinkDto,
|
|
150
141
|
this.em
|
|
151
142
|
);
|
|
@@ -156,7 +147,7 @@ export class BasePaymentLinkService<
|
|
|
156
147
|
|
|
157
148
|
const updatedLinkDto = {
|
|
158
149
|
...existingLink,
|
|
159
|
-
...(await this
|
|
150
|
+
...(await this._mappers.PaymentLinkMapper.serializeEntityToDto(
|
|
160
151
|
paymentLink
|
|
161
152
|
))
|
|
162
153
|
};
|
|
@@ -170,18 +161,18 @@ export class BasePaymentLinkService<
|
|
|
170
161
|
return updatedLinkDto;
|
|
171
162
|
}
|
|
172
163
|
|
|
173
|
-
async getPaymentLink({ id }: IdDto): Promise<Dto['
|
|
164
|
+
async getPaymentLink({ id }: IdDto): Promise<Dto['PaymentLinkMapper']> {
|
|
174
165
|
if (this.evaluatedTelemetryOptions.logging) {
|
|
175
166
|
this.openTelemetryCollector.info('Getting payment link', { id });
|
|
176
167
|
}
|
|
177
168
|
const cacheKey = this.createCacheKey(id);
|
|
178
169
|
const paymentLink =
|
|
179
|
-
await this.cache.readRecord<Entities['
|
|
170
|
+
await this.cache.readRecord<Entities['PaymentLinkMapper']>(cacheKey);
|
|
180
171
|
if (!paymentLink) {
|
|
181
172
|
throw new Error('Payment link not found');
|
|
182
173
|
}
|
|
183
174
|
|
|
184
|
-
return this
|
|
175
|
+
return this._mappers.PaymentLinkMapper.serializeEntityToDto(
|
|
185
176
|
paymentLink.value
|
|
186
177
|
);
|
|
187
178
|
}
|
|
@@ -223,9 +214,7 @@ export class BasePaymentLinkService<
|
|
|
223
214
|
await this.cache.deleteRecord(this.createCacheKey(id));
|
|
224
215
|
}
|
|
225
216
|
|
|
226
|
-
async listPaymentLinks(
|
|
227
|
-
idsDto?: IdsDto
|
|
228
|
-
): Promise<Dto['PaymentLinkDtoMapper'][]> {
|
|
217
|
+
async listPaymentLinks(idsDto?: IdsDto): Promise<Dto['PaymentLinkMapper'][]> {
|
|
229
218
|
const keys =
|
|
230
219
|
idsDto?.ids.map((id) => this.createCacheKey(id)) ??
|
|
231
220
|
(await this.cache.listKeys(this.cacheKeyPrefix));
|
|
@@ -233,9 +222,9 @@ export class BasePaymentLinkService<
|
|
|
233
222
|
return Promise.all(
|
|
234
223
|
keys.map(async (key) => {
|
|
235
224
|
const paymentLink =
|
|
236
|
-
await this.cache.readRecord<Entities['
|
|
225
|
+
await this.cache.readRecord<Entities['PaymentLinkMapper']>(key);
|
|
237
226
|
const paymentLinkDto =
|
|
238
|
-
this
|
|
227
|
+
this._mappers.PaymentLinkMapper.serializeEntityToDto(
|
|
239
228
|
paymentLink.value
|
|
240
229
|
);
|
|
241
230
|
return paymentLinkDto;
|
|
@@ -5,83 +5,76 @@ import {
|
|
|
5
5
|
OpenTelemetryCollector,
|
|
6
6
|
TelemetryOptions
|
|
7
7
|
} from '@forklaunch/core/http';
|
|
8
|
-
import {
|
|
9
|
-
InternalDtoMapper,
|
|
10
|
-
RequestDtoMapperConstructor,
|
|
11
|
-
ResponseDtoMapperConstructor,
|
|
12
|
-
transformIntoInternalDtoMapper
|
|
13
|
-
} from '@forklaunch/core/mappers';
|
|
14
8
|
import { PlanService } from '@forklaunch/interfaces-billing/interfaces';
|
|
15
9
|
import {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
10
|
+
InternalMapper,
|
|
11
|
+
RequestMapperConstructor,
|
|
12
|
+
ResponseMapperConstructor,
|
|
13
|
+
transformIntoInternalMapper
|
|
14
|
+
} from '@forklaunch/internal';
|
|
20
15
|
import { AnySchemaValidator } from '@forklaunch/validator';
|
|
21
16
|
import { EntityManager } from '@mikro-orm/core';
|
|
17
|
+
import { BasePlanDtos } from '../domain/types/baseBillingDto.types';
|
|
18
|
+
import { BasePlanEntities } from '../domain/types/baseBillingEntity.types';
|
|
22
19
|
|
|
23
20
|
export class BasePlanService<
|
|
24
21
|
SchemaValidator extends AnySchemaValidator,
|
|
25
22
|
PlanCadenceEnum,
|
|
23
|
+
CurrencyEnum,
|
|
26
24
|
BillingProviderEnum,
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
PlanDtoMapper: PlanDto<PlanCadenceEnum, BillingProviderEnum>;
|
|
39
|
-
CreatePlanDtoMapper: PlanDto<PlanCadenceEnum, BillingProviderEnum>;
|
|
40
|
-
UpdatePlanDtoMapper: PlanDto<PlanCadenceEnum, BillingProviderEnum>;
|
|
41
|
-
} = {
|
|
42
|
-
PlanDtoMapper: PlanDto<PlanCadenceEnum, BillingProviderEnum>;
|
|
43
|
-
CreatePlanDtoMapper: PlanDto<PlanCadenceEnum, BillingProviderEnum>;
|
|
44
|
-
UpdatePlanDtoMapper: PlanDto<PlanCadenceEnum, BillingProviderEnum>;
|
|
45
|
-
}
|
|
46
|
-
> implements PlanService<PlanCadenceEnum, BillingProviderEnum>
|
|
25
|
+
Entities extends BasePlanEntities<
|
|
26
|
+
PlanCadenceEnum,
|
|
27
|
+
CurrencyEnum,
|
|
28
|
+
BillingProviderEnum
|
|
29
|
+
>,
|
|
30
|
+
Dto extends BasePlanDtos<
|
|
31
|
+
PlanCadenceEnum,
|
|
32
|
+
CurrencyEnum,
|
|
33
|
+
BillingProviderEnum
|
|
34
|
+
> = BasePlanDtos<PlanCadenceEnum, CurrencyEnum, BillingProviderEnum>
|
|
35
|
+
> implements PlanService<PlanCadenceEnum, CurrencyEnum, BillingProviderEnum>
|
|
47
36
|
{
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
Entities,
|
|
51
|
-
Dto
|
|
52
|
-
>;
|
|
53
|
-
private evaluatedTelemetryOptions: {
|
|
37
|
+
protected _mappers: InternalMapper<InstanceTypeRecord<typeof this.mappers>>;
|
|
38
|
+
protected evaluatedTelemetryOptions: {
|
|
54
39
|
logging?: boolean;
|
|
55
40
|
metrics?: boolean;
|
|
56
41
|
tracing?: boolean;
|
|
57
42
|
};
|
|
58
43
|
|
|
59
44
|
constructor(
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
45
|
+
protected em: EntityManager,
|
|
46
|
+
protected readonly openTelemetryCollector: OpenTelemetryCollector<MetricsDefinition>,
|
|
47
|
+
protected readonly schemaValidator: SchemaValidator,
|
|
48
|
+
protected readonly mappers: {
|
|
49
|
+
PlanMapper: ResponseMapperConstructor<
|
|
65
50
|
SchemaValidator,
|
|
66
|
-
Dto['
|
|
67
|
-
Entities['
|
|
51
|
+
Dto['PlanMapper'],
|
|
52
|
+
Entities['PlanMapper']
|
|
68
53
|
>;
|
|
69
|
-
|
|
54
|
+
CreatePlanMapper: RequestMapperConstructor<
|
|
70
55
|
SchemaValidator,
|
|
71
|
-
Dto['
|
|
72
|
-
Entities['
|
|
56
|
+
Dto['CreatePlanMapper'],
|
|
57
|
+
Entities['CreatePlanMapper'],
|
|
58
|
+
(
|
|
59
|
+
dto: Dto['CreatePlanMapper'],
|
|
60
|
+
em: EntityManager
|
|
61
|
+
) => Promise<Entities['CreatePlanMapper']>
|
|
73
62
|
>;
|
|
74
|
-
|
|
63
|
+
UpdatePlanMapper: RequestMapperConstructor<
|
|
75
64
|
SchemaValidator,
|
|
76
|
-
Dto['
|
|
77
|
-
Entities['
|
|
65
|
+
Dto['UpdatePlanMapper'],
|
|
66
|
+
Entities['UpdatePlanMapper'],
|
|
67
|
+
(
|
|
68
|
+
dto: Dto['UpdatePlanMapper'],
|
|
69
|
+
em: EntityManager
|
|
70
|
+
) => Promise<Entities['UpdatePlanMapper']>
|
|
78
71
|
>;
|
|
79
72
|
},
|
|
80
73
|
readonly options?: {
|
|
81
74
|
telemetry?: TelemetryOptions;
|
|
82
75
|
}
|
|
83
76
|
) {
|
|
84
|
-
this
|
|
77
|
+
this._mappers = transformIntoInternalMapper(mappers, schemaValidator);
|
|
85
78
|
this.evaluatedTelemetryOptions = options?.telemetry
|
|
86
79
|
? evaluateTelemetryOptions(options.telemetry).enabled
|
|
87
80
|
: {
|
|
@@ -94,7 +87,7 @@ export class BasePlanService<
|
|
|
94
87
|
async listPlans(
|
|
95
88
|
idsDto?: IdsDto,
|
|
96
89
|
em?: EntityManager
|
|
97
|
-
): Promise<Dto['
|
|
90
|
+
): Promise<Dto['PlanMapper'][]> {
|
|
98
91
|
if (this.evaluatedTelemetryOptions.logging) {
|
|
99
92
|
this.openTelemetryCollector.info('Listing plans', idsDto);
|
|
100
93
|
}
|
|
@@ -104,51 +97,48 @@ export class BasePlanService<
|
|
|
104
97
|
filters: idsDto?.ids ? { id: { $in: idsDto.ids } } : undefined
|
|
105
98
|
})
|
|
106
99
|
).map((plan) =>
|
|
107
|
-
this
|
|
108
|
-
plan as Entities['
|
|
100
|
+
this._mappers.PlanMapper.serializeEntityToDto(
|
|
101
|
+
plan as Entities['PlanMapper']
|
|
109
102
|
)
|
|
110
103
|
)
|
|
111
104
|
);
|
|
112
105
|
}
|
|
113
106
|
|
|
114
107
|
async createPlan(
|
|
115
|
-
planDto: Dto['
|
|
108
|
+
planDto: Dto['CreatePlanMapper'],
|
|
116
109
|
em?: EntityManager
|
|
117
|
-
): Promise<Dto['
|
|
110
|
+
): Promise<Dto['PlanMapper']> {
|
|
118
111
|
if (this.evaluatedTelemetryOptions.logging) {
|
|
119
112
|
this.openTelemetryCollector.info('Creating plan', planDto);
|
|
120
113
|
}
|
|
121
|
-
const plan = await this
|
|
114
|
+
const plan = await this._mappers.CreatePlanMapper.deserializeDtoToEntity(
|
|
122
115
|
planDto,
|
|
123
116
|
em ?? this.em
|
|
124
117
|
);
|
|
125
118
|
await (em ?? this.em).transactional(async (innerEm) => {
|
|
126
119
|
await innerEm.persist(plan);
|
|
127
120
|
});
|
|
128
|
-
return this
|
|
121
|
+
return this._mappers.PlanMapper.serializeEntityToDto(plan);
|
|
129
122
|
}
|
|
130
123
|
|
|
131
|
-
async getPlan(
|
|
132
|
-
idDto: IdDto,
|
|
133
|
-
em?: EntityManager
|
|
134
|
-
): Promise<Dto['PlanDtoMapper']> {
|
|
124
|
+
async getPlan(idDto: IdDto, em?: EntityManager): Promise<Dto['PlanMapper']> {
|
|
135
125
|
if (this.evaluatedTelemetryOptions.logging) {
|
|
136
126
|
this.openTelemetryCollector.info('Getting plan', idDto);
|
|
137
127
|
}
|
|
138
128
|
const plan = await (em ?? this.em).findOneOrFail('Plan', idDto);
|
|
139
|
-
return this
|
|
140
|
-
plan as Entities['
|
|
129
|
+
return this._mappers.PlanMapper.serializeEntityToDto(
|
|
130
|
+
plan as Entities['PlanMapper']
|
|
141
131
|
);
|
|
142
132
|
}
|
|
143
133
|
|
|
144
134
|
async updatePlan(
|
|
145
|
-
planDto: Dto['
|
|
135
|
+
planDto: Dto['UpdatePlanMapper'],
|
|
146
136
|
em?: EntityManager
|
|
147
|
-
): Promise<Dto['
|
|
137
|
+
): Promise<Dto['PlanMapper']> {
|
|
148
138
|
if (this.evaluatedTelemetryOptions.logging) {
|
|
149
139
|
this.openTelemetryCollector.info('Updating plan', planDto);
|
|
150
140
|
}
|
|
151
|
-
const plan = await this
|
|
141
|
+
const plan = await this._mappers.UpdatePlanMapper.deserializeDtoToEntity(
|
|
152
142
|
planDto,
|
|
153
143
|
em ?? this.em
|
|
154
144
|
);
|
|
@@ -157,7 +147,7 @@ export class BasePlanService<
|
|
|
157
147
|
await innerEm.persist(plan);
|
|
158
148
|
});
|
|
159
149
|
const updatedPlanDto =
|
|
160
|
-
await this
|
|
150
|
+
await this._mappers.PlanMapper.serializeEntityToDto(updatedPlan);
|
|
161
151
|
return updatedPlanDto;
|
|
162
152
|
}
|
|
163
153
|
|