Skip to content

Commit 872caa5

Browse files
committed
feat(subscription-info): check whether sub was already hydrated
1 parent 3ebd4fd commit 872caa5

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

lib/subscription-info.js

+9-5
Original file line numberDiff line numberDiff line change
@@ -436,14 +436,18 @@ class SubscriptionInfo {
436436
* to already store subscription-related data and let the user already enjoy some goodies.
437437
*
438438
* @param {Array} ids ids pointing to the target subscription object
439-
* @param {Object} subscription the current local subscription instance
439+
* @param {Object} subscription the current local subscription status instance
440440
* @param {String} checkoutId checkout id of paddle.com
441441
* @throws Error if hydration failed unexepectedly
442442
* @returns
443443
*/
444-
async hydrateSubscriptionCreated(ids, { subscription_id, ...localSubscriptionInstance }, checkoutId) {
445-
if (localSubscriptionInstance.status.length > 1) {
446-
return
444+
async hydrateSubscriptionCreated(ids, { subscription_id }, checkoutId) {
445+
{
446+
const { subscription } = await this._storage.get(ids)
447+
448+
if (subscription.status.length > 1) {
449+
return
450+
}
447451
}
448452

449453
const subscriptions = await this._api.getSubscription({ subscription_id })
@@ -494,7 +498,7 @@ class SubscriptionInfo {
494498
* because paddle API does not return when the subscription will actually end.
495499
*
496500
* @param {Array} ids ids pointing to the target subscription object
497-
* @param {Object} subscription the current local subscription instance
501+
* @param {Object} subscription the current local subscription status instance
498502
* @param {String} checkoutId checkout id of paddle.com^^
499503
* @throws Error if hydration failed unexepectedly
500504
* @returns

test-e2e/spec/hydration.spec.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ test('hydrate an active subscription', async ({ page }) => {
9090
await createNewSubscription(page)
9191

9292
let { subscription } = await storage.get(['4815162342'])
93+
const subscriptionId = subscription.status[1].subscription_id
9394

9495
// .. and check subscription is active to make sure setup was correct
9596
let sub = await subscriptionInfo.getAllSubscriptionsStatus(subscription)
@@ -107,7 +108,7 @@ test('hydrate an active subscription', async ({ page }) => {
107108
expect(sub['33590']).toBeFalsy()
108109

109110
// .. now hydrate status again ..
110-
await subscriptionInfo.hydrateSubscriptionCreated(['4815162342'], subscription, 'checkoutId');
111+
await subscriptionInfo.hydrateSubscriptionCreated(['4815162342'], { subscription_id: subscriptionId }, 'checkoutId');
111112

112113
// .. and expect subscription to be active again
113114
({ subscription } = await storage.get(['4815162342']))
@@ -120,13 +121,14 @@ test('does not hydrate if status created was already received', async ({ page })
120121
await createNewSubscription(page)
121122

122123
let { subscription } = await storage.get(['4815162342'])
124+
const subscriptionId = subscription.status[1].subscription_id
123125

124126
// .. and check subscription is active to make sure setup was correct
125127
let sub = await subscriptionInfo.getAllSubscriptionsStatus(subscription)
126128
expect(sub['33590']).toBeTruthy()
127129

128130
// .. now hydrate status again ..
129-
await subscriptionInfo.hydrateSubscriptionCreated(['4815162342'], subscription, 'checkoutId');
131+
await subscriptionInfo.hydrateSubscriptionCreated(['4815162342'], { subscription_id: subscriptionId }, 'checkoutId');
130132

131133
// .. and expect subscription to be active again
132134
({ subscription } = await storage.get(['4815162342']))
@@ -138,6 +140,7 @@ test('hydrate a deleted subscription', async ({ page }) => {
138140
await createNewSubscription(page)
139141

140142
let { subscription } = await storage.get(['4815162342'])
143+
const subscriptionId = subscription.status[1].subscription_id
141144

142145
// .. and check subscription is active to make sure setup was correct
143146
let sub = await subscriptionInfo.getAllSubscriptionsStatus(subscription)
@@ -163,8 +166,10 @@ test('hydrate a deleted subscription', async ({ page }) => {
163166
'subscription.payments': []
164167
})
165168

169+
console.log(JSON.stringify(subscription, null, 2))
170+
166171
// .. now hydrate status again ..
167-
await subscriptionInfo.hydrateSubscriptionCancelled(['4815162342'], subscription, 'checkoutId');
172+
await subscriptionInfo.hydrateSubscriptionCancelled(['4815162342'], { subscription_id: subscriptionId }, 'checkoutId');
168173

169174
// .. and expect subscription to be active again
170175
({ subscription } = await storage.get(['4815162342']))

0 commit comments

Comments
 (0)