@forklaunch/implementation-billing-base 0.5.8 → 0.6.0
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/types/index.d.mts +326 -0
- package/lib/domain/types/index.d.ts +326 -0
- package/lib/domain/types/index.js +22 -0
- package/lib/domain/types/index.mjs +0 -0
- package/lib/eject/domain/types/billingPortal.mapper.types.ts +28 -0
- package/lib/eject/domain/types/checkoutSession.mapper.types.ts +39 -0
- package/lib/eject/domain/types/index.ts +7 -0
- package/lib/eject/domain/types/paymentLink.mapper.types.ts +39 -0
- package/lib/eject/domain/types/plan.mapper.types.ts +39 -0
- package/lib/eject/domain/types/subscription.mapper.types.ts +33 -0
- package/lib/eject/services/billingPortal.service.ts +33 -87
- package/lib/eject/services/checkoutSession.service.ts +40 -78
- package/lib/eject/services/paymentLink.service.ts +69 -97
- package/lib/eject/services/plan.service.ts +49 -86
- package/lib/eject/services/subscription.service.ts +36 -83
- package/lib/services/index.d.mts +168 -495
- package/lib/services/index.d.ts +168 -495
- package/lib/services/index.js +94 -139
- package/lib/services/index.mjs +94 -124
- package/package.json +9 -9
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { IdDto
|
|
1
|
+
import { IdDto } from '@forklaunch/common';
|
|
2
2
|
import {
|
|
3
3
|
evaluateTelemetryOptions,
|
|
4
4
|
MetricsDefinition,
|
|
@@ -7,15 +7,14 @@ import {
|
|
|
7
7
|
} from '@forklaunch/core/http';
|
|
8
8
|
import { SubscriptionService } from '@forklaunch/interfaces-billing/interfaces';
|
|
9
9
|
import {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
transformIntoInternalMapper
|
|
14
|
-
} from '@forklaunch/internal';
|
|
10
|
+
CreateSubscriptionDto,
|
|
11
|
+
UpdateSubscriptionDto
|
|
12
|
+
} from '@forklaunch/interfaces-billing/types';
|
|
15
13
|
import { AnySchemaValidator } from '@forklaunch/validator';
|
|
16
14
|
import { EntityManager } from '@mikro-orm/core';
|
|
17
15
|
import { BaseSubscriptionDtos } from '../domain/types/baseBillingDto.types';
|
|
18
16
|
import { BaseSubscriptionEntities } from '../domain/types/baseBillingEntity.types';
|
|
17
|
+
import { SubscriptionMappers } from '../domain/types/subscription.mapper.types';
|
|
19
18
|
|
|
20
19
|
export class BaseSubscriptionService<
|
|
21
20
|
SchemaValidator extends AnySchemaValidator,
|
|
@@ -28,7 +27,6 @@ export class BaseSubscriptionService<
|
|
|
28
27
|
> = BaseSubscriptionDtos<PartyType, BillingProviderType>
|
|
29
28
|
> implements SubscriptionService<PartyType, BillingProviderType>
|
|
30
29
|
{
|
|
31
|
-
protected _mappers: InternalMapper<InstanceTypeRecord<typeof this.mappers>>;
|
|
32
30
|
protected evaluatedTelemetryOptions: {
|
|
33
31
|
logging?: boolean;
|
|
34
32
|
metrics?: boolean;
|
|
@@ -37,61 +35,18 @@ export class BaseSubscriptionService<
|
|
|
37
35
|
protected em: EntityManager;
|
|
38
36
|
protected readonly openTelemetryCollector: OpenTelemetryCollector<MetricsDefinition>;
|
|
39
37
|
protected readonly schemaValidator: SchemaValidator;
|
|
40
|
-
protected readonly mappers:
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
CreateSubscriptionMapper: RequestMapperConstructor<
|
|
47
|
-
SchemaValidator,
|
|
48
|
-
Dto['CreateSubscriptionMapper'],
|
|
49
|
-
Entities['CreateSubscriptionMapper'],
|
|
50
|
-
(
|
|
51
|
-
dto: Dto['CreateSubscriptionMapper'],
|
|
52
|
-
em: EntityManager
|
|
53
|
-
) => Promise<Entities['CreateSubscriptionMapper']>
|
|
54
|
-
>;
|
|
55
|
-
UpdateSubscriptionMapper: RequestMapperConstructor<
|
|
56
|
-
SchemaValidator,
|
|
57
|
-
Dto['UpdateSubscriptionMapper'],
|
|
58
|
-
Entities['UpdateSubscriptionMapper'],
|
|
59
|
-
(
|
|
60
|
-
dto: Dto['UpdateSubscriptionMapper'],
|
|
61
|
-
em: EntityManager
|
|
62
|
-
) => Promise<Entities['UpdateSubscriptionMapper']>
|
|
63
|
-
>;
|
|
64
|
-
};
|
|
38
|
+
protected readonly mappers: SubscriptionMappers<
|
|
39
|
+
PartyType,
|
|
40
|
+
BillingProviderType,
|
|
41
|
+
Entities,
|
|
42
|
+
Dto
|
|
43
|
+
>;
|
|
65
44
|
|
|
66
45
|
constructor(
|
|
67
46
|
em: EntityManager,
|
|
68
47
|
openTelemetryCollector: OpenTelemetryCollector<MetricsDefinition>,
|
|
69
48
|
schemaValidator: SchemaValidator,
|
|
70
|
-
mappers:
|
|
71
|
-
SubscriptionMapper: ResponseMapperConstructor<
|
|
72
|
-
SchemaValidator,
|
|
73
|
-
Dto['SubscriptionMapper'],
|
|
74
|
-
Entities['SubscriptionMapper']
|
|
75
|
-
>;
|
|
76
|
-
CreateSubscriptionMapper: RequestMapperConstructor<
|
|
77
|
-
SchemaValidator,
|
|
78
|
-
Dto['CreateSubscriptionMapper'],
|
|
79
|
-
Entities['CreateSubscriptionMapper'],
|
|
80
|
-
(
|
|
81
|
-
dto: Dto['CreateSubscriptionMapper'],
|
|
82
|
-
em: EntityManager
|
|
83
|
-
) => Promise<Entities['CreateSubscriptionMapper']>
|
|
84
|
-
>;
|
|
85
|
-
UpdateSubscriptionMapper: RequestMapperConstructor<
|
|
86
|
-
SchemaValidator,
|
|
87
|
-
Dto['UpdateSubscriptionMapper'],
|
|
88
|
-
Entities['UpdateSubscriptionMapper'],
|
|
89
|
-
(
|
|
90
|
-
dto: Dto['UpdateSubscriptionMapper'],
|
|
91
|
-
em: EntityManager
|
|
92
|
-
) => Promise<Entities['UpdateSubscriptionMapper']>
|
|
93
|
-
>;
|
|
94
|
-
},
|
|
49
|
+
mappers: SubscriptionMappers<PartyType, BillingProviderType, Entities, Dto>,
|
|
95
50
|
readonly options?: {
|
|
96
51
|
telemetry?: TelemetryOptions;
|
|
97
52
|
}
|
|
@@ -100,7 +55,6 @@ export class BaseSubscriptionService<
|
|
|
100
55
|
this.openTelemetryCollector = openTelemetryCollector;
|
|
101
56
|
this.schemaValidator = schemaValidator;
|
|
102
57
|
this.mappers = mappers;
|
|
103
|
-
this._mappers = transformIntoInternalMapper(mappers, this.schemaValidator);
|
|
104
58
|
this.evaluatedTelemetryOptions = options?.telemetry
|
|
105
59
|
? evaluateTelemetryOptions(options.telemetry).enabled
|
|
106
60
|
: {
|
|
@@ -111,8 +65,9 @@ export class BaseSubscriptionService<
|
|
|
111
65
|
}
|
|
112
66
|
|
|
113
67
|
async createSubscription(
|
|
114
|
-
subscriptionDto:
|
|
115
|
-
em?: EntityManager
|
|
68
|
+
subscriptionDto: CreateSubscriptionDto<PartyType, BillingProviderType>,
|
|
69
|
+
em?: EntityManager,
|
|
70
|
+
...args: unknown[]
|
|
116
71
|
): Promise<Dto['SubscriptionMapper']> {
|
|
117
72
|
if (this.evaluatedTelemetryOptions.logging) {
|
|
118
73
|
this.openTelemetryCollector.info(
|
|
@@ -120,16 +75,16 @@ export class BaseSubscriptionService<
|
|
|
120
75
|
subscriptionDto
|
|
121
76
|
);
|
|
122
77
|
}
|
|
123
|
-
const subscription =
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
78
|
+
const subscription = await this.mappers.CreateSubscriptionMapper.toEntity(
|
|
79
|
+
subscriptionDto,
|
|
80
|
+
em ?? this.em,
|
|
81
|
+
...args
|
|
82
|
+
);
|
|
128
83
|
await (em ?? this.em).transactional(async (innerEm) => {
|
|
129
84
|
await innerEm.persist(subscription);
|
|
130
85
|
});
|
|
131
86
|
const createdSubscriptionDto =
|
|
132
|
-
await this.
|
|
87
|
+
await this.mappers.SubscriptionMapper.toDto(subscription);
|
|
133
88
|
return createdSubscriptionDto;
|
|
134
89
|
}
|
|
135
90
|
|
|
@@ -144,7 +99,7 @@ export class BaseSubscriptionService<
|
|
|
144
99
|
'Subscription',
|
|
145
100
|
idDto
|
|
146
101
|
);
|
|
147
|
-
return this.
|
|
102
|
+
return this.mappers.SubscriptionMapper.toDto(
|
|
148
103
|
subscription as Entities['SubscriptionMapper']
|
|
149
104
|
);
|
|
150
105
|
}
|
|
@@ -162,7 +117,7 @@ export class BaseSubscriptionService<
|
|
|
162
117
|
active: true
|
|
163
118
|
});
|
|
164
119
|
|
|
165
|
-
return this.
|
|
120
|
+
return this.mappers.SubscriptionMapper.toDto(
|
|
166
121
|
subscription as Entities['SubscriptionMapper']
|
|
167
122
|
);
|
|
168
123
|
}
|
|
@@ -179,14 +134,15 @@ export class BaseSubscriptionService<
|
|
|
179
134
|
partyType: 'ORGANIZATION',
|
|
180
135
|
active: true
|
|
181
136
|
});
|
|
182
|
-
return this.
|
|
137
|
+
return this.mappers.SubscriptionMapper.toDto(
|
|
183
138
|
subscription as Entities['SubscriptionMapper']
|
|
184
139
|
);
|
|
185
140
|
}
|
|
186
141
|
|
|
187
142
|
async updateSubscription(
|
|
188
|
-
subscriptionDto:
|
|
189
|
-
em?: EntityManager
|
|
143
|
+
subscriptionDto: UpdateSubscriptionDto<PartyType, BillingProviderType>,
|
|
144
|
+
em?: EntityManager,
|
|
145
|
+
...args: unknown[]
|
|
190
146
|
): Promise<Dto['SubscriptionMapper']> {
|
|
191
147
|
if (this.evaluatedTelemetryOptions.logging) {
|
|
192
148
|
this.openTelemetryCollector.info(
|
|
@@ -194,19 +150,17 @@ export class BaseSubscriptionService<
|
|
|
194
150
|
subscriptionDto
|
|
195
151
|
);
|
|
196
152
|
}
|
|
197
|
-
const subscription =
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
153
|
+
const subscription = await this.mappers.UpdateSubscriptionMapper.toEntity(
|
|
154
|
+
subscriptionDto,
|
|
155
|
+
em ?? this.em,
|
|
156
|
+
...args
|
|
157
|
+
);
|
|
202
158
|
const updatedSubscription = await (em ?? this.em).upsert(subscription);
|
|
203
159
|
await (em ?? this.em).transactional(async (innerEm) => {
|
|
204
160
|
await innerEm.persist(updatedSubscription);
|
|
205
161
|
});
|
|
206
162
|
const updatedSubscriptionDto =
|
|
207
|
-
await this.
|
|
208
|
-
updatedSubscription
|
|
209
|
-
);
|
|
163
|
+
await this.mappers.SubscriptionMapper.toDto(updatedSubscription);
|
|
210
164
|
|
|
211
165
|
return updatedSubscriptionDto;
|
|
212
166
|
}
|
|
@@ -244,10 +198,9 @@ export class BaseSubscriptionService<
|
|
|
244
198
|
|
|
245
199
|
return Promise.all(
|
|
246
200
|
subscriptions.map((subscription) => {
|
|
247
|
-
const subscriptionDto =
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
);
|
|
201
|
+
const subscriptionDto = this.mappers.SubscriptionMapper.toDto(
|
|
202
|
+
subscription as Entities['SubscriptionMapper']
|
|
203
|
+
);
|
|
251
204
|
return subscriptionDto;
|
|
252
205
|
})
|
|
253
206
|
);
|