Skip to content

Commit 12b1a7b

Browse files
authored
change discord group fetching logic (#261)
* fix discord group not working * fix tests
1 parent e111a13 commit 12b1a7b

File tree

7 files changed

+64
-105
lines changed

7 files changed

+64
-105
lines changed

src/constants/responses.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ export const FAILED_TO_FETCH_TASKS = `Failed to fetch tasks for **{{assignee}}**
6565
export const USER_NOT_FOUND = `User Not Found`;
6666
export const USER_STATUS_NOT_FOUND = "No Status Found";
6767

68-
export const FAILED_TO_FETCH_DISCORD_GROUPS = "Failed to fetch discord groups";
68+
export const FAILED_TO_FETCH_DISCORD_GUILD_ROLE =
69+
"Failed to fetch discord guild role";
6970

7071
export const OVERDUE_DEFAULT_MESSAGE = "You have overdue tasks.";
7172
export const OVERDUE_CUSTOM_MESSAGE =

src/controllers/groupInvite.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11
import config from "../../config/config";
22
import { env } from "../typeDefinitions/default.types";
33
import { discordTextResponse } from "../utils/discordResponse";
4-
import * as DiscordGroups from "../utils/fetchDiscordGroups";
4+
import * as DiscordGroups from "../utils/fetchDiscordGroupById";
55
import JSONResponse from "../utils/JsonResponse";
66

77
export async function groupInvite(
88
userId: string,
99
roleId: string,
1010
env: env
1111
): Promise<JSONResponse> {
12-
const response = await DiscordGroups.fetchDiscordGroups(env);
13-
const group = response.groups.find((group) => group.roleid === roleId);
12+
const group = await DiscordGroups.fetchDiscordGroupById(roleId, env);
1413

15-
if (!group) {
14+
if (!group.name.startsWith("group-")) {
1615
return discordTextResponse(`<@&${roleId}> is not a valid group.`);
1716
}
1817

19-
const groupName = group.rolename.replace(/^group-/, "");
18+
const groupName = group.name.replace(/^group-/, "");
2019

2120
return discordTextResponse(
2221
`<@${userId}> join the group <@&${roleId}> via the link below:\n ${
Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,4 @@
1-
export type GroupType = {
1+
export type DiscordGuildRole = {
2+
name: string;
23
id: string;
3-
date: {
4-
_seconds: number;
5-
_nanoseconds: number;
6-
};
7-
createdBy: string;
8-
rolename: string;
9-
roleid: string;
10-
description: string;
11-
memberCount: number;
12-
isMember: boolean;
13-
};
14-
15-
export type GroupResponseType = {
16-
message: string;
17-
groups: GroupType[];
184
};

src/utils/fetchDiscordGroupById.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { FAILED_TO_FETCH_DISCORD_GUILD_ROLE } from "../constants/responses";
2+
import { DISCORD_BASE_URL } from "../constants/urls";
3+
import { env } from "../typeDefinitions/default.types";
4+
import { DiscordGuildRole } from "../typeDefinitions/group.types";
5+
import createDiscordHeaders from "./createDiscordHeaders";
6+
7+
async function fetchDiscordGroupById(
8+
roleId: string,
9+
env: env
10+
): Promise<DiscordGuildRole> {
11+
try {
12+
const url = `${DISCORD_BASE_URL}/guilds/${env.DISCORD_GUILD_ID}/roles/${roleId}`;
13+
const headers: HeadersInit = createDiscordHeaders({
14+
token: env.DISCORD_TOKEN,
15+
});
16+
const options = {
17+
method: "GET",
18+
headers,
19+
};
20+
const response = await fetch(url, options);
21+
22+
if (!response.ok) {
23+
throw new Error(FAILED_TO_FETCH_DISCORD_GUILD_ROLE);
24+
}
25+
26+
const responseData: DiscordGuildRole = await response.json();
27+
return responseData;
28+
} catch (error) {
29+
console.error("An error occurred while fetching discord groups:", error);
30+
throw error;
31+
}
32+
}
33+
34+
export { fetchDiscordGroupById };

src/utils/fetchDiscordGroups.ts

Lines changed: 0 additions & 23 deletions
This file was deleted.

tests/fixtures/groups.ts

Lines changed: 9 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,11 @@
1-
import { GroupResponseType } from "../../src/typeDefinitions/group.types";
1+
import { DiscordGuildRole } from "../../src/typeDefinitions/group.types";
22

3-
export const groups: GroupResponseType = {
4-
message: "Roles fetched successfully!",
5-
groups: [
6-
{
7-
id: "27EdauP9UmxCTgAMDPpl",
8-
date: {
9-
_seconds: 1719334613,
10-
_nanoseconds: 934000000,
11-
},
12-
createdBy: "zXQpimWaGWOFF2sLyrFt",
13-
rolename: "group-testing 3",
14-
roleid: "1255205109340573782",
15-
description: "for testing",
16-
memberCount: 0,
17-
isMember: false,
18-
},
19-
{
20-
id: "ELjCeNZxhHupn8qU5pWI",
21-
date: {
22-
_seconds: 1718771669,
23-
_nanoseconds: 27000000,
24-
},
25-
createdBy: "zXQpimWaGWOFF2sLyrFt",
26-
rolename: "group-testing",
27-
roleid: "1252843931306164298",
28-
description: "for testing",
29-
memberCount: 0,
30-
isMember: false,
31-
},
32-
{
33-
id: "tO4vZe5CC690yOb9Txlh",
34-
date: {
35-
_seconds: 1718771969,
36-
_nanoseconds: 680000000,
37-
},
38-
createdBy: "zXQpimWaGWOFF2sLyrFt",
39-
rolename: "group-testinge",
40-
roleid: "1252845191472087050",
41-
description: "",
42-
memberCount: 0,
43-
isMember: false,
44-
},
45-
],
3+
export const group: DiscordGuildRole = {
4+
id: "1",
5+
name: "group-frontend",
6+
};
7+
8+
export const invalidGroup: DiscordGuildRole = {
9+
id: "2",
10+
name: "invalidRole",
4611
};

tests/unit/handlers/groupInvite.test.ts

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,36 @@
11
import { environment } from "../../fixtures/config";
2-
import * as DiscordGroups from "../../../src/utils/fetchDiscordGroups";
2+
import * as DiscordGroups from "../../../src/utils/fetchDiscordGroupById";
33
import JSONResponse from "../../../src/utils/JsonResponse";
44
import { groupInvite } from "../../../src/controllers/groupInvite";
5-
import { groups } from "../../fixtures/groups";
5+
import { group, invalidGroup } from "../../fixtures/groups";
66
import { discordTextResponse } from "../../../src/utils/discordResponse";
77

88
describe("Test /group-invite command", () => {
9-
beforeEach(() => {
10-
jest
11-
.spyOn(DiscordGroups, "fetchDiscordGroups")
12-
.mockImplementation(() => Promise.resolve(groups));
13-
});
14-
159
afterEach(() => {
1610
jest.resetAllMocks();
1711
jest.restoreAllMocks();
1812
});
1913

2014
it("Should be an instance of JSONResponse", async () => {
21-
const response = await groupInvite(
22-
"1",
23-
groups.groups[0].roleid,
24-
environment[0]
25-
);
15+
jest
16+
.spyOn(DiscordGroups, "fetchDiscordGroupById")
17+
.mockImplementation(() => Promise.resolve(group));
18+
19+
const response = await groupInvite("1", group.id, environment[0]);
2620

2721
expect(response).toBeInstanceOf(JSONResponse);
2822
});
2923

3024
it("Should return a discordTextResponse if group is not found", async () => {
31-
const invalidRoleId = "invalidRoleId";
25+
jest
26+
.spyOn(DiscordGroups, "fetchDiscordGroupById")
27+
.mockImplementation(() => Promise.resolve(invalidGroup));
28+
3229
const expectedResponse = discordTextResponse(
33-
`<@&${invalidRoleId}> is not a valid group.`
30+
`<@&${invalidGroup.id}> is not a valid group.`
3431
);
3532

36-
const response = await groupInvite("1", invalidRoleId, environment[0]);
33+
const response = await groupInvite("1", invalidGroup.id, environment[0]);
3734

3835
expect(await response.json()).toEqual(await expectedResponse.json());
3936
});

0 commit comments

Comments
 (0)