@misterhomer1992/miit-bot-payment 1.0.7 → 1.0.9

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 (53) hide show
  1. package/README.md +1 -359
  2. package/dist/modules/cancellableAPI/utils.d.ts +1 -2
  3. package/dist/modules/cancellableAPI/utils.d.ts.map +1 -1
  4. package/dist/modules/cancellableAPI/utils.js.map +1 -1
  5. package/dist/modules/invoice/repository.d.ts +0 -25
  6. package/dist/modules/invoice/repository.d.ts.map +1 -1
  7. package/dist/modules/invoice/repository.js +0 -25
  8. package/dist/modules/invoice/repository.js.map +1 -1
  9. package/dist/modules/invoice/service.d.ts +0 -21
  10. package/dist/modules/invoice/service.d.ts.map +1 -1
  11. package/dist/modules/invoice/service.js +0 -21
  12. package/dist/modules/invoice/service.js.map +1 -1
  13. package/dist/modules/invoice/types.d.ts +0 -8
  14. package/dist/modules/invoice/types.d.ts.map +1 -1
  15. package/dist/modules/payments/api.d.ts +3 -4
  16. package/dist/modules/payments/api.d.ts.map +1 -1
  17. package/dist/modules/payments/api.js +3 -3
  18. package/dist/modules/payments/api.js.map +1 -1
  19. package/dist/modules/payments/repository.d.ts +1 -59
  20. package/dist/modules/payments/repository.d.ts.map +1 -1
  21. package/dist/modules/payments/repository.js +2 -59
  22. package/dist/modules/payments/repository.js.map +1 -1
  23. package/dist/modules/payments/service.d.ts +2 -66
  24. package/dist/modules/payments/service.d.ts.map +1 -1
  25. package/dist/modules/payments/service.js +5 -68
  26. package/dist/modules/payments/service.js.map +1 -1
  27. package/dist/modules/payments/types.d.ts +4 -11
  28. package/dist/modules/payments/types.d.ts.map +1 -1
  29. package/dist/modules/payments/utils.d.ts +3 -4
  30. package/dist/modules/payments/utils.d.ts.map +1 -1
  31. package/dist/modules/payments/utils.js +6 -6
  32. package/dist/modules/payments/utils.js.map +1 -1
  33. package/dist/modules/subscription/repository.d.ts +5 -83
  34. package/dist/modules/subscription/repository.d.ts.map +1 -1
  35. package/dist/modules/subscription/repository.js +40 -104
  36. package/dist/modules/subscription/repository.js.map +1 -1
  37. package/dist/modules/subscription/service.d.ts +6 -89
  38. package/dist/modules/subscription/service.d.ts.map +1 -1
  39. package/dist/modules/subscription/service.js +24 -83
  40. package/dist/modules/subscription/service.js.map +1 -1
  41. package/dist/modules/subscription/types.d.ts +13 -27
  42. package/dist/modules/subscription/types.d.ts.map +1 -1
  43. package/dist/modules/user/types.d.ts +7 -8
  44. package/dist/modules/user/types.d.ts.map +1 -1
  45. package/dist/modules/user/userRepository.d.ts +3 -46
  46. package/dist/modules/user/userRepository.d.ts.map +1 -1
  47. package/dist/modules/user/userRepository.js +6 -48
  48. package/dist/modules/user/userRepository.js.map +1 -1
  49. package/dist/modules/user/userService.d.ts +3 -37
  50. package/dist/modules/user/userService.d.ts.map +1 -1
  51. package/dist/modules/user/userService.js +3 -36
  52. package/dist/modules/user/userService.js.map +1 -1
  53. package/package.json +1 -1
@@ -2,14 +2,14 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.createOrderReference = createOrderReference;
4
4
  exports.parseOrderReference = parseOrderReference;
