deleting subscriptions
This commit is contained in:
parent
e93b059dad
commit
00ff565a81
|
@ -106,6 +106,7 @@ export async function updateLingdocsUser(uuid: T.UUID, toUpdate:
|
|||
wordlistDbName: T.WordlistDbName,
|
||||
couchDbPassword: T.UserDbPassword,
|
||||
upgradeToStudentRequest: undefined,
|
||||
subscriptionId: string | undefined,
|
||||
} |
|
||||
{
|
||||
level: "basic",
|
||||
|
|
|
@ -28,6 +28,7 @@ const student: AT.LingdocsUser = {
|
|||
"lastActive":1630414108552 as AT.TimeStamp,
|
||||
"couchDbPassword": "12345" as AT.UserDbPassword,
|
||||
"wordlistDbName": "jim-db" as AT.WordlistDbName,
|
||||
subscriptionId: undefined,
|
||||
};
|
||||
|
||||
const admin: AT.LingdocsUser = {
|
||||
|
@ -45,6 +46,7 @@ const admin: AT.LingdocsUser = {
|
|||
"lastActive":1630414108552 as AT.TimeStamp,
|
||||
"couchDbPassword": "12345" as AT.UserDbPassword,
|
||||
"wordlistDbName": "jim-db" as AT.WordlistDbName,
|
||||
subscriptionId: undefined,
|
||||
};
|
||||
|
||||
const editor: AT.LingdocsUser = {
|
||||
|
@ -61,6 +63,7 @@ const editor: AT.LingdocsUser = {
|
|||
"lastActive":1630414108552 as AT.TimeStamp,
|
||||
"couchDbPassword": "12345" as AT.UserDbPassword,
|
||||
"wordlistDbName": "jim-db" as AT.WordlistDbName,
|
||||
subscriptionId: undefined,
|
||||
};
|
||||
|
||||
// @ts-ignore
|
||||
|
|
|
@ -16,6 +16,12 @@ import {
|
|||
} from "../lib/mail-utils";
|
||||
import { outsideProviders } from "../middleware/setup-passport";
|
||||
import * as T from "../../../website/src/types/account-types";
|
||||
import env from "../lib/env-vars";
|
||||
import Stripe from "stripe";
|
||||
|
||||
const stripe = new Stripe(env.stripeSecretKey, {
|
||||
apiVersion: "2022-08-01",
|
||||
});
|
||||
|
||||
function getUUID(): T.UUID {
|
||||
return uuidv4() as T.UUID;
|
||||
|
@ -51,7 +57,7 @@ export function getEmailFromGoogleProfile(profile: T.GoogleProfile): { email: st
|
|||
};
|
||||
}
|
||||
|
||||
export async function upgradeUser(userId: T.UUID): Promise<T.UpgradeUserResponse> {
|
||||
export async function upgradeUser(userId: T.UUID, subscriptionId?: string): Promise<T.UpgradeUserResponse> {
|
||||
// add user to couchdb authentication db
|
||||
const { password, userDbName } = await addCouchDbAuthUser(userId);
|
||||
// // create user db
|
||||
|
@ -61,6 +67,7 @@ export async function upgradeUser(userId: T.UUID): Promise<T.UpgradeUserResponse
|
|||
wordlistDbName: userDbName,
|
||||
couchDbPassword: password,
|
||||
upgradeToStudentRequest: undefined,
|
||||
subscriptionId,
|
||||
});
|
||||
if (user.email) {
|
||||
sendAccountUpgradeMessage(user).catch(console.error);
|
||||
|
@ -72,8 +79,11 @@ export async function upgradeUser(userId: T.UUID): Promise<T.UpgradeUserResponse
|
|||
};
|
||||
}
|
||||
|
||||
export async function downgradeUser(userId: T.UUID): Promise<T.DowngradeUserResponse> {
|
||||
export async function downgradeUser(userId: T.UUID, subscriptionId?: string): Promise<T.DowngradeUserResponse> {
|
||||
await deleteCouchDbAuthUser(userId);
|
||||
if (subscriptionId) {
|
||||
stripe.subscriptions.del(subscriptionId);
|
||||
}
|
||||
const user = await updateLingdocsUser(userId, {
|
||||
level: "basic",
|
||||
wordlistDbName: undefined,
|
||||
|
|
|
@ -196,7 +196,9 @@ const authRouter = (passport: PassportStatic) => {
|
|||
if (!req.user) {
|
||||
return res.redirect("/");
|
||||
}
|
||||
await downgradeUser(req.user.userId);
|
||||
await downgradeUser(req.user.userId, "subscriptionId" in req.user
|
||||
? req.user.subscriptionId
|
||||
: undefined);
|
||||
res.redirect("/");
|
||||
} catch (e) {
|
||||
next(e);
|
||||
|
|
|
@ -54,7 +54,7 @@ paymentRouter.post(
|
|||
subscription = event.data.object;
|
||||
status = subscription.status;
|
||||
console.log(`Upgrading user ${userId}.`);
|
||||
await upgradeUser(userId);
|
||||
await upgradeUser(userId, subscription.id);
|
||||
// TODO: save subscription to db
|
||||
break;
|
||||
default:
|
||||
|
|
|
@ -56,6 +56,7 @@ export type LingdocsUser = {
|
|||
level: "student" | "editor",
|
||||
couchDbPassword: UserDbPassword,
|
||||
wordlistDbName: WordlistDbName,
|
||||
subscriptionId: string | undefined,
|
||||
}
|
||||
) & import("nano").MaybeDocument;
|
||||
|
||||
|
|
Loading…
Reference in New Issue