Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MNTOR-3897: add reactivate after apply coupon #5571

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ import { Button } from "../../../../../components/client/Button";
import type { checkChurnCouponCode } from "../../../../../functions/server/applyCoupon";
import { CONST_URL_PLUS_CONTACT_SUPPORT } from "../../../../../../constants";
import { CloseBtn, OpenInNew } from "../../../../../components/server/Icons";
import type { applyRenewalCoupon } from "./actions";
import type { applyRenewalCoupon, reactivateSubscriber } from "./actions";
import { useL10n } from "../../../../../hooks/l10n";

export type Props = {
subscriber: SubscriberRow;
couponCheckResult: Awaited<ReturnType<typeof checkChurnCouponCode>>;
applyCouponAction: typeof applyRenewalCoupon;
reactivateSubscriberAction: typeof reactivateSubscriber;
manageSubscriptionsUrl: string;
isOnExpirationList: boolean;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { getSubscriberByFxaUid } from "../../../../../../db/tables/subscribers";
import { applyChurnCouponCode } from "../../../../../functions/server/applyCoupon";
import { getServerSession } from "../../../../../functions/server/getServerSession";
import { logger } from "../../../../../functions/server/logging";

import { reactivateAccount } from "../../../../../functions/server/reactivateAccount";
export type ApplyRenewalCouponResult =
| {
success: false;
Expand All @@ -20,6 +20,16 @@ export type ApplyRenewalCouponResult =
}
| { success: true };

export type ReactivateResult =
| {
success: false;
error:
| "reactivate_subscriber_no_subscriber"
| "reactivate_subscriber_error"
| "reactivate_subscriber_no_subscriber_for_session";
}
| { success: true };

export async function applyRenewalCoupon(): Promise<ApplyRenewalCouponResult> {
const session = await getServerSession();
if (typeof session?.user.subscriber?.fxa_uid !== "string") {
Expand All @@ -39,9 +49,36 @@ export async function applyRenewalCoupon(): Promise<ApplyRenewalCouponResult> {
}
try {
await applyChurnCouponCode(subscriber);
await reactivateAccount(subscriber);
return { success: true };
} catch (e) {
logger.error("apply_renewal_coupon_error", { error: e });
return { error: "apply_renewal_coupon_error", success: false };
}
}

export async function reactivateSubscriber(): Promise<ReactivateResult> {
const session = await getServerSession();
if (typeof session?.user.subscriber?.fxa_uid !== "string") {
logger.error("reactivate_subscriber_no_subscriber");
return { error: "reactivate_subscriber_no_subscriber", success: false };
}

const subscriber = await getSubscriberByFxaUid(
session.user.subscriber?.fxa_uid,
);
if (!subscriber) {
logger.warn("reactivate_subscriber_no_subscriber_for_session");
return {
error: "reactivate_subscriber_no_subscriber_for_session",
success: false,
};
}
try {
await reactivateAccount(subscriber);
return { success: true };
} catch (e) {
logger.error("reactivate_subscriber_error", { error: e });
return { error: "reactivate_subscriber_error", success: false };
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { getServerSession } from "../../../../../functions/server/getServerSessi
import { View } from "./View";
import { logger } from "../../../../../functions/server/logging";
import { checkChurnCouponCode } from "../../../../../functions/server/applyCoupon";
import { applyRenewalCoupon } from "./actions";
import { applyRenewalCoupon, reactivateSubscriber } from "./actions";
import { getEnabledFeatureFlags } from "../../../../../../db/tables/featureFlags";
import { getChurnsToEmail } from "../../../../../../db/tables/subscriber_churns";

Expand Down Expand Up @@ -48,6 +48,7 @@ export default async function PlusExpirationPage() {
subscriber={subscriber}
couponCheckResult={couponCheckResult}
applyCouponAction={applyRenewalCoupon}
reactivateSubscriberAction={reactivateSubscriber}
manageSubscriptionsUrl={process.env.FXA_SUBSCRIPTIONS_URL!}
isOnExpirationList={
typeof subscribersToEmail.find(
Expand Down
43 changes: 43 additions & 0 deletions src/app/functions/server/reactivateAccount.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

import { SubscriberRow } from "knex/types/tables";
import { logger } from "./logging";
import { reactivate } from "../../../utils/fxa";
import { record } from "./glean";

export async function reactivateAccount(subscriber: SubscriberRow) {
logger.info("fxa_reactivate_user", {
subscriber: subscriber.id,
});

record("account", "reactivate", {
string: {
monitorUserId: subscriber.id.toString(),
},
});

// try to reactivate from subplat
if (subscriber.fxa_access_token) {
try {
await reactivate(subscriber.fxa_access_token);
logger.info("reactivate_from_subplat", {
subscriber_id: subscriber.id,
success: true,
});
} catch (ex) {
logger.error("reactivate_from_subplat", {
subscriber_id: subscriber.id,
exception: ex,
});
throw ex;
}
} else {
logger.error("reactivate_from_subplat_no_bearer_token", {
subscriber_id: subscriber.id,
exception: "Subscriber does not have permission or missing token",
});
throw new Error("Subscriber does not have permission or missing token");
}
}
5 changes: 4 additions & 1 deletion src/utils/fxa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,10 @@ async function applyCoupon(
}
} catch (e) {
if (e instanceof Error) {
logger.error("apply_fxa_coupon", { stack: e.stack, message: e.message });
logger.error(
"apply_fxa_coupon",
JSON.stringify({ stack: e.stack, message: e.message }),
);
}
throw e;
}
Expand Down
Loading