@forklaunch/implementation-billing-base 0.5.8 → 0.6.1

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.
@@ -1,4 +1,4 @@
1
- import { IdDto, IdsDto, InstanceTypeRecord } from '@forklaunch/common';
1
+ import { IdDto, IdsDto } from '@forklaunch/common';
2
2
  import { createCacheKey, TtlCache } from '@forklaunch/core/cache';
3
3
  import {
4
4
  evaluateTelemetryOptions,
@@ -8,103 +8,65 @@ import {
8
8
  } from '@forklaunch/core/http';
9
9
  import { PaymentLinkService } from '@forklaunch/interfaces-billing/interfaces';
10
10
  import {
11
- InternalMapper,
12
- RequestMapperConstructor,
13
- ResponseMapperConstructor,
14
- transformIntoInternalMapper
15
- } from '@forklaunch/internal';
11
+ CreatePaymentLinkDto,
12
+ UpdatePaymentLinkDto
13
+ } from '@forklaunch/interfaces-billing/types';
16
14
  import { AnySchemaValidator } from '@forklaunch/validator';
17
15
  import { EntityManager } from '@mikro-orm/core';
18
16
  import { BasePaymentLinkDtos } from '../domain/types/baseBillingDto.types';
19
17
  import { BasePaymentLinkEntities } from '../domain/types/baseBillingEntity.types';
18
+ import { PaymentLinkMappers } from '../domain/types/paymentLink.mapper.types';
20
19
 
21
20
  export class BasePaymentLinkService<
22
21
  SchemaValidator extends AnySchemaValidator,
23
22
  PaymentMethodEnum,
24
23
  CurrencyEnum,
25
24
  StatusEnum,
26
- Entities extends BasePaymentLinkEntities<
25
+ MapperEntities extends BasePaymentLinkEntities<
27
26
  PaymentMethodEnum,
28
27
  CurrencyEnum,
29
28
  StatusEnum
30
29
  >,
31
- Dto extends BasePaymentLinkDtos<
30
+ MapperDomains extends BasePaymentLinkDtos<
32
31
  PaymentMethodEnum,
33
32
  CurrencyEnum,
34
33
  StatusEnum
35
34
  > = BasePaymentLinkDtos<PaymentMethodEnum, CurrencyEnum, StatusEnum>
36
35
  > implements PaymentLinkService<PaymentMethodEnum, CurrencyEnum, StatusEnum>
