Skip to content

Commit eb9e75d

Browse files
authored
Merge pull request RealDevSquad#153 from Real-Dev-Squad/develop
Dev to main sync
2 parents a0b23d9 + 1a1e418 commit eb9e75d

25 files changed

+749
-6
lines changed

config/config.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,27 @@ import {
66
VERIFICATION_SITE_URL,
77
STAGING_VERIFICATION_SITE_URL,
88
DEVELOPMENT_VERIFICATION_SITE_URL,
9+
RDS_TRACKING_CHANNEL_URL,
10+
DEVELOPMENT_RDS_TRACKING_CHANNEL_URL,
11+
STAGING_RDS_TRACKING_CHANNEL_URL,
912
} from "../src/constants/urls";
1013

1114
const config = (env: env) => {
1215
const environment: environment = {
1316
production: {
1417
RDS_BASE_API_URL: RDS_BASE_API_URL,
1518
VERIFICATION_SITE_URL: VERIFICATION_SITE_URL,
19+
TRACKING_CHANNEL_URL: RDS_TRACKING_CHANNEL_URL,
1620
},
1721
staging: {
1822
RDS_BASE_API_URL: RDS_BASE_STAGING_API_URL,
1923
VERIFICATION_SITE_URL: STAGING_VERIFICATION_SITE_URL,
24+
TRACKING_CHANNEL_URL: STAGING_RDS_TRACKING_CHANNEL_URL,
2025
},
2126
default: {
2227
RDS_BASE_API_URL: RDS_BASE_DEVELOPMENT_API_URL,
2328
VERIFICATION_SITE_URL: DEVELOPMENT_VERIFICATION_SITE_URL,
29+
TRACKING_CHANNEL_URL: DEVELOPMENT_RDS_TRACKING_CHANNEL_URL,
2430
},
2531
};
2632

src/constants/commands.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,50 @@ export const USER = {
7979
},
8080
],
8181
};
82+
83+
export const NOTIFY = {
84+
name: "notify",
85+
description: "notify the user",
86+
options: [
87+
{
88+
name: "type",
89+
description: "type of notification",
90+
type: 3,
91+
required: true,
92+
choices: [
93+
{
94+
name: "OVERDUE",
95+
value: "OVERDUE",
96+
},
97+
{
98+
name: "ONBOARDING",
99+
value: "ONBOARDING",
100+
},
101+
],
102+
},
103+
{
104+
name: "sub-type",
105+
description: "sub-type of notification",
106+
type: 3,
107+
required: false,
108+
choices: [
109+
{
110+
name: "In 1 Day",
111+
value: "1",
112+
},
113+
{
114+
name: "In 2 Day",
115+
value: "2",
116+
},
117+
{
118+
name: "> 7 Days",
119+
value: "7",
120+
},
121+
{
122+
name: "> 31 Days",
123+
value: "31",
124+
},
125+
],
126+
},
127+
],
128+
};

src/constants/inviteOptions.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export const INVITE_OPTIONS = {
2+
MAX_USE: 1,
3+
UNIQUE: true,
4+
};

src/constants/responses.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ export const STATUS_CHECK = {
1414
message: "Welcome to our discord Bot Server 👋",
1515
};
1616

17+
export const TOO_MANY_REQUESTS = {
18+
error: "Too many requests!",
19+
};
20+
1721
export const COMMAND_NOT_FOUND = "Command Not Found";
1822

1923
export const INTERNAL_SERVER_ERROR =
@@ -24,6 +28,8 @@ export const RETRY_COMMAND =
2428

2529
export const ROLE_ADDED = "Role added successfully";
2630

31+
export const INVITED_CREATED = "Invite created successfully!";
32+
2733
export const NAME_CHANGED = "User nickname changed successfully";
2834

2935
export const ROLE_REMOVED = "Role Removed successfully";
@@ -40,6 +46,9 @@ export const ROLE_FETCH_FAILED =
4046
export const BAD_REQUEST = {
4147
error: "Oops! This is not a proper request.",
4248
};
49+
export const UNAUTHORIZED = {
50+
error: "UnAuthorized!",
51+
};
4352

