@forklaunch/implementation-billing-base 0.1.13 → 0.1.15

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.
Files changed (50) hide show
  1. package/lib/__test__/schemaEquality.test.d.ts +1 -1
  2. package/lib/__test__/schemaEquality.test.js +305 -146
  3. package/lib/jest.config.d.ts +1 -1
  4. package/lib/jest.config.js +16 -16
  5. package/lib/schemas/billingPortal.schema.d.ts +103 -34
  6. package/lib/schemas/billingPortal.schema.js +4 -1
  7. package/lib/schemas/checkoutSession.schema.d.ts +169 -54
  8. package/lib/schemas/checkoutSession.schema.js +4 -1
  9. package/lib/schemas/index.d.ts +1 -1
  10. package/lib/schemas/paymentLink.schema.d.ts +220 -64
  11. package/lib/schemas/paymentLink.schema.js +4 -1
  12. package/lib/schemas/plan.schema.d.ts +328 -76
  13. package/lib/schemas/plan.schema.js +4 -1
  14. package/lib/schemas/subscription.schema.d.ts +356 -88
  15. package/lib/schemas/subscription.schema.js +4 -1
  16. package/lib/schemas/typebox/billingPortal.schema.d.ts +152 -38
  17. package/lib/schemas/typebox/billingPortal.schema.js +23 -17
  18. package/lib/schemas/typebox/checkoutSession.schema.d.ts +189 -55
  19. package/lib/schemas/typebox/checkoutSession.schema.js +25 -13
  20. package/lib/schemas/typebox/paymentLink.schema.d.ts +267 -65
  21. package/lib/schemas/typebox/paymentLink.schema.js +27 -15
  22. package/lib/schemas/typebox/plan.schema.d.ts +401 -77
  23. package/lib/schemas/typebox/plan.schema.js +31 -17
  24. package/lib/schemas/typebox/subscription.schema.d.ts +449 -89
  25. package/lib/schemas/typebox/subscription.schema.js +31 -19
  26. package/lib/schemas/zod/billingPortal.schema.d.ts +54 -38
  27. package/lib/schemas/zod/billingPortal.schema.js +23 -17
  28. package/lib/schemas/zod/checkoutSession.schema.d.ts +115 -55
  29. package/lib/schemas/zod/checkoutSession.schema.js +25 -13
  30. package/lib/schemas/zod/paymentLink.schema.d.ts +111 -65
  31. package/lib/schemas/zod/paymentLink.schema.js +27 -15
  32. package/lib/schemas/zod/plan.schema.d.ts +189 -77
  33. package/lib/schemas/zod/plan.schema.js +31 -17
  34. package/lib/schemas/zod/subscription.schema.d.ts +197 -89
  35. package/lib/schemas/zod/subscription.schema.js +31 -19
  36. package/lib/services/billingPortal.service.d.ts +77 -28
  37. package/lib/services/billingPortal.service.js +64 -45
  38. package/lib/services/checkoutSession.service.d.ts +77 -29
  39. package/lib/services/checkoutSession.service.js +54 -43
  40. package/lib/services/index.d.ts +1 -1
  41. package/lib/services/paymentLink.service.d.ts +82 -32
  42. package/lib/services/paymentLink.service.js +84 -66
  43. package/lib/services/plan.service.d.ts +73 -26
  44. package/lib/services/plan.service.js +45 -40
  45. package/lib/services/subscription.service.d.ts +136 -44
  46. package/lib/services/subscription.service.js +111 -88
  47. package/lib/tsconfig.tsbuildinfo +1 -1
  48. package/lib/vitest.config.d.ts +2 -2
  49. package/lib/vitest.config.js +4 -4
  50. package/package.json +8 -8
@@ -1,43 +1,48 @@
1
1
  import { transformIntoInternalDtoMapper } from '@forklaunch/core/mappers';