37
36
  {
38
- protected _mappers: InternalMapper<InstanceTypeRecord<typeof this.mappers>>;
39
- protected evaluatedTelemetryOptions: {
37
+ private evaluatedTelemetryOptions: {
40
38
  logging?: boolean;
41
39
  metrics?: boolean;
42
40
  tracing?: boolean;
43
41
  };
44
42
  protected enableDatabaseBackup: boolean;
45
- protected readonly em: EntityManager;
46
- protected readonly cache: TtlCache;
47
- protected readonly openTelemetryCollector: OpenTelemetryCollector<MetricsDefinition>;
48
- protected readonly schemaValidator: SchemaValidator;
49
- protected readonly mappers: {
50
- PaymentLinkMapper: ResponseMapperConstructor<
51
- SchemaValidator,
52
- Dto['PaymentLinkMapper'],
53
- Entities['PaymentLinkMapper']
54
- >;
55
- CreatePaymentLinkMapper: RequestMapperConstructor<
56
- SchemaValidator,
57
- Dto['CreatePaymentLinkMapper'],
58
- Entities['CreatePaymentLinkMapper'],
59
- (
60
- dto: Dto['CreatePaymentLinkMapper'],
61
- em: EntityManager
62
- ) => Promise<Entities['CreatePaymentLinkMapper']>
63
- >;
64
- UpdatePaymentLinkMapper: RequestMapperConstructor<
65
- SchemaValidator,
66
- Dto['UpdatePaymentLinkMapper'],
67
- Entities['UpdatePaymentLinkMapper'],
68
- (
69
- dto: Dto['UpdatePaymentLinkMapper'],
70
- em: EntityManager
71
- ) => Promise<Entities['UpdatePaymentLinkMapper']>
72
- >;
73
- };
43
+ public em: EntityManager;
44
+ protected cache: TtlCache;
45
+ protected openTelemetryCollector: OpenTelemetryCollector<MetricsDefinition>;
46
+ protected schemaValidator: SchemaValidator;
47
+ protected mappers: PaymentLinkMappers<
48
+ PaymentMethodEnum,
49
+ CurrencyEnum,
50
+ StatusEnum,
51
+ MapperEntities,
52
+ MapperDomains
53
+ >;
74
54
 
75
55
  constructor(
76
56
  em: EntityManager,
77
57
  cache: TtlCache,
78
58
  openTelemetryCollector: OpenTelemetryCollector<MetricsDefinition>,
79
59
  schemaValidator: SchemaValidator,
80
- mappers: {
81
- PaymentLinkMapper: ResponseMapperConstructor<
82
- SchemaValidator,
83
- Dto['PaymentLinkMapper'],
84
- Entities['PaymentLinkMapper']
85
- >;
86
- CreatePaymentLinkMapper: RequestMapperConstructor<
87
- SchemaValidator,
88
- Dto['CreatePaymentLinkMapper'],
89
- Entities['CreatePaymentLinkMapper'],
90
- (
91
- dto: Dto['CreatePaymentLinkMapper'],
92
- em: EntityManager
93
- ) => Promise<Entities['CreatePaymentLinkMapper']>
94
- >;
95
- UpdatePaymentLinkMapper: RequestMapperConstructor<
96
- SchemaValidator,
97
- Dto['UpdatePaymentLinkMapper'],
98
- Entities['UpdatePaymentLinkMapper'],
99
- (
100
- dto: Dto['UpdatePaymentLinkMapper'],
101
- em: EntityManager
102
- ) => Promise<Entities['UpdatePaymentLinkMapper']>
103
- >;
104
- },
105
- readonly options?: {
106
- enableDatabaseBackup?: boolean;
60
+ mappers: PaymentLinkMappers<
61
+ PaymentMethodEnum,
62
+ CurrencyEnum,
63
+ StatusEnum,
64
+ MapperEntities,
65
+ MapperDomains
66
+ >,
67
+ options?: {
107
68
  telemetry?: TelemetryOptions;
69
+ enableDatabaseBackup?: boolean;
108
70
  }
109
71
  ) {
110
72
  this.em = em;
@@ -112,7 +74,6 @@ export class BasePaymentLinkService<
112
74
  this.openTelemetryCollector = openTelemetryCollector;
113
75
  this.schemaValidator = schemaValidator;
114
76
  this.mappers = mappers;
115
- this._mappers = transformIntoInternalMapper(mappers, schemaValidator);
116
77
  this.enableDatabaseBackup = options?.enableDatabaseBackup ?? false;
117
78
  this.evaluatedTelemetryOptions = options?.telemetry
118
79
  ? evaluateTelemetryOptions(options.telemetry).enabled
@@ -127,24 +88,29 @@ export class BasePaymentLinkService<
127
88
  protected createCacheKey = createCacheKey(this.cacheKeyPrefix);
128
89
 
129
90
  async createPaymentLink(
130
- paymentLinkDto: Dto['CreatePaymentLinkMapper']
131
- ): Promise<Dto['PaymentLinkMapper']> {
91
+ paymentLinkDto: CreatePaymentLinkDto<
92
+ PaymentMethodEnum,
93
+ CurrencyEnum,
94
+ StatusEnum
95
+ >,
96
+ ...args: unknown[]
97
+ ): Promise<MapperDomains['PaymentLinkMapper']> {
132
98
  if (this.evaluatedTelemetryOptions.logging) {
133
99
  this.openTelemetryCollector.info('Creating payment link', paymentLinkDto);
134
100
  }
135
101
 
136
- const paymentLink =
137
- await this._mappers.CreatePaymentLinkMapper.deserializeDtoToEntity(
138
- paymentLinkDto,
139
- this.em
140
- );
102
+ const paymentLink = await this.mappers.CreatePaymentLinkMapper.toEntity(
103
+ paymentLinkDto,
104
+ this.em,
105
+ ...args
106
+ );
141
107
 
142
108
  if (this.enableDatabaseBackup) {
143
109
  await this.em.persistAndFlush(paymentLink);
144
110
  }
145
111
 
146
112
  const createdPaymentLinkDto =
147
- await this._mappers.PaymentLinkMapper.serializeEntityToDto(paymentLink);
113
+ await this.mappers.PaymentLinkMapper.toDto(paymentLink);
148
114
 
149
115
  await this.cache.putRecord({
150
116
  key: this.createCacheKey(createdPaymentLinkDto.id),
@@ -156,24 +122,29 @@ export class BasePaymentLinkService<
156
122
  }
157
123
 
158
124
  async updatePaymentLink(
159
- paymentLinkDto: Dto['UpdatePaymentLinkMapper']
160
- ): Promise<Dto['PaymentLinkMapper']> {
125
+ paymentLinkDto: UpdatePaymentLinkDto<
126
+ PaymentMethodEnum,
127
+ CurrencyEnum,
128
+ StatusEnum
129
+ >,
130
+ ...args: unknown[]
131
+ ): Promise<MapperDomains['PaymentLinkMapper']> {
161
132
  if (this.evaluatedTelemetryOptions.logging) {
162
133
  this.openTelemetryCollector.info('Updating payment link', paymentLinkDto);
163
134
  }
164
135
 
165
136
  const cacheKey = this.createCacheKey(paymentLinkDto.id);
166
137
  const existingLink = (
167
- await this.cache.readRecord<Entities['PaymentLinkMapper']>(cacheKey)
138
+ await this.cache.readRecord<MapperEntities['PaymentLinkMapper']>(cacheKey)
168
139
  )?.value;
169
140
  if (!existingLink) {
170
141
  throw new Error('Payment link not found');
171
142
  }
172
- const paymentLink =
173
- await this._mappers.UpdatePaymentLinkMapper.deserializeDtoToEntity(
174
- paymentLinkDto,
175
- this.em
176
- );
143
+ const paymentLink = await this.mappers.UpdatePaymentLinkMapper.toEntity(
144
+ paymentLinkDto,
145
+ this.em,
146
+ ...args
147
+ );
177
148
 
178
149
  if (this.enableDatabaseBackup) {
179
150
  await this.em.persistAndFlush(paymentLink);
@@ -181,9 +152,7 @@ export class BasePaymentLinkService<
181
152
 
182
153
  const updatedLinkDto = {
183
154
  ...existingLink,
184
- ...(await this._mappers.PaymentLinkMapper.serializeEntityToDto(
185
- paymentLink
186
- ))
155
+ ...(await this.mappers.PaymentLinkMapper.toDto(paymentLink))
187
156
  };
188
157
 
189
158
  await this.cache.putRecord({
@@ -195,20 +164,22 @@ export class BasePaymentLinkService<
195
164
  return updatedLinkDto;
196
165
  }
197
166
 
198
- async getPaymentLink({ id }: IdDto): Promise<Dto['PaymentLinkMapper']> {
167
+ async getPaymentLink({
168
+ id
169
+ }: IdDto): Promise<MapperDomains['PaymentLinkMapper']> {
199
170
  if (this.evaluatedTelemetryOptions.logging) {
200
171
  this.openTelemetryCollector.info('Getting payment link', { id });
201
172
  }
202
173
  const cacheKey = this.createCacheKey(id);
203
174
  const paymentLink =
204
- await this.cache.readRecord<Entities['PaymentLinkMapper']>(cacheKey);
175
+ await this.cache.readRecord<MapperEntities['PaymentLinkMapper']>(
176
+ cacheKey
177
+ );
205
178
  if (!paymentLink) {
206
179
  throw new Error('Payment link not found');
207
180
  }
208
181
 
209
- return this._mappers.PaymentLinkMapper.serializeEntityToDto(
210
- paymentLink.value
211
- );
182
+ return this.mappers.PaymentLinkMapper.toDto(paymentLink.value);
212
183
  }
213
184
 
214
185
  async expirePaymentLink({ id }: IdDto): Promise<void> {
@@ -248,7 +219,9 @@ export class BasePaymentLinkService<
248
219
  await this.cache.deleteRecord(this.createCacheKey(id));
249
220
  }
250
221
 
251
- async listPaymentLinks(idsDto?: IdsDto): Promise<Dto['PaymentLinkMapper'][]> {
222
+ async listPaymentLinks(
223
+ idsDto?: IdsDto
224
+ ): Promise<MapperDomains['PaymentLinkMapper'][]> {
252
225
  const keys =
253
226
  idsDto?.ids.map((id) => this.createCacheKey(id)) ??
254
227
  (await this.cache.listKeys(this.cacheKeyPrefix));
@@ -256,11 +229,10 @@ export class BasePaymentLinkService<
256
229
  return Promise.all(
257
230
  keys.map(async (key) => {
258
231
  const paymentLink =
259
- await this.cache.readRecord<Entities['PaymentLinkMapper']>(key);
260
- const paymentLinkDto =
261
- this._mappers.PaymentLinkMapper.serializeEntityToDto(
262
- paymentLink.value
263
- );
232
+ await this.cache.readRecord<MapperEntities['PaymentLinkMapper']>(key);
233
+ const paymentLinkDto = this.mappers.PaymentLinkMapper.toDto(
234
+ paymentLink.value
235
+ );
264
236
  return paymentLinkDto;
265
237
  })
266
238
  );
@@ -1,4 +1,4 @@
1
- import { IdDto, IdsDto, InstanceTypeRecord } from '@forklaunch/common';
1
+ import { IdDto, IdsDto } from '@forklaunch/common';
2
2
  import {
3
3
  evaluateTelemetryOptions,
4
4
  MetricsDefinition,
@@ -7,98 +7,60 @@ import {
7
7
  } from '@forklaunch/core/http';
8
8
  import { PlanService } from '@forklaunch/interfaces-billing/interfaces';
9
9
  import {
10
- InternalMapper,
11
- RequestMapperConstructor,
12
- ResponseMapperConstructor,
13
- transformIntoInternalMapper
14
- } from '@forklaunch/internal';
10
+ CreatePlanDto,
11
+ UpdatePlanDto
12
+ } from '@forklaunch/interfaces-billing/types';
15
13
  import { AnySchemaValidator } from '@forklaunch/validator';
16
14
  import { EntityManager } from '@mikro-orm/core';
17
15
  import { BasePlanDtos } from '../domain/types/baseBillingDto.types';
18
16
  import { BasePlanEntities } from '../domain/types/baseBillingEntity.types';
17
+ import { PlanMappers } from '../domain/types/plan.mapper.types';
19
18
 
20
19
  export class BasePlanService<
21
20
  SchemaValidator extends AnySchemaValidator,
22
21
  PlanCadenceEnum,
23
22
  CurrencyEnum,
24
23
  BillingProviderEnum,
25
- Entities extends BasePlanEntities<
24
+ MapperEntities extends BasePlanEntities<
26
25
  PlanCadenceEnum,
27
26
  CurrencyEnum,
28
27
  BillingProviderEnum
29
28
  >,
30
- Dto extends BasePlanDtos<
29
+ MapperDomains extends BasePlanDtos<
31
30
  PlanCadenceEnum,
32
31
  CurrencyEnum,
33
32
  BillingProviderEnum
34
33
  > = BasePlanDtos<PlanCadenceEnum, CurrencyEnum, BillingProviderEnum>
35
34
  > implements PlanService<PlanCadenceEnum, CurrencyEnum, BillingProviderEnum>
36
35
  {
37
- protected _mappers: InternalMapper<InstanceTypeRecord<typeof this.mappers>>;
38
- protected evaluatedTelemetryOptions: {
36
+ private evaluatedTelemetryOptions: {
39
37
  logging?: boolean;
40
38
  metrics?: boolean;
41
39
  tracing?: boolean;
42
40
  };
43
- protected em: EntityManager;
44
- protected readonly openTelemetryCollector: OpenTelemetryCollector<MetricsDefinition>;
45
- protected readonly schemaValidator: SchemaValidator;
46
- protected readonly mappers: {
47
- PlanMapper: ResponseMapperConstructor<
48
- SchemaValidator,
49
- Dto['PlanMapper'],
50
- Entities['PlanMapper']
51
- >;
52
- CreatePlanMapper: RequestMapperConstructor<
53
- SchemaValidator,
54
- Dto['CreatePlanMapper'],
55
- Entities['CreatePlanMapper'],
56
- (
57
- dto: Dto['CreatePlanMapper'],
58
- em: EntityManager
59
- ) => Promise<Entities['CreatePlanMapper']>
60
- >;
61
- UpdatePlanMapper: RequestMapperConstructor<
62
- SchemaValidator,
63
- Dto['UpdatePlanMapper'],
64
- Entities['UpdatePlanMapper'],
65
- (
66
- dto: Dto['UpdatePlanMapper'],
67
- em: EntityManager
68
- ) => Promise<Entities['UpdatePlanMapper']>
69
- >;
70
- };
41
+ public em: EntityManager;
42
+ protected openTelemetryCollector: OpenTelemetryCollector<MetricsDefinition>;
43
+ protected schemaValidator: SchemaValidator;
44
+ protected mappers: PlanMappers<
45
+ PlanCadenceEnum,
46
+ CurrencyEnum,
47
+ BillingProviderEnum,
48
+ MapperEntities,
49
+ MapperDomains
50
+ >;
71
51
 
72
52
  constructor(
73
53
  em: EntityManager,
74
54
  openTelemetryCollector: OpenTelemetryCollector<MetricsDefinition>,
75
55
  schemaValidator: SchemaValidator,
76
- mappers: {
77
- PlanMapper: ResponseMapperConstructor<
78
- SchemaValidator,
79
- Dto['PlanMapper'],
80
- Entities['PlanMapper']
81
- >;
82
- CreatePlanMapper: RequestMapperConstructor<
83
- SchemaValidator,
84
- Dto['CreatePlanMapper'],
85
- Entities['CreatePlanMapper'],
86
- (
87
- dto: Dto['CreatePlanMapper'],
88
- em: EntityManager
89
- ) => Promise<Entities['CreatePlanMapper']>
90
- >;
91
- UpdatePlanMapper: RequestMapperConstructor<
92
- SchemaValidator,
93
- Dto['UpdatePlanMapper'],
94
- Entities['UpdatePlanMapper'],
95
- (
96
- dto: Dto['UpdatePlanMapper'],
97
- em: EntityManager
98
- ) => Promise<Entities['UpdatePlanMapper']>
99
- >;
100
- },
101
- readonly options?: {
56
+ mappers: PlanMappers<
57
+ PlanCadenceEnum,
58
+ CurrencyEnum,
59
+ BillingProviderEnum,
60
+ MapperEntities,
61
+ MapperDomains
62
+ >,
63
+ options?: {
102
64
  telemetry?: TelemetryOptions;
103
65
  }
104
66
  ) {
@@ -106,7 +68,6 @@ export class BasePlanService<
106
68
  this.openTelemetryCollector = openTelemetryCollector;
107
69
  this.schemaValidator = schemaValidator;
108
70
  this.mappers = mappers;
109
- this._mappers = transformIntoInternalMapper(mappers, schemaValidator);
110
71
  this.evaluatedTelemetryOptions = options?.telemetry
111
72
  ? evaluateTelemetryOptions(options.telemetry).enabled
112
73
  : {
@@ -119,7 +80,7 @@ export class BasePlanService<
119
80
  async listPlans(
120
81
  idsDto?: IdsDto,
121
82
  em?: EntityManager
122
- ): Promise<Dto['PlanMapper'][]> {
83
+ ): Promise<MapperDomains['PlanMapper'][]> {
123
84
  if (this.evaluatedTelemetryOptions.logging) {
124
85
  this.openTelemetryCollector.info('Listing plans', idsDto);
125
86
  }
@@ -129,57 +90,59 @@ export class BasePlanService<
129
90
  filters: idsDto?.ids ? { id: { $in: idsDto.ids } } : undefined
130
91
  })
131
92
  ).map((plan) =>
132
- this._mappers.PlanMapper.serializeEntityToDto(
133
- plan as Entities['PlanMapper']
134
- )
93
+ this.mappers.PlanMapper.toDto(plan as MapperEntities['PlanMapper'])
135
94
  )
136
95
  );
137
96
  }
138
97
 
139
98
  async createPlan(
140
- planDto: Dto['CreatePlanMapper'],
141
- em?: EntityManager
142
- ): Promise<Dto['PlanMapper']> {
99
+ planDto: CreatePlanDto<PlanCadenceEnum, CurrencyEnum, BillingProviderEnum>,
100
+ em?: EntityManager,
101
+ ...args: unknown[]
102
+ ): Promise<MapperDomains['PlanMapper']> {
143
103
  if (this.evaluatedTelemetryOptions.logging) {
144
104
  this.openTelemetryCollector.info('Creating plan', planDto);
145
105
  }
146
- const plan = await this._mappers.CreatePlanMapper.deserializeDtoToEntity(
106
+ const plan = await this.mappers.CreatePlanMapper.toEntity(
147
107
  planDto,
148
- em ?? this.em
108
+ em ?? this.em,
109
+ ...args
149
110
  );
150
111
  await (em ?? this.em).transactional(async (innerEm) => {
151
112
  await innerEm.persist(plan);
152
113
  });
153
- return this._mappers.PlanMapper.serializeEntityToDto(plan);
114
+ return this.mappers.PlanMapper.toDto(plan);
154
115
  }
155
116
 
156
- async getPlan(idDto: IdDto, em?: EntityManager): Promise<Dto['PlanMapper']> {
117
+ async getPlan(
118
+ idDto: IdDto,
119
+ em?: EntityManager
120
+ ): Promise<MapperDomains['PlanMapper']> {
157
121
  if (this.evaluatedTelemetryOptions.logging) {
158
122
  this.openTelemetryCollector.info('Getting plan', idDto);
159
123
  }
160
124
  const plan = await (em ?? this.em).findOneOrFail('Plan', idDto);
161
- return this._mappers.PlanMapper.serializeEntityToDto(
162
- plan as Entities['PlanMapper']
163
- );
125
+ return this.mappers.PlanMapper.toDto(plan as MapperEntities['PlanMapper']);
164
126
  }
165
127
 
166
128
  async updatePlan(
167
- planDto: Dto['UpdatePlanMapper'],
168
- em?: EntityManager
169
- ): Promise<Dto['PlanMapper']> {
129
+ planDto: UpdatePlanDto<PlanCadenceEnum, CurrencyEnum, BillingProviderEnum>,
130
+ em?: EntityManager,
131
+ ...args: unknown[]
132
+ ): Promise<MapperDomains['PlanMapper']> {
170
133
  if (this.evaluatedTelemetryOptions.logging) {
171
134
  this.openTelemetryCollector.info('Updating plan', planDto);
172
135
  }
173
- const plan = await this._mappers.UpdatePlanMapper.deserializeDtoToEntity(
136
+ const plan = await this.mappers.UpdatePlanMapper.toEntity(
174
137
  planDto,
175
- em ?? this.em
138
+ em ?? this.em,
139
+ ...args
176
140
  );
177
141
  const updatedPlan = await (em ?? this.em).upsert(plan);
178
142
  await (em ?? this.em).transactional(async (innerEm) => {
179
143
  await innerEm.persist(plan);
180
144
  });
181
- const updatedPlanDto =
182
- await this._mappers.PlanMapper.serializeEntityToDto(updatedPlan);
145
+ const updatedPlanDto = await this.mappers.PlanMapper.toDto(updatedPlan);
183
146
  return updatedPlanDto;
184
147
  }
185
148