Skip to content

Commit b92d39b

Browse files
put the changes under same feature flag
1 parent 4d08cf6 commit b92d39b

File tree

3 files changed

+48
-74
lines changed

3 files changed

+48
-74
lines changed

src/controllers/mentionEachUser.ts

Lines changed: 20 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -21,49 +21,35 @@ export async function mentionEachUser(
2121
env: env,
2222
ctx: ExecutionContext
2323
) {
24-
const isFeatureEnabled = true;
25-
26-
if (!isFeatureEnabled) {
27-
return discordTextResponse("This feature is currently disabled.");
28-
}
29-
3024
const getMembersInServerResponse = await getMembersInServer(env);
3125
const roleId = transformedArgument.roleToBeTaggedObj.value;
32-
const msgToBeSent = transformedArgument?.displayMessageObj?.value; // Get custom message
26+
const msgToBeSent = transformedArgument?.displayMessageObj?.value;
3327
const dev = transformedArgument?.dev?.value || false;
3428

3529
const usersWithMatchingRole = filterUserByRoles(
3630
getMembersInServerResponse as UserArray[],
3731
roleId
3832
);
39-
40-
// Use the custom message if provided, otherwise construct the default message
41-
let responseMessage;
42-
const roleTag = `<@&${roleId}>`;
43-
const userList = usersWithMatchingRole.join(", ");
44-
if (usersWithMatchingRole.length === 0) {
45-
responseMessage =
46-
msgToBeSent || `Sorry no user found under <@&${roleId}> role.`;
47-
} else if (usersWithMatchingRole.length === 1) {
48-
responseMessage = `The user with ${roleTag} roles is: ${userList}`;
49-
} else {
50-
responseMessage = `The users with ${roleTag} roles are: ${userList}`;
51-
}
52-
33+
const payload = {
34+
channelId: transformedArgument.channelId,
35+
roleId: roleId,
36+
message: msgToBeSent,
37+
usersWithMatchingRole,
38+
};
5339
if (!dev || usersWithMatchingRole.length === 0) {
54-
return discordTextResponse(responseMessage);
40+
const responseData = checkDisplayType({
41+
usersWithMatchingRole,
42+
msgToBeSent,
43+
roleId,
44+
});
45+
return discordTextResponse(responseData);
5546
} else {
56-
ctx.waitUntil(
57-
mentionEachUserInMessage({
58-
message: responseMessage,
59-
userIds: usersWithMatchingRole,
60-
channelId: transformedArgument.channelId,
61-
env,
62-
})
63-
);
64-
65-
return discordTextResponse(
66-
`Found ${usersWithMatchingRole.length} users with matched role, mentioning them shortly...`
67-
);
47+
let responseMessage = "";
48+
if (usersWithMatchingRole.length === 1) {
49+
responseMessage = `The user with <@&${roleId}> role is: ${payload.usersWithMatchingRole}`;
50+
} else {
51+
responseMessage = `The users with <@&${roleId}> role are: ${payload.usersWithMatchingRole} `;
52+
}
53+
return discordTextResponse(responseMessage);
6854
}
6955
}

src/utils/checkDisplayType.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@
44
export function checkDisplayType({
55
usersWithMatchingRole,
66
msgToBeSent,
7+
roleId,
78
}: {
89
msgToBeSent?: string;
910
usersWithMatchingRole: string[];
11+
roleId?: string;
1012
}) {
1113
if (usersWithMatchingRole.length > 0) {
1214
const returnString = msgToBeSent ? msgToBeSent : "";
1315
return `${returnString} ${usersWithMatchingRole}`;
1416
} else {
15-
return `Sorry no user found under this role.`;
17+
return `Sorry no user found with <@&${roleId ?? "undefined"}> role.`;
1618
}
1719
}

tests/unit/handlers/mentionEachUser.test.ts

