Skip to content

Commit 5fe2b5f

Browse files
committed
feat: add method to compile status trail
1 parent 8b90029 commit 5fe2b5f

File tree

2 files changed

+64
-2
lines changed

2 files changed

+64
-2
lines changed

lib/index.js

+21-1
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ class Subscriptions {
268268
/**
269269
*
270270
* @param {Object} subscription
271-
* @returns {StartAndEndDate} containing the start and end date
271+
* @returns {Array<Object>} containing the start and end date
272272
*/
273273
async getPaymentsTrail(subscription) {
274274
const payments = subscription.payments
@@ -338,6 +338,26 @@ class Subscriptions {
338338
})
339339
}
340340

341+
/**
342+
*
343+
* @param {Object} subscription
344+
* @returns {Array<Object>} containing the start and end date
345+
*/
346+
async getStatusTrail(subscription) {
347+
const status = subscription.status
348+
.sort((a, b) => new Date(a.start_at).getTime() - new Date(b.start_at).getTime())
349+
350+
// skip placeholder
351+
status.shift()
352+
return status.map(status => {
353+
return {
354+
start_at: status.cancellation_effective_date || status.start_at,
355+
description: status.description,
356+
type: status.alert_name
357+
}
358+
})
359+
}
360+
341361
/**
342362
*
343363
* @param {Array} subscriptions

test/spec/index.spec.js

+43-1
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ describe('PaddleIntegration', () => {
411411
})
412412
})
413413

414-
describe('.getPaymentsTrail', () => {
414+
describe('.getStatusTrail', () => {
415415
beforeEach(async () => {
416416
const subscriptionId = uuid()
417417
const createPayload = Object.assign({}, subscriptionCreated, {
@@ -484,4 +484,46 @@ describe('PaddleIntegration', () => {
484484
expect(trail[2].subscription_plan_id).to.equal(paymentSucceded.subscription_plan_id)
485485
})
486486
})
487+
describe('.getStatusTrail', () => {
488+
beforeEach(async () => {
489+
const subscriptionId = uuid()
490+
491+
const createPayload = Object.assign({}, subscriptionCreated,
492+
{
493+
event_time: '2024-08-08 10:47:47',
494+
subscription_id: subscriptionId, passthrough: JSON.stringify({ ids })
495+
}
496+
)
497+
await paddleIntegration.addSubscriptionCreatedStatus(createPayload)
498+
499+
const updatePayload = Object.assign({}, subscriptionUpdated,
500+
{
501+
event_time: '2026-08-08 10:47:47',
502+
subscription_id: subscriptionId, passthrough: JSON.stringify({ ids })
503+
}
504+
)
505+
await paddleIntegration.addSubscriptionUpdatedStatus(updatePayload)
506+
507+
const cancelPayload = Object.assign({}, subscriptionCancelled,
508+
{
509+
subscription_id: subscriptionId,
510+
passthrough: JSON.stringify({ ids }),
511+
cancellation_effective_date: '2028-08-08 10:47:47',
512+
}
513+
)
514+
await paddleIntegration.addSubscriptionCancelledStatus(cancelPayload)
515+
})
516+
it('returns a sorted listed of payments', async () => {
517+
const { subscription: sub } = await storage.get(ids)
518+
const trail = await paddleIntegration.getStatusTrail(sub)
519+
520+
expect(trail).to.have.length(3)
521+
expect(trail[0].type).to.equal('subscription_created')
522+
expect(trail[0].description).to.equal('active')
523+
expect(trail[1].type).to.equal('subscription_updated')
524+
expect(trail[1].description).to.equal('active')
525+
expect(trail[2].type).to.equal('subscription_cancelled')
526+
expect(trail[2].description).to.equal('deleted')
527+
})
528+
})
487529
})

0 commit comments

Comments
 (0)