diff --git a/account/src/lib/couch-db.ts b/account/src/lib/couch-db.ts index 6ccdd3d..3c0b953 100644 --- a/account/src/lib/couch-db.ts +++ b/account/src/lib/couch-db.ts @@ -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, diff --git a/account/src/routers/payment-router.ts b/account/src/routers/payment-router.ts index d291065..e21ab45 100644 --- a/account/src/routers/payment-router.ts +++ b/account/src/routers/payment-router.ts @@ -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;