@forklaunch/implementation-billing-base 0.3.2 → 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.
@@ -8,22 +8,24 @@ import {
8
8
 
9
9
  export const CreateBillingPortalSchema = {
10
10
  customerId: string,
11
- uri: string,
12
- expiresAt: date
11
+ expiresAt: date,
12
+ uri: optional(string),
13
+ providerFields: optional(unknown)
13
14
  };
14
15
 
15
16
  export const UpdateBillingPortalSchema = ({ uuidId }: { uuidId: boolean }) => ({
16
17
  id: uuidId ? uuid : string,
17
18
  uri: optional(string),
18
- expiresAt: optional(date)
19
+ expiresAt: optional(date),
20
+ providerFields: optional(unknown)
19
21
  });
20
22
 
21
23
  export const BillingPortalSchema = ({ uuidId }: { uuidId: boolean }) => ({
22
24
  id: uuidId ? uuid : string,
23
25
  customerId: string,
24
- uri: string,
26
+ uri: optional(string),
25
27
  expiresAt: date,
26
- extraFields: optional(unknown),
28
+ providerFields: optional(unknown),
27
29
  createdAt: optional(date),
28
30
  updatedAt: optional(date)
29
31
  });
@@ -11,57 +11,65 @@ import {
11
11
 
12
12
  export const CreateCheckoutSessionSchema = <
13
13
  T extends Record<string, LiteralSchema>,
14
- U extends Record<string, LiteralSchema>
14
+ U extends Record<string, LiteralSchema>,
15
+ V extends Record<string, LiteralSchema>
15
16
  >(
16
17
  PaymentMethodEnum: T,
18
+ CurrencyEnum: V,
17
19
  StatusEnum: U
18
20
  ) => ({
19
21
  customerId: string,
20
22
  paymentMethods: array(enum_(PaymentMethodEnum)),
21
- successRedirectUri: string,
22
- cancelRedirectUri: string,
23
+ currency: enum_(CurrencyEnum),
24
+ successRedirectUri: optional(string),
25
+ cancelRedirectUri: optional(string),
23
26
  expiresAt: date,
24
27
  status: enum_(StatusEnum),
25
- extraFields: optional(unknown)
28
+ providerFields: optional(unknown)
26
29
  });
27
30
 
28
31
  export const UpdateCheckoutSessionSchema =
29
32
  ({ uuidId }: { uuidId: boolean }) =>
30
33
  <
31
34
  T extends Record<string, LiteralSchema>,
32
- U extends Record<string, LiteralSchema>
35
+ U extends Record<string, LiteralSchema>,
36
+ V extends Record<string, LiteralSchema>
33
37
  >(
34
38
  PaymentMethodEnum: T,
35
- StatusEnum: U
39
+ CurrencyEnum: U,
40
+ StatusEnum: V
36
41
  ) => ({
37
42
  id: uuidId ? uuid : string,
38
43
  customerId: optional(string),
39
44
  paymentMethods: optional(array(enum_(PaymentMethodEnum))),
45
+ currency: optional(enum_(CurrencyEnum)),
40
46
  successRedirectUri: optional(string),
41
47
  cancelRedirectUri: optional(string),
42
48
  expiresAt: optional(date),
43
49
  status: optional(enum_(StatusEnum)),
44
- extraFields: optional(unknown)
50
+ providerFields: optional(unknown)
45
51
  });
46
52
 
47
53
  export const CheckoutSessionSchema =
48
54
  ({ uuidId }: { uuidId: boolean }) =>
49
55
  <
50
56
  T extends Record<string, LiteralSchema>,
51
- U extends Record<string, LiteralSchema>
57
+ U extends Record<string, LiteralSchema>,
58
+ V extends Record<string, LiteralSchema>
52
59
  >(
53
60
  PaymentMethodEnum: T,
54
- StatusEnum: U
61
+ CurrencyEnum: U,
62
+ StatusEnum: V
55
63
  ) => ({
56
64
  id: uuidId ? uuid : string,
57
65
  customerId: string,
58
- metadata: optional(unknown),
59
66
  paymentMethods: array(enum_(PaymentMethodEnum)),
60
- successRedirectUri: string,
61
- cancelRedirectUri: string,
67
+ currency: enum_(CurrencyEnum),
68
+ successRedirectUri: optional(string),
69
+ cancelRedirectUri: optional(string),
62
70
  expiresAt: date,
63
71
  status: enum_(StatusEnum),
64
- extraFields: optional(unknown),
72
+ providerFields: optional(unknown),
65
73
  createdAt: optional(date),
66
74
  updatedAt: optional(date)
67
75
  });
@@ -1,5 +1,6 @@
1
1
  import { LiteralSchema } from '@forklaunch/validator';
2
2
  import {
3
+ array,
3
4
  date,
4
5
  enum_,
5
6
  number,
@@ -11,62 +12,56 @@ import {
11
12
 
12
13
  export const CreatePaymentLinkSchema = <
13
14
  T extends Record<string, LiteralSchema>,
14
- U extends Record<string, LiteralSchema>
15
+ U extends Record<string, LiteralSchema>,
16
+ V extends Record<string, LiteralSchema>
15
17
  >(
16
- CurrencyEnum: T,
17
- StatusEnum: U
18
+ PaymentMethodEnum: T,
19
+ CurrencyEnum: U,
20
+ StatusEnum: V
18
21
  ) => ({
19
22
  amount: number,
23
+ paymentMethods: array(enum_(PaymentMethodEnum)),
20
24
  currency: enum_(CurrencyEnum),
21
- description: optional(string),
22
- metadata: optional(unknown),
23
- successRedirectUri: string,
24
- cancelRedirectUri: string,
25
- expiresAt: date,
26
25
  status: enum_(StatusEnum),
27
- extraFields: optional(unknown)
26
+ providerFields: optional(unknown)
28
27
  });
29
28
 
30
29
  export const UpdatePaymentLinkSchema =
31
30
  ({ uuidId }: { uuidId: boolean }) =>
32
31
  <
33
32
  T extends Record<string, LiteralSchema>,
34
- U extends Record<string, LiteralSchema>
33
+ U extends Record<string, LiteralSchema>,
34
+ V extends Record<string, LiteralSchema>
35
35
  >(
36
- CurrencyEnum: T,
37
- StatusEnum: U
36
+ PaymentMethodEnum: T,
37
+ CurrencyEnum: U,
38
+ StatusEnum: V
38
39
  ) => ({
39
40
  id: uuidId ? uuid : string,
40
41
  amount: optional(number),
42
+ paymentMethods: optional(array(enum_(PaymentMethodEnum))),
41
43
  currency: optional(enum_(CurrencyEnum)),
42
- description: optional(string),
43
- metadata: optional(unknown),
44
- successRedirectUri: optional(string),
45
- cancelRedirectUri: optional(string),
46
- expiresAt: optional(date),
47
44
  status: optional(enum_(StatusEnum)),
48
- extraFields: optional(unknown)
45
+ providerFields: optional(unknown)
49
46
  });
50
47
 
51
48
  export const PaymentLinkSchema =
52
49
  ({ uuidId }: { uuidId: boolean }) =>
53
50
  <
54
51
  T extends Record<string, LiteralSchema>,
55
- U extends Record<string, LiteralSchema>
52
+ U extends Record<string, LiteralSchema>,
53
+ V extends Record<string, LiteralSchema>
56
54
  >(
57
- CurrencyEnum: T,
58
- StatusEnum: U
55
+ PaymentMethodEnum: T,
56
+ CurrencyEnum: U,
57
+ StatusEnum: V
59
58
  ) => ({
60
59
  id: uuidId ? uuid : string,
61
60
  amount: number,
61
+ paymentMethods: array(enum_(PaymentMethodEnum)),
62
62
  currency: enum_(CurrencyEnum),
63
- description: optional(string),
64
- metadata: optional(unknown),
65
- successRedirectUri: string,
66
- cancelRedirectUri: string,
67
- expiresAt: date,
68
63
  status: enum_(StatusEnum),
69
- extraFields: optional(unknown),
64
+ providerFields: optional(unknown),
70
65
  createdAt: optional(date),
71
66
  updatedAt: optional(date)
72
67
  });
@@ -13,62 +13,71 @@ import {
13
13
 
14
14
  export const CreatePlanSchema = <
15
15
  T extends Record<string, LiteralSchema>,
16
- U extends Record<string, LiteralSchema>
16
+ U extends Record<string, LiteralSchema>,
17
+ V extends Record<string, LiteralSchema>
17
18
  >(
18
19
  PlanCadenceEnum: T,
19
- BillingProviderEnum: U
20
+ CurrencyEnum: U,
21
+ BillingProviderEnum: V
20
22
  ) => ({
21
23
  name: string,
22
24
  description: optional(string),
23
25
  price: number,
24
26
  cadence: enum_(PlanCadenceEnum),
25
- features: array(string),
26
- extraFields: optional(unknown),
27
+ currency: enum_(CurrencyEnum),
28
+ features: optional(array(string)),
27
29
  externalId: string,
28
30
  billingProvider: optional(enum_(BillingProviderEnum)),
29
- active: boolean
31
+ active: boolean,
32
+ providerFields: optional(unknown)
30
33
  });
31
34
 
32
35
  export const UpdatePlanSchema =
33
36
  ({ uuidId }: { uuidId: boolean }) =>
34
37
  <
35
38
  T extends Record<string, LiteralSchema>,
36
- U extends Record<string, LiteralSchema>
39
+ U extends Record<string, LiteralSchema>,
40
+ V extends Record<string, LiteralSchema>
37
41
  >(
38
42
  PlanCadenceEnum: T,
39
- BillingProviderEnum: U
43
+ CurrencyEnum: U,
44
+ BillingProviderEnum: V
40
45
  ) => ({
41
46
  id: uuidId ? uuid : string,
42
47
  name: optional(string),
43
48
  description: optional(string),
44
49
  price: optional(number),
45
50
  cadence: optional(enum_(PlanCadenceEnum)),
51
+ currency: optional(enum_(CurrencyEnum)),
46
52
  features: optional(array(string)),
47
- extraFields: optional(unknown),
48
53
  externalId: optional(string),
49
54
  billingProvider: optional(enum_(BillingProviderEnum)),
50
- active: optional(boolean)
55
+ active: optional(boolean),
56
+ providerFields: optional(unknown)
51
57
  });
52
58
 
53
59
  export const PlanSchema =
54
60
  ({ uuidId }: { uuidId: boolean }) =>
55
61
  <
56
62
  T extends Record<string, LiteralSchema>,
57
- U extends Record<string, LiteralSchema>
63
+ U extends Record<string, LiteralSchema>,
64
+ V extends Record<string, LiteralSchema>
58
65
  >(
59
66
  PlanCadenceEnum: T,
60
- BillingProviderEnum: U
67
+ CurrencyEnum: U,
68
+ BillingProviderEnum: V
61
69
  ) => ({
62
70
  id: uuidId ? uuid : string,
63
71
  name: string,
64
72
  description: optional(string),
65
73
  price: number,
66
74
  cadence: enum_(PlanCadenceEnum),
75
+ currency: enum_(CurrencyEnum),
67
76
  features: optional(array(string)),
68
- extraFields: optional(unknown),
69
77
  externalId: string,
70
78
  billingProvider: optional(enum_(BillingProviderEnum)),
71
79
  active: boolean,
80
+ providerFields: optional(unknown),
72
81
  createdAt: optional(date),
73
82
  updatedAt: optional(date)
74
83
  });
@@ -23,10 +23,10 @@ export const CreateSubscriptionSchema = <
23
23
  active: boolean,
24
24
  externalId: string,
25
25
  startDate: date,
26
- endDate: date,
26
+ endDate: optional(date),
27
27
  status: string,
28
28
  billingProvider: optional(enum_(BillingProviderEnum)),
29
- extraFields: optional(unknown)
29
+ providerFields: optional(unknown)
30
30
  });
31
31
 
32
32
  export const UpdateSubscriptionSchema =
@@ -49,7 +49,7 @@ export const UpdateSubscriptionSchema =
49
49
  endDate: optional(date),
50
50
  status: optional(string),
51
51
  billingProvider: optional(enum_(BillingProviderEnum)),
52
- extraFields: optional(unknown)
52
+ providerFields: optional(unknown)
53
53
  });
54
54
 
55
55
  export const SubscriptionSchema =
@@ -69,10 +69,10 @@ export const SubscriptionSchema =
69
69
  active: boolean,
70
70
  externalId: string,
71
71
  startDate: date,
72
- endDate: date,
72
+ endDate: optional(date),
73
73
  status: string,
74
74
  billingProvider: optional(enum_(BillingProviderEnum)),
75
- extraFields: optional(unknown),
75
+ providerFields: optional(unknown),
76
76
  createdAt: optional(date),
77
77
  updatedAt: optional(date)
78
78
  });
