Skip to content

Commit 3acc96f

Browse files
committed
feat(subscription-info): add update subscription plan method
1 parent 6231ce6 commit 3acc96f

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed

lib/subscription-info.js

+22
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,28 @@ class SubscriptionInfo {
102102
return false
103103
}
104104

105+
/**
106+
* Will update the subscription plan of the given subscription. The actual subscription id
107+
* will be looked at at runtime by peaking at all subscription events. The current plan will
108+
* be cancelled in favor of the new one.
109+
*
110+
* @param {Object} subscription
111+
* @param {String} currentSubscriptionPlanId the current plan id to be terminated
112+
* @param {String} newSubscriptionPlanId the new plan id
113+
* @returns
114+
*/
115+
async updateSubscription(subscription, currentSubscriptionPlanId, newSubscriptionPlanId) {
116+
const subscriptionId = await this._findActiveSubscriptionIdByPlanId(subscription, currentSubscriptionPlanId)
117+
118+
try {
119+
const response = await this._api.updateSubscriptionPlan({ subscription_id: subscriptionId }, newSubscriptionPlanId)
120+
return response.subscription_id !== undefined
121+
} catch (e) {
122+
console.error(`Failed to update subscription because of: ${e}`)
123+
}
124+
return false
125+
}
126+
105127
/**
106128
* Finds the id of an active subscription by peaking at the status events.
107129
*

test-e2e/spec/subscription.spec.js

+34
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,40 @@ test('test cancel via api', async ({ page }) => {
203203
expect(sub['33590']).toBeFalsy()
204204
})
205205

206+
test('test update via subscription info', async ({ page }) => {
207+
// create new subscription and ...
208+
await createNewSubscription(page)
209+
210+
// .. check it was stored and payment status was received ..
211+
let { subscription } = await storage.get(['4815162342'])
212+
expect(subscription).not.toBeFalsy()
213+
expect(subscription.status).toHaveLength(2)
214+
expect(subscription.payments).toHaveLength(1)
215+
216+
validateStatus(subscription.status[1])
217+
218+
// .. and check it is active
219+
let sub = await subscriptionInfo.getAllSubscriptionsStatus(subscription)
220+
expect(sub['33590']).toBeTruthy()
221+
222+
// update subscription plan via api ...
223+
const updated = await subscriptionInfo.updateSubscription(subscription, '33590', '35141')
224+
expect(updated).toBeTruthy()
225+
await page.waitForTimeout(30000);
226+
227+
// .. check new status and payments added ...
228+
({ subscription } = await storage.get(['4815162342']))
229+
expect(subscription).not.toBeFalsy()
230+
expect(subscription.status).toHaveLength(4)
231+
expect(subscription.payments).toHaveLength(2)
232+
233+
// .. and still active
234+
sub = await subscriptionInfo.getAllSubscriptionsStatus(subscription)
235+
console.log('active subs', { sub })
236+
expect(sub['35141']).toBeTruthy()
237+
expect(sub['33590']).toBeFalsy()
238+
})
239+
206240
test('test update via api', async ({ page }) => {
207241
// create new subscription and ...
208242
await createNewSubscription(page)

test/spec/subscription-info.spec.js

+45
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,51 @@ describe('SubscriptionInfo', () => {
220220
}
221221
})
222222
})
223+
224+
describe('.updateSubscription', () => {
225+
beforeEach(async () => {
226+
const subscriptionId = uuid()
227+
const createPayload = Object.assign({}, subscriptionCreated,
228+
{
229+
subscription_id: subscriptionId,
230+
passthrough: JSON.stringify({ ids }),
231+
event_time: new Date().toISOString()
232+
}
233+
)
234+
await subscriptions.addSubscriptionCreatedStatus(createPayload)
235+
})
236+
it('throws if no subscription with plan was found', async () => {
237+
const { subscription: sub } = await storage.get(ids)
238+
239+
try {
240+
await subscriptionInfo.updateSubscription(sub, '99', '123')
241+
throw new Error('Method must throw not found')
242+
} catch (e) {
243+
const message = e.message
244+
expect(message).to.equal(SubscriptionInfo.ERROR_MESSAGE_NOT_FOUND)
245+
}
246+
})
247+
it('throws if subscription was already cancelled', async () => {
248+
const payload = Object.assign({}, subscriptionCancelled,
249+
{
250+
subscription_id: 'subscriptionId',
251+
passthrough: JSON.stringify({ ids }),
252+
cancellation_effective_date: new Date(new Date().getTime()).toISOString()
253+
}
254+
)
255+
256+
await subscriptions.addSubscriptionCancelledStatus(payload)
257+
const { subscription: sub } = await storage.get(ids)
258+
259+
try {
260+
await subscriptionInfo.updateSubscription(sub, '8', '123')
261+
throw new Error('Method must throw "SubscriptionInfo.ERROR_SUBSCRIPTION_ALREADY_CANCELLED"')
262+
} catch (e) {
263+
const message = e.message
264+
expect(message).to.equal(SubscriptionInfo.ERROR_SUBSCRIPTION_ALREADY_CANCELLED)
265+
}
266+
})
267+
})
223268

224269
describe('.getAllSubscriptionsStatus', () => {
225270
it('takes the most recent status into account', async () => {

0 commit comments

Comments
 (0)