Add currently active effect to list

This commit is contained in:
Ethan Lane 2025-01-29 18:07:56 +00:00
parent d9e8fee1a6
commit 0adf9c67c9
4 changed files with 91 additions and 4 deletions

View file

@ -71,4 +71,16 @@ export default class UserEffect extends AppBaseEntity {
return query; return query;
} }
public static async FetchActiveEffectByUserId(userId: string): Promise<UserEffect | null> {
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;
}
} }

View file

@ -73,6 +73,7 @@ export default class EffectHelper {
const itemsPerPage = 10; const itemsPerPage = 10;
const query = await UserEffect.FetchAllByUserIdPaginated(userId, page - 1, itemsPerPage); const query = await UserEffect.FetchAllByUserIdPaginated(userId, page - 1, itemsPerPage);
const activeEffect = await UserEffect.FetchActiveEffectByUserId(userId);
const effects = query[0]; const effects = query[0];
const count = query[1]; const count = query[1];
@ -91,6 +92,15 @@ export default class EffectHelper {
.setColor(EmbedColours.Ok) .setColor(EmbedColours.Ok)
.setFooter({ text: `Page ${page} of ${totalPages}` }); .setFooter({ text: `Page ${page} of ${totalPages}` });
if (activeEffect) {
embed.addFields([
{
name: "Active",
value: `${EffectDetails.get(activeEffect.Name)?.friendlyName} (Exp. <t:${Math.round(activeEffect.WhenExpires!.getTime() / 1000)}>)`,
}
]);
}
const row = new ActionRowBuilder<ButtonBuilder>() const row = new ActionRowBuilder<ButtonBuilder>()
.addComponents( .addComponents(
new ButtonBuilder() new ButtonBuilder()

View file

@ -14,7 +14,7 @@ describe("GenerateEffectEmbed", () => {
} }
], ],
1, 1,
]) ]);
// Act // Act
const result = await EffectHelper.GenerateEffectEmbed("userId", 1); const result = await EffectHelper.GenerateEffectEmbed("userId", 1);
@ -37,7 +37,7 @@ describe("GenerateEffectEmbed", () => {
(UserEffect.FetchAllByUserIdPaginated as jest.Mock).mockResolvedValue([ (UserEffect.FetchAllByUserIdPaginated as jest.Mock).mockResolvedValue([
effects, effects,
15, 15,
]) ]);
// Act // Act
const result = await EffectHelper.GenerateEffectEmbed("userId", 1); const result = await EffectHelper.GenerateEffectEmbed("userId", 1);
@ -60,7 +60,7 @@ describe("GenerateEffectEmbed", () => {
(UserEffect.FetchAllByUserIdPaginated as jest.Mock).mockResolvedValue([ (UserEffect.FetchAllByUserIdPaginated as jest.Mock).mockResolvedValue([
effects, effects,
15, 15,
]) ]);
// Act // Act
const result = await EffectHelper.GenerateEffectEmbed("userId", 2); const result = await EffectHelper.GenerateEffectEmbed("userId", 2);
@ -74,7 +74,31 @@ describe("GenerateEffectEmbed", () => {
(UserEffect.FetchAllByUserIdPaginated as jest.Mock).mockResolvedValue([ (UserEffect.FetchAllByUserIdPaginated as jest.Mock).mockResolvedValue([
[], [],
0, 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 // Act
const result = await EffectHelper.GenerateEffectEmbed("userId", 1); const result = await EffectHelper.GenerateEffectEmbed("userId", 1);

View file

@ -1,5 +1,46 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP // 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. <t:1738174>)",
},
],
"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`] = ` exports[`GenerateEffectEmbed GIVEN user does NOT have an effect, EXPECT empty embed to be returned 1`] = `
{ {
"embed": { "embed": {