4453
export const LISTENING_SUCCESS_MESSAGE =
4554
"Your name is now changed to reflect your listening status";
@@ -55,3 +64,11 @@ export const TASKS_FETCH_FAILED = "An error occurred while fetching tasks.";
5564
export const FAILED_TO_FETCH_TASKS = `Failed to fetch tasks for **{{assignee}}**.`;
5665
export const USER_NOT_FOUND = `User Not Found`;
5766
export const USER_STATUS_NOT_FOUND = "No Status Found";
67+
68+
export const OVERDUE_DEFAULT_MESSAGE = "You have overdue tasks.";
69+
export const OVERDUE_CUSTOM_MESSAGE =
70+
"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.";
71+
72+
export const ONBOARDING_DEFAULT_MESSAGE = `You currently have an onboarding status. Please provide an update explaining any challenges you're facing in completing your tasks. If you're finished, consider assigning new tasks to Admin.`;
73+
74+
export const ONBOARDING_CUSTOM_MESSAGE = `Please update your status explaining why you are unable to complete your onboarding tasks within {{days}} days.`;

src/constants/urls.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,10 @@ export const STAGING_VERIFICATION_SITE_URL =
1313
export const RDS_TRACKING_CHANNEL_URL =
1414
"https://discord.com/api/v10/channels/1136650427878871090/messages";
1515

16+
export const STAGING_RDS_TRACKING_CHANNEL_URL =
17+
"https://discord.com/api/v10/channels/1154572818902696028/messages";
18+
19+
export const DEVELOPMENT_RDS_TRACKING_CHANNEL_URL =
20+
"https://discord.com/api/v10/channels/1136583634845962261/messages";
21+
1622
export const RDS_STATUS_SITE_URL = "https://status.realdevsquad.com";

src/controllers/baseHandler.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { helloCommand } from "./helloCommand";
33
import { verifyCommand } from "./verifyCommand";
44
import { mentionEachUser } from "./mentionEachUser";
55
import { taskCommand } from "./taskCommand";
6+
import { notifyCommand } from "./notifyCommand";
67
import { oooCommand } from "./oooCommand";
78
import { userCommand } from "./userCommand";
89

