@forklaunch/implementation-billing-base 0.3.1 → 0.3.3

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.
@@ -5,76 +5,31 @@ 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 { SubscriptionService } from '@forklaunch/interfaces-billing/interfaces';
15
9
  import {
16
- CreateSubscriptionDto,
17
- SubscriptionDto,
18
- UpdateSubscriptionDto
19
- } from '@forklaunch/interfaces-billing/types';
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 { BaseSubscriptionDtos } from '../types/baseBillingDto.types';
18
+ import { BaseSubscriptionEntities } from '../types/baseBillingEntity.types';
22
19
 
23
20
  export class BaseSubscriptionService<
24
21
  SchemaValidator extends AnySchemaValidator,
25
22
  PartyType,
26
23
  BillingProviderType,
27
- Metrics extends MetricsDefinition = MetricsDefinition,
28
- Dto extends {
29
- SubscriptionDtoMapper: SubscriptionDto<PartyType, BillingProviderType>;
30
- CreateSubscriptionDtoMapper: CreateSubscriptionDto<
31
- PartyType,
32
- BillingProviderType
33
- >;
34
- UpdateSubscriptionDtoMapper: UpdateSubscriptionDto<
35
- PartyType,
36
- BillingProviderType
37
- >;
38
- } = {
39
- SubscriptionDtoMapper: SubscriptionDto<PartyType, BillingProviderType>;
40
- CreateSubscriptionDtoMapper: CreateSubscriptionDto<
41
- PartyType,
42
- BillingProviderType
43
- >;
44
- UpdateSubscriptionDtoMapper: UpdateSubscriptionDto<
45
- PartyType,
46
- BillingProviderType
47
- >;
48
- },
49
- Entities extends {
50
- SubscriptionDtoMapper: SubscriptionDto<PartyType, BillingProviderType>;
51
- CreateSubscriptionDtoMapper: SubscriptionDto<
52
- PartyType,
53
- BillingProviderType
54
- >;
55
- UpdateSubscriptionDtoMapper: SubscriptionDto<
56
- PartyType,
57
- BillingProviderType
58
- >;
59
- } = {
60
- SubscriptionDtoMapper: SubscriptionDto<PartyType, BillingProviderType>;
61
- CreateSubscriptionDtoMapper: SubscriptionDto<
62
- PartyType,
63
- BillingProviderType
64
- >;
65
- UpdateSubscriptionDtoMapper: SubscriptionDto<
66
- PartyType,
67
- BillingProviderType
68
- >;
69
- }
24
+ Entities extends BaseSubscriptionEntities<PartyType, BillingProviderType>,
25
+ Dto extends BaseSubscriptionDtos<
26
+ PartyType,
27
+ BillingProviderType
28
+ > = BaseSubscriptionDtos<PartyType, BillingProviderType>
70
29
  > implements SubscriptionService<PartyType, BillingProviderType>
