Skip to content

Commit

Permalink
feat: add new reactivate endpoint wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
mansaj committed Jan 8, 2025
1 parent 780dfd4 commit 8d2b593
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/utils/fxa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,50 @@ async function deleteSubscription(bearerToken: string): Promise<boolean> {
}
/* 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<void> {
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(
Expand Down Expand Up @@ -418,6 +462,7 @@ export {
getSubscriptions,
getBillingAndSubscriptions,
deleteSubscription,
reactivate,
applyCoupon,
getAttachedClients,
};

0 comments on commit 8d2b593

Please sign in to comment.