also log payments in db

This commit is contained in:
adueck 2022-10-15 21:23:02 +05:00
parent 690a840358
commit 0a2da5860b
2 changed files with 14 additions and 0 deletions

View File

@ -7,12 +7,17 @@ import * as T from "../../../website/src/types/account-types";
const nano = Nano(env.couchDbURL);
const usersDb = nano.db.use("lingdocs-users");
const feedbackDb = nano.db.use("feedback");
const paymentsDb = nano.db.use("payments");
const userDbPrefix = "userdb-";
export async function addFeedback(feedback: any) {
await feedbackDb.insert(feedback);
}
export async function addToPaymentsDb(payment: any) {
await paymentsDb.insert(payment);
}
export function updateLastLogin(user: T.LingdocsUser): T.LingdocsUser {
return {
...user,

View File

@ -3,6 +3,7 @@ import * as T from "../../../website/src/types/account-types";
import env from "../lib/env-vars";
import Stripe from "stripe";
import { downgradeUser, upgradeUser } from "../lib/user-utils";
import { addToPaymentsDb } from "../lib/couch-db";
const stripe = new Stripe(env.stripeSecretKey, {
apiVersion: "2022-08-01",
@ -42,12 +43,20 @@ paymentRouter.post(
switch (event.type) {
case 'customer.subscription.deleted':
subscription = event.data.object;
addToPaymentsDb({
action: "deleted",
subscription,
});
await downgradeUser(userId);
// Then define and call a method to handle the subscription deleted.
// handleSubscriptionDeleted(subscriptionDeleted);
break;
case 'customer.subscription.created':
subscription = event.data.object;
addToPaymentsDb({
action: "created",
subscription,
});
await upgradeUser(userId, subscription);
// TODO: save subscription to db
break;