From 523eaf5b6ae5a85a6770924a6ec99df8048a4a7b Mon Sep 17 00:00:00 2001 From: austinkelsay Date: Mon, 28 Oct 2024 15:16:22 -0500 Subject: [PATCH] Fix for pool connection issues on cron --- prisma/schema.prisma | 1 + src/db/models/userModels.js | 120 ++++++++++++++++++++---------------- src/db/prisma.js | 16 ++++- 3 files changed, 82 insertions(+), 55 deletions(-) diff --git a/prisma/schema.prisma b/prisma/schema.prisma index faa87eb..a9aeb9f 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -7,6 +7,7 @@ datasource db { provider = "postgresql" url = env("POSTGRES_PRISMA_URL") directUrl = env("POSTGRES_URL_NON_POOLING") + relationMode = "prisma" } generator client { diff --git a/src/db/models/userModels.js b/src/db/models/userModels.js index 1b02e3e..2f219c9 100644 --- a/src/db/models/userModels.js +++ b/src/db/models/userModels.js @@ -151,72 +151,84 @@ export const deleteUser = async (id) => { }; export const updateUserSubscription = async (userId, isSubscribed, nwc) => { - const now = new Date(); - return await prisma.user.update({ - where: { id: userId }, - data: { - role: { - upsert: { - create: { - subscribed: isSubscribed, - subscriptionStartDate: isSubscribed ? now : null, - lastPaymentAt: isSubscribed ? now : null, - nwc: nwc ? nwc : null, - subscriptionExpiredAt: null, - }, - update: { - subscribed: isSubscribed, - subscriptionStartDate: isSubscribed ? { set: now } : { set: null }, - lastPaymentAt: isSubscribed ? now : { set: null }, - nwc: nwc ? nwc : null, - subscriptionExpiredAt: null, + try { + const now = new Date(); + return await prisma.user.update({ + where: { id: userId }, + data: { + role: { + upsert: { + create: { + subscribed: isSubscribed, + subscriptionStartDate: isSubscribed ? now : null, + lastPaymentAt: isSubscribed ? now : null, + nwc: nwc ? nwc : null, + subscriptionExpiredAt: null, + }, + update: { + subscribed: isSubscribed, + subscriptionStartDate: isSubscribed ? { set: now } : { set: null }, + lastPaymentAt: isSubscribed ? now : { set: null }, + nwc: nwc ? nwc : null, + subscriptionExpiredAt: null, + }, }, }, }, - }, - include: { - role: true, - }, - }); + include: { + role: true, + }, + }); + } finally { + await prisma.$disconnect(); + } }; export const findExpiredSubscriptions = async () => { - const now = new Date(); - // const thirtyOneDaysAgo = new Date(now.getTime() - 31 * 24 * 60 * 60 * 1000); - const oneDayAndOneHourAgo = new Date(now.getTime() - 1 * 24 * 60 * 60 * 1000 - 1 * 60 * 60 * 1000); + try { + const now = new Date(); + const oneDayAndOneHourAgo = new Date(now.getTime() - 1 * 24 * 60 * 60 * 1000 - 1 * 60 * 60 * 1000); - return await prisma.role.findMany({ - where: { - subscribed: true, - lastPaymentAt: { - // lt: thirtyOneDaysAgo - lt: oneDayAndOneHourAgo + const result = await prisma.role.findMany({ + where: { + subscribed: true, + lastPaymentAt: { + lt: oneDayAndOneHourAgo + } + }, + select: { + userId: true, + nwc: true } - }, - select: { - userId: true, - nwc: true - } - }); + }); + + return result; + } finally { + await prisma.$disconnect(); + } }; export const expireUserSubscriptions = async (userIds) => { - const now = new Date(); - const updatePromises = userIds.map((userId) => - prisma.role.update({ - where: { userId }, - data: { - subscribed: false, - subscriptionStartDate: null, - lastPaymentAt: null, - nwc: null, - subscriptionExpiredAt: now, - } - }) - ); + try { + const now = new Date(); + const updatePromises = userIds.map((userId) => + prisma.role.update({ + where: { userId }, + data: { + subscribed: false, + subscriptionStartDate: null, + lastPaymentAt: null, + nwc: null, + subscriptionExpiredAt: now, + } + }) + ); - await prisma.$transaction(updatePromises); - return userIds.length; + await prisma.$transaction(updatePromises); + return userIds.length; + } finally { + await prisma.$disconnect(); + } }; export const getUserByEmail = async (email) => { diff --git a/src/db/prisma.js b/src/db/prisma.js index 0dfec71..c377dc2 100644 --- a/src/db/prisma.js +++ b/src/db/prisma.js @@ -7,7 +7,21 @@ let prisma; // If not, create a new instance of PrismaClient and attach it to the global object. // This ensures that the same instance of PrismaClient is reused across multiple invocations. if (!global.prisma) { - global.prisma = new PrismaClient(); + global.prisma = new PrismaClient({ + datasources: { + db: { + url: process.env.POSTGRES_PRISMA_URL + } + }, + // Add connection pool configuration + connection: { + pool: { + min: 0, + max: 1, + idleTimeoutMillis: 5000 + } + } + }); } // Assign the global PrismaClient instance to the prisma variable.