@@ -22,6 +23,7 @@ import {
2223
MENTION_EACH,
2324
VERIFY,
2425
TASK,
26+
NOTIFY,
2527
OOO,
2628
USER,
2729
} from "../constants/commands";
@@ -118,7 +120,11 @@ export async function baseHandler(
118120
}
119121
case getCommandName(TASK): {
120122
const data = message.data?.options as Array<messageRequestDataOptions>;
121-
return await taskCommand(data[0].value, env);
123+
return await taskCommand(data[0].value);
124+
}
125+
case getCommandName(NOTIFY): {
126+
const data = message.data?.options as Array<messageRequestDataOptions>;
127+
return await notifyCommand(data);
122128
}
123129
case getCommandName(OOO): {
124130
const data = message.data?.options as Array<messageRequestDataOptions>;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { IRequest } from "itty-router";
2+
import * as response from "../constants/responses";
3+
import { env } from "../typeDefinitions/default.types";
4+
import JSONResponse from "../utils/JsonResponse";
5+
import { verifyAuthToken } from "../utils/verifyAuthToken";
6+
import { generateDiscordLink } from "../utils/generateDiscordInvite";
7+
import { inviteLinkBody } from "../typeDefinitions/discordLink.types";
8+
9+
export async function generateInviteLink(request: IRequest, env: env) {
10+
const authHeader = request.headers.get("Authorization");
11+
if (!authHeader) {
12+
return new JSONResponse(response.BAD_SIGNATURE);
13+
}
14+
try {
15+
await verifyAuthToken(authHeader, env);
16+
const body: inviteLinkBody = await request.json();
17+
const res = await generateDiscordLink(body, env);
18+
return new JSONResponse(res);
19+
} catch (err) {
20+
return new JSONResponse(response.BAD_SIGNATURE);
21+
}
22+
}

src/controllers/notifyCommand.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import {
2+
OVERDUE_DEFAULT_MESSAGE,
3+
OVERDUE_CUSTOM_MESSAGE,
4+
ONBOARDING_DEFAULT_MESSAGE,
5+
ONBOARDING_CUSTOM_MESSAGE,
6+
} from "../constants/responses";
7+
import {
8+
UserOverdueTaskResponseType,
9+
UserResponseType,
10+
} from "../typeDefinitions/rdsUser";
11+
import { discordTextResponse } from "../utils/discordResponse";
12+
import { fetchRdsData } from "../utils/fetchRdsData";
13+
import { createTaggableDiscordIds } from "../utils/createTaggableDiscordIds";
14+
import { extractDiscordIds } from "../utils/extractDiscordIds";
15+
16+
export async function notifyCommand(data: Array<{ value: string }>) {
17+
const typeValue = data[0].value;
18+
const daysValue = data[1]?.value;
19+
20+
try {
21+
if (typeValue === "OVERDUE") {
22+
const options = {
23+
isOverdue: true,
24+
days: daysValue,
25+
};
26+
const usersResponse = (await fetchRdsData(
27+
options
28+
)) as UserOverdueTaskResponseType;
29+
const discordIDs = extractDiscordIds(usersResponse);
30+
const formattedIds = createTaggableDiscordIds(discordIDs);
31+
32+
const message = `**Message:** ${
33+
daysValue
34+
? OVERDUE_CUSTOM_MESSAGE.replace("{{days}}", daysValue)
35+
: OVERDUE_DEFAULT_MESSAGE
36+
}`;
37+
38+
const users = `**Developers:** ${formattedIds.join(", ")}`;
39+
const responseMessage = `${message}\n${users}`;
40+
return discordTextResponse(responseMessage);
41+
} else if (typeValue === "ONBOARDING") {
42+
const options = {
43+
isOnboarding: true,
44+
days: daysValue,
45+
};
46+
const users = (await fetchRdsData(options)) as UserResponseType;
47+
const discordIDs = extractDiscordIds(users);
48+
const formattedIds = createTaggableDiscordIds(discordIDs);
49+
50+
const message = `**Message:** ${
51+
daysValue
52+
? ONBOARDING_CUSTOM_MESSAGE.replace("{{days}}", daysValue)
53+
: ONBOARDING_DEFAULT_MESSAGE
54+
}`;
55+
56+
const usersMessage = `**Developers:** ${formattedIds.join(", ")}`;
57+
const responseMessage = `${message}\n${usersMessage}`;
58+
return discordTextResponse(responseMessage);
59+
}
60+
const responseMessage = "Please provide a valid type";
61+
return discordTextResponse(responseMessage);
62+
} catch (error) {
63+
console.error(error);
64+
return discordTextResponse("Something went wrong");
65+
}
66+
}

src/handlers/scheduledEventHandler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { env } from "../typeDefinitions/default.types";
22
import { taskOverDueDiscordMembers } from "../utils/taskOverDueDiscordMembers";
33
import * as error from "../constants/responses";
44
import { getDiscordIds } from "../utils/getDiscordIds";
5-
import { RDS_TRACKING_CHANNEL_URL } from "../constants/urls";
5+
import config from "../../config/config";
66
import { SUPER_USER_ONE, SUPER_USER_TWO } from "../constants/variables";
77

88
export async function send(env: env): Promise<void> {
@@ -40,7 +40,7 @@ export async function send(env: env): Promise<void> {
4040
content: stringToBeSent,
4141
};
4242

43-
const url = `${RDS_TRACKING_CHANNEL_URL}`;
43+
const url = config(env).TRACKING_CHANNEL_URL;
4444

4545
const res = await fetch(url, {
4646
method: "POST",

src/index.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { getMembersInServerHandler } from "./controllers/getMembersInServer";
1717
import { changeNickname } from "./controllers/changeNickname";
1818
import { getGuildMemberDetailsHandler } from "./controllers/getGuildMemberDetailsHandler";
1919
import { send } from "./handlers/scheduledEventHandler";
20+
import { generateInviteLink } from "./controllers/generateDiscordInvite";
2021

2122
const router = Router();
2223

@@ -44,6 +45,7 @@ router.get("/member/:id", getGuildMemberDetailsHandler);
4445
router.patch("/guild/member", changeNickname);
4546

4647
router.put("/roles/create", createGuildRoleHandler);
48+
router.post("/invite", generateInviteLink);
4749

4850
router.put("/roles/add", addGroupRoleHandler);
4951

@@ -71,10 +73,11 @@ router.all("*", async () => {
7173

7274
export default {
7375
async fetch(request: Request, env: env): Promise<Response> {
74-
if (request.method === "POST") {
76+
const apiUrls = ["/invite"];
77+
const url = new URL(request.url);
78+
if (request.method === "POST" && !apiUrls.includes(url.pathname)) {
7579
const isVerifiedRequest = await verifyBot(request, env);
7680
if (!isVerifiedRequest) {
77-
console.error("Invalid Request");
7881
return new JSONResponse(response.BAD_SIGNATURE, { status: 401 });
7982
}
8083
}

0 commit comments

Comments
 (0)