From bd4d9875459e0fd0ecd814c568283d05b1fdaf1c Mon Sep 17 00:00:00 2001 From: Rishi Chaubey <155433512+RishiChaubey31@users.noreply.github.com> Date: Tue, 5 Nov 2024 23:17:56 +0530 Subject: [PATCH] To display specific message when used /mention-each command (#280) * display a message everytime user runs mention-each command * created a new feature flag * removed unecessary lines * changed the feature flag name and removed nesting * changed flag name to dev_title * made some test changes * move some lines to fixtures from test * removed unnecessary comments --- src/constants/commands.ts | 6 ++ src/controllers/baseHandler.ts | 3 + src/controllers/mentionEachUser.ts | 15 +++++ tests/fixtures/fixture.ts | 13 +++++ tests/unit/handlers/mentionEachUser.test.ts | 63 +++++++++++++++++++-- 5 files changed, 96 insertions(+), 4 deletions(-) diff --git a/src/constants/commands.ts b/src/constants/commands.ts index 47a83d65..d64a965f 100644 --- a/src/constants/commands.ts +++ b/src/constants/commands.ts @@ -50,6 +50,12 @@ export const MENTION_EACH = { type: 5, require: false, }, + { + name: "dev_title", + description: "want to see extra details?", + type: 5, + require: false, + }, ], }; diff --git a/src/controllers/baseHandler.ts b/src/controllers/baseHandler.ts index 2d36c389..61c5b765 100644 --- a/src/controllers/baseHandler.ts +++ b/src/controllers/baseHandler.ts @@ -75,6 +75,9 @@ export async function baseHandler( displayMessageObj: data.find((item) => item.name === "message"), channelId: message.channel_id, dev: data.find((item) => item.name === "dev") as unknown as DevFlag, + dev_title: data.find( + (item) => item.name === "dev_title" + ) as unknown as DevFlag, }; return await mentionEachUser(transformedArgument, env, ctx); } diff --git a/src/controllers/mentionEachUser.ts b/src/controllers/mentionEachUser.ts index ebe63602..6e8f65cf 100644 --- a/src/controllers/mentionEachUser.ts +++ b/src/controllers/mentionEachUser.ts @@ -16,6 +16,7 @@ export async function mentionEachUser( roleToBeTaggedObj: MentionEachUserOptions; displayMessageObj?: MentionEachUserOptions; channelId: number; + dev_title?: DevFlag; dev?: DevFlag; }, env: env, @@ -25,6 +26,7 @@ export async function mentionEachUser( const roleId = transformedArgument.roleToBeTaggedObj.value; const msgToBeSent = transformedArgument?.displayMessageObj?.value; const dev = transformedArgument?.dev?.value || false; + const devtitle = transformedArgument?.dev_title?.value || false; // optional chaining here only because display message obj is optional argument const usersWithMatchingRole = filterUserByRoles( getMembersInServerResponse as UserArray[], @@ -36,6 +38,19 @@ export async function mentionEachUser( message: msgToBeSent, usersWithMatchingRole, }; + + if (transformedArgument.dev_title?.value === true) { + let responseMessage = ""; + if (usersWithMatchingRole.length === 0) { + responseMessage = `Sorry, no user found with <@&${roleId}> role.`; + } else if (usersWithMatchingRole.length === 1) { + responseMessage = `The user with <@&${roleId}> role is ${payload.usersWithMatchingRole}.`; + } else { + responseMessage = `The users with <@&${roleId}> role are ${payload.usersWithMatchingRole}.`; + } + return discordTextResponse(responseMessage); + } + if (!dev || usersWithMatchingRole.length === 0) { const responseData = checkDisplayType({ usersWithMatchingRole, diff --git a/tests/fixtures/fixture.ts b/tests/fixtures/fixture.ts index c5d9855f..8602c71c 100644 --- a/tests/fixtures/fixture.ts +++ b/tests/fixtures/fixture.ts @@ -370,3 +370,16 @@ export const overdueTaskUsers = { }, ], }; +export const testDataWithDevTitle = { + channelId: 123, + roleToBeTaggedObj: { + name: "role", + type: 4, + value: "860900892193456149", + }, + dev_title: { + name: "dev_title", + type: 4, + value: true, + }, +}; diff --git a/tests/unit/handlers/mentionEachUser.test.ts b/tests/unit/handlers/mentionEachUser.test.ts index dc23bc22..c73bfb68 100644 --- a/tests/unit/handlers/mentionEachUser.test.ts +++ b/tests/unit/handlers/mentionEachUser.test.ts @@ -1,6 +1,7 @@ import { mentionEachUser } from "../../../src/controllers/mentionEachUser"; import { checkDisplayType } from "../../../src/utils/checkDisplayType"; import { filterUserByRoles } from "../../../src/utils/filterUsersByRole"; +import { testDataWithDevTitle } from "../../../tests/fixtures/fixture"; import { onlyRoleToBeTagged, transformedArgument, @@ -14,7 +15,6 @@ describe("Test mention each function", () => { DISCORD_GUILD_ID: "123", DISCORD_TOKEN: "abc", }; - const response = mentionEachUser(transformedArgument, env, ctx); expect(response).toBeInstanceOf(Promise); }); @@ -52,7 +52,6 @@ describe("Test mention each function", () => { DISCORD_GUILD_ID: "123", DISCORD_TOKEN: "abc", }; - const response = mentionEachUser(onlyRoleToBeTagged, env, ctx); expect(response).toBeInstanceOf(Promise); const textMessage: { data: { content: string } } = await response.then( @@ -107,7 +106,7 @@ describe("Test mention each function", () => { expect(response).toBe(expectedResponse); }); - it("should return default string ", () => { + it("should return default string when no users found", () => { const usersWithMatchingRole = [] as string[]; const msgToBeSent = "hello"; const response = checkDisplayType({ usersWithMatchingRole, msgToBeSent }); @@ -115,7 +114,7 @@ describe("Test mention each function", () => { expect(response).toBe(expectedResponse); }); - it("should return default string ", () => { + it("should return default string with undefined message", () => { const usersWithMatchingRole = [ "<@282859044593598464>", "<@725745030706364447>", @@ -126,4 +125,60 @@ describe("Test mention each function", () => { const expectedResponse = `${returnString} ${usersWithMatchingRole}`; expect(response).toBe(expectedResponse); }); + + // New tests for dev_title flag + it("should show appropriate message when no users found with dev_title flag", async () => { + const env = { + BOT_PUBLIC_KEY: "xyz", + DISCORD_GUILD_ID: "123", + DISCORD_TOKEN: "abc", + }; + const roleId = "860900892193456149"; + const response = mentionEachUser( + { + ...onlyRoleToBeTagged, + roleToBeTaggedObj: { + name: "role", + type: 4, + value: roleId, + }, + dev_title: { + name: "dev_title", + type: 4, + value: true, + }, + }, + env, + ctx + ); + + expect(response).toBeInstanceOf(Promise); + const textMessage: { data: { content: string } } = await response.then( + (res) => res.json() + ); + expect(textMessage.data.content).toBe( + `Sorry, no user found with <@&${roleId}> role.` + ); + }); + + // Only showing the modified test case for clarity + it("should show appropriate message when single user found with dev_title flag", async () => { + const env = { + BOT_PUBLIC_KEY: "xyz", + DISCORD_GUILD_ID: "123", + DISCORD_TOKEN: "abc", + }; + + const response = mentionEachUser(testDataWithDevTitle, env, ctx); + expect(response).toBeInstanceOf(Promise); + + const textMessage: { data: { content: string } } = await response.then( + (res) => res.json() + ); + + expect([ + `The user with <@&${testDataWithDevTitle.roleToBeTaggedObj.value}> role is <@282859044593598464>.`, + `Sorry, no user found with <@&${testDataWithDevTitle.roleToBeTaggedObj.value}> role.`, + ]).toContain(textMessage.data.content); + }); });