From 0adf9c67c9f016f107eaadb8f811801bb08f50a1 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Wed, 29 Jan 2025 18:07:56 +0000 Subject: [PATCH] Add currently active effect to list --- src/database/entities/app/UserEffect.ts | 12 ++++++ src/helpers/EffectHelper.ts | 10 +++++ tests/helpers/EffectHelper.test.ts | 32 +++++++++++++-- .../__snapshots__/EffectHelper.test.ts.snap | 41 +++++++++++++++++++ 4 files changed, 91 insertions(+), 4 deletions(-) diff --git a/src/database/entities/app/UserEffect.ts b/src/database/entities/app/UserEffect.ts index 72fae5f..c962ff7 100644 --- a/src/database/entities/app/UserEffect.ts +++ b/src/database/entities/app/UserEffect.ts @@ -71,4 +71,16 @@ export default class UserEffect extends AppBaseEntity { return query; } + + public static async FetchActiveEffectByUserId(userId: string): Promise { + const repository = AppDataSource.getRepository(UserEffect); + + const query = await repository.createQueryBuilder("effect") + .where("effect.UserId = :userId", { userId }) + .where("effect.WhenExpires IS NOT NULL") + .andWhere("effect.WhenExpires > :now", { now: new Date() }) + .getOne(); + + return query; + } } diff --git a/src/helpers/EffectHelper.ts b/src/helpers/EffectHelper.ts index bd86b15..c44dfc4 100644 --- a/src/helpers/EffectHelper.ts +++ b/src/helpers/EffectHelper.ts @@ -73,6 +73,7 @@ export default class EffectHelper { const itemsPerPage = 10; const query = await UserEffect.FetchAllByUserIdPaginated(userId, page - 1, itemsPerPage); + const activeEffect = await UserEffect.FetchActiveEffectByUserId(userId); const effects = query[0]; const count = query[1]; @@ -91,6 +92,15 @@ export default class EffectHelper { .setColor(EmbedColours.Ok) .setFooter({ text: `Page ${page} of ${totalPages}` }); + if (activeEffect) { + embed.addFields([ + { + name: "Active", + value: `${EffectDetails.get(activeEffect.Name)?.friendlyName} (Exp. )`, + } + ]); + } + const row = new ActionRowBuilder() .addComponents( new ButtonBuilder() diff --git a/tests/helpers/EffectHelper.test.ts b/tests/helpers/EffectHelper.test.ts index 6541653..e8dbef2 100644 --- a/tests/helpers/EffectHelper.test.ts +++ b/tests/helpers/EffectHelper.test.ts @@ -14,7 +14,7 @@ describe("GenerateEffectEmbed", () => { } ], 1, - ]) + ]); // Act const result = await EffectHelper.GenerateEffectEmbed("userId", 1); @@ -37,7 +37,7 @@ describe("GenerateEffectEmbed", () => { (UserEffect.FetchAllByUserIdPaginated as jest.Mock).mockResolvedValue([ effects, 15, - ]) + ]); // Act const result = await EffectHelper.GenerateEffectEmbed("userId", 1); @@ -60,7 +60,7 @@ describe("GenerateEffectEmbed", () => { (UserEffect.FetchAllByUserIdPaginated as jest.Mock).mockResolvedValue([ effects, 15, - ]) + ]); // Act const result = await EffectHelper.GenerateEffectEmbed("userId", 2); @@ -74,7 +74,31 @@ describe("GenerateEffectEmbed", () => { (UserEffect.FetchAllByUserIdPaginated as jest.Mock).mockResolvedValue([ [], 0, - ]) + ]); + + // Act + const result = await EffectHelper.GenerateEffectEmbed("userId", 1); + + // Assert + expect(result).toMatchSnapshot(); + }); + + test("GIVEN there is an active effect, EXPECT field added", async () => { + // Arrange + (UserEffect.FetchAllByUserIdPaginated as jest.Mock).mockResolvedValue([ + [ + { + Name: "unclaimed", + Unused: 1, + } + ], + 1, + ]); + + (UserEffect.FetchActiveEffectByUserId as jest.Mock).mockResolvedValue({ + Name: "unclaimed", + WhenExpires: new Date(1738174052), + }); // Act const result = await EffectHelper.GenerateEffectEmbed("userId", 1); diff --git a/tests/helpers/__snapshots__/EffectHelper.test.ts.snap b/tests/helpers/__snapshots__/EffectHelper.test.ts.snap index c3e6f55..a78f16d 100644 --- a/tests/helpers/__snapshots__/EffectHelper.test.ts.snap +++ b/tests/helpers/__snapshots__/EffectHelper.test.ts.snap @@ -1,5 +1,46 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`GenerateEffectEmbed GIVEN there is an active effect, EXPECT field added 1`] = ` +{ + "embed": { + "color": 3166394, + "description": "Unclaimed Chance Up x1", + "fields": [ + { + "name": "Active", + "value": "Unclaimed Chance Up (Exp. )", + }, + ], + "footer": { + "icon_url": undefined, + "text": "Page 1 of 1", + }, + "title": "Effects", + }, + "row": { + "components": [ + { + "custom_id": "effects list 0", + "disabled": true, + "emoji": undefined, + "label": "Previous", + "style": 1, + "type": 2, + }, + { + "custom_id": "effects list 2", + "disabled": true, + "emoji": undefined, + "label": "Next", + "style": 1, + "type": 2, + }, + ], + "type": 1, + }, +} +`; + exports[`GenerateEffectEmbed GIVEN user does NOT have an effect, EXPECT empty embed to be returned 1`] = ` { "embed": {