From 9e366eab5dccdb954dc2fdd8f68fce855ea6bf4f Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Sat, 15 Jun 2024 20:56:43 +0100 Subject: [PATCH] Create timer to automatically purge expired claims (#268) # Description Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. List any dependencies that are required for this change. - Create a timer which will purge expired claims automatically - Update claiming so it can only be done within 5 minutes of being dropped #215 ## Type of change Please delete options that are not relevant. - [ ] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected) - [ ] This change requires a documentation update # How Has This Been Tested? Please describe the tests that you ran to verify the changes. Provide instructions so we can reproduce. Please also list any relevant details to your test configuration. # Checklist - [ ] My code follows the style guidelines of this project - [ ] I have performed a self-review of my own code - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [ ] My changes generate no new warnings - [ ] I have added tests that provde my fix is effective or that my feature works - [ ] New and existing unit tests pass locally with my changes - [ ] Any dependent changes have been merged and published in downstream modules Reviewed-on: https://git.vylpes.xyz/External/card-drop/pulls/268 Co-authored-by: Ethan Lane Co-committed-by: Ethan Lane --- src/buttonEvents/Claim.ts | 8 ++++++++ src/client/client.ts | 7 +++++-- src/contracts/AppBaseEntity.ts | 6 ++++++ src/timers/PurgeClaims.ts | 14 ++++++++++++++ 4 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 src/timers/PurgeClaims.ts diff --git a/src/buttonEvents/Claim.ts b/src/buttonEvents/Claim.ts index 4f7f5ae..04c7f7f 100644 --- a/src/buttonEvents/Claim.ts +++ b/src/buttonEvents/Claim.ts @@ -20,6 +20,14 @@ export default class Claim extends ButtonEvent { const droppedBy = interaction.customId.split(" ")[3]; const userId = interaction.user.id; + const whenDropped = interaction.message.createdAt; + const lastClaimableDate = new Date(Date.now() - (1000 * 60 * 5)); // 5 minutes ago + + if (whenDropped < lastClaimableDate) { + await interaction.channel.send(`${interaction.user}, Cards can only be claimed within 5 minutes of it being dropped!`); + return; + } + AppLogger.LogSilly("Button/Claim", `Parameters: cardNumber=${cardNumber}, claimId=${claimId}, droppedBy=${droppedBy}, userId=${userId}`); const user = await User.FetchOneById(User, userId) || new User(userId, CardConstants.StartingCurrency); diff --git a/src/client/client.ts b/src/client/client.ts index 384843d..117bdb9 100644 --- a/src/client/client.ts +++ b/src/client/client.ts @@ -16,6 +16,7 @@ import { SeriesMetadata } from "../contracts/SeriesMetadata"; import AppLogger from "./appLogger"; import TimerHelper from "../helpers/TimerHelper"; import GiveCurrency from "../timers/GiveCurrency"; +import PurgeClaims from "../timers/PurgeClaims"; export class CoreClient extends Client { private static _commandItems: ICommandItem[]; @@ -79,8 +80,10 @@ export class CoreClient extends Client { .then(() => { AppLogger.LogInfo("Client", "App Data Source Initialised"); - const timerId = this._timerHelper.AddTimer("*/20 * * * *", "Europe/London", GiveCurrency, false); - this._timerHelper.StartTimer(timerId); + this._timerHelper.AddTimer("*/20 * * * *", "Europe/London", GiveCurrency, false); + this._timerHelper.AddTimer("0 0 * * *", "Europe/London", PurgeClaims, false); + + this._timerHelper.StartAllTimers(); }) .catch(err => { AppLogger.LogError("Client", "App Data Source Initialisation Failed"); diff --git a/src/contracts/AppBaseEntity.ts b/src/contracts/AppBaseEntity.ts index 3eb9684..43bed74 100644 --- a/src/contracts/AppBaseEntity.ts +++ b/src/contracts/AppBaseEntity.ts @@ -39,6 +39,12 @@ export default class AppBaseEntity { await repository.remove(entity); } + public static async RemoveMany(target: EntityTarget, entity: T[]): Promise { + const repository = AppDataSource.getRepository(target); + + await repository.remove(entity); + } + public static async FetchAll(target: EntityTarget, relations?: string[]): Promise { const repository = AppDataSource.getRepository(target); diff --git a/src/timers/PurgeClaims.ts b/src/timers/PurgeClaims.ts new file mode 100644 index 0000000..a0ed9d0 --- /dev/null +++ b/src/timers/PurgeClaims.ts @@ -0,0 +1,14 @@ +import AppLogger from "../client/appLogger"; +import Claim from "../database/entities/app/Claim"; + +export default async function PurgeClaims() { + const claims = await Claim.FetchAll(Claim); + + const whenLastClaimable = new Date(Date.now() - (1000 * 60 * 5)); // 5 minutes ago + + const expiredClaims = claims.filter(x => x.WhenCreated < whenLastClaimable); + + await Claim.RemoveMany(Claim, expiredClaims); + + AppLogger.LogInfo("Timers/PurgeClaims", `Purged ${expiredClaims.length} claims from the database`); +} \ No newline at end of file