@@ -6,76 +6,61 @@ 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 { BillingPortalService } from '@forklaunch/interfaces-billing/interfaces';
10
+ import { UpdateBillingPortalDto } from '@forklaunch/interfaces-billing/types';
16
11
  import {
17
- BillingPortalDto,
18
- CreateBillingPortalDto,
19
- UpdateBillingPortalDto
20
- } from '@forklaunch/interfaces-billing/types';
12
+ InternalMapper,
13
+ RequestMapperConstructor,
14
+ ResponseMapperConstructor,
15
+ transformIntoInternalMapper
16
+ } from '@forklaunch/internal';
21
17
  import { AnySchemaValidator } from '@forklaunch/validator';
22
18
  import { EntityManager } from '@mikro-orm/core';
19
+ import { BaseBillingDtos } from '../types/baseBillingDto.types';
20
+ import { BaseBillingEntities } from '../types/baseBillingEntity.types';
23
21
 
24
22
  export class BaseBillingPortalService<
25
23
  SchemaValidator extends AnySchemaValidator,
26
- Metrics extends MetricsDefinition = MetricsDefinition,
27
- Dto extends {
28
- BillingPortalDtoMapper: BillingPortalDto;
29
- CreateBillingPortalDtoMapper: CreateBillingPortalDto;
30
- UpdateBillingPortalDtoMapper: UpdateBillingPortalDto;
31
- } = {
32
- BillingPortalDtoMapper: BillingPortalDto;
33
- CreateBillingPortalDtoMapper: CreateBillingPortalDto;
34
- UpdateBillingPortalDtoMapper: UpdateBillingPortalDto;
35
- },
36
- Entities extends {
37
- BillingPortalDtoMapper: BillingPortalDto;
38
- CreateBillingPortalDtoMapper: BillingPortalDto;
39
- UpdateBillingPortalDtoMapper: BillingPortalDto;
40
- } = {
41
- BillingPortalDtoMapper: BillingPortalDto;
42
- CreateBillingPortalDtoMapper: BillingPortalDto;
43
- UpdateBillingPortalDtoMapper: BillingPortalDto;
44
- }
24
+ Entities extends BaseBillingEntities,
25
+ Dto extends BaseBillingDtos = BaseBillingDtos
45
26
  > implements BillingPortalService
