mirror of
https://github.com/AustinKelsay/plebdevs.git
synced 2025-05-20 00:42:04 +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"
|
provider = "postgresql"
|
||||||
url = env("POSTGRES_PRISMA_URL")
|
url = env("POSTGRES_PRISMA_URL")
|
||||||
directUrl = env("POSTGRES_URL_NON_POOLING")
|
directUrl = env("POSTGRES_URL_NON_POOLING")
|
||||||
|
relationMode = "prisma"
|
||||||
}
|
}
|
||||||
|
|
||||||
generator client {
|
generator client {
|
||||||
|
@ -151,72 +151,84 @@ export const deleteUser = async (id) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const updateUserSubscription = async (userId, isSubscribed, nwc) => {
|
export const updateUserSubscription = async (userId, isSubscribed, nwc) => {
|
||||||
const now = new Date();
|
try {
|
||||||
return await prisma.user.update({
|
const now = new Date();
|
||||||
where: { id: userId },
|
return await prisma.user.update({
|
||||||
data: {
|
where: { id: userId },
|
||||||
role: {
|
data: {
|
||||||
upsert: {
|
role: {
|
||||||
create: {
|
upsert: {
|
||||||
subscribed: isSubscribed,
|
create: {
|
||||||
subscriptionStartDate: isSubscribed ? now : null,
|
subscribed: isSubscribed,
|
||||||
lastPaymentAt: isSubscribed ? now : null,
|
subscriptionStartDate: isSubscribed ? now : null,
|
||||||
nwc: nwc ? nwc : null,
|
lastPaymentAt: isSubscribed ? now : null,
|
||||||
subscriptionExpiredAt: null,
|
nwc: nwc ? nwc : null,
|
||||||
},
|
subscriptionExpiredAt: null,
|
||||||
update: {
|
},
|
||||||
subscribed: isSubscribed,
|
update: {
|
||||||
subscriptionStartDate: isSubscribed ? { set: now } : { set: null },
|
subscribed: isSubscribed,
|
||||||
lastPaymentAt: isSubscribed ? now : { set: null },
|
subscriptionStartDate: isSubscribed ? { set: now } : { set: null },
|
||||||
nwc: nwc ? nwc : null,
|
lastPaymentAt: isSubscribed ? now : { set: null },
|
||||||
subscriptionExpiredAt: null,
|
nwc: nwc ? nwc : null,
|
||||||
|
subscriptionExpiredAt: null,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
include: {
|
||||||
include: {
|
role: true,
|
||||||
role: true,
|
},
|
||||||
},
|
});
|
||||||
});
|
} finally {
|
||||||
|
await prisma.$disconnect();
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export const findExpiredSubscriptions = async () => {
|
export const findExpiredSubscriptions = async () => {
|
||||||
const now = new Date();
|
try {
|
||||||
// const thirtyOneDaysAgo = new Date(now.getTime() - 31 * 24 * 60 * 60 * 1000);
|
const now = new Date();
|
||||||
const oneDayAndOneHourAgo = new Date(now.getTime() - 1 * 24 * 60 * 60 * 1000 - 1 * 60 * 60 * 1000);
|
const oneDayAndOneHourAgo = new Date(now.getTime() - 1 * 24 * 60 * 60 * 1000 - 1 * 60 * 60 * 1000);
|
||||||
|
|
||||||
return await prisma.role.findMany({
|
const result = await prisma.role.findMany({
|
||||||
where: {
|
where: {
|
||||||
subscribed: true,
|
subscribed: true,
|
||||||
lastPaymentAt: {
|
lastPaymentAt: {
|
||||||
// lt: thirtyOneDaysAgo
|
lt: oneDayAndOneHourAgo
|
||||||
lt: oneDayAndOneHourAgo
|
}
|
||||||
|
},
|
||||||
|
select: {
|
||||||
|
userId: true,
|
||||||
|
nwc: true
|
||||||
}
|
}
|
||||||
},
|
});
|
||||||
select: {
|
|
||||||
userId: true,
|
return result;
|
||||||
nwc: true
|
} finally {
|
||||||
}
|
await prisma.$disconnect();
|
||||||
});
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export const expireUserSubscriptions = async (userIds) => {
|
export const expireUserSubscriptions = async (userIds) => {
|
||||||
const now = new Date();
|
try {
|
||||||
const updatePromises = userIds.map((userId) =>
|
const now = new Date();
|
||||||
prisma.role.update({
|
const updatePromises = userIds.map((userId) =>
|
||||||
where: { userId },
|
prisma.role.update({
|
||||||
data: {
|
where: { userId },
|
||||||
subscribed: false,
|
data: {
|
||||||
subscriptionStartDate: null,
|
subscribed: false,
|
||||||
lastPaymentAt: null,
|
subscriptionStartDate: null,
|
||||||
nwc: null,
|
lastPaymentAt: null,
|
||||||
subscriptionExpiredAt: now,
|
nwc: null,
|
||||||
}
|
subscriptionExpiredAt: now,
|
||||||
})
|
}
|
||||||
);
|
})
|
||||||
|
);
|
||||||
|
|
||||||
await prisma.$transaction(updatePromises);
|
await prisma.$transaction(updatePromises);
|
||||||
return userIds.length;
|
return userIds.length;
|
||||||
|
} finally {
|
||||||
|
await prisma.$disconnect();
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getUserByEmail = async (email) => {
|
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.
|
// 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.
|
// This ensures that the same instance of PrismaClient is reused across multiple invocations.
|
||||||
if (!global.prisma) {
|
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.
|
// Assign the global PrismaClient instance to the prisma variable.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user