⚡️⚡️⚡️ Self-hosted collaborative bookmark manager to collect, read, annotate, and fully preserve what matters, all in one place.
TypeScript bookmarkbookmark-managercollaborationnextjsreact-nativeread-it-laterself-hostedtypescript
bug fixed
daniel31x13f68582e28c14
diff --git a/lib/api/checkSubscriptionByEmail.ts b/lib/api/checkSubscriptionByEmail.tsindex 62a2320d..1df51a7c 100644--- a/lib/api/checkSubscriptionByEmail.ts+++ b/lib/api/checkSubscriptionByEmail.ts@@ -1,22 +1,9 @@import Stripe from "stripe";-const MONTHLY_PRICE_ID = process.env.MONTHLY_PRICE_ID;-const YEARLY_PRICE_ID = process.env.YEARLY_PRICE_ID;const STRIPE_SECRET_KEY = process.env.STRIPE_SECRET_KEY;export default async function checkSubscriptionByEmail(email: string) {- let active: boolean | undefined,- stripeSubscriptionId: string | undefined,- currentPeriodStart: number | undefined,- currentPeriodEnd: number | undefined;-- if (!STRIPE_SECRET_KEY)- return {- active,- stripeSubscriptionId,- currentPeriodStart,- currentPeriodEnd,- };+ if (!STRIPE_SECRET_KEY) return null;const stripe = new Stripe(STRIPE_SECRET_KEY, {apiVersion: "2022-11-15",@@ -28,26 +15,17 @@ export default async function checkSubscriptionByEmail(email: string) {expand: ["data.subscriptions"],});- listByEmail.data.some((customer) => {- customer.subscriptions?.data.some((subscription) => {- subscription.current_period_end;-- active =- subscription.items.data.some(- (e) =>- (e.price.id === MONTHLY_PRICE_ID && e.price.active === true) ||- (e.price.id === YEARLY_PRICE_ID && e.price.active === true)- ) || false;- stripeSubscriptionId = subscription.id;- currentPeriodStart = subscription.current_period_start * 1000;- currentPeriodEnd = subscription.current_period_end * 1000;- });- });-- return {- active: active || false,- stripeSubscriptionId,- currentPeriodStart,- currentPeriodEnd,- };+ if (listByEmail?.data[0]?.subscriptions?.data[0]) {+ return {+ active: (listByEmail.data[0].subscriptions?.data[0] as any).plan.active,+ stripeSubscriptionId: listByEmail.data[0].subscriptions?.data[0].id,+ currentPeriodStart:+ listByEmail.data[0].subscriptions?.data[0].current_period_start * 1000,+ currentPeriodEnd:+ listByEmail.data[0].subscriptions?.data[0].current_period_end * 1000,+ quantity: (listByEmail?.data[0]?.subscriptions?.data[0] as any).quantity,+ };+ } else {+ return null;+ }}diff --git a/lib/api/verifySubscription.ts b/lib/api/verifySubscription.tsindex b153cc6d..6fd5aab7 100644--- a/lib/api/verifySubscription.ts+++ b/lib/api/verifySubscription.ts@@ -9,64 +9,56 @@ interface UserIncludingSubscription extends User {export default async function verifySubscription(user?: UserIncludingSubscription) {- if (!user) {+ if (!user || !user.subscriptions) {return null;}- const subscription = user.subscriptions;+ if (+ !user.subscriptions.active ||+ new Date() > user.subscriptions.currentPeriodEnd+ ) {+ const subscription = await checkSubscriptionByEmail(user.email as string);- const currentDate = new Date();+ if (+ !subscription ||+ !subscription.stripeSubscriptionId ||+ !subscription.currentPeriodEnd ||+ !subscription.currentPeriodStart ||+ !subscription.quantity+ ) {+ return null;+ }- if (!subscription?.active || currentDate > subscription.currentPeriodEnd) {const {active,stripeSubscriptionId,currentPeriodStart,currentPeriodEnd,- } = await checkSubscriptionByEmail(user.email as string);+ quantity,+ } = subscription;- if (- active &&- stripeSubscriptionId &&- currentPeriodStart &&- currentPeriodEnd- ) {- await prisma.subscription- .upsert({- where: {- userId: user.id,- },- create: {- active,- stripeSubscriptionId,- currentPeriodStart: new Date(currentPeriodStart),- currentPeriodEnd: new Date(currentPeriodEnd),- userId: user.id,- },- update: {- active,- stripeSubscriptionId,- currentPeriodStart: new Date(currentPeriodStart),- currentPeriodEnd: new Date(currentPeriodEnd),- },- })- .catch((err) => console.log(err));- } else if (!active) {- const subscription = await prisma.subscription.findFirst({+ await prisma.subscription+ .upsert({where: {userId: user.id,},- });-- if (subscription)- await prisma.subscription.delete({- where: {- userId: user.id,- },- });-- return null;- }+ create: {+ active,+ stripeSubscriptionId,+ currentPeriodStart: new Date(currentPeriodStart),+ currentPeriodEnd: new Date(currentPeriodEnd),+ quantity,+ userId: user.id,+ },+ update: {+ active,+ stripeSubscriptionId,+ currentPeriodStart: new Date(currentPeriodStart),+ currentPeriodEnd: new Date(currentPeriodEnd),+ quantity,+ },+ })+ .catch((err) => console.log(err));}return user;diff --git a/pages/api/v1/webhook/index.ts b/pages/api/v1/webhook/index.tsindex a266af3c..2635fda3 100644--- a/pages/api/v1/webhook/index.ts+++ b/pages/api/v1/webhook/index.ts@@ -59,30 +59,6 @@ export default async function webhook(return res.status(400).send("Webhook signature verification failed.");}- // switch (event.type) {- // case "invoice.payment_succeeded":- // console.log(- // `Payment for ${event.data.object.customer_email} was successful!`- // );- // await handleCreateSubscription(event.data.object);- // break;- // case "customer.subscription.updated":- // console.log(- // `Subscription for ${event.data.object.customer_email} was updated.`- // );- // await handleUpdateSubscription(event.data.object);- // break;- // case "customer.subscription.deleted":- // console.log(- // `Subscription for ${event.data.object.customer_email} was deleted.`- // );- // await handleDeleteSubscription(event.data.object);- // break;- // default:- // // Unexpected event type- // console.log(`Unhandled event type ${event.type}.`);- // }-// Handle the event based on its typeconst eventType = event.type;const data = event.data.object;