Skip to content

Commit

Permalink
feat: add method to compile status trail
Browse files Browse the repository at this point in the history
  • Loading branch information
stfsy committed Sep 9, 2022
1 parent 8b90029 commit 5fe2b5f
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 2 deletions.
22 changes: 21 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ class Subscriptions {
/**
*
* @param {Object} subscription
* @returns {StartAndEndDate} containing the start and end date
* @returns {Array<Object>} containing the start and end date
*/
async getPaymentsTrail(subscription) {
const payments = subscription.payments
Expand Down Expand Up @@ -338,6 +338,26 @@ class Subscriptions {
})
}

/**
*
* @param {Object} subscription
* @returns {Array<Object>} containing the start and end date
*/
async getStatusTrail(subscription) {
const status = subscription.status
.sort((a, b) => new Date(a.start_at).getTime() - new Date(b.start_at).getTime())

// skip placeholder
status.shift()
return status.map(status => {
return {
start_at: status.cancellation_effective_date || status.start_at,
description: status.description,
type: status.alert_name
}
})
}

/**
*
* @param {Array} subscriptions
Expand Down
44 changes: 43 additions & 1 deletion test/spec/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ describe('PaddleIntegration', () => {
})
})

describe('.getPaymentsTrail', () => {
describe('.getStatusTrail', () => {
beforeEach(async () => {
const subscriptionId = uuid()
const createPayload = Object.assign({}, subscriptionCreated, {
Expand Down Expand Up @@ -484,4 +484,46 @@ describe('PaddleIntegration', () => {
expect(trail[2].subscription_plan_id).to.equal(paymentSucceded.subscription_plan_id)
})
})
describe('.getStatusTrail', () => {
beforeEach(async () => {
const subscriptionId = uuid()

const createPayload = Object.assign({}, subscriptionCreated,
{
event_time: '2024-08-08 10:47:47',
subscription_id: subscriptionId, passthrough: JSON.stringify({ ids })
}
)
await paddleIntegration.addSubscriptionCreatedStatus(createPayload)

const updatePayload = Object.assign({}, subscriptionUpdated,
{
event_time: '2026-08-08 10:47:47',
subscription_id: subscriptionId, passthrough: JSON.stringify({ ids })
}
)
await paddleIntegration.addSubscriptionUpdatedStatus(updatePayload)

const cancelPayload = Object.assign({}, subscriptionCancelled,
{
subscription_id: subscriptionId,
passthrough: JSON.stringify({ ids }),
cancellation_effective_date: '2028-08-08 10:47:47',
}
)
await paddleIntegration.addSubscriptionCancelledStatus(cancelPayload)
})
it('returns a sorted listed of payments', async () => {
const { subscription: sub } = await storage.get(ids)
const trail = await paddleIntegration.getStatusTrail(sub)

expect(trail).to.have.length(3)
expect(trail[0].type).to.equal('subscription_created')
expect(trail[0].description).to.equal('active')
expect(trail[1].type).to.equal('subscription_updated')
expect(trail[1].description).to.equal('active')
expect(trail[2].type).to.equal('subscription_cancelled')
expect(trail[2].description).to.equal('deleted')
})
})
})

0 comments on commit 5fe2b5f

Please sign in to comment.