From 8d2b5938eeffecb512c69d5528a2aa34f0a058d5 Mon Sep 17 00:00:00 2001 From: Joey Zhou Date: Tue, 7 Jan 2025 18:50:27 -0800 Subject: [PATCH] feat: add new reactivate endpoint wrapper --- src/utils/fxa.ts | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/src/utils/fxa.ts b/src/utils/fxa.ts index a7ea81b1a80..0a17abbaae7 100644 --- a/src/utils/fxa.ts +++ b/src/utils/fxa.ts @@ -304,6 +304,50 @@ async function deleteSubscription(bearerToken: string): Promise { } /* c8 ignore stop */ +// Not covered by tests; mostly side-effects. See test-coverage.md#mock-heavy +/* c8 ignore start */ +async function reactivate(bearerToken: string): Promise { + try { + const subs = (await getSubscriptions(bearerToken)) ?? []; + let subscriptionId; + for (const sub of subs) { + if ( + sub && + sub.productId && + sub.productId === process.env.PREMIUM_PRODUCT_ID + ) { + subscriptionId = sub.subscriptionId; + } + } + if (subscriptionId) { + const reactivateSubscriptionUrl = `${envVars.OAUTH_ACCOUNT_URI}/oauth/subscriptions/reactivate`; + const response = await fetch(reactivateSubscriptionUrl, { + method: "POST", + headers: { + "Content-Type": "application/json", + Accept: "application/json", + Authorization: `Bearer ${bearerToken}`, + }, + body: JSON.stringify({ + subscriptionId, + }), + }); + const responseJson = await response.json(); + if (!response.ok) throw new Error(responseJson); + logger.info("reactivate_fxa_subscription_success"); + } + } catch (e) { + if (e instanceof Error) { + logger.error("reactivate_fxa_subscription", { + stack: e.stack, + message: e.message, + }); + } + throw e; + } +} +/* c8 ignore stop */ + // Not covered by tests; mostly side-effects. See test-coverage.md#mock-heavy /* c8 ignore start */ async function applyCoupon( @@ -418,6 +462,7 @@ export { getSubscriptions, getBillingAndSubscriptions, deleteSubscription, + reactivate, applyCoupon, getAttachedClients, };