Skip to content
This repository was archived by the owner on Apr 8, 2024. It is now read-only.

Commit db8e775

Browse files
committed
feat: add get start and end date method
1 parent 14d6e58 commit db8e775

File tree

2 files changed

+74
-1
lines changed

2 files changed

+74
-1
lines changed

lib/index.js

+28-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const resource = require('./firestore/nested-firestore-resource')
44
const htmlEncode = require('./html-encoder')
55
const flattenObject = require('./firestore/flatten')
6+
const PLACEHOLDER_DESCRIPTION = 'pre-checkout-placeholder'
67

78
const SUBSCRIPTION_ACTIVE_STATUS = [
89
"active",
@@ -99,7 +100,7 @@ class Subscriptions {
99100
*/
100101
async addSubscriptionPlaceholder(ids) {
101102
const statusModel = {
102-
description: 'pre-checkout-placeholder',
103+
description: PLACEHOLDER_DESCRIPTION
103104
}
104105

105106
const subscriptionModel = {
@@ -232,6 +233,32 @@ class Subscriptions {
232233
return this._isOneOfSubscriptionsActive([subscription], atDate)
233234
}
234235

236+
/**
237+
*
238+
* @param {Object} subscription
239+
* @returns {Object} containing the start and end date
240+
*/
241+
async getStartAndEndDates(subscription) {
242+
const status = subscription.status
243+
.sort((a, b) => new Date(a.start_at).getTime() - new Date(b.start_at).getTime())
244+
245+
const first = status.find((s) => s.description !== PLACEHOLDER_DESCRIPTION)
246+
let last = status.at(-1)
247+
248+
if (first.alert_id === last.alert_id) {
249+
last == null
250+
}
251+
252+
const start = first.start_at
253+
let end = null
254+
255+
if (last && !this._isSubscriptionStatusCurrentlyActive(status.at(-1))) {
256+
end = status.at(-1).start_at
257+
}
258+
259+
return { start, end }
260+
}
261+
235262
/**
236263
*
237264
* @param {Array} subscriptions

test/spec/index.spec.js

+46
Original file line numberDiff line numberDiff line change
@@ -364,4 +364,50 @@ describe('PaddleIntegration', () => {
364364
expect(isActive).to.be.false
365365
})
366366
})
367+
368+
describe('.getStartAndEndDates', () => {
369+
it('returns only start date if theres no end date', async () => {
370+
const subscriptionId = uuid()
371+
const startTimeString = new Date().toISOString()
372+
const createPayload = Object.assign({}, subscriptionCreated,
373+
{
374+
subscription_id: subscriptionId, passthrough: JSON.stringify({ ids }),
375+
event_time: startTimeString
376+
}
377+
)
378+
await paddleIntegration.addSubscriptionCreatedStatus(createPayload)
379+
380+
const { subscription: sub } = await storage.get(ids)
381+
const { start, end } = await paddleIntegration.getStartAndEndDates(sub)
382+
expect(start).to.equal(startTimeString)
383+
expect(end).to.be.null
384+
})
385+
it('returns start and end date', async () => {
386+
const subscriptionId = uuid()
387+
const startTimeString = new Date().toISOString()
388+
const endTimeString = new Date(new Date().getTime() + 1000 * 3600 * 24 * 33).toISOString()
389+
390+
const createPayload = Object.assign({}, subscriptionCreated,
391+
{
392+
subscription_id: subscriptionId, passthrough: JSON.stringify({ ids }),
393+
event_time: startTimeString
394+
}
395+
)
396+
await paddleIntegration.addSubscriptionCreatedStatus(createPayload)
397+
398+
const payload = Object.assign({}, subscriptionCancelled,
399+
{
400+
subscription_id: subscriptionId,
401+
passthrough: JSON.stringify({ ids }),
402+
cancellation_effective_date: endTimeString
403+
}
404+
)
405+
await paddleIntegration.addSubscriptionCancelledStatus(payload)
406+
407+
const { subscription: sub } = await storage.get(ids)
408+
const { start, end } = await paddleIntegration.getStartAndEndDates(sub)
409+
expect(start).to.equal(startTimeString)
410+
expect(end).to.equal(endTimeString)
411+
})
412+
})
367413
})

0 commit comments

Comments
 (0)