5
- function createOrderReference({ userId, appNamespace, planId, utcDate, }) {
6
- return `miia_${appNamespace}_bot_${userId}_${planId}_v1_${utcDate}`;
5
+ function createOrderReference({ userId, platform, planId, utcDate, }) {
6
+ return `miia_${platform}_bot_${userId}_${planId}_v1_${utcDate}`;
7
7
  }
8
8
  function parseOrderReference(referenceString) {
9
- // `miia_${appNamespace}_bot_${userId}_${planId}_v1_${formattedUtc}`;
10
- const [appName, appNamespace, serviceType, userId, planId, version, timestamp, chargeId] = referenceString.split('_');
9
+ // `miia_${platform}_bot_${userId}_${planId}_v1_${formattedUtc}`;
10
+ const [appName, platform, serviceType, userId, planId, version, timestamp, chargeId] = referenceString.split('_');
11
11
  if (typeof appName !== 'string' ||
12
- typeof appNamespace !== 'string' ||
12
+ typeof platform !== 'string' ||
13
13
  typeof serviceType !== 'string' ||
14
14
  typeof userId !== 'string' ||
15
15
  typeof planId !== 'string' ||
@@ -19,7 +19,7 @@ function parseOrderReference(referenceString) {
19
19
  }
20
20
  return {
21
21
  appName,
22
- appNamespace: appNamespace,
22
+ platform,
23
23
  serviceType,
24
24
  userId,
25
25
  planId,
@@ -1 +1 @@
1
- {"version":3,"file":"utils.js","sourceRoot":"","sources":["../../../src/modules/payments/utils.ts"],"names":[],"mappings":";;AAwDS,oDAAoB;AAAE,kDAAmB;AAtDlD,SAAS,oBAAoB,CAAC,EAC1B,MAAM,EACN,YAAY,EACZ,MAAM,EACN,OAAO,GAMV;IACG,OAAO,QAAQ,YAAY,QAAQ,MAAM,IAAI,MAAM,OAAO,OAAO,EAAE,CAAC;AACxE,CAAC;AAaD,SAAS,mBAAmB,CAAC,eAAuB;IAChD,qEAAqE;IACrE,MAAM,CAAC,OAAO,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,SAAS,EAAE,QAAQ,CAAC,GACpF,eAAe,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IAE/B,IACI,OAAO,OAAO,KAAK,QAAQ;QAC3B,OAAO,YAAY,KAAK,QAAQ;QAChC,OAAO,WAAW,KAAK,QAAQ;QAC/B,OAAO,MAAM,KAAK,QAAQ;QAC1B,OAAO,MAAM,KAAK,QAAQ;QAC1B,OAAO,OAAO,KAAK,QAAQ;QAC3B,OAAO,SAAS,KAAK,QAAQ,EAC/B,CAAC;QACC,OAAO,IAAI,CAAC;IAChB,CAAC;IAED,OAAO;QACH,OAAO;QACP,YAAY,EAAE,YAA4B;QAC1C,WAAW;QACX,MAAM;QACN,MAAM;QACN,OAAO;QACP,SAAS;QACT,QAAQ;KACX,CAAC;AACN,CAAC"}
1
+ {"version":3,"file":"utils.js","sourceRoot":"","sources":["../../../src/modules/payments/utils.ts"],"names":[],"mappings":";;AAqDS,oDAAoB;AAAE,kDAAmB;AArDlD,SAAS,oBAAoB,CAAC,EAC1B,MAAM,EACN,QAAQ,EACR,MAAM,EACN,OAAO,GAMV;IACG,OAAO,QAAQ,QAAQ,QAAQ,MAAM,IAAI,MAAM,OAAO,OAAO,EAAE,CAAC;AACpE,CAAC;AAaD,SAAS,mBAAmB,CAAC,eAAuB;IAChD,iEAAiE;IACjE,MAAM,CAAC,OAAO,EAAE,QAAQ,EAAE,WAAW,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,SAAS,EAAE,QAAQ,CAAC,GAAG,eAAe,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IAElH,IACI,OAAO,OAAO,KAAK,QAAQ;QAC3B,OAAO,QAAQ,KAAK,QAAQ;QAC5B,OAAO,WAAW,KAAK,QAAQ;QAC/B,OAAO,MAAM,KAAK,QAAQ;QAC1B,OAAO,MAAM,KAAK,QAAQ;QAC1B,OAAO,OAAO,KAAK,QAAQ;QAC3B,OAAO,SAAS,KAAK,QAAQ,EAC/B,CAAC;QACC,OAAO,IAAI,CAAC;IAChB,CAAC;IAED,OAAO;QACH,OAAO;QACP,QAAQ;QACR,WAAW;QACX,MAAM;QACN,MAAM;QACN,OAAO;QACP,SAAS;QACT,QAAQ;KACX,CAAC;AACN,CAAC"}
@@ -1,118 +1,40 @@
1
1
  import { FieldValue, Firestore } from 'firebase-admin/firestore';
2
2
  import { SubscriptionEntity, SubscriptionFieldPath, ISubscriptionRepository } from './types';
3
- import { AppNamespace } from '../app/types';
4
3
  export type UpdateDBSubscriptionFields = [
5
4
  SubscriptionFieldPath,
6
5
  FieldValue | string | number | boolean | Date | [] | {}
7
6
  ][];
8
- /**
9
- * SubscriptionRepository class handles all database operations related to subscriptions.
10
- * Implements repository pattern for subscription data access.
11
- */
12
7
  export declare class SubscriptionRepository implements ISubscriptionRepository {
13
8
  private readonly db;
14
9
  private readonly collectionName;
15
- /**
16
- * Creates an instance of SubscriptionRepository.
17
- * @param params - Repository dependencies
18
- * @param params.db - Optional Firestore instance for dependency injection (defaults to getFirestore())
19
- */
20
10
  constructor({ db }?: {
21
11
  db?: Firestore;
22
12
  });
23
- /**
24
- * Retrieves a subscription for a specific user.
25
- * @param params - Query parameters
26
- * @param params.userId - The user's unique identifier
27
- * @param params.appNamespace - The application namespace/platform
28
- * @param params.status - Optional status filter. Can be a single status or array of statuses. Defaults to 'active'
29
- * @returns Promise resolving to SubscriptionEntity or null if not found
30
- */
31
13
  findSubscription(params: {
32
14
  userId: string;
33
- appNamespace: string;
15
+ platform: string;
34
16
  status?: SubscriptionEntity['status'] | SubscriptionEntity['status'][];
35
17
  }): Promise<SubscriptionEntity | null>;
36
- /**
37
- * Creates a new subscription record in the database.
38
- * @param params - Subscription creation parameters
39
- * @param params.userId - The user's unique identifier
40
- * @param params.appNamespace - The application namespace/platform
41
- * @param params.planId - The subscription plan identifier
42
- * @param params.expiresAt - ISO timestamp when subscription expires
43
- * @param params.startedAt - ISO timestamp when subscription started
44
- * @returns Promise resolving to created SubscriptionEntity with ID
45
- * @throws Error if subscription creation fails
46
- */
47
18
  create(params: {
48
19
  userId: string;
49
- appNamespace: AppNamespace;
20
+ platform: string;
50
21
  planId: string;
51
22
  expiresAt: string;
52
23
  startedAt: string;
53
24
  }): Promise<SubscriptionEntity>;
54
- /**
55
- * Updates specific fields of a subscription identified by userId and appNamespace.
56
- * @param params - Update parameters
57
- * @param params.userId - The user's unique identifier
58
- * @param params.appNamespace - The application namespace/platform
59
- * @param params.fields - Array of field paths and values to update
60
- * @throws Error if subscription not found or update fails
61
- */
62
25
  updateFieldsByUserId(params: {
63
26
  userId: string;
64
- appNamespace: AppNamespace;
27
+ platform: string;
65
28
  fields: UpdateDBSubscriptionFields;
66
29
  }): Promise<void>;
67
- /**
68
- * Retrieves all active subscriptions that have expired.
69
- * Used for batch processing to update subscription statuses.
70
- * @returns Promise resolving to array of expired SubscriptionEntity
71
- */
72
30
  getExpiredActiveSubscriptions(): Promise<SubscriptionEntity[]>;
73
- /**
74
- * Updates specific fields of a subscription identified by subscription ID.
75
- * @param params - Update parameters
76
- * @param params.subscriptionId - The subscription document ID
77
- * @param params.fields - Array of field paths and values to update
78
- * @throws Error if update fails
79
- */
80
31
  updateFieldsById(params: {
81
32
  subscriptionId: string;
82
33
  fields: UpdateDBSubscriptionFields;
83
34
  }): Promise<void>;
84
- /**
85
- * Maps a Firestore document to a SubscriptionEntity.
86
- * @param doc - Firestore document snapshot
87
- * @returns SubscriptionEntity with document ID
88
- */
89
35
  private mapDocumentToEntity;
90
- /**
91
- * Activates a subscription for a user using a transaction.
92
- * Creates a new subscription if one doesn't exist, or updates the existing one.
93
- * Also updates the user document to reflect active subscription status.
94
- * @param params - Activation parameters
95
- * @param params.userId - The user's unique identifier
96
- * @param params.appNamespace - The application namespace/platform
97
- * @param params.startedAt - ISO timestamp when subscription period starts
98
- * @param params.expiresAt - ISO timestamp when subscription period ends
99
- * @param params.planId - The subscription plan identifier
100
- * @param params.provider - Payment provider (e.g., 'wayforpay')
101
- * @throws Error if user not found or transaction fails
102
- */
103
- activateSubscription(params: {
104
- userId: string;
105
- appNamespace: AppNamespace;
106
- startedAt: string;
107
- expiresAt: string;
108
- planId: string;
109
- provider: 'wayforpay';
110
- }): Promise<void>;
111
- /**
112
- * Builds an update object from field tuples.
113
- * @param fields - Array of field paths and values
114
- * @returns Object with field paths as keys and values to update
115
- */
36
+ activateSubscription(params: Omit<SubscriptionEntity, 'id' | 'status'>): Promise<void>;
37
+ renewSubscription(params: Pick<SubscriptionEntity, 'userId' | 'platform' | 'expiresAt'>): Promise<void>;
116
38
  private buildUpdateObject;
117
39
  }
118
40
  //# sourceMappingURL=repository.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"repository.d.ts","sourceRoot":"","sources":["../../../src/modules/subscription/repository.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,SAAS,EAAuC,MAAM,0BAA0B,CAAC;AACtG,OAAO,EAAE,kBAAkB,EAAE,qBAAqB,EAAE,uBAAuB,EAAE,MAAM,SAAS,CAAC;AAC7F,OAAO,EAAE,YAAY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AC,kBAAkB,GAAG,IAAI,CAAC;IA6BzB,MAAM,CAAC,MAAM,EAAE;QACxB,MAAM,EAAE,MAAM,CAAC;QACf,QAAQ,EAAE,MAAM,CAAC;QACjB,MAAM,EAAE,MAAM,CAAC;QACf,SAAS,EAAE,MAAM,CAAC;QAClB,SAAS,EAAE,MAAM,CAAC;KACrB,GAAG,OAAO,CAAC,kBAAkB,CAAC;IAqBlB,oBAAoB,CAAC,MAAM,EAAE;QACtC,MAAM,EAAE,MAAM,CAAC;QACf,QAAQ,EAAE,MAAM,CAAC;QACjB,MAAM,EAAE,0BAA0B,CAAC;KACtC,GAAG,OAAO,CAAC,IAAI,CAAC;IAmBJ,6BAA6B,IAAI,OAAO,CAAC,kBAAkB,EAAE,CAAC;IAgB9D,gBAAgB,CAAC,MAAM,EAAE;QAClC,cAAc,EAAE,MAAM,CAAC;QACvB,MAAM,EAAE,0BAA0B,CAAC;KACtC,GAAG,OAAO,CAAC,IAAI,CAAC;IAQjB,OAAO,CAAC,mBAAmB;IAOd,oBAAoB,CAAC,MAAM,EAAE,IAAI,CAAC,kBAAkB,EAAE,IAAI,GAAG,QAAQ,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC;IA2BtF,iBAAiB,CAC1B,MAAM,EAAE,IAAI,CAAC,kBAAkB,EAAE,QAAQ,GAAG,UAAU,GAAG,WAAW,CAAC,GACtE,OAAO,CAAC,IAAI,CAAC;IAsChB,OAAO,CAAC,iBAAiB;CAS5B"}
@@ -3,33 +3,16 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.SubscriptionRepository = void 0;
4
4
  const firestore_1 = require("firebase-admin/firestore");
5
5
  const utils_1 = require("../../utils");
6
- /**
7
- * SubscriptionRepository class handles all database operations related to subscriptions.
8
- * Implements repository pattern for subscription data access.
9
- */
10
6
  class SubscriptionRepository {
11
- /**
12
- * Creates an instance of SubscriptionRepository.
13
- * @param params - Repository dependencies
14
- * @param params.db - Optional Firestore instance for dependency injection (defaults to getFirestore())
15
- */
16
7
  constructor({ db } = {}) {
17
8
  this.collectionName = 'subscriptions';
18
9
  this.db = db || (0, firestore_1.getFirestore)();
19
10
  }
20
- /**
21
- * Retrieves a subscription for a specific user.
22
- * @param params - Query parameters
23
- * @param params.userId - The user's unique identifier
24
- * @param params.appNamespace - The application namespace/platform
25
- * @param params.status - Optional status filter. Can be a single status or array of statuses. Defaults to 'active'
26
- * @returns Promise resolving to SubscriptionEntity or null if not found
27
- */
28
11
  async findSubscription(params) {
29
- const { userId, appNamespace } = params;
12
+ const { userId, platform } = params;
30
13
  let query = this.db
31
14
  .collection(this.collectionName)
32
- .where('platform', '==', appNamespace)
15
+ .where('platform', '==', platform)
33
16
  .where('userId', '==', userId);
34
17
  if (!(0, utils_1.isUndefined)(params.status)) {
35
18
  // Handle status filter
@@ -49,24 +32,13 @@ class SubscriptionRepository {
49
32
  const doc = querySnapshot.docs[0];
50
33
  return this.mapDocumentToEntity(doc);
51
34
  }
52
- /**
53
- * Creates a new subscription record in the database.
54
- * @param params - Subscription creation parameters
55
- * @param params.userId - The user's unique identifier
56
- * @param params.appNamespace - The application namespace/platform
57
- * @param params.planId - The subscription plan identifier
58
- * @param params.expiresAt - ISO timestamp when subscription expires
59
- * @param params.startedAt - ISO timestamp when subscription started
60
- * @returns Promise resolving to created SubscriptionEntity with ID
61
- * @throws Error if subscription creation fails
62
- */
63
35
  async create(params) {
64
- const { userId, appNamespace, planId, expiresAt, startedAt } = params;
36
+ const { userId, platform, planId, expiresAt, startedAt } = params;
65
37
  const docRef = this.db.collection(this.collectionName).doc();
66
38
  const subscriptionEntity = {
67
39
  id: docRef.id,
68
40
  userId,
69
- platform: appNamespace,
41
+ platform,
70
42
  planId,
71
43
  expiresAt,
72
44
  startedAt,
@@ -76,20 +48,12 @@ class SubscriptionRepository {
76
48
  docRef.set(subscriptionEntity);
77
49
  return subscriptionEntity;
78
50
  }
79
- /**
80
- * Updates specific fields of a subscription identified by userId and appNamespace.
81
- * @param params - Update parameters
82
- * @param params.userId - The user's unique identifier
83
- * @param params.appNamespace - The application namespace/platform
84
- * @param params.fields - Array of field paths and values to update
85
- * @throws Error if subscription not found or update fails
86
- */
87
51
  async updateFieldsByUserId(params) {
88
- const { userId, appNamespace, fields } = params;
52
+ const { userId, platform, fields } = params;
89
53
  const updateObject = this.buildUpdateObject(fields);
90
54
  const querySnapshot = await this.db
91
55
  .collection(this.collectionName)
92
- .where('platform', '==', appNamespace)
56
+ .where('platform', '==', platform)
93
57
  .where('userId', '==', userId)
94
58
  .limit(1)
95
59
  .get();
@@ -98,11 +62,6 @@ class SubscriptionRepository {
98
62
  }
99
63
  await querySnapshot.docs[0].ref.update(updateObject);
100
64
  }
101
- /**
102
- * Retrieves all active subscriptions that have expired.
103
- * Used for batch processing to update subscription statuses.
104
- * @returns Promise resolving to array of expired SubscriptionEntity
105
- */
106
65
  async getExpiredActiveSubscriptions() {
107
66
  const now = new Date().toISOString();
108
67
  const querySnapshot = await this.db
@@ -115,80 +74,62 @@ class SubscriptionRepository {
115
74
  }
116
75
  return querySnapshot.docs.map((doc) => this.mapDocumentToEntity(doc));
117
76
  }
118
- /**
119
- * Updates specific fields of a subscription identified by subscription ID.
120
- * @param params - Update parameters
121
- * @param params.subscriptionId - The subscription document ID
122
- * @param params.fields - Array of field paths and values to update
123
- * @throws Error if update fails
124
- */
125
77
  async updateFieldsById(params) {
126
78
  const { subscriptionId, fields } = params;
127
79
  const updateObject = this.buildUpdateObject(fields);
128
80
  await this.db.collection(this.collectionName).doc(subscriptionId).update(updateObject);
129
81
  }
130
- /**
131
- * Maps a Firestore document to a SubscriptionEntity.
132
- * @param doc - Firestore document snapshot
133
- * @returns SubscriptionEntity with document ID
134
- */
135
82
  mapDocumentToEntity(doc) {
136
83
  return {
137
84
  id: doc.id,
138
85
  ...doc.data(),
139
86
  };
140
87
  }
141
- /**
142
- * Activates a subscription for a user using a transaction.
143
- * Creates a new subscription if one doesn't exist, or updates the existing one.
144
- * Also updates the user document to reflect active subscription status.
145
- * @param params - Activation parameters
146
- * @param params.userId - The user's unique identifier
147
- * @param params.appNamespace - The application namespace/platform
148
- * @param params.startedAt - ISO timestamp when subscription period starts
149
- * @param params.expiresAt - ISO timestamp when subscription period ends
150
- * @param params.planId - The subscription plan identifier
151
- * @param params.provider - Payment provider (e.g., 'wayforpay')
152
- * @throws Error if user not found or transaction fails
153
- */
154
88
  async activateSubscription(params) {
155
- const { userId, appNamespace, startedAt, expiresAt, planId, provider } = params;
89
+ const { userId, platform } = params;
156
90
  return await this.db.runTransaction(async (tx) => {
157
91
  // Get user document
158
- const userSnapshot = await tx.get(this.db.collection(`platform/${appNamespace}/users`).doc(userId));
92
+ const userSnapshot = await tx.get(this.db.collection(`platform/${platform}/users`).doc(userId));
93
+ if (!userSnapshot.exists) {
94
+ throw new Error(`User not found: ${userId}`);
95
+ }
96
+ const docRef = this.db.collection(this.collectionName).doc();
97
+ const newSubscription = {
98
+ ...params,
99
+ id: docRef.id,
100
+ status: 'active',
101
+ };
102
+ await tx.create(docRef, newSubscription);
103
+ // Update user document
104
+ await tx.update(userSnapshot.ref, {
105
+ 'subscription.isActive': true,
106
+ 'subscription.isTrial': false,
107
+ });
108
+ });
109
+ }
110
+ async renewSubscription(params) {
111
+ const { userId, platform } = params;
112
+ return await this.db.runTransaction(async (tx) => {
113
+ // Get user document
114
+ const userSnapshot = await tx.get(this.db.collection(`platform/${platform}/users`).doc(userId));
159
115
  // Get subscription document
160
116
  const subscriptionSnapshot = await tx.get(this.db
161
117
  .collection(this.collectionName)
162
118
  .where('userId', '==', userId)
163
- .where('platform', '==', appNamespace)
119
+ .where('platform', '==', platform)
120
+ .where('status', '==', 'active')
164
121
  .limit(1));
165
- if (!userSnapshot.exists) {
166
- throw new Error(`User not found: ${userId}`);
167
- }
168
122
  if (subscriptionSnapshot.empty) {
169
- // Create new subscription
170
- const subscriptionEntity = {
171
- planId,
172
- expiresAt,
173
- status: 'active',
174
- platform: appNamespace,
175
- provider,
176
- startedAt,
177
- userId,
178
- };
179
- const docRef = this.db.collection(this.collectionName).doc();
180
- await tx.create(docRef, {
181
- id: docRef.id,
182
- ...subscriptionEntity,
183
- });
123
+ throw new Error(`Subscription not found for userId: ${userId}`);
184
124
  }
185
- else {
186
- // Update existing subscription
187
- await tx.update(subscriptionSnapshot.docs[0].ref, {
188
- expiresAt,
189
- status: 'active',
190
- });
125
+ if (!userSnapshot.exists) {
126
+ throw new Error(`User not found: ${userId}`);
191
127
  }
128
+ const updateObject = {
129
+ status: 'active',
130
+ expiresAt: params.expiresAt,
131
+ };
132
+ await tx.update(subscriptionSnapshot.docs[0].ref, updateObject);
192
133
  // Update user document
193
134
  await tx.update(userSnapshot.ref, {
194
135
  'subscription.isActive': true,
@@ -196,11 +137,6 @@ class SubscriptionRepository {
196
137
  });
197
138
  });
198
139
  }
199
- /**
200
- * Builds an update object from field tuples.
201
- * @param fields - Array of field paths and values
202
- * @returns Object with field paths as keys and values to update
203
- */
204
140
  buildUpdateObject(fields) {
205
141
  const updateObject = {};
206
142
  fields.forEach(([fieldPath, value]) => {
@@ -1 +1 @@
1
- {"version":3,"file":"repository.js","sourceRoot":"","sources":["../../../src/modules/subscription/repository.ts"],"names":[],"mappings":";;;AAAA,wDAAsG;AAGtG,uCAA0C;AAO1C;;;GAGG;AACH,MAAa,sBAAsB;IAI/B;;;;OAIG;IACH,YAAY,EAAE,EAAE,KAAyB,EAAE;QAP1B,mBAAc,GAAG,eAAe,CAAC;QAQ9C,IAAI,CAAC,EAAE,GAAG,EAAE,IAAI,IAAA,wBAAY,GAAE,CAAC;IACnC,CAAC;IAED;;;;;;;OAOG;IACI,KAAK,CAAC,gBAAgB,CAAC,MAI7B;QACG,MAAM,EAAE,MAAM,EAAE,YAAY,EAAE,GAAG,MAAM,CAAC;QAExC,IAAI,KAAK,GAAG,IAAI,CAAC,EAAE;aACd,UAAU,CAAC,IAAI,CAAC,cAAc,CAAC;aAC/B,KAAK,CAAC,UAAU,EAAE,IAAI,EAAE,YAAY,CAAC;a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,YAAY,CAAC;IACxB,CAAC;CACJ;AAzMD,wDAyMC"}
@@ -1,105 +1,41 @@
1
1
  import { Logger } from '../logger/types';
2
2
  import { UpdateDBSubscriptionFields } from './repository';
3
3
  import { SubscriptionEntity, ISubscriptionRepository, ISubscriptionService } from './types';
4
- import { AppNamespace } from '../app/types';
5
4
  import { IPaymentService } from '../payments/types';
6
- /**
7
- * SubscriptionService class handles business logic related to subscriptions.
8
- * Acts as an intermediary between controllers/handlers and the repository layer.
9
- */
10
5
  export declare class SubscriptionService implements ISubscriptionService {
11
6
  private readonly logger;
12
7
  private readonly repository;
13
8
  private readonly paymentService;
14
- /**
15
- * Creates an instance of SubscriptionService.
16
- * @param params - Service dependencies
17
- * @param params.logger - Application logger instance for logging operations
18
- * @param params.repository - Optional subscription repository instance for dependency injection (defaults to new SubscriptionRepository())
19
- * @param params.paymentService - Optional payment service instance for dependency injection (defaults to new PaymentService())
20
- */
21
9
  constructor({ logger, repository, paymentService, }: {
22
10
  logger: Logger;
23
11
  repository?: ISubscriptionRepository;
24
12
  paymentService?: IPaymentService;
25
13
  });
26
- /**
27
- * Retrieves a subscription for a specific user.
28
- * @param params - Query parameters
29
- * @param params.userId - The user's unique identifier
30
- * @param params.appNamespace - The application namespace/platform
31
- * @param params.status - Optional status filter. Can be a single status or array of statuses. Defaults to 'active'
32
- * @returns Promise resolving to SubscriptionEntity or null if not found
33
- */
34
14
  findSubscription(params: {
35
15
  userId: string;
36
- appNamespace: string;
16
+ platform: string;
37
17
  status?: SubscriptionEntity['status'] | SubscriptionEntity['status'][];
38
18
  }): Promise<SubscriptionEntity | null>;
39
- /**
40
- * Creates a new subscription record in the database.
41
- * @param params - Subscription creation parameters
42
- * @param params.userId - The user's unique identifier
43
- * @param params.appNamespace - The application namespace/platform
44
- * @param params.planId - The subscription plan identifier
45
- * @param params.expiresAt - ISO timestamp when subscription expires
46
- * @param params.startedAt - ISO timestamp when subscription started
47
- * @returns Promise resolving to created SubscriptionEntity with ID
48
- * @throws Error if subscription creation fails
49
- */
50
19
  create(params: {
51
20
  userId: string;
52
- appNamespace: AppNamespace;
21
+ platform: string;
53
22
  planId: string;
54
23
  expiresAt: string;
55
24
  startedAt: string;
56
25
  }): Promise<SubscriptionEntity>;
57
- /**
58
- * Updates specific fields of a subscription identified by userId and appNamespace.
59
- * @param params - Update parameters
60
- * @param params.userId - The user's unique identifier
61
- * @param params.appNamespace - The application namespace/platform
62
- * @param params.fields - Array of field paths and values to update
63
- * @throws Error if subscription not found or update fails
64
- */
65
26
  updateFieldsByUserId(params: {
66
27
  userId: string;
67
- appNamespace: AppNamespace;
28
+ platform: string;
68
29
  fields: UpdateDBSubscriptionFields;
69
30
  }): Promise<void>;
70
- /**
71
- * Retrieves all active subscriptions that have expired.
72
- * Used for batch processing to update subscription statuses.
73
- * @returns Promise resolving to array of expired SubscriptionEntity
74
- */
75
31
  getExpiredActiveSubscriptions(): Promise<SubscriptionEntity[]>;
76
- /**
77
- * Updates specific fields of a subscription identified by subscription ID.
78
- * @param params - Update parameters
79
- * @param params.subscriptionId - The subscription document ID
80
- * @param params.fields - Array of field paths and values to update
81
- * @throws Error if update fails
82
- */
83
32
  updateFieldsById(params: {
84
33
  subscriptionId: string;
85
34
  fields: UpdateDBSubscriptionFields;
86
35
  }): Promise<void>;
87
- /**
88
- * Gets or creates a subscription payment URL.
89
- * Checks for recent pending payments (within 3 minutes) and reuses the payment link if found.
90
- * Otherwise creates a new payment intent for the subscription.
91
- * @param params - Payment URL creation parameters
92
- * @param params.userId - The user's unique identifier
93
- * @param params.appNamespace - The application namespace/platform
94
- * @param params.planId - The subscription plan identifier
95
- * @param params.languageCode - Language code for translations
96
- * @param params.translate - Translation function
97
- * @returns Promise resolving to the payment URL string
98
- * @throws Error if subscription plan not found or payment creation fails
99
- */
100
36
  getOrCreateSubscriptionPaymentUrl(params: {
101
37
  userId: string;
102
- appNamespace: AppNamespace;
38
+ platform: string;
103
39
  planId: string;
104
40
  languageCode: string;
105
41
  translate: (params: {
@@ -107,26 +43,7 @@ export declare class SubscriptionService implements ISubscriptionService {
107
43
  path: string;
108
44
  }) => string;
109
45
  }): Promise<string>;
110
- /**
111
- * Activates a subscription for a user.
112
- * This operation is transactional - it creates/updates the subscription document
113
- * and updates the user's subscription status atomically.
114
- * @param params - Activation parameters
115
- * @param params.userId - The user's unique identifier
116
- * @param params.appNamespace - The application namespace/platform
117
- * @param params.startedAt - ISO timestamp when subscription period starts
118
- * @param params.expiresAt - ISO timestamp when subscription period ends
119
- * @param params.planId - The subscription plan identifier
120
- * @param params.provider - Payment provider (e.g., 'wayforpay')
121
- * @throws Error if user not found or activation fails
122
- */
123
- activateSubscription(params: {
124
- userId: string;
125
- appNamespace: AppNamespace;
126
- startedAt: string;
127
- expiresAt: string;
128
- planId: string;
129
- provider: 'wayforpay';
130
- }): Promise<void>;
46
+ activateSubscription(params: Omit<SubscriptionEntity, 'id' | 'status'>): Promise<void>;
47
+ renewSubscription(params: Pick<SubscriptionEntity, 'userId' | 'platform' | 'expiresAt'>): Promise<void>;
131
48
  }
132
49
  //# sourceMappingURL=service.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"service.d.ts","sourceRoot":"","sources":["../../../src/modules/subscription/service.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAC;AACzC,OAAO,EAA0B,0BAA0B,EAAE,MAAM,cAAc,CAAC;AAClF,OAAO,EAAE,kBAAkB,EAAE,uBAAuB,EAAE,oBAAoB,EAAE,MAAM,SAAS,CAAC;AAC5F,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAG5C,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAEpD;;;GAGG;AACH,qBAAa,mBAAoB,YAAW,oBAAoB;IAC5D,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAS;IAChC,OAAO,CAAC,QAAQ,CAAC,UAAU,CAA0B;IACrD,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAk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