Add friendly name and active effect to user effect list embed #422
4 changed files with 91 additions and 4 deletions
|
@ -71,4 +71,16 @@ export default class UserEffect extends AppBaseEntity {
|
|||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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. <t:${Math.round(activeEffect.WhenExpires!.getTime() / 1000)}>)`,
|
||||
}
|
||||
]);
|
||||
}
|
||||
|
||||
const row = new ActionRowBuilder<ButtonBuilder>()
|
||||
.addComponents(
|
||||
new ButtonBuilder()
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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. <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`] = `
|
||||
{
|
||||
"embed": {
|
||||
|
|
Loading…
Reference in a new issue