71
30
  {
72
- #mappers: InternalDtoMapper<
73
- InstanceTypeRecord<typeof this.mappers>,
74
- Entities,
75
- Dto
76
- >;
77
- private evaluatedTelemetryOptions: {
31
+ protected _mappers: InternalMapper<InstanceTypeRecord<typeof this.mappers>>;
32
+ protected evaluatedTelemetryOptions: {
78
33
  logging?: boolean;
79
34
  metrics?: boolean;
80
35
  tracing?: boolean;
@@ -82,36 +37,38 @@ export class BaseSubscriptionService<
82
37
 
83
38
  constructor(
84
39
  protected em: EntityManager,
85
- protected readonly openTelemetryCollector: OpenTelemetryCollector<Metrics>,
40
+ protected readonly openTelemetryCollector: OpenTelemetryCollector<MetricsDefinition>,
86
41
  protected readonly schemaValidator: SchemaValidator,
87
42
  protected readonly mappers: {
88
- SubscriptionDtoMapper: ResponseDtoMapperConstructor<
43
+ SubscriptionMapper: ResponseMapperConstructor<
89
44
  SchemaValidator,
90
- Dto['SubscriptionDtoMapper'],
91
- Entities['SubscriptionDtoMapper']
45
+ Dto['SubscriptionMapper'],
46
+ Entities['SubscriptionMapper']
92
47
  >;
93
- CreateSubscriptionDtoMapper: RequestDtoMapperConstructor<
48
+ CreateSubscriptionMapper: RequestMapperConstructor<
94
49
  SchemaValidator,
95
- Dto['CreateSubscriptionDtoMapper'],
96
- Entities['CreateSubscriptionDtoMapper']
50
+ Dto['CreateSubscriptionMapper'],
51
+ Entities['CreateSubscriptionMapper'],
52
+ (
53
+ dto: Dto['CreateSubscriptionMapper'],
54
+ em: EntityManager
55
+ ) => Promise<Entities['CreateSubscriptionMapper']>
97
56
  >;
98
- UpdateSubscriptionDtoMapper: RequestDtoMapperConstructor<
57
+ UpdateSubscriptionMapper: RequestMapperConstructor<
99
58
  SchemaValidator,
100
- Dto['UpdateSubscriptionDtoMapper'],
101
- Entities['UpdateSubscriptionDtoMapper']
59
+ Dto['UpdateSubscriptionMapper'],
60
+ Entities['UpdateSubscriptionMapper'],
61
+ (
62
+ dto: Dto['UpdateSubscriptionMapper'],
63
+ em: EntityManager
64
+ ) => Promise<Entities['UpdateSubscriptionMapper']>
102
65
  >;
103
66
  },
104
67
  readonly options?: {
105
- enableDatabaseBackup?: boolean;
106
68
  telemetry?: TelemetryOptions;
107
69
  }
108
70
  ) {
109
- this.#mappers = transformIntoInternalDtoMapper<
110
- SchemaValidator,
111
- typeof this.mappers,
112
- Entities,
113
- Dto
114
- >(mappers, this.schemaValidator);
71
+ this._mappers = transformIntoInternalMapper(mappers, this.schemaValidator);
115
72
  this.evaluatedTelemetryOptions = options?.telemetry
116
73
  ? evaluateTelemetryOptions(options.telemetry).enabled
117
74
  : {
@@ -122,9 +79,9 @@ export class BaseSubscriptionService<
122
79
  }
123
80
 
124
81
  async createSubscription(
125
- subscriptionDto: Dto['CreateSubscriptionDtoMapper'],
82
+ subscriptionDto: Dto['CreateSubscriptionMapper'],
126
83
  em?: EntityManager
127
- ): Promise<Dto['SubscriptionDtoMapper']> {
84
+ ): Promise<Dto['SubscriptionMapper']> {
128
85
  if (this.evaluatedTelemetryOptions.logging) {
129
86
  this.openTelemetryCollector.info(
130
87
  'Creating subscription',
@@ -132,7 +89,7 @@ export class BaseSubscriptionService<
132
89
  );
133
90
  }
134
91
  const subscription =
135
- await this.#mappers.CreateSubscriptionDtoMapper.deserializeDtoToEntity(
92
+ await this._mappers.CreateSubscriptionMapper.deserializeDtoToEntity(
136
93
  subscriptionDto,
137
94
  em ?? this.em
138
95
  );
@@ -140,16 +97,14 @@ export class BaseSubscriptionService<
140
97
  await innerEm.persist(subscription);
141
98
  });
142
99
  const createdSubscriptionDto =
143
- await this.#mappers.SubscriptionDtoMapper.serializeEntityToDto(
144
- subscription
145
- );
100
+ await this._mappers.SubscriptionMapper.serializeEntityToDto(subscription);
146
101
  return createdSubscriptionDto;
147
102
  }
148
103
 
149
104
  async getSubscription(
150
105
  idDto: IdDto,
151
106
  em?: EntityManager
152
- ): Promise<Dto['SubscriptionDtoMapper']> {
107
+ ): Promise<Dto['SubscriptionMapper']> {
153
108
  if (this.evaluatedTelemetryOptions.logging) {
154
109
  this.openTelemetryCollector.info('Getting subscription', idDto);
155
110
  }
@@ -157,15 +112,15 @@ export class BaseSubscriptionService<
157
112
  'Subscription',
158
113
  idDto
159
114
  );
160
- return this.#mappers.SubscriptionDtoMapper.serializeEntityToDto(
161
- subscription as Entities['SubscriptionDtoMapper']
115
+ return this._mappers.SubscriptionMapper.serializeEntityToDto(
116
+ subscription as Entities['SubscriptionMapper']
162
117
  );
163
118
  }
