upgrade on purchase - and starting to work on the downgrade (not there yet)
This commit is contained in:
parent
2de4a45ba2
commit
3917fde3c2
|
@ -107,6 +107,12 @@ export async function updateLingdocsUser(uuid: T.UUID, toUpdate:
|
||||||
couchDbPassword: T.UserDbPassword,
|
couchDbPassword: T.UserDbPassword,
|
||||||
upgradeToStudentRequest: undefined,
|
upgradeToStudentRequest: undefined,
|
||||||
} |
|
} |
|
||||||
|
{
|
||||||
|
level: "basic",
|
||||||
|
wordlistDbName: undefined,
|
||||||
|
couchDbPassword: undefined,
|
||||||
|
upgradeToStudentRequest: undefined,
|
||||||
|
} |
|
||||||
{ upgradeToStudentRequest: "waiting" } |
|
{ upgradeToStudentRequest: "waiting" } |
|
||||||
{ upgradeToStudentRequest: "denied" } |
|
{ upgradeToStudentRequest: "denied" } |
|
||||||
{ tests: T.TestResult[] }
|
{ tests: T.TestResult[] }
|
||||||
|
|
|
@ -3,6 +3,7 @@ import {
|
||||||
insertLingdocsUser,
|
insertLingdocsUser,
|
||||||
addCouchDbAuthUser,
|
addCouchDbAuthUser,
|
||||||
updateLingdocsUser,
|
updateLingdocsUser,
|
||||||
|
deleteCouchDbAuthUser,
|
||||||
} from "../lib/couch-db";
|
} from "../lib/couch-db";
|
||||||
import {
|
import {
|
||||||
getHash,
|
getHash,
|
||||||
|
@ -71,6 +72,25 @@ export async function upgradeUser(userId: T.UUID): Promise<T.UpgradeUserResponse
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function downgradeUser(userId: T.UUID): Promise<T.DowngradeUserResponse> {
|
||||||
|
await deleteCouchDbAuthUser(userId);
|
||||||
|
const user = await updateLingdocsUser(userId, {
|
||||||
|
level: "basic",
|
||||||
|
wordlistDbName: undefined,
|
||||||
|
couchDbPassword: undefined,
|
||||||
|
upgradeToStudentRequest: undefined,
|
||||||
|
});
|
||||||
|
if (user.email) {
|
||||||
|
// TODO
|
||||||
|
// sendAccountDowngradeMessage(user).catch(console.error);
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
ok: true,
|
||||||
|
message: "user downgraded to basic",
|
||||||
|
user,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
export async function denyUserUpgradeRequest(userId: T.UUID): Promise<void> {
|
export async function denyUserUpgradeRequest(userId: T.UUID): Promise<void> {
|
||||||
await updateLingdocsUser(userId, {
|
await updateLingdocsUser(userId, {
|
||||||
upgradeToStudentRequest: "denied",
|
upgradeToStudentRequest: "denied",
|
||||||
|
|
|
@ -2,7 +2,7 @@ import express from "express";
|
||||||
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 env from "../lib/env-vars";
|
||||||
import Stripe from "stripe";
|
import Stripe from "stripe";
|
||||||
import { upgradeUser } from "../lib/user-utils";
|
import { downgradeUser, upgradeUser } from "../lib/user-utils";
|
||||||
|
|
||||||
const stripe = new Stripe(env.stripeSecretKey, {
|
const stripe = new Stripe(env.stripeSecretKey, {
|
||||||
apiVersion: "2022-08-01",
|
apiVersion: "2022-08-01",
|
||||||
|
@ -39,18 +39,20 @@ paymentRouter.post(
|
||||||
let subscription: Stripe.Subscription;
|
let subscription: Stripe.Subscription;
|
||||||
let status: Stripe.Subscription.Status;
|
let status: Stripe.Subscription.Status;
|
||||||
// Handle the event
|
// Handle the event
|
||||||
|
const userId = event.data.object.subscription.metadata.userId as T.UUID;
|
||||||
switch (event.type) {
|
switch (event.type) {
|
||||||
case 'customer.subscription.deleted':
|
case 'customer.subscription.deleted':
|
||||||
subscription = event.data.object;
|
subscription = event.data.object;
|
||||||
status = subscription.status;
|
status = subscription.status;
|
||||||
console.log(`Subscription status is ${status}.`);
|
console.log(`Subscription status is ${status}.`);
|
||||||
|
console.log(`Downgrading user ${userId}.`);
|
||||||
|
await downgradeUser(userId);
|
||||||
// Then define and call a method to handle the subscription deleted.
|
// Then define and call a method to handle the subscription deleted.
|
||||||
// handleSubscriptionDeleted(subscriptionDeleted);
|
// handleSubscriptionDeleted(subscriptionDeleted);
|
||||||
break;
|
break;
|
||||||
case 'customer.subscription.created':
|
case 'customer.subscription.created':
|
||||||
subscription = event.data.object;
|
subscription = event.data.object;
|
||||||
status = subscription.status;
|
status = subscription.status;
|
||||||
const userId = subscription.metadata.userId as T.UUID;
|
|
||||||
console.log(`Upgrading user ${userId}.`);
|
console.log(`Upgrading user ${userId}.`);
|
||||||
await upgradeUser(userId);
|
await upgradeUser(userId);
|
||||||
// TODO: save subscription to db
|
// TODO: save subscription to db
|
||||||
|
|
|
@ -75,6 +75,12 @@ export type UpgradeUserResponse = {
|
||||||
user: LingdocsUser,
|
user: LingdocsUser,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export type DowngradeUserResponse = {
|
||||||
|
ok: true,
|
||||||
|
message: "user downgraded to basic",
|
||||||
|
user: LingdocsUser,
|
||||||
|
};
|
||||||
|
|
||||||
export type PostTestResultsBody = { tests: TestResult[] };
|
export type PostTestResultsBody = { tests: TestResult[] };
|
||||||
export type PostTestResultsResponse = {
|
export type PostTestResultsResponse = {
|
||||||
ok: true,
|
ok: true,
|
||||||
|
|
Loading…
Reference in New Issue