Skip to content
Merged
6 changes: 6 additions & 0 deletions src/constants/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
],
};

Expand Down
3 changes: 3 additions & 0 deletions src/controllers/baseHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
18 changes: 18 additions & 0 deletions src/controllers/mentionEachUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export async function mentionEachUser(
roleToBeTaggedObj: MentionEachUserOptions;
displayMessageObj?: MentionEachUserOptions;
channelId: number;
dev_title?: DevFlag;
dev?: DevFlag;
},
env: env,
Expand All @@ -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[],
Expand All @@ -36,13 +38,29 @@ 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) {
// Mention the single user by ID
responseMessage = `The user with <@&${roleId}> role is ${payload.usersWithMatchingRole}.`;
} else {
// Mention multiple users by their IDs
responseMessage = `The users with <@&${roleId}> role are ${payload.usersWithMatchingRole}.`;
}
return discordTextResponse(responseMessage);
}

if (!dev || usersWithMatchingRole.length === 0) {
const responseData = checkDisplayType({
usersWithMatchingRole,
msgToBeSent,
});
return discordTextResponse(responseData);
} else {
// Regular dev flow to mention users
ctx.waitUntil(
mentionEachUserInMessage({
message: payload.message,
Expand Down
13 changes: 13 additions & 0 deletions tests/fixtures/fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
};
63 changes: 59 additions & 4 deletions tests/unit/handlers/mentionEachUser.test.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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);
});
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -107,15 +106,15 @@ 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 });
const expectedResponse = `Sorry no user found under this role.`;
expect(response).toBe(expectedResponse);
});

it("should return default string ", () => {
it("should return default string with undefined message", () => {
const usersWithMatchingRole = [
"<@282859044593598464>",
"<@725745030706364447>",
Expand All @@ -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);
});
});
Loading