46
27
  {
47
- #mappers: InternalDtoMapper<
48
- InstanceTypeRecord<typeof this.mappers>,
49
- Entities,
50
- Dto
51
- >;
52
- private evaluatedTelemetryOptions: {
28
+ protected _mappers: InternalMapper<InstanceTypeRecord<typeof this.mappers>>;
29
+ protected evaluatedTelemetryOptions: {
53
30
  logging?: boolean;
54
31
  metrics?: boolean;
55
32
  tracing?: boolean;
56
33
  };
57
- private enableDatabaseBackup: boolean;
34
+ protected enableDatabaseBackup: boolean;
58
35
 
59
36
  constructor(
60
37
  protected em: EntityManager,
61
38
  protected cache: TtlCache,
62
- protected openTelemetryCollector: OpenTelemetryCollector<Metrics>,
39
+ protected openTelemetryCollector: OpenTelemetryCollector<MetricsDefinition>,
63
40
  protected schemaValidator: SchemaValidator,
64
41
  protected mappers: {
65
- BillingPortalDtoMapper: ResponseDtoMapperConstructor<
42
+ BillingPortalMapper: ResponseMapperConstructor<
66
43
  SchemaValidator,
67
- Dto['BillingPortalDtoMapper'],
68
- Entities['BillingPortalDtoMapper']
44
+ Dto['BillingPortalMapper'],
45
+ Entities['BillingPortalMapper']
69
46
  >;
70
- CreateBillingPortalDtoMapper: RequestDtoMapperConstructor<
47
+ CreateBillingPortalMapper: RequestMapperConstructor<
71
48
  SchemaValidator,
72
- Dto['CreateBillingPortalDtoMapper'],
73
- Entities['CreateBillingPortalDtoMapper']
49
+ Dto['CreateBillingPortalMapper'],
50
+ Entities['CreateBillingPortalMapper'],
51
+ (
52
+ dto: Dto['CreateBillingPortalMapper'],
53
+ em: EntityManager
54
+ ) => Promise<Entities['CreateBillingPortalMapper']>
74
55
  >;
75
- UpdateBillingPortalDtoMapper: RequestDtoMapperConstructor<
56
+ UpdateBillingPortalMapper: RequestMapperConstructor<
76
57
  SchemaValidator,
77
- Dto['UpdateBillingPortalDtoMapper'],
78
- Entities['UpdateBillingPortalDtoMapper']
58
+ Dto['UpdateBillingPortalMapper'],
59
+ Entities['UpdateBillingPortalMapper'],
60
+ (
61
+ dto: Dto['UpdateBillingPortalMapper'],
62
+ em: EntityManager
63
+ ) => Promise<Entities['UpdateBillingPortalMapper']>
79
64
  >;
80
65
  },
81
66
  readonly options?: {
@@ -83,7 +68,7 @@ export class BaseBillingPortalService<
83
68
  enableDatabaseBackup?: boolean;
84
69
  }
85
70
  ) {
86
- this.#mappers = transformIntoInternalDtoMapper(mappers, schemaValidator);
71
+ this._mappers = transformIntoInternalMapper(mappers, schemaValidator);
87
72
  this.enableDatabaseBackup = options?.enableDatabaseBackup ?? false;
88
73
  this.evaluatedTelemetryOptions = options?.telemetry
89
74
  ? evaluateTelemetryOptions(options.telemetry).enabled
@@ -97,8 +82,8 @@ export class BaseBillingPortalService<
97
82
  protected createCacheKey = createCacheKey('billing_portal_session');
98
83
 
99
84
  async createBillingPortalSession(
100
- billingPortalDto: Dto['CreateBillingPortalDtoMapper']
101
- ): Promise<Dto['BillingPortalDtoMapper']> {
85
+ billingPortalDto: Dto['CreateBillingPortalMapper']
86
+ ): Promise<Dto['BillingPortalMapper']> {
102
87
  if (this.evaluatedTelemetryOptions.logging) {
103
88
  this.openTelemetryCollector.info(
104
89
  'Creating billing portal session',
@@ -107,7 +92,7 @@ export class BaseBillingPortalService<
107
92
  }
108
93
 
109
94
  const billingPortal =
110
- await this.#mappers.CreateBillingPortalDtoMapper.deserializeDtoToEntity(
95
+ await this._mappers.CreateBillingPortalMapper.deserializeDtoToEntity(
111
96
  billingPortalDto,
112
97
  this.em
113
98
  );
@@ -117,7 +102,7 @@ export class BaseBillingPortalService<
117
102
  }
118
103
 
119
104
  const createdBillingPortalDto =
120
- await this.#mappers.BillingPortalDtoMapper.serializeEntityToDto(
105
+ await this._mappers.BillingPortalMapper.serializeEntityToDto(
121
106
  billingPortal
122
107
  );
123
108
 
@@ -132,13 +117,13 @@ export class BaseBillingPortalService<
132
117
 
133
118
  async getBillingPortalSession(
134
119
  idDto: IdDto
135
- ): Promise<Dto['BillingPortalDtoMapper']> {
120
+ ): Promise<Dto['BillingPortalMapper']> {
136
121
  if (this.evaluatedTelemetryOptions.logging) {
137
122
  this.openTelemetryCollector.info('Getting billing portal session', idDto);
138
123
  }
139
124
 
140
125
  const billingPortalDetails = await this.cache.readRecord<
141
- Entities['BillingPortalDtoMapper']
126
+ Entities['BillingPortalMapper']
142
127
  >(this.createCacheKey(idDto.id));
143
128
 
144
129
  if (!billingPortalDetails) {
@@ -150,7 +135,7 @@ export class BaseBillingPortalService<
150
135
 
151
136
  async updateBillingPortalSession(
152
137
  billingPortalDto: UpdateBillingPortalDto
153
- ): Promise<Dto['BillingPortalDtoMapper']> {
138
+ ): Promise<Dto['BillingPortalMapper']> {
154
139
  if (this.evaluatedTelemetryOptions.logging) {
155
140
  this.openTelemetryCollector.info(
156
141
  'Updating billing portal session',
@@ -159,7 +144,7 @@ export class BaseBillingPortalService<
159
144
  }
160
145
 
161
146
  const existingBillingPortal = (
162
- await this.cache.readRecord<Entities['BillingPortalDtoMapper']>(
147
+ await this.cache.readRecord<Entities['BillingPortalMapper']>(
163
148
  this.createCacheKey(billingPortalDto.id)
164
149
  )
165
150
  )?.value;
@@ -169,7 +154,7 @@ export class BaseBillingPortalService<
169
154
  }
170
155
 
171
156
  const billingPortal =
172
- await this.#mappers.UpdateBillingPortalDtoMapper.deserializeDtoToEntity(
157
+ await this._mappers.UpdateBillingPortalMapper.deserializeDtoToEntity(
173
158
  billingPortalDto,
174
159
  this.em
175
160
  );
@@ -182,7 +167,7 @@ export class BaseBillingPortalService<
182
167
 
183
168
  const updatedBillingPortalDto = {
184
169
  ...existingBillingPortal,
185
- ...(await this.#mappers.BillingPortalDtoMapper.serializeEntityToDto(
170
+ ...(await this._mappers.BillingPortalMapper.serializeEntityToDto(
186
171
  billingPortal
187
172
  ))
188
173
  };