From d9fd5ab371de44ddb9da0c8062d72c065c9c0769 Mon Sep 17 00:00:00 2001 From: Kalle <38327916+Sendouc@users.noreply.github.com> Date: Tue, 12 Nov 2024 22:28:07 +0200 Subject: [PATCH] Automatically set old groups as inactive --- app/entry.server.tsx | 4 ++- app/features/sendouq/QRepository.server.ts | 32 ++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/app/entry.server.tsx b/app/entry.server.tsx index a1b13aa24..874e4620f 100644 --- a/app/entry.server.tsx +++ b/app/entry.server.tsx @@ -94,8 +94,10 @@ if (!global.appStartSignal && process.env.NODE_ENV === "production") { // every hour cron.schedule("0 */1 * * *", async () => { const { numDeletedRows } = await QRepository.deleteOldTrust(); - logger.info(`Deleted ${numDeletedRows} old trusts`); + + const { numUpdatedRows } = await QRepository.setOldGroupsAsInactive(); + logger.info(`Set ${numUpdatedRows} as inactive`); }); } diff --git a/app/features/sendouq/QRepository.server.ts b/app/features/sendouq/QRepository.server.ts index a92e4ab4d..4f263be62 100644 --- a/app/features/sendouq/QRepository.server.ts +++ b/app/features/sendouq/QRepository.server.ts @@ -359,3 +359,35 @@ export async function deleteOldTrust() { .where("lastUsedAt", "<", dateToDatabaseTimestamp(twoMonthsAgo)) .executeTakeFirst(); } + +export async function setOldGroupsAsInactive() { + const oneHourAgo = sub(new Date(), { hours: 1 }); + + return db.transaction().execute(async (trx) => { + const groupsToSetInactive = await trx + .selectFrom("Group") + .leftJoin("GroupMatch", (join) => + join.on((eb) => + eb.or([ + eb("GroupMatch.alphaGroupId", "=", eb.ref("Group.id")), + eb("GroupMatch.bravoGroupId", "=", eb.ref("Group.id")), + ]), + ), + ) + .select(["Group.id"]) + .where("status", "!=", "INACTIVE") + .where("GroupMatch.id", "is", null) + .where("latestActionAt", "<", dateToDatabaseTimestamp(oneHourAgo)) + .execute(); + + return trx + .updateTable("Group") + .set({ status: "INACTIVE" }) + .where( + "Group.id", + "in", + groupsToSetInactive.map((g) => g.id), + ) + .executeTakeFirst(); + }); +}