2
2
  export class BasePlanService {
3
- em;
4
- openTelemetryCollector;
5
- schemaValidator;
6
- mapperss;
7
- #mapperss;
8
- constructor(em, openTelemetryCollector, schemaValidator, mapperss) {
9
- this.em = em;
10
- this.openTelemetryCollector = openTelemetryCollector;
11
- this.schemaValidator = schemaValidator;
12
- this.mapperss = mapperss;
13
- this.#mapperss = transformIntoInternalDtoMapper(mapperss, schemaValidator);
14
- }
15
- async listPlans(idsDto, em) {
16
- return (await (em ?? this.em).findAll('Plan', {
17
- filters: idsDto?.ids ? { id: { $in: idsDto.ids } } : undefined
18
- })).map((plan) => this.#mapperss.PlanDtoMapper.serializeEntityToDto(plan));
19
- }
20
- async createPlan(planDto, em) {
21
- const plan = this.#mapperss.CreatePlanDtoMapper.deserializeDtoToEntity(planDto);
22
- await (em ?? this.em).transactional(async (innerEm) => {
23
- await innerEm.persist(plan);
24
- });
25
- return this.#mapperss.PlanDtoMapper.serializeEntityToDto(plan);
26
- }
27
- async getPlan(idDto, em) {
28
- const plan = await (em ?? this.em).findOneOrFail('Plan', idDto);
29
- return this.#mapperss.PlanDtoMapper.serializeEntityToDto(plan);
30
- }
31
- async updatePlan(planDto, em) {
32
- const plan = this.#mapperss.UpdatePlanDtoMapper.deserializeDtoToEntity(planDto);
33
- const updatedPlan = await (em ?? this.em).upsert(plan);
34
- await (em ?? this.em).transactional(async (innerEm) => {
35
- await innerEm.persist(plan);
36
- });
37
- const updatedPlanDto = this.#mapperss.PlanDtoMapper.serializeEntityToDto(updatedPlan);
38
- return updatedPlanDto;
39
- }
40
- async deletePlan(idDto, em) {
41
- await (em ?? this.em).nativeDelete('Plan', idDto);
42
- }
3
+ em;
4
+ openTelemetryCollector;
5
+ schemaValidator;
6
+ mapperss;
7
+ #mapperss;
8
+ constructor(em, openTelemetryCollector, schemaValidator, mapperss) {
9
+ this.em = em;
10
+ this.openTelemetryCollector = openTelemetryCollector;
11
+ this.schemaValidator = schemaValidator;
12
+ this.mapperss = mapperss;
13
+ this.#mapperss = transformIntoInternalDtoMapper(mapperss, schemaValidator);
14
+ }
15
+ async listPlans(idsDto, em) {
16
+ return (
17
+ await (em ?? this.em).findAll('Plan', {
18
+ filters: idsDto?.ids ? { id: { $in: idsDto.ids } } : undefined
19
+ })
20
+ ).map((plan) => this.#mapperss.PlanDtoMapper.serializeEntityToDto(plan));
21
+ }
22
+ async createPlan(planDto, em) {
23
+ const plan =
24
+ this.#mapperss.CreatePlanDtoMapper.deserializeDtoToEntity(planDto);
25
+ await (em ?? this.em).transactional(async (innerEm) => {
26
+ await innerEm.persist(plan);
27
+ });
28
+ return this.#mapperss.PlanDtoMapper.serializeEntityToDto(plan);
29
+ }
30
+ async getPlan(idDto, em) {
31
+ const plan = await (em ?? this.em).findOneOrFail('Plan', idDto);
32
+ return this.#mapperss.PlanDtoMapper.serializeEntityToDto(plan);
33
+ }
34
+ async updatePlan(planDto, em) {
35
+ const plan =
36
+ this.#mapperss.UpdatePlanDtoMapper.deserializeDtoToEntity(planDto);
37
+ const updatedPlan = await (em ?? this.em).upsert(plan);
38
+ await (em ?? this.em).transactional(async (innerEm) => {
39
+ await innerEm.persist(plan);
40
+ });
41
+ const updatedPlanDto =
42
+ this.#mapperss.PlanDtoMapper.serializeEntityToDto(updatedPlan);
43
+ return updatedPlanDto;
44
+ }
45
+ async deletePlan(idDto, em) {
46
+ await (em ?? this.em).nativeDelete('Plan', idDto);
47
+ }
43
48
  }
@@ -1,53 +1,145 @@
1
1
  import { IdDto } from '@forklaunch/common';
2
- import { RequestDtoMapperConstructor, ResponseDtoMapperConstructor } from '@forklaunch/core/mappers';
3
- import { MetricsDefinition, OpenTelemetryCollector } from '@forklaunch/core/http';
2
+ import {
3
+ RequestDtoMapperConstructor,
4
+ ResponseDtoMapperConstructor
5
+ } from '@forklaunch/core/mappers';
6
+ import {
7
+ MetricsDefinition,
8
+ OpenTelemetryCollector
9
+ } from '@forklaunch/core/http';
4
10
  import { SubscriptionService } from '@forklaunch/interfaces-billing/interfaces';
5
- import { CreateSubscriptionDto, SubscriptionDto, UpdateSubscriptionDto } from '@forklaunch/interfaces-billing/types';
11
+ import {
12
+ CreateSubscriptionDto,
13
+ SubscriptionDto,
14
+ UpdateSubscriptionDto
15
+ } from '@forklaunch/interfaces-billing/types';
6
16
  import { AnySchemaValidator } from '@forklaunch/validator';
7
17
  import { EntityManager } from '@mikro-orm/core';
8
- export declare class BaseSubscriptionService<SchemaValidator extends AnySchemaValidator, PartyType, BillingProviderType, Metrics extends MetricsDefinition = MetricsDefinition, Dto extends {
18
+ export declare class BaseSubscriptionService<
19
+ SchemaValidator extends AnySchemaValidator,
20
+ PartyType,
21
+ BillingProviderType,
22
+ Metrics extends MetricsDefinition = MetricsDefinition,
23
+ Dto extends {
9
24
  SubscriptionDtoMapper: SubscriptionDto<PartyType, BillingProviderType>;
10
- CreateSubscriptionDtoMapper: CreateSubscriptionDto<PartyType, BillingProviderType>;
11
- UpdateSubscriptionDtoMapper: UpdateSubscriptionDto<PartyType, BillingProviderType>;
12
- } = {
25
+ CreateSubscriptionDtoMapper: CreateSubscriptionDto<
26
+ PartyType,
27
+ BillingProviderType
28
+ >;
29
+ UpdateSubscriptionDtoMapper: UpdateSubscriptionDto<
30
+ PartyType,
31
+ BillingProviderType
32
+ >;
33
+ } = {
13
34
  SubscriptionDtoMapper: SubscriptionDto<PartyType, BillingProviderType>;
14
- CreateSubscriptionDtoMapper: CreateSubscriptionDto<PartyType, BillingProviderType>;
15
- UpdateSubscriptionDtoMapper: UpdateSubscriptionDto<PartyType, BillingProviderType>;
16
- }, Entities extends {
35
+ CreateSubscriptionDtoMapper: CreateSubscriptionDto<
36
+ PartyType,
37
+ BillingProviderType
38
+ >;
39
+ UpdateSubscriptionDtoMapper: UpdateSubscriptionDto<
40
+ PartyType,
41
+ BillingProviderType
42
+ >;
43
+ },
44
+ Entities extends {
17
45
  SubscriptionDtoMapper: SubscriptionDto<PartyType, BillingProviderType>;
18
- CreateSubscriptionDtoMapper: SubscriptionDto<PartyType, BillingProviderType>;
19
- UpdateSubscriptionDtoMapper: SubscriptionDto<PartyType, BillingProviderType>;
20
- } = {
46
+ CreateSubscriptionDtoMapper: SubscriptionDto<
47
+ PartyType,
48
+ BillingProviderType
49
+ >;
50
+ UpdateSubscriptionDtoMapper: SubscriptionDto<
51
+ PartyType,
52
+ BillingProviderType
53
+ >;
54
+ } = {
21
55
  SubscriptionDtoMapper: SubscriptionDto<PartyType, BillingProviderType>;
22
- CreateSubscriptionDtoMapper: SubscriptionDto<PartyType, BillingProviderType>;
23
- UpdateSubscriptionDtoMapper: SubscriptionDto<PartyType, BillingProviderType>;
24
- }> implements SubscriptionService<PartyType, BillingProviderType> {
25
- #private;
26
- protected em: EntityManager;
27
- protected readonly openTelemetryCollector: OpenTelemetryCollector<Metrics>;
28
- protected readonly schemaValidator: SchemaValidator;
29
- protected readonly mapperss: {
30
- SubscriptionDtoMapper: ResponseDtoMapperConstructor<SchemaValidator, Dto['SubscriptionDtoMapper'], Entities['SubscriptionDtoMapper']>;
31
- CreateSubscriptionDtoMapper: RequestDtoMapperConstructor<SchemaValidator, Dto['CreateSubscriptionDtoMapper'], Entities['CreateSubscriptionDtoMapper']>;
32
- UpdateSubscriptionDtoMapper: RequestDtoMapperConstructor<SchemaValidator, Dto['UpdateSubscriptionDtoMapper'], Entities['UpdateSubscriptionDtoMapper']>;
33
- };
34
- constructor(em: EntityManager, openTelemetryCollector: OpenTelemetryCollector<Metrics>, schemaValidator: SchemaValidator, mapperss: {
35
- SubscriptionDtoMapper: ResponseDtoMapperConstructor<SchemaValidator, Dto['SubscriptionDtoMapper'], Entities['SubscriptionDtoMapper']>;
36
- CreateSubscriptionDtoMapper: RequestDtoMapperConstructor<SchemaValidator, Dto['CreateSubscriptionDtoMapper'], Entities['CreateSubscriptionDtoMapper']>;
37
- UpdateSubscriptionDtoMapper: RequestDtoMapperConstructor<SchemaValidator, Dto['UpdateSubscriptionDtoMapper'], Entities['UpdateSubscriptionDtoMapper']>;
38
- });
39
- createSubscription(subscriptionDto: Dto['CreateSubscriptionDtoMapper'], em?: EntityManager): Promise<Dto['SubscriptionDtoMapper']>;
40
- getSubscription(idDto: IdDto, em?: EntityManager): Promise<Dto['SubscriptionDtoMapper']>;
41
- getUserSubscription({ id }: IdDto, em?: EntityManager): Promise<Dto['SubscriptionDtoMapper']>;
42
- getOrganizationSubscription({ id }: IdDto, em?: EntityManager): Promise<Dto['SubscriptionDtoMapper']>;
43
- updateSubscription(subscriptionDto: Dto['UpdateSubscriptionDtoMapper'], em?: EntityManager): Promise<Dto['SubscriptionDtoMapper']>;
44
- deleteSubscription(idDto: {
45
- id: string;
46
- }, em?: EntityManager): Promise<void>;
47
- listSubscriptions(idsDto: {
48
- ids?: string[];
49
- }, em?: EntityManager): Promise<Dto['SubscriptionDtoMapper'][]>;
50
- cancelSubscription(idDto: IdDto, em?: EntityManager): Promise<void>;
51
- resumeSubscription(idDto: IdDto, em?: EntityManager): Promise<void>;
56
+ CreateSubscriptionDtoMapper: SubscriptionDto<
57
+ PartyType,
58
+ BillingProviderType
59
+ >;
60
+ UpdateSubscriptionDtoMapper: SubscriptionDto<
61
+ PartyType,
62
+ BillingProviderType
63
+ >;
64
+ }
65
+ > implements SubscriptionService<PartyType, BillingProviderType>
66
+ {
67
+ #private;
68
+ protected em: EntityManager;
69
+ protected readonly openTelemetryCollector: OpenTelemetryCollector<Metrics>;
70
+ protected readonly schemaValidator: SchemaValidator;
71
+ protected readonly mapperss: {
72
+ SubscriptionDtoMapper: ResponseDtoMapperConstructor<
73
+ SchemaValidator,
74
+ Dto['SubscriptionDtoMapper'],
75
+ Entities['SubscriptionDtoMapper']
76
+ >;
77
+ CreateSubscriptionDtoMapper: RequestDtoMapperConstructor<
78
+ SchemaValidator,
79
+ Dto['CreateSubscriptionDtoMapper'],
80
+ Entities['CreateSubscriptionDtoMapper']
81
+ >;
82
+ UpdateSubscriptionDtoMapper: RequestDtoMapperConstructor<
83
+ SchemaValidator,
84
+ Dto['UpdateSubscriptionDtoMapper'],
85
+ Entities['UpdateSubscriptionDtoMapper']
86
+ >;
87
+ };
88
+ constructor(
89
+ em: EntityManager,
90
+ openTelemetryCollector: OpenTelemetryCollector<Metrics>,
91
+ schemaValidator: SchemaValidator,
92
+ mapperss: {
93
+ SubscriptionDtoMapper: ResponseDtoMapperConstructor<
94
+ SchemaValidator,
95
+ Dto['SubscriptionDtoMapper'],
96
+ Entities['SubscriptionDtoMapper']
97
+ >;
98
+ CreateSubscriptionDtoMapper: RequestDtoMapperConstructor<
99
+ SchemaValidator,
100
+ Dto['CreateSubscriptionDtoMapper'],
101
+ Entities['CreateSubscriptionDtoMapper']
102
+ >;
103
+ UpdateSubscriptionDtoMapper: RequestDtoMapperConstructor<
104
+ SchemaValidator,
105
+ Dto['UpdateSubscriptionDtoMapper'],
106
+ Entities['UpdateSubscriptionDtoMapper']
107
+ >;
108
+ }
109
+ );
110
+ createSubscription(
111
+ subscriptionDto: Dto['CreateSubscriptionDtoMapper'],
112
+ em?: EntityManager
113
+ ): Promise<Dto['SubscriptionDtoMapper']>;
114
+ getSubscription(
115
+ idDto: IdDto,
116
+ em?: EntityManager
117
+ ): Promise<Dto['SubscriptionDtoMapper']>;
118
+ getUserSubscription(
119
+ { id }: IdDto,
120
+ em?: EntityManager
121
+ ): Promise<Dto['SubscriptionDtoMapper']>;
122
+ getOrganizationSubscription(
123
+ { id }: IdDto,
124
+ em?: EntityManager
125
+ ): Promise<Dto['SubscriptionDtoMapper']>;
126
+ updateSubscription(
127
+ subscriptionDto: Dto['UpdateSubscriptionDtoMapper'],
128
+ em?: EntityManager
129
+ ): Promise<Dto['SubscriptionDtoMapper']>;
130
+ deleteSubscription(
131
+ idDto: {
132
+ id: string;
133
+ },
134
+ em?: EntityManager
135
+ ): Promise<void>;
136
+ listSubscriptions(
137
+ idsDto: {
138
+ ids?: string[];
139
+ },
140
+ em?: EntityManager
141
+ ): Promise<Dto['SubscriptionDtoMapper'][]>;
142
+ cancelSubscription(idDto: IdDto, em?: EntityManager): Promise<void>;
143
+ resumeSubscription(idDto: IdDto, em?: EntityManager): Promise<void>;
52
144
  }
53
- //# sourceMappingURL=subscription.service.d.ts.map
145
+ //# sourceMappingURL=subscription.service.d.ts.map
@@ -1,94 +1,117 @@
1
1
  import { transformIntoInternalDtoMapper } from '@forklaunch/core/mappers';
2
2
  export class BaseSubscriptionService {
3
- em;
4
- openTelemetryCollector;
5
- schemaValidator;
6
- mapperss;
7
- #mapperss;
8
- constructor(em, openTelemetryCollector, schemaValidator, mapperss) {
9
- this.em = em;
10
- this.openTelemetryCollector = openTelemetryCollector;
11
- this.schemaValidator = schemaValidator;
12
- this.mapperss = mapperss;
13
- this.#mapperss = transformIntoInternalDtoMapper(mapperss, this.schemaValidator);
3
+ em;
4
+ openTelemetryCollector;
5
+ schemaValidator;
6
+ mapperss;
7
+ #mapperss;
8
+ constructor(em, openTelemetryCollector, schemaValidator, mapperss) {
9
+ this.em = em;
10
+ this.openTelemetryCollector = openTelemetryCollector;
11
+ this.schemaValidator = schemaValidator;
12
+ this.mapperss = mapperss;
13
+ this.#mapperss = transformIntoInternalDtoMapper(
14
+ mapperss,
15
+ this.schemaValidator
16
+ );
17
+ }
18
+ async createSubscription(subscriptionDto, em) {
19
+ const subscription =
20
+ this.#mapperss.CreateSubscriptionDtoMapper.deserializeDtoToEntity(
21
+ subscriptionDto
22
+ );
23
+ await (em ?? this.em).transactional(async (innerEm) => {
24
+ await innerEm.persist(subscription);
25
+ });
26
+ const createdSubscriptionDto =
27
+ this.#mapperss.SubscriptionDtoMapper.serializeEntityToDto(subscription);
28
+ return createdSubscriptionDto;
29
+ }
30
+ async getSubscription(idDto, em) {
31
+ const subscription = await (em ?? this.em).findOneOrFail(
32
+ 'Subscription',
33
+ idDto
34
+ );
35
+ return this.#mapperss.SubscriptionDtoMapper.serializeEntityToDto(
36
+ subscription
37
+ );
38
+ }
39
+ async getUserSubscription({ id }, em) {
40
+ const subscription = await (em ?? this.em).findOneOrFail('Subscription', {
41
+ partyId: id,
42
+ partyType: 'USER',
43
+ active: true
44
+ });
45
+ return this.#mapperss.SubscriptionDtoMapper.serializeEntityToDto(
46
+ subscription
47
+ );
48
+ }
49
+ async getOrganizationSubscription({ id }, em) {
50
+ const subscription = await (em ?? this.em).findOneOrFail('Subscription', {
51
+ partyId: id,
52
+ partyType: 'ORGANIZATION',
53
+ active: true
54
+ });
55
+ return this.#mapperss.SubscriptionDtoMapper.serializeEntityToDto(
56
+ subscription
57
+ );
58
+ }
59
+ async updateSubscription(subscriptionDto, em) {
60
+ const subscription =
61
+ this.#mapperss.UpdateSubscriptionDtoMapper.deserializeDtoToEntity(
62
+ subscriptionDto
63
+ );
64
+ const updatedSubscription = await (em ?? this.em).upsert(subscription);
65
+ await (em ?? this.em).transactional(async (innerEm) => {
66
+ await innerEm.persist(updatedSubscription);
67
+ });
68
+ const updatedSubscriptionDto =
69
+ this.#mapperss.SubscriptionDtoMapper.serializeEntityToDto(
70
+ updatedSubscription
71
+ );
72
+ return updatedSubscriptionDto;
73
+ }
74
+ async deleteSubscription(idDto, em) {
75
+ const subscription = await (em ?? this.em).findOne('Subscription', idDto);
76
+ if (!subscription) {
77
+ throw new Error('Subscription not found');
14
78
  }
15
- async createSubscription(subscriptionDto, em) {
16
- const subscription = this.#mapperss.CreateSubscriptionDtoMapper.deserializeDtoToEntity(subscriptionDto);
17
- await (em ?? this.em).transactional(async (innerEm) => {
18
- await innerEm.persist(subscription);
19
- });
20
- const createdSubscriptionDto = this.#mapperss.SubscriptionDtoMapper.serializeEntityToDto(subscription);
21
- return createdSubscriptionDto;
79
+ await (em ?? this.em).removeAndFlush(subscription);
80
+ }
81
+ async listSubscriptions(idsDto, em) {
82
+ const subscriptions = await (em ?? this.em).findAll('Subscription', {
83
+ where: idsDto.ids
84
+ ? {
85
+ id: {
86
+ $in: idsDto.ids
87
+ }
88
+ }
89
+ : undefined
90
+ });
91
+ return subscriptions.map((subscription) => {
92
+ const subscriptionDto =
93
+ this.#mapperss.SubscriptionDtoMapper.serializeEntityToDto(subscription);
94
+ return subscriptionDto;
95
+ });
96
+ }
97
+ async cancelSubscription(idDto, em) {
98
+ const subscription = await (em ?? this.em).findOne('Subscription', idDto);
99
+ if (!subscription) {
100
+ throw new Error('Subscription not found');
22
101
  }
23
- async getSubscription(idDto, em) {
24
- const subscription = await (em ?? this.em).findOneOrFail('Subscription', idDto);
25
- return this.#mapperss.SubscriptionDtoMapper.serializeEntityToDto(subscription);
26
- }
27
- async getUserSubscription({ id }, em) {
28
- const subscription = await (em ?? this.em).findOneOrFail('Subscription', {
29
- partyId: id,
30
- partyType: 'USER',
31
- active: true
32
- });
33
- return this.#mapperss.SubscriptionDtoMapper.serializeEntityToDto(subscription);
34
- }
35
- async getOrganizationSubscription({ id }, em) {
36
- const subscription = await (em ?? this.em).findOneOrFail('Subscription', {
37
- partyId: id,
38
- partyType: 'ORGANIZATION',
39
- active: true
40
- });
41
- return this.#mapperss.SubscriptionDtoMapper.serializeEntityToDto(subscription);
42
- }
43
- async updateSubscription(subscriptionDto, em) {
44
- const subscription = this.#mapperss.UpdateSubscriptionDtoMapper.deserializeDtoToEntity(subscriptionDto);
45
- const updatedSubscription = await (em ?? this.em).upsert(subscription);
46
- await (em ?? this.em).transactional(async (innerEm) => {
47
- await innerEm.persist(updatedSubscription);
48
- });
49
- const updatedSubscriptionDto = this.#mapperss.SubscriptionDtoMapper.serializeEntityToDto(updatedSubscription);
50
- return updatedSubscriptionDto;
51
- }
52
- async deleteSubscription(idDto, em) {
53
- const subscription = await (em ?? this.em).findOne('Subscription', idDto);
54
- if (!subscription) {
55
- throw new Error('Subscription not found');
56
- }
57
- await (em ?? this.em).removeAndFlush(subscription);
58
- }
59
- async listSubscriptions(idsDto, em) {
60
- const subscriptions = await (em ?? this.em).findAll('Subscription', {
61
- where: idsDto.ids
62
- ? {
63
- id: {
64
- $in: idsDto.ids
65
- }
66
- }
67
- : undefined
68
- });
69
- return subscriptions.map((subscription) => {
70
- const subscriptionDto = this.#mapperss.SubscriptionDtoMapper.serializeEntityToDto(subscription);
71
- return subscriptionDto;
72
- });
73
- }
74
- async cancelSubscription(idDto, em) {
75
- const subscription = await (em ?? this.em).findOne('Subscription', idDto);
76
- if (!subscription) {
77
- throw new Error('Subscription not found');
78
- }
79
- subscription.active = false;
80
- await (em ?? this.em).transactional(async (innerEm) => {
81
- await innerEm.persist(subscription);
82
- });
83
- }
84
- async resumeSubscription(idDto, em) {
85
- const subscription = await (em ?? this.em).findOne('Subscription', idDto);
86
- if (!subscription) {
87
- throw new Error('Subscription not found');
88
- }
89
- subscription.active = true;
90
- await (em ?? this.em).transactional(async (innerEm) => {
91
- await innerEm.persist(subscription);
92
- });
102
+ subscription.active = false;
103
+ await (em ?? this.em).transactional(async (innerEm) => {
104
+ await innerEm.persist(subscription);
105
+ });
106
+ }
107
+ async resumeSubscription(idDto, em) {
108
+ const subscription = await (em ?? this.em).findOne('Subscription', idDto);
109
+ if (!subscription) {
110
+ throw new Error('Subscription not found');
93
111
  }
112
+ subscription.active = true;
113
+ await (em ?? this.em).transactional(async (innerEm) => {
114
+ await innerEm.persist(subscription);
115
+ });
116
+ }
94
117
  }