Create timer to automatically purge expired claims #268

Merged
Vylpes merged 2 commits from feature/215-purge-claims into develop 2024-06-15 20:56:43 +01:00
4 changed files with 33 additions and 2 deletions

View file

@ -20,6 +20,14 @@ export default class Claim extends ButtonEvent {
const droppedBy = interaction.customId.split(" ")[3]; const droppedBy = interaction.customId.split(" ")[3];
const userId = interaction.user.id; 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}`); 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); const user = await User.FetchOneById(User, userId) || new User(userId, CardConstants.StartingCurrency);

View file

@ -16,6 +16,7 @@ import { SeriesMetadata } from "../contracts/SeriesMetadata";
import AppLogger from "./appLogger"; import AppLogger from "./appLogger";
import TimerHelper from "../helpers/TimerHelper"; import TimerHelper from "../helpers/TimerHelper";
import GiveCurrency from "../timers/GiveCurrency"; import GiveCurrency from "../timers/GiveCurrency";
import PurgeClaims from "../timers/PurgeClaims";
export class CoreClient extends Client { export class CoreClient extends Client {
private static _commandItems: ICommandItem[]; private static _commandItems: ICommandItem[];
@ -79,8 +80,10 @@ export class CoreClient extends Client {
.then(() => { .then(() => {
AppLogger.LogInfo("Client", "App Data Source Initialised"); AppLogger.LogInfo("Client", "App Data Source Initialised");
const timerId = this._timerHelper.AddTimer("*/20 * * * *", "Europe/London", GiveCurrency, false); this._timerHelper.AddTimer("*/20 * * * *", "Europe/London", GiveCurrency, false);
this._timerHelper.StartTimer(timerId); this._timerHelper.AddTimer("0 0 * * *", "Europe/London", PurgeClaims, false);
this._timerHelper.StartAllTimers();
}) })
.catch(err => { .catch(err => {
AppLogger.LogError("Client", "App Data Source Initialisation Failed"); AppLogger.LogError("Client", "App Data Source Initialisation Failed");

View file

@ -39,6 +39,12 @@ export default class AppBaseEntity {
await repository.remove(entity); 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[]> { public static async FetchAll<T extends AppBaseEntity>(target: EntityTarget<T>, relations?: string[]): Promise<T[]> {
const repository = AppDataSource.getRepository<T>(target); const repository = AppDataSource.getRepository<T>(target);

14
src/timers/PurgeClaims.ts Normal file
View file

@ -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`);
}