@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, InstanceTypeRecord } from '@forklaunch/common';
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
- InternalMapper,
11
- RequestMapperConstructor,
12
- ResponseMapperConstructor,
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
- SubscriptionMapper: ResponseMapperConstructor<
42
- SchemaValidator,
43
- Dto['SubscriptionMapper'],
44
- Entities['SubscriptionMapper']
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: Dto['CreateSubscriptionMapper'],
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
- await this._mappers.CreateSubscriptionMapper.deserializeDtoToEntity(
125
- subscriptionDto,
126
- em ?? this.em
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._mappers.SubscriptionMapper.serializeEntityToDto(subscription);
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._mappers.SubscriptionMapper.serializeEntityToDto(
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._mappers.SubscriptionMapper.serializeEntityToDto(
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._mappers.SubscriptionMapper.serializeEntityToDto(
137
+ return this.mappers.SubscriptionMapper.toDto(
183
138
  subscription as Entities['SubscriptionMapper']
184
139
  );
185
140
  }
186
141
 
187
142
  async updateSubscription(
188
- subscriptionDto: Dto['UpdateSubscriptionMapper'],
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
- this._mappers.UpdateSubscriptionMapper.deserializeDtoToEntity(
199
- subscriptionDto,
200
- em ?? this.em
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._mappers.SubscriptionMapper.serializeEntityToDto(
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
- this._mappers.SubscriptionMapper.serializeEntityToDto(
249
- subscription as Entities['SubscriptionMapper']
250
- );
201
+ const subscriptionDto = this.mappers.SubscriptionMapper.toDto(
202
+ subscription as Entities['SubscriptionMapper']
203
+ );
251
204
  return subscriptionDto;
252
205
  })
253
206
  );