Skip to content

Commit

Permalink
feat(subscription-hydration): require plan id instead of subscription…
Browse files Browse the repository at this point in the history
… id param
  • Loading branch information
stfsy committed Jan 8, 2023
1 parent 7e9e228 commit 48394ce
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 23 deletions.
9 changes: 5 additions & 4 deletions lib/subscription-hydration.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,12 +187,13 @@ class SubscriptionHydration {
* because paddle API does not return when the subscription will actually end.
*
* @param {Array} ids ids pointing to the target subscription object
* @param {Object} subscription the current local subscription status instance
* @param {String} planId the target plan that was cancelled
* @throws Error if hydration failed unexepectedly
* @returns
*/
async hydrateSubscriptionCancelled(ids, { subscription_id }) {
const subscriptions = await this._api.getSubscription({ subscription_id })
async hydrateSubscriptionCancelled(ids, planId) {
const subscriptionId = await this._subscriptionInfo.findSubscriptionIdByPlanId(ids, planId)
const subscriptions = await this._api.getSubscription({ subscription_id: String(subscriptionId) })

if (!Array.isArray(subscriptions) || subscriptions.length < 1) {
return
Expand All @@ -218,7 +219,7 @@ class SubscriptionHydration {
}

if (subscription.state !== 'deleted') {
console.error(`Subscription ${subscription_id} of client ${ids} is still active. Refusing to hydrate cancellation.`)
console.error(`Subscription ${subscriptionId} of client ${ids} is still active. Refusing to hydrate cancellation.`)
throw new Error(SubscriptionHydration.HYDRATION_BAD_REQUEST)
}

Expand Down
25 changes: 6 additions & 19 deletions test-e2e/spec/hydration.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ test('does not hydrate if status created was already received', async ({ page })
expect(sub['33590']).toBeTruthy()

// .. now hydrate status again ..
await subscriptionHydration.hydrateSubscriptionCreated([apiClientId], { subscription_id: subscriptionId }, 'checkoutId');
await subscriptionHydration.hydrateSubscriptionCreated([apiClientId], '33590');

// .. and expect subscription to be active again
({ subscription } = await storage.get([apiClientId]))
Expand All @@ -214,7 +214,6 @@ test('hydrate a deleted subscription', async ({ page }) => {
// create new subscription and ...
const result = await createNewSubscription(page, apiClientId)
const { order } = result
const { subscription_id: subscriptionId } = order

let { subscription } = await storage.get([apiClientId])

Expand All @@ -227,32 +226,20 @@ test('hydrate a deleted subscription', async ({ page }) => {
}
}

({ subscription } = await storage.get([apiClientId]))
// .. expect sub to be not active anymore in the future
let sub = await subscriptionInfo.getAllSubscriptionsStatus(subscription, new Date(new Date().getTime() + 1000 * 3600 * 24 * 35))
expect(sub['33590']).toBeFalsy()

// remove status and payments to verify hydration process
await storage.update([apiClientId], {
'subscription.status': [],
'subscription.payments': []
})

// .. now hydrate status again ..
await subscriptionHydration.hydrateSubscriptionCancelled([apiClientId], { subscription_id: subscriptionId }, 'checkoutId');
await subscriptionHydration.hydrateSubscriptionCancelled([apiClientId], '33590');

// .. and expect subscription to be active again
({ subscription } = await storage.get([apiClientId]))
sub = await subscriptionInfo.getAllSubscriptionsStatus(subscription)
let sub = await subscriptionInfo.getAllSubscriptionsStatus(subscription, new Date(new Date().getTime() + 1000 * 3600 * 24 * 35))
expect(sub['33590']).toBeFalsy()

const { status: subscriptionStatus } = subscription
expect(subscriptionStatus).toHaveLength(1)
expect(subscriptionStatus).toHaveLength(4)

const subscriptionsFromApi = await api.getSubscription(subscription.status.at(0))
const subscriptionsFromApi = await api.getSubscription(subscription.status.at(-1))
const subscriptionFromApi = subscriptionsFromApi.at(0)

const status = subscriptionStatus.at(0)
const status = subscriptionStatus.at(-1)

expect(status.alert_id).toEqual(index.SubscriptionInfo.HYDRATION_SUBSCRIPTION_CANCELLED)
expect(status.alert_name).toEqual(index.SubscriptionInfo.HYDRATION_SUBSCRIPTION_CANCELLED)
Expand Down

0 comments on commit 48394ce

Please sign in to comment.