From 3713ad399f96d4936c2867d684f979e9396c4489 Mon Sep 17 00:00:00 2001 From: Eric Bailey Date: Wed, 30 Oct 2024 09:46:39 -0500 Subject: [PATCH] Fix logic --- subs-mock/src/index.ts | 62 +++++++++++++++++++++--------------------- subs-mock/src/util.ts | 16 +++++------ 2 files changed, 38 insertions(+), 40 deletions(-) diff --git a/subs-mock/src/index.ts b/subs-mock/src/index.ts index fcfb8558b3..c0c7757787 100644 --- a/subs-mock/src/index.ts +++ b/subs-mock/src/index.ts @@ -59,6 +59,37 @@ export default new Hono<{Bindings: Bindings; Variables: Variables}>() await next() }) + /** + * Update a subscription (web only) + */ + .post(`/subscriptions/update`, async c => { + const stripe = c.get('stripe') + const {stripeSubscriptionId, newPriceId} = await c.req.json() + const subscriptionItem = await stripe.subscriptionItems.retrieve( + stripeSubscriptionId, + ) + // TODO get customer and compare DID to auth state to ensure it's legit + const subscription = await stripe.subscriptions.update( + subscriptionItem.subscription, + { + items: [ + { + id: stripeSubscriptionId, + deleted: true, + }, + { + price: newPriceId, + }, + ], + }, + ) + + return c.json({ + oldItem: subscriptionItem, + subscription, + }) + }) + /** * Get subscription products for a given offering */ @@ -155,37 +186,6 @@ export default new Hono<{Bindings: Bindings; Variables: Variables}>() }) }) - /** - * Update a subscription (web only) - */ - .post(`/subscriptions/update`, async c => { - const stripe = c.get('stripe') - const {stripeSubscriptionItemId, newPriceId} = await c.req.json() - const subscriptionItem = await stripe.subscriptionItems.retrieve( - stripeSubscriptionItemId, - ) - // TODO get customer and compare DID to auth state to ensure it's legit - const subscription = await stripe.subscriptions.update( - subscriptionItem.subscription, - { - items: [ - { - id: stripeSubscriptionItemId, - deleted: true, - }, - { - price: newPriceId, - }, - ], - }, - ) - - return c.json({ - oldItem: subscriptionItem, - subscription, - }) - }) - /** * Get entitlements for a user */ diff --git a/subs-mock/src/util.ts b/subs-mock/src/util.ts index d730772388..5eaf2bcd27 100644 --- a/subs-mock/src/util.ts +++ b/subs-mock/src/util.ts @@ -246,14 +246,13 @@ export function mergeSubscriptions( ): Subscription[] { const merged: Subscription[] = [] - for (const a of active) { - for (const s of all) { - if (a.id === s.id) { - // @ts-ignore - merged.push({...s, state: a.state}) - } else { - merged.push(s) - } + for (const s of all) { + const a = active.find(p => p.id === s.id) + if (a) { + // @ts-ignore + merged.push({...s, state: a.state}) + } else { + merged.push(s) } } @@ -294,7 +293,6 @@ export function getSubscriptionState( } export function getSubscriptionPlatform(id: string): 'web' | 'android' | 'ios' { - console.log(id) switch (id) { case 'prod_R2eNjNa6mB1Jlu': case 'prod_R37Zg28EeQ9XAz':