Skip to content

Commit e111a13

Browse files
authored
Add group invite slash command (#259)
* add group invite command * add test for group invite
1 parent 71db764 commit e111a13

File tree

12 files changed

+194
-0
lines changed

12 files changed

+194
-0
lines changed

config/config.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import {
1111
STAGING_RDS_TRACKING_CHANNEL_URL,
1212
RDS_STATUS_SITE_URL,
1313
RDS_STAGING_STATUS_SITE_URL,
14+
RDS_DASHBOARD_SITE_URL,
15+
RDS_STAGING_DASHBOARD_SITE_URL,
1416
} from "../src/constants/urls";
1517
import {
1618
DISCORD_PROFILE_SERVICE_HELP_GROUP,
@@ -26,13 +28,15 @@ const config = (env: env) => {
2628
TRACKING_CHANNEL_URL: RDS_TRACKING_CHANNEL_URL,
2729
PROFILE_SERVICE_HELP_GROUP_ID: DISCORD_PROFILE_SERVICE_HELP_GROUP,
2830
RDS_STATUS_SITE_URL: RDS_STATUS_SITE_URL,
31+
DASHBOARD_SITE_URL: RDS_DASHBOARD_SITE_URL,
2932
},
3033
staging: {
3134
RDS_BASE_API_URL: RDS_BASE_STAGING_API_URL,
3235
VERIFICATION_SITE_URL: STAGING_VERIFICATION_SITE_URL,
3336
TRACKING_CHANNEL_URL: STAGING_RDS_TRACKING_CHANNEL_URL,
3437
PROFILE_SERVICE_HELP_GROUP_ID: DISCORD_PROFILE_SERVICE_STAGING_HELP_GROUP,
3538
RDS_STATUS_SITE_URL: RDS_STAGING_STATUS_SITE_URL,
39+
DASHBOARD_SITE_URL: RDS_STAGING_DASHBOARD_SITE_URL,
3640
},
3741
default: {
3842
RDS_BASE_API_URL: RDS_BASE_DEVELOPMENT_API_URL,
@@ -41,6 +45,7 @@ const config = (env: env) => {
4145
PROFILE_SERVICE_HELP_GROUP_ID:
4246
DISCORD_PROFILE_SERVICE_DEVELOPMENT_HELP_GROUP,
4347
RDS_STATUS_SITE_URL: RDS_STATUS_SITE_URL,
48+
DASHBOARD_SITE_URL: RDS_DASHBOARD_SITE_URL,
4449
},
4550
};
4651

src/constants/commands.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,25 @@ export const VERIFY = {
99
"Generate a link with user specific token to link with RDS backend.",
1010
};
1111

12+
export const GROUP_INVITE = {
13+
name: "group-invite",
14+
description: "Send group invite link for the user.",
15+
options: [
16+
{
17+
name: "name",
18+
description: "User to send group invite link",
19+
type: 6,
20+
required: true,
21+
},
22+
{
23+
name: "role",
24+
description: "Role you want to invite to the user",
25+
type: 8,
26+
required: true,
27+
},
28+
],
29+
};
30+
1231
export const MENTION_EACH = {
1332
name: "mention-each",
1433
description: "mention each user with this role",

src/constants/responses.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +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";
69+
6870
export const OVERDUE_DEFAULT_MESSAGE = "You have overdue tasks.";
6971
export const OVERDUE_CUSTOM_MESSAGE =
7072
"Please be aware that you currently have tasks that are overdue or due within the next {{days}} day. If you require additional time to complete these tasks, kindly submit an extension request.";

src/constants/urls.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,7 @@ export const DEVELOPMENT_RDS_TRACKING_CHANNEL_URL =
2222
export const RDS_STATUS_SITE_URL = "https://status.realdevsquad.com";
2323
export const RDS_STAGING_STATUS_SITE_URL =
2424
"https://staging-status.realdevsquad.com";
25+
26+
export const RDS_DASHBOARD_SITE_URL = "https://dashboard.realdevsquad.com";
27+
export const RDS_STAGING_DASHBOARD_SITE_URL =
28+
"https://staging-dashboard.realdevsquad.com";

src/controllers/baseHandler.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
OOO,
2929
USER,
3030
REMOVE,
31+
GROUP_INVITE,
3132
} from "../constants/commands";
3233
import { updateNickName } from "../utils/updateNickname";
3334
import { discordEphemeralResponse } from "../utils/discordEphemeralResponse";
@@ -42,6 +43,7 @@ import {
4243
} from "../constants/responses";
4344
import { DevFlag } from "../typeDefinitions/filterUsersByRole";
4445
import { kickEachUser } from "./kickEachUser";
46+
import { groupInvite } from "./groupInvite";
4547

4648
export async function baseHandler(
4749
message: discordMessageRequest,
@@ -159,6 +161,12 @@ export async function baseHandler(
159161
) as unknown as DevFlag;
160162
return await userCommand(data[0].value, env, dev);
161163
}
164+
165+
case getCommandName(GROUP_INVITE): {
166+
const data = message.data?.options as Array<messageRequestDataOptions>;
167+
168+
return await groupInvite(data[0].value, data[1].value, env);
169+
}
162170
default: {
163171
return commandNotFound();
164172
}

src/controllers/groupInvite.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import config from "../../config/config";
2+
import { env } from "../typeDefinitions/default.types";
3+
import { discordTextResponse } from "../utils/discordResponse";
4+
import * as DiscordGroups from "../utils/fetchDiscordGroups";
5+
import JSONResponse from "../utils/JsonResponse";
6+
7+
export async function groupInvite(
8+
userId: string,
9+
roleId: string,
10+
env: env
11+
): Promise<JSONResponse> {
12+
const response = await DiscordGroups.fetchDiscordGroups(env);
13+
const group = response.groups.find((group) => group.roleid === roleId);
14+
15+
if (!group) {
16+
return discordTextResponse(`<@&${roleId}> is not a valid group.`);
17+
}
18+
19+
const groupName = group.rolename.replace(/^group-/, "");
20+
21+
return discordTextResponse(
22+
`<@${userId}> join the group <@&${roleId}> via the link below:\n ${
23+
config(env).DASHBOARD_SITE_URL
24+
}/groups/?dev=true&name=${groupName}`
25+
);
26+
}

src/register.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
OOO,
1010
USER,
1111
REMOVE,
12+
GROUP_INVITE,
1213
} from "./constants/commands";
1314
import { config } from "dotenv";
1415
import { DISCORD_BASE_URL } from "./constants/urls";
@@ -39,6 +40,7 @@ async function registerGuildCommands(
3940
NOTIFY_OVERDUE,
4041
NOTIFY_ONBOARDING,
4142
REMOVE,
43+
GROUP_INVITE,
4244
];
4345

4446
try {

src/typeDefinitions/default.types.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export interface environment {
77
}
88

99
export interface variables {
10+
DASHBOARD_SITE_URL: string;
1011
RDS_BASE_API_URL: string;
1112
VERIFICATION_SITE_URL: string;
1213
TRACKING_CHANNEL_URL: string;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
export type GroupType = {
2+
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[];
18+
};

src/utils/fetchDiscordGroups.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import config from "../../config/config";
2+
import { FAILED_TO_FETCH_DISCORD_GROUPS } from "../constants/responses";
3+
import { env } from "../typeDefinitions/default.types";
4+
import { GroupResponseType } from "../typeDefinitions/group.types";
5+
6+
async function fetchDiscordGroups(env: env): Promise<GroupResponseType> {
7+
try {
8+
const url = `${config(env).RDS_BASE_API_URL}/discord-actions/groups`;
9+
const response = await fetch(url);
10+
11+
if (!response.ok) {
12+
throw new Error(FAILED_TO_FETCH_DISCORD_GROUPS);
13+
}
14+
15+
const responseData: GroupResponseType = await response.json();
16+
return responseData;
17+
} catch (error) {
18+
console.error("An error occurred while fetching discord groups:", error);
19+
throw error;
20+
}
21+
}
22+
23+
export { fetchDiscordGroups };

0 commit comments

Comments
 (0)