164
119
 
165
120
  async getUserSubscription(
166
121
  { id }: IdDto,
167
122
  em?: EntityManager
168
- ): Promise<Dto['SubscriptionDtoMapper']> {
123
+ ): Promise<Dto['SubscriptionMapper']> {
169
124
  if (this.evaluatedTelemetryOptions.logging) {
170
125
  this.openTelemetryCollector.info('Getting user subscription', id);
171
126
  }
@@ -175,15 +130,15 @@ export class BaseSubscriptionService<
175
130
  active: true
176
131
  });
177
132
 
178
- return this.#mappers.SubscriptionDtoMapper.serializeEntityToDto(
179
- subscription as Entities['SubscriptionDtoMapper']
133
+ return this._mappers.SubscriptionMapper.serializeEntityToDto(
134
+ subscription as Entities['SubscriptionMapper']
180
135
  );
181
136
  }
182
137
 
183
138
  async getOrganizationSubscription(
184
139
  { id }: IdDto,
185
140
  em?: EntityManager
186
- ): Promise<Dto['SubscriptionDtoMapper']> {
141
+ ): Promise<Dto['SubscriptionMapper']> {
187
142
  if (this.evaluatedTelemetryOptions.logging) {
188
143
  this.openTelemetryCollector.info('Getting organization subscription', id);
189
144
  }
@@ -192,15 +147,15 @@ export class BaseSubscriptionService<
192
147
  partyType: 'ORGANIZATION',
193
148
  active: true
194
149
  });
195
- return this.#mappers.SubscriptionDtoMapper.serializeEntityToDto(
196
- subscription as Entities['SubscriptionDtoMapper']
150
+ return this._mappers.SubscriptionMapper.serializeEntityToDto(
151
+ subscription as Entities['SubscriptionMapper']
197
152
  );
198
153
  }
199
154
 
200
155
  async updateSubscription(
201
- subscriptionDto: Dto['UpdateSubscriptionDtoMapper'],
156
+ subscriptionDto: Dto['UpdateSubscriptionMapper'],
202
157
  em?: EntityManager
203
- ): Promise<Dto['SubscriptionDtoMapper']> {
158
+ ): Promise<Dto['SubscriptionMapper']> {
204
159
  if (this.evaluatedTelemetryOptions.logging) {
205
160
  this.openTelemetryCollector.info(
206
161
  'Updating subscription',
@@ -208,7 +163,7 @@ export class BaseSubscriptionService<
208
163
  );
209
164
  }
210
165
  const subscription =
211
- this.#mappers.UpdateSubscriptionDtoMapper.deserializeDtoToEntity(
166
+ this._mappers.UpdateSubscriptionMapper.deserializeDtoToEntity(
212
167
  subscriptionDto,
213
168
  em ?? this.em
214
169
  );
@@ -217,7 +172,7 @@ export class BaseSubscriptionService<
217
172
  await innerEm.persist(updatedSubscription);
218
173
  });
219
174
  const updatedSubscriptionDto =
220
- await this.#mappers.SubscriptionDtoMapper.serializeEntityToDto(
175
+ await this._mappers.SubscriptionMapper.serializeEntityToDto(
221
176
  updatedSubscription
222
177
  );
223
178
 
