mirror of
https://github.com/AustinKelsay/plebdevs.git
synced 2025-04-19 19:01:19 +00:00
Fix for pool connection issues on cron
This commit is contained in:
parent
e96e0ba1dc
commit
523eaf5b6a
@ -7,6 +7,7 @@ datasource db {
|
||||
provider = "postgresql"
|
||||
url = env("POSTGRES_PRISMA_URL")
|
||||
directUrl = env("POSTGRES_URL_NON_POOLING")
|
||||
relationMode = "prisma"
|
||||
}
|
||||
|
||||
generator client {
|
||||
|
@ -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) => {
|
||||
|
@ -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.
|
||||
|
Loading…
x
Reference in New Issue
Block a user