Lines changed: 25 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ describe("Test mention each function", () => {
4343
(res) => res.json()
4444
);
4545
expect(textMessage.data.content).toBe(
46-
`Sorry no user found under <@&${roleId}> role.`
46+
`Sorry no user found with <@&${roleId}> role.`
4747
);
4848
});
4949

@@ -60,7 +60,7 @@ describe("Test mention each function", () => {
6060
(res) => res.json()
6161
);
6262
expect(textMessage.data.content).toBe(
63-
`Sorry no user found under <@&${roleId}> role.`
63+
`Sorry no user found with <@&${roleId}> role.`
6464
);
6565
});
6666

@@ -108,11 +108,16 @@ describe("Test mention each function", () => {
108108
expect(response).toBe(expectedResponse);
109109
});
110110

111-
it("should return default string ", () => {
112-
const usersWithMatchingRole = [] as string[];
111+
it("should return default string", () => {
112+
const roleId = "1118201414078976192";
113+
const usersWithMatchingRole: string[] = [];
113114
const msgToBeSent = "hello";
114-
const response = checkDisplayType({ usersWithMatchingRole, msgToBeSent });
115-
const expectedResponse = `Sorry no user found under this role.`;
115+
const response = checkDisplayType({
116+
usersWithMatchingRole,
117+
msgToBeSent,
118+
roleId,
119+
});
120+
const expectedResponse = `Sorry no user found with <@&${roleId}> role.`;
116121
expect(response).toBe(expectedResponse);
117122
});
118123

@@ -128,37 +133,18 @@ describe("Test mention each function", () => {
128133
expect(response).toBe(expectedResponse);
129134
});
130135

131-
it("should handle custom message when provided", async () => {
132-
const env = {
133-
BOT_PUBLIC_KEY: "xyz",
134-
DISCORD_GUILD_ID: "123",
135-
DISCORD_TOKEN: "abc",
136-
};
137-
const customArg = {
138-
...transformedArgument,
139-
displayMessageObj: {
140-
name: "displayMessage",
141-
type: 3,
142-
value: "Custom message:",
143-
},
144-
};
145-
const response = mentionEachUser(customArg, env, ctx);
146-
expect(response).toBeInstanceOf(Promise);
147-
const textMessage: { data: { content: string } } = await response.then(
148-
(res) => res.json()
149-
);
150-
expect(textMessage.data.content).toContain("Custom message:");
151-
});
152-
153-
it("should handle message with no matching users", () => {
154-
const usersWithMatchingRole = [] as string[];
155-
const roleId = "1118201414078976192";
156-
const msgToBeSent = "No users found:";
157-
const response = checkDisplayType({ usersWithMatchingRole, msgToBeSent });
158-
const modifiedResponse = response.replace("this", `<@&${roleId}>`);
159-
expect(modifiedResponse).toBe(
160-
`Sorry no user found under <@&${roleId}> role.`
161-
);
136+
describe("checkDisplayType", () => {
137+
it("should handle message with no matching users", () => {
138+
const usersWithMatchingRole: string[] = [];
139+
const roleId = "1118201414078976192";
140+
const msgToBeSent = "No users found:";
141+
const response = checkDisplayType({
142+
usersWithMatchingRole,
143+
msgToBeSent,
144+
roleId,
145+
});
146+
expect(response).toBe(`Sorry no user found with <@&${roleId}> role.`);
147+
});
162148
});
163149
it("should handle case when only one user found", () => {
164150
const roleId = "860900892193456149";
@@ -175,9 +161,9 @@ describe("Test mention each function", () => {
175161
},
176162
];
177163
const response = filterUserByRoles(optionsArray, roleId);
178-
const message = `The user with <@&${roleId}> roles is: ${response}`;
164+
const message = `The user with <@&${roleId}> role is: ${response}`;
179165
expect(message).toBe(
180-
`The user with <@&${roleId}> roles is: <@${optionsArray[0].user.id}>`
166+
`The user with <@&${roleId}> role is: <@${optionsArray[0].user.id}>`
181167
);
182168
});
183169
});

0 commit comments

Comments
 (0)