@@ -241,7 +196,7 @@ export class BaseSubscriptionService<
241
196
  async listSubscriptions(
242
197
  idsDto: { ids?: string[] },
243
198
  em?: EntityManager
244
- ): Promise<Dto['SubscriptionDtoMapper'][]> {
199
+ ): Promise<Dto['SubscriptionMapper'][]> {
245
200
  if (this.evaluatedTelemetryOptions.logging) {
246
201
  this.openTelemetryCollector.info('Listing subscriptions', idsDto);
247
202
  }
@@ -258,8 +213,8 @@ export class BaseSubscriptionService<
258
213
  return Promise.all(
259
214
  subscriptions.map((subscription) => {
260
215
  const subscriptionDto =
261
- this.#mappers.SubscriptionDtoMapper.serializeEntityToDto(
262
- subscription as Entities['SubscriptionDtoMapper']
216
+ this._mappers.SubscriptionMapper.serializeEntityToDto(
217
+ subscription as Entities['SubscriptionMapper']
263
218
  );
264
219
  return subscriptionDto;
265
220
  })
@@ -274,7 +229,7 @@ export class BaseSubscriptionService<
274
229
  if (!subscription) {
275
230
  throw new Error('Subscription not found');
276
231
  }
277
- (subscription as Entities['SubscriptionDtoMapper']).active = false;
232
+ (subscription as Entities['SubscriptionMapper']).active = false;
278
233
  await (em ?? this.em).transactional(async (innerEm) => {
279
234
  await innerEm.persist(subscription);
280
235
  });
@@ -288,7 +243,7 @@ export class BaseSubscriptionService<
288
243
  if (!subscription) {
289
244
  throw new Error('Subscription not found');
290
245
  }
291
- (subscription as Entities['SubscriptionDtoMapper']).active = true;
246
+ (subscription as Entities['SubscriptionMapper']).active = true;
292
247
  await (em ?? this.em).transactional(async (innerEm) => {
293
248
  await innerEm.persist(subscription);
294
249
  });
@@ -0,0 +1,94 @@
1
+ import {
2
+ BillingPortalDto,
3
+ CheckoutSessionDto,
4
+ CreateBillingPortalDto,
5
+ CreateCheckoutSessionDto,
6
+ CreatePaymentLinkDto,
7
+ CreatePlanDto,
8
+ CreateSubscriptionDto,
9
+ PaymentLinkDto,
10
+ PlanDto,
11
+ SubscriptionDto,
12
+ UpdateBillingPortalDto,
13
+ UpdateCheckoutSessionDto,
14
+ UpdatePaymentLinkDto,
15
+ UpdatePlanDto,
16
+ UpdateSubscriptionDto
17
+ } from '@forklaunch/interfaces-billing/types';
18
+
19
+ // billing dto types
20
+ export type BaseBillingDtos = {
21
+ BillingPortalMapper: BillingPortalDto;
22
+ CreateBillingPortalMapper: CreateBillingPortalDto;
23
+ UpdateBillingPortalMapper: UpdateBillingPortalDto;
24
+ };
25
+
26
+ // checkout session dto types
27
+ export type BaseCheckoutSessionDtos<
28
+ PaymentMethodEnum,
29
+ CurrencyEnum,
30
+ StatusEnum
31
+ > = {
32
+ CheckoutSessionMapper: CheckoutSessionDto<
33
+ PaymentMethodEnum,
34
+ CurrencyEnum,
35
+ StatusEnum
36
+ >;
37
+ CreateCheckoutSessionMapper: CreateCheckoutSessionDto<
38
+ PaymentMethodEnum,
39
+ CurrencyEnum,
40
+ StatusEnum
41
+ >;
42
+ UpdateCheckoutSessionMapper: UpdateCheckoutSessionDto<
43
+ PaymentMethodEnum,
44
+ CurrencyEnum,
45
+ StatusEnum
46
+ >;
47
+ };
48
+
49
+ // payment link dto types
50
+ export type BasePaymentLinkDtos<PaymentMethodEnum, CurrencyEnum, StatusEnum> = {
51
+ PaymentLinkMapper: PaymentLinkDto<
52
+ PaymentMethodEnum,
53
+ CurrencyEnum,
54
+ StatusEnum
55
+ >;
56
+ CreatePaymentLinkMapper: CreatePaymentLinkDto<
57
+ PaymentMethodEnum,
58
+ CurrencyEnum,
59
+ StatusEnum
60
+ >;
61
+ UpdatePaymentLinkMapper: UpdatePaymentLinkDto<
62
+ PaymentMethodEnum,
63
+ CurrencyEnum,
64
+ StatusEnum
65
+ >;
66
+ };
67
+
68
+ // plan dto types
69
+ export type BasePlanDtos<PlanCadenceEnum, CurrencyEnum, BillingProviderEnum> = {
70
+ PlanMapper: PlanDto<PlanCadenceEnum, CurrencyEnum, BillingProviderEnum>;
71
+ CreatePlanMapper: CreatePlanDto<
72
+ PlanCadenceEnum,
73
+ CurrencyEnum,
74
+ BillingProviderEnum
75
+ >;
76
+ UpdatePlanMapper: UpdatePlanDto<
77
+ PlanCadenceEnum,
78
+ CurrencyEnum,
79
+ BillingProviderEnum
80
+ >;
81
+ };
82
+
83
+ // subscription dto types
84
+ export type BaseSubscriptionDtos<PartyType, BillingProviderType> = {
85
+ SubscriptionMapper: SubscriptionDto<PartyType, BillingProviderType>;
86
+ CreateSubscriptionMapper: CreateSubscriptionDto<
87
+ PartyType,
88
+ BillingProviderType
89
+ >;
90
+ UpdateSubscriptionMapper: UpdateSubscriptionDto<
91
+ PartyType,
92
+ BillingProviderType
93
+ >;
94
+ };
@@ -0,0 +1,78 @@
1
+ import {
2
+ BillingPortalDto,
3
+ CheckoutSessionDto,
4
+ PaymentLinkDto,
5
+ PlanDto,
6
+ SubscriptionDto
7
+ } from '@forklaunch/interfaces-billing/types';
8
+
9
+ // billing entity types
10
+ export type BaseBillingEntities = {
11
+ BillingPortalMapper: BillingPortalDto;
12
+ CreateBillingPortalMapper: BillingPortalDto;
13
+ UpdateBillingPortalMapper: BillingPortalDto;
14
+ };
15
+
16
+ // checkout session entity types
17
+ export type BaseCheckoutSessionEntities<
18
+ PaymentMethodEnum,
19
+ CurrencyEnum,
20
+ StatusEnum
21
+ > = {
22
+ CheckoutSessionMapper: CheckoutSessionDto<
23
+ PaymentMethodEnum,
24
+ CurrencyEnum,
25
+ StatusEnum
26
+ >;
27
+ CreateCheckoutSessionMapper: CheckoutSessionDto<
28
+ PaymentMethodEnum,
29
+ CurrencyEnum,
30
+ StatusEnum
31
+ >;
32
+ UpdateCheckoutSessionMapper: CheckoutSessionDto<
33
+ PaymentMethodEnum,
34
+ CurrencyEnum,
35
+ StatusEnum
36
+ >;
37
+ };
38
+
39
+ // payment link entity types
40
+ export type BasePaymentLinkEntities<
41
+ PaymentMethodEnum,
42
+ CurrencyEnum,
43
+ StatusEnum
44
+ > = {
45
+ PaymentLinkMapper: PaymentLinkDto<
46
+ PaymentMethodEnum,
47
+ CurrencyEnum,
48
+ StatusEnum
49
+ >;
50
+ CreatePaymentLinkMapper: PaymentLinkDto<
51
+ PaymentMethodEnum,
52
+ CurrencyEnum,
53
+ StatusEnum
54
+ >;
55
+ UpdatePaymentLinkMapper: PaymentLinkDto<
56
+ PaymentMethodEnum,
57
+ CurrencyEnum,
58
+ StatusEnum
59
+ >;
60
+ };
61
+
62
+ // plan entity types
63
+ export type BasePlanEntities<
64
+ PlanCadenceEnum,
65
+ CurrencyEnum,
66
+ BillingProviderEnum
67
+ > = {
68
+ PlanMapper: PlanDto<PlanCadenceEnum, CurrencyEnum, BillingProviderEnum>;
69
+ CreatePlanMapper: PlanDto<PlanCadenceEnum, CurrencyEnum, BillingProviderEnum>;
70
+ UpdatePlanMapper: PlanDto<PlanCadenceEnum, CurrencyEnum, BillingProviderEnum>;
71
+ };
72
+
73
+ // subscription entity types
74
+ export type BaseSubscriptionEntities<PartyType, BillingProviderType> = {
75
+ SubscriptionMapper: SubscriptionDto<PartyType, BillingProviderType>;
76
+ CreateSubscriptionMapper: SubscriptionDto<PartyType, BillingProviderType>;
77
+ UpdateSubscriptionMapper: SubscriptionDto<PartyType, BillingProviderType>;
78
+ };