also log payments in db
This commit is contained in:
parent
690a840358
commit
0a2da5860b
|
@ -7,12 +7,17 @@ import * as T from "../../../website/src/types/account-types";
|
||||||
const nano = Nano(env.couchDbURL);
|
const nano = Nano(env.couchDbURL);
|
||||||
const usersDb = nano.db.use("lingdocs-users");
|
const usersDb = nano.db.use("lingdocs-users");
|
||||||
const feedbackDb = nano.db.use("feedback");
|
const feedbackDb = nano.db.use("feedback");
|
||||||
|
const paymentsDb = nano.db.use("payments");
|
||||||
const userDbPrefix = "userdb-";
|
const userDbPrefix = "userdb-";
|
||||||
|
|
||||||
export async function addFeedback(feedback: any) {
|
export async function addFeedback(feedback: any) {
|
||||||
await feedbackDb.insert(feedback);
|
await feedbackDb.insert(feedback);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function addToPaymentsDb(payment: any) {
|
||||||
|
await paymentsDb.insert(payment);
|
||||||
|
}
|
||||||
|
|
||||||
export function updateLastLogin(user: T.LingdocsUser): T.LingdocsUser {
|
export function updateLastLogin(user: T.LingdocsUser): T.LingdocsUser {
|
||||||
return {
|
return {
|
||||||
...user,
|
...user,
|
||||||
|
|
|
@ -3,6 +3,7 @@ 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 { downgradeUser, upgradeUser } from "../lib/user-utils";
|
import { downgradeUser, upgradeUser } from "../lib/user-utils";
|
||||||
|
import { addToPaymentsDb } from "../lib/couch-db";
|
||||||
|
|
||||||
const stripe = new Stripe(env.stripeSecretKey, {
|
const stripe = new Stripe(env.stripeSecretKey, {
|
||||||
apiVersion: "2022-08-01",
|
apiVersion: "2022-08-01",
|
||||||
|
@ -42,12 +43,20 @@ paymentRouter.post(
|
||||||
switch (event.type) {
|
switch (event.type) {
|
||||||
case 'customer.subscription.deleted':
|
case 'customer.subscription.deleted':
|
||||||
subscription = event.data.object;
|
subscription = event.data.object;
|
||||||
|
addToPaymentsDb({
|
||||||
|
action: "deleted",
|
||||||
|
subscription,
|
||||||
|
});
|
||||||
await downgradeUser(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;
|
||||||
|
addToPaymentsDb({
|
||||||
|
action: "created",
|
||||||
|
subscription,
|
||||||
|
});
|
||||||
await upgradeUser(userId, subscription);
|
await upgradeUser(userId, subscription);
|
||||||
// TODO: save subscription to db
|
// TODO: save subscription to db
|
||||||
break;
|
break;
|
||||||
|
|
Loading…
Reference in New Issue