Skip to content

Commit

Permalink
Merge branch 'develop' into feature/issue#257-2
Browse files Browse the repository at this point in the history
  • Loading branch information
Saitharun279 authored Nov 6, 2024
2 parents 1802ad4 + bd4d987 commit fe7938f
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 4 deletions.
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
15 changes: 15 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,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,
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);
});
});

0 comments on commit fe7938f

Please sign in to comment.