Skip to content

Commit 1961b68

Browse files
authored
Merge branch 'develop' into feat/delete-guild-role-api
2 parents 81ed85e + bd4d987 commit 1961b68

File tree

5 files changed

+96
-4
lines changed

5 files changed

+96
-4
lines changed

src/constants/commands.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ export const MENTION_EACH = {
5050
type: 5,
5151
require: false,
5252
},
53+
{
54+
name: "dev_title",
55+
description: "want to see extra details?",
56+
type: 5,
57+
require: false,
58+
},
5359
],
5460
};
5561

src/controllers/baseHandler.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ export async function baseHandler(
7575
displayMessageObj: data.find((item) => item.name === "message"),
7676
channelId: message.channel_id,
7777
dev: data.find((item) => item.name === "dev") as unknown as DevFlag,
78+
dev_title: data.find(
79+
(item) => item.name === "dev_title"
80+
) as unknown as DevFlag,
7881
};
7982
return await mentionEachUser(transformedArgument, env, ctx);
8083
}

src/controllers/mentionEachUser.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export async function mentionEachUser(
1616
roleToBeTaggedObj: MentionEachUserOptions;
1717
displayMessageObj?: MentionEachUserOptions;
1818
channelId: number;
19+
dev_title?: DevFlag;
1920
dev?: DevFlag;
2021
},
2122
env: env,
@@ -25,6 +26,7 @@ export async function mentionEachUser(
2526
const roleId = transformedArgument.roleToBeTaggedObj.value;
2627
const msgToBeSent = transformedArgument?.displayMessageObj?.value;
2728
const dev = transformedArgument?.dev?.value || false;
29+
const devtitle = transformedArgument?.dev_title?.value || false;
2830
// optional chaining here only because display message obj is optional argument
2931
const usersWithMatchingRole = filterUserByRoles(
3032
getMembersInServerResponse as UserArray[],
@@ -36,6 +38,19 @@ export async function mentionEachUser(
3638
message: msgToBeSent,
3739
usersWithMatchingRole,
3840
};
41+
42+
if (transformedArgument.dev_title?.value === true) {
43+
let responseMessage = "";
44+
if (usersWithMatchingRole.length === 0) {
45+
responseMessage = `Sorry, no user found with <@&${roleId}> role.`;
46+
} else if (usersWithMatchingRole.length === 1) {
47+
responseMessage = `The user with <@&${roleId}> role is ${payload.usersWithMatchingRole}.`;
48+
} else {
49+
responseMessage = `The users with <@&${roleId}> role are ${payload.usersWithMatchingRole}.`;
50+
}
51+
return discordTextResponse(responseMessage);
52+
}
53+
3954
if (!dev || usersWithMatchingRole.length === 0) {
4055
const responseData = checkDisplayType({
4156
usersWithMatchingRole,

tests/fixtures/fixture.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,3 +370,16 @@ export const overdueTaskUsers = {
370370
},
371371
],
372372
};
373+
export const testDataWithDevTitle = {
374+
channelId: 123,
375+
roleToBeTaggedObj: {
376+
name: "role",
377+
type: 4,
378+
value: "860900892193456149",
379+
},
380+
dev_title: {
381+
name: "dev_title",
382+
type: 4,
383+
value: true,
384+
},
385+
};

tests/unit/handlers/mentionEachUser.test.ts

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { mentionEachUser } from "../../../src/controllers/mentionEachUser";
22
import { checkDisplayType } from "../../../src/utils/checkDisplayType";
33
import { filterUserByRoles } from "../../../src/utils/filterUsersByRole";
4+
import { testDataWithDevTitle } from "../../../tests/fixtures/fixture";
45
import {
56
onlyRoleToBeTagged,
67
transformedArgument,
@@ -14,7 +15,6 @@ describe("Test mention each function", () => {
1415
DISCORD_GUILD_ID: "123",
1516
DISCORD_TOKEN: "abc",
1617
};
17-
1818
const response = mentionEachUser(transformedArgument, env, ctx);
1919
expect(response).toBeInstanceOf(Promise);
2020
});
@@ -52,7 +52,6 @@ describe("Test mention each function", () => {
5252
DISCORD_GUILD_ID: "123",
5353
DISCORD_TOKEN: "abc",
5454
};
55-
5655
const response = mentionEachUser(onlyRoleToBeTagged, env, ctx);
5756
expect(response).toBeInstanceOf(Promise);
5857
const textMessage: { data: { content: string } } = await response.then(
@@ -107,15 +106,15 @@ describe("Test mention each function", () => {
107106
expect(response).toBe(expectedResponse);
108107
});
109108

110-
it("should return default string ", () => {
109+
it("should return default string when no users found", () => {
111110
const usersWithMatchingRole = [] as string[];
112111
const msgToBeSent = "hello";
113112
const response = checkDisplayType({ usersWithMatchingRole, msgToBeSent });
114113
const expectedResponse = `Sorry no user found under this role.`;
115114
expect(response).toBe(expectedResponse);
116115
});
117116

118-
it("should return default string ", () => {
117+
it("should return default string with undefined message", () => {
119118
const usersWithMatchingRole = [
120119
"<@282859044593598464>",
121120
"<@725745030706364447>",
@@ -126,4 +125,60 @@ describe("Test mention each function", () => {
126125
const expectedResponse = `${returnString} ${usersWithMatchingRole}`;
127126
expect(response).toBe(expectedResponse);
128127
});
128+
129+
// New tests for dev_title flag
130+
it("should show appropriate message when no users found with dev_title flag", async () => {
131+
const env = {
132+
BOT_PUBLIC_KEY: "xyz",
133+
DISCORD_GUILD_ID: "123",
134+
DISCORD_TOKEN: "abc",
135+
};
136+
const roleId = "860900892193456149";
137+
const response = mentionEachUser(
138+
{
139+
...onlyRoleToBeTagged,
140+
roleToBeTaggedObj: {
141+
name: "role",
142+
type: 4,
143+
value: roleId,
144+
},
145+
dev_title: {
146+
name: "dev_title",
147+
type: 4,
148+
value: true,
149+
},
150+
},
151+
env,
152+
ctx
153+
);
154+
155+
expect(response).toBeInstanceOf(Promise);
156+
const textMessage: { data: { content: string } } = await response.then(
157+
(res) => res.json()
158+
);
159+
expect(textMessage.data.content).toBe(
160+
`Sorry, no user found with <@&${roleId}> role.`
161+
);
162+
});
163+
164+
// Only showing the modified test case for clarity
165+
it("should show appropriate message when single user found with dev_title flag", async () => {
166+
const env = {
167+
BOT_PUBLIC_KEY: "xyz",
168+
DISCORD_GUILD_ID: "123",
169+
DISCORD_TOKEN: "abc",
170+
};
171+
172+
const response = mentionEachUser(testDataWithDevTitle, env, ctx);
173+
expect(response).toBeInstanceOf(Promise);
174+
175+
const textMessage: { data: { content: string } } = await response.then(
176+
(res) => res.json()
177+
);
178+
179+
expect([
180+
`The user with <@&${testDataWithDevTitle.roleToBeTaggedObj.value}> role is <@282859044593598464>.`,
181+
`Sorry, no user found with <@&${testDataWithDevTitle.roleToBeTaggedObj.value}> role.`,
182+
]).toContain(textMessage.data.content);
183+
});
129184
});

0 commit comments

Comments
 (0)