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,
|
wordlistDbName: T.WordlistDbName,
|
||||||
couchDbPassword: T.UserDbPassword,
|
couchDbPassword: T.UserDbPassword,
|
||||||
upgradeToStudentRequest: undefined,
|
upgradeToStudentRequest: undefined,
|
||||||
|
subscriptionId: string | undefined,
|
||||||
} |
|
} |
|
||||||
{
|
{
|
||||||
level: "basic",
|
level: "basic",
|
||||||
|
|
|
@ -28,6 +28,7 @@ const student: AT.LingdocsUser = {
|
||||||
"lastActive":1630414108552 as AT.TimeStamp,
|
"lastActive":1630414108552 as AT.TimeStamp,
|
||||||
"couchDbPassword": "12345" as AT.UserDbPassword,
|
"couchDbPassword": "12345" as AT.UserDbPassword,
|
||||||
"wordlistDbName": "jim-db" as AT.WordlistDbName,
|
"wordlistDbName": "jim-db" as AT.WordlistDbName,
|
||||||
|
subscriptionId: undefined,
|
||||||
};
|
};
|
||||||
|
|
||||||
const admin: AT.LingdocsUser = {
|
const admin: AT.LingdocsUser = {
|
||||||
|
@ -45,6 +46,7 @@ const admin: AT.LingdocsUser = {
|
||||||
"lastActive":1630414108552 as AT.TimeStamp,
|
"lastActive":1630414108552 as AT.TimeStamp,
|
||||||
"couchDbPassword": "12345" as AT.UserDbPassword,
|
"couchDbPassword": "12345" as AT.UserDbPassword,
|
||||||
"wordlistDbName": "jim-db" as AT.WordlistDbName,
|
"wordlistDbName": "jim-db" as AT.WordlistDbName,
|
||||||
|
subscriptionId: undefined,
|
||||||
};
|
};
|
||||||
|
|
||||||
const editor: AT.LingdocsUser = {
|
const editor: AT.LingdocsUser = {
|
||||||
|
@ -61,6 +63,7 @@ const editor: AT.LingdocsUser = {
|
||||||
"lastActive":1630414108552 as AT.TimeStamp,
|
"lastActive":1630414108552 as AT.TimeStamp,
|
||||||
"couchDbPassword": "12345" as AT.UserDbPassword,
|
"couchDbPassword": "12345" as AT.UserDbPassword,
|
||||||
"wordlistDbName": "jim-db" as AT.WordlistDbName,
|
"wordlistDbName": "jim-db" as AT.WordlistDbName,
|
||||||
|
subscriptionId: undefined,
|
||||||
};
|
};
|
||||||
|
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
|
|
|
@ -16,6 +16,12 @@ import {
|
||||||
} from "../lib/mail-utils";
|
} from "../lib/mail-utils";
|
||||||
import { outsideProviders } from "../middleware/setup-passport";
|
import { outsideProviders } from "../middleware/setup-passport";
|
||||||
import * as T from "../../../website/src/types/account-types";
|
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 {
|
function getUUID(): T.UUID {
|
||||||
return uuidv4() as 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
|
// add user to couchdb authentication db
|
||||||
const { password, userDbName } = await addCouchDbAuthUser(userId);
|
const { password, userDbName } = await addCouchDbAuthUser(userId);
|
||||||
// // create user db
|
// // create user db
|
||||||
|
@ -61,6 +67,7 @@ export async function upgradeUser(userId: T.UUID): Promise<T.UpgradeUserResponse
|
||||||
wordlistDbName: userDbName,
|
wordlistDbName: userDbName,
|
||||||
couchDbPassword: password,
|
couchDbPassword: password,
|
||||||
upgradeToStudentRequest: undefined,
|
upgradeToStudentRequest: undefined,
|
||||||
|
subscriptionId,
|
||||||
});
|
});
|
||||||
if (user.email) {
|
if (user.email) {
|
||||||
sendAccountUpgradeMessage(user).catch(console.error);
|
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);
|
await deleteCouchDbAuthUser(userId);
|
||||||
|
if (subscriptionId) {
|
||||||
|
stripe.subscriptions.del(subscriptionId);
|
||||||
|
}
|
||||||
const user = await updateLingdocsUser(userId, {
|
const user = await updateLingdocsUser(userId, {
|
||||||
level: "basic",
|
level: "basic",
|
||||||
wordlistDbName: undefined,
|
wordlistDbName: undefined,
|
||||||
|
|
|
@ -196,7 +196,9 @@ const authRouter = (passport: PassportStatic) => {
|
||||||
if (!req.user) {
|
if (!req.user) {
|
||||||
return res.redirect("/");
|
return res.redirect("/");
|
||||||
}
|
}
|
||||||
await downgradeUser(req.user.userId);
|
await downgradeUser(req.user.userId, "subscriptionId" in req.user
|
||||||
|
? req.user.subscriptionId
|
||||||
|
: undefined);
|
||||||
res.redirect("/");
|
res.redirect("/");
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
next(e);
|
next(e);
|
||||||
|
|
|
@ -54,7 +54,7 @@ paymentRouter.post(
|
||||||
subscription = event.data.object;
|
subscription = event.data.object;
|
||||||
status = subscription.status;
|
status = subscription.status;
|
||||||
console.log(`Upgrading user ${userId}.`);
|
console.log(`Upgrading user ${userId}.`);
|
||||||
await upgradeUser(userId);
|
await upgradeUser(userId, subscription.id);
|
||||||
// TODO: save subscription to db
|
// TODO: save subscription to db
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
|
|
@ -56,6 +56,7 @@ export type LingdocsUser = {
|
||||||
level: "student" | "editor",
|
level: "student" | "editor",
|
||||||
couchDbPassword: UserDbPassword,
|
couchDbPassword: UserDbPassword,
|
||||||
wordlistDbName: WordlistDbName,
|
wordlistDbName: WordlistDbName,
|
||||||
|
subscriptionId: string | undefined,
|
||||||
}
|
}
|
||||||
) & import("nano").MaybeDocument;
|
) & import("nano").MaybeDocument;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue