mirror of
https://github.com/Sendouc/sendou.ink.git
synced 2026-10-01 15:48:23 -05:00
Automatically set old groups as inactive
This commit is contained in:
@@ -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`);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -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();
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user