Skip to content

Commit

Permalink
Create timer to automatically purge expired claims (#80)
Browse files Browse the repository at this point in the history
* Add PurgeClaims timer

* Update logging level in PurgeClaims timer
  • Loading branch information
Vylpes authored Jun 14, 2024
1 parent f968332 commit 1909fce
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/buttonEvents/Claim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 5 additions & 2 deletions src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
Expand Down Expand Up @@ -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");
Expand Down
6 changes: 6 additions & 0 deletions src/contracts/AppBaseEntity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ export default class AppBaseEntity {
await repository.remove(entity);
}

public static async RemoveMany<T extends AppBaseEntity>(target: EntityTarget<T>, entity: T[]): Promise<void> {
const repository = AppDataSource.getRepository<T>(target);

await repository.remove(entity);
}

public static async FetchAll<T extends AppBaseEntity>(target: EntityTarget<T>, relations?: string[]): Promise<T[]> {
const repository = AppDataSource.getRepository<T>(target);

Expand Down
14 changes: 14 additions & 0 deletions src/timers/PurgeClaims.ts
Original file line number Diff line number Diff line change
@@ -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`);
}

0 comments on commit 1909fce

Please sign in to comment.