@@ -97,7 +97,27 @@ class Subscriptions {
97
97
/**
98
98
* Adds a placeholder for a subscription so that webhooks only need to append to existing subscription
99
99
*
100
- * @param {Array<String> } ids
100
+ * The `ids` parameter must point to an existing document in a collection or subcollection. The number
101
+ * of necessary elements in the `ids` array corresponds to the number of resources/collections that were
102
+ * passed to the constructor
103
+ *
104
+ * @example <caption>Subscriptions stored in a collection called api_clients</caption>
105
+ * const { Subscriptions } = require('@discue/paddle-firebase-integration')
106
+ * const subscriptions = new Subscriptions('api_clients')
107
+ * // assuming api_client with id 4815162342 exists we can simple play an array with one element
108
+ * const { passthrough } = await subscriptions.addSubscriptionPlaceholder(['4815162342'])
109
+
110
+ * @example <caption>Subscriptions stored in a subcollection called external</caption>
111
+ * const { Subscriptions } = require('@discue/paddle-firebase-integration')
112
+ *
113
+ * const subscriptions = new Subscriptions('api_clients/external')
114
+ * // as subscriptions are stored in a sub collection we need to pass the id of the parent and the child document
115
+ * // 4815162342: api_client id
116
+ * // 123: external client id
117
+ * const { passthrough } = await subscriptions.addSubscriptionPlaceholder(['4815162342', '123'])
118
+ *
119
+ * @param {Array<String> } ids ids to the target document
120
+ * @returns {object } an object containing the `passthrough` parameter required to correlate webhooks with this subscription
101
121
*/
102
122
async addSubscriptionPlaceholder ( ids ) {
103
123
const statusModel = {
@@ -123,6 +143,9 @@ class Subscriptions {
123
143
}
124
144
125
145
/**
146
+ * Add an subscription created event. Uses the `passthrough` parameter to identify the target subscription.
147
+ *
148
+ * You can directly pass the payload of the respective `subscription_created` webhook.
126
149
*
127
150
* @param {SubscriptionCreatedPayload } subscription
128
151
*/
@@ -160,6 +183,9 @@ class Subscriptions {
160
183
}
161
184
162
185
/**
186
+ * Add an subscription updated event. Uses the `passthrough` parameter to identify the target subscription.
187
+ *
188
+ * You can directly pass the payload of the respective `subscription_updated` webhook.
163
189
*
164
190
* @param {SubscriptionUpdatedPayload } subscription
165
191
*/
@@ -196,6 +222,9 @@ class Subscriptions {
196
222
}
197
223
198
224
/**
225
+ * Add an subscription cancelled event. Uses the `passthrough` parameter to identify the target subscription.
226
+ *
227
+ * You can directly pass the payload of the respective `subscription_cancelled` webhook.
199
228
*
200
229
* @param {SubscriptionCancelledPayload } subscription
201
230
*/
@@ -226,23 +255,14 @@ class Subscriptions {
226
255
await this . _storage . update ( ids , flatModel )
227
256
}
228
257
229
- /**
230
- *
231
- * @param {Object } subscription
232
- * @param {Date } [atDate=new Date()] date for the calculation
233
- * @returns {boolean } true subscription is active
234
- */
235
- async isSubscriptionActive ( subscription , atDate = new Date ( ) ) {
236
- return this . _isOneOfSubscriptionsActive ( subscription , atDate )
237
- }
238
-
239
258
/**
240
259
* @typedef {Object } StartAndEndDate
241
260
* @property {String } start - subscription start date as ISO formatted string
242
261
* @property {String } start - subscription end date as ISO formatted string
243
262
*/
244
263
245
264
/**
265
+ * Returns start and end dates for all subscription plans found in the document.
246
266
*
247
267
* @param {Object } subscription
248
268
* @returns {StartAndEndDate } containing the start and end date
@@ -255,9 +275,11 @@ class Subscriptions {
255
275
return context
256
276
} , { } )
257
277
}
258
-
278
+
259
279
/**
280
+ * Returns start and end dates for the given list of status objects.
260
281
*
282
+ * @private
261
283
* @param {Object } subscription
262
284
* @returns {StartAndEndDate } containing the start and end date
263
285
*/
@@ -283,6 +305,7 @@ class Subscriptions {
283
305
}
284
306
285
307
/**
308
+ * Returns a list of payments for all subscription plans found in the document.
286
309
*
287
310
* @param {Object } subscription
288
311
* @returns {Object }
@@ -297,7 +320,9 @@ class Subscriptions {
297
320
}
298
321
299
322
/**
323
+ * Returns a list of payments sorted by event_time descending.
300
324
*
325
+ * @private
301
326
* @param {Object } subscription
302
327
* @returns {Array<Object> } containing the start and end date
303
328
*/
@@ -369,6 +394,7 @@ class Subscriptions {
369
394
}
370
395
371
396
/**
397
+ * Returns a list of update and changes events for all subscription plans found in the document.
372
398
*
373
399
* @param {Object } subscription
374
400
* @returns {Object }
@@ -383,6 +409,7 @@ class Subscriptions {
383
409
}
384
410
385
411
/**
412
+ * Returns a list of update events sorted by event_time descending.
386
413
*
387
414
* @param {Array<Object> } status
388
415
* @returns {Array<Object> } containing the start and end date
@@ -400,10 +427,17 @@ class Subscriptions {
400
427
}
401
428
402
429
/**
430
+ * Returns the status of each subscription found in the document.
431
+ *
432
+ * For each found subscription the method will add a boolean value
433
+ * indicating the subscription plan is active (true) or not active (false).
434
+ *
435
+ * Unless the second parameter is passed, the status will always be calculated
436
+ * using the current time
403
437
*
404
438
* @param {Object } subscription
405
439
* @param {Date } [atDate=new Date()] date for the calculation
406
- * @returns {boolean } true if an active subscription was given
440
+ * @returns {Object } true if an active subscription was given
407
441
*/
408
442
async getAllSubscriptionsStatus ( subscriptions , atDate = new Date ( ) ) {
409
443
const result = { }
@@ -422,6 +456,7 @@ class Subscriptions {
422
456
423
457
/**
424
458
*
459
+ * @private
425
460
* @param {Array<Object> } statusOrPayments
426
461
* @param {Number } validUntilMillis
427
462
* @returns
@@ -440,18 +475,24 @@ class Subscriptions {
440
475
}
441
476
442
477
/**
443
- *
444
478
* Returns true if the given status has a description that we recognize as active
445
- *
446
- * <strong>Status must be decrypted</strong>
447
- *
479
+ *
480
+ * @private
448
481
* @param {Object } activeStatus
449
482
* @returns {Boolean } true or false
450
483
*/
451
484
_isSubscriptionStatusCurrentlyActive ( status ) {
452
485
return SUBSCRIPTION_ACTIVE_STATUS . includes ( status . description )
453
486
}
454
487
488
+ /**
489
+ * Add a payment event. Uses the `passthrough` parameter to identify the target subscription.
490
+ *
491
+ * You can directly pass the payload of the respective `subscription_payment_succeeded` webhook.
492
+ *
493
+ * @param {Object } payment
494
+ * @returns
495
+ */
455
496
async addSuccessfulPayment ( payment ) {
456
497
if ( ! payment ) {
457
498
return Promise . reject ( new Error ( this . ERROR_INVALID_ARGUMENTS ) )
@@ -470,6 +511,14 @@ class Subscriptions {
470
511
await this . _storage . update ( ids , flatModel )
471
512
}
472
513
514
+ /**
515
+ * Add a payment event. Uses the `passthrough` parameter to identify the target subscription.
516
+ *
517
+ * You can directly pass the payload of the respective `subscription_payment_failed` webhook.
518
+ *
519
+ * @param {Object } payment
520
+ * @returns
521
+ */
473
522
async addFailedPayment ( payment ) {
474
523
if ( ! payment ) {
475
524
return Promise . reject ( new Error ( this . ERROR_INVALID_ARGUMENTS ) )
@@ -488,6 +537,14 @@ class Subscriptions {
488
537
await this . _storage . update ( ids , flatModel )
489
538
}
490
539
540
+ /**
541
+ * Add a payment event. Uses the `passthrough` parameter to identify the target subscription.
542
+ *
543
+ * You can directly pass the payload of the respective `subscription_payment_refunded` webhook.
544
+ *
545
+ * @param {Object } payment
546
+ * @returns
547
+ */
491
548
async addRefundedPayment ( payment ) {
492
549
if ( ! payment ) {
493
550
return Promise . reject ( new Error ( this . ERROR_INVALID_ARGUMENTS ) )
0 commit comments