Skip to content

Commit

Permalink
feat(subscription-hydration): set effective cancellation date based o…
Browse files Browse the repository at this point in the history
…n last payment
  • Loading branch information
stfsy committed Jan 7, 2023
1 parent 5d0d90e commit 931d9e6
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
31 changes: 25 additions & 6 deletions lib/subscription-hydration.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,10 @@ class SubscriptionHydration {
*
* @param {Array} ids ids pointing to the target subscription object
* @param {Object} subscription the current local subscription status instance
* @param {String} checkoutId checkout id of paddle.com^^
* @throws Error if hydration failed unexepectedly
* @returns
*/
async hydrateSubscriptionCancelled(ids, { subscription_id }, checkoutId) {
async hydrateSubscriptionCancelled(ids, { subscription_id }) {
const subscriptions = await this._api.getSubscription({ subscription_id })

if (!Array.isArray(subscriptions) || subscriptions.length < 1) {
Expand All @@ -218,6 +217,28 @@ class SubscriptionHydration {
throw new Error(SubscriptionHydration.HYDRATION_UNAUTHORIZED)
}

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

const signUpDate = new Date(subscription.signup_date)
const lastPaymentDate = new Date(subscription.last_payment.date)
let theoreticalNextPaymentDate = new Date(lastPaymentDate.getTime())

// we use the day of sign up as base
// and add one day until to the current date
// until next payment + 1 === sign up day
while (theoreticalNextPaymentDate.getDate() !== signUpDate.getDate() - 1) {
theoreticalNextPaymentDate.setTime(theoreticalNextPaymentDate.getTime() + 1000 * 60 * 60 * 24)
}

theoreticalNextPaymentDate.setUTCHours(23)
theoreticalNextPaymentDate.setUTCMinutes(59)
theoreticalNextPaymentDate.setUTCSeconds(59)
theoreticalNextPaymentDate.setUTCMilliseconds(0)
const subscriptionValidUntil = theoreticalNextPaymentDate.toISOString()

const hookPayload = {
alert_id: SubscriptionHydration.HYDRATION_SUBSCRIPTION_CANCELLED,
alert_name: SubscriptionHydration.HYDRATION_SUBSCRIPTION_CANCELLED,
Expand All @@ -235,10 +256,8 @@ class SubscriptionHydration {
passthrough: JSON.stringify({ '_pi': { ids: idsFromCustomData } })
}

if (subscription.state === 'deleted') {
hookPayload.cancellation_effective_date = new Date().toISOString()
await this._hookStorage.addSubscriptionCancelledStatus(hookPayload)
}
hookPayload.cancellation_effective_date = subscriptionValidUntil
await this._hookStorage.addSubscriptionCancelledStatus(hookPayload)
}
}

Expand Down
17 changes: 15 additions & 2 deletions test-e2e/spec/hydration.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ test('hydrate a deleted subscription', async ({ page }) => {
let { subscription } = await storage.get([apiClientId])

try {
await subscriptionInfo.cancelSubscription(subscription, '33590')
await api.cancelSubscription(order)
await page.waitForTimeout(10000)
} catch (e) {
if (e.message !== index.SubscriptionInfo.ERROR_SUBSCRIPTION_ALREADY_CANCELLED) {
Expand Down Expand Up @@ -260,11 +260,24 @@ test('hydrate a deleted subscription', async ({ page }) => {
expect(status.description).toEqual('deleted')
expect(status.next_bill_date).toBeUndefined()
expect(status.quantity).toEqual('')
expect(new Date(status.event_time).getTime()).toBeGreaterThanOrEqual(Date.now() - 2000)
expect(status.update_url).toBeUndefined()
expect(status.subscription_id).toEqual(subscriptionFromApi.subscription_id)
expect(status.subscription_plan_id).toEqual(subscriptionFromApi.plan_id)
expect(status.cancel_url).toBeUndefined()
expect(status.checkout_id).toEqual('checkoutId')
expect(status.vendor_user_id).toEqual(subscriptionFromApi.user_id)

const signUpDate = new Date(subscriptionFromApi.signup_date)
const expectedEndDate = new Date(signUpDate.getTime())

while (expectedEndDate.getDate() !== signUpDate.getDate() - 1) {
expectedEndDate.setTime(expectedEndDate.getTime() + 1000 * 60 * 60 * 24)
}

expectedEndDate.setUTCHours(23)
expectedEndDate.setUTCMinutes(59)
expectedEndDate.setUTCSeconds(59)
expectedEndDate.setUTCMilliseconds(0)

expect(new Date(status.event_time).toISOString()).toEqual(expectedEndDate.toISOString())
})

0 comments on commit 931d9e6

Please sign in to comment.