Skip to content

Commit 0a9df25

Browse files
sahsisunnyfakhruddinkwvinit717shreya-mishraprakashchoudhary07
authored
Dev to main sync (RealDevSquad#135)
* Auto fetch task which are overdue * added type definitions for taskoverdue, refactored the code and removed the use of any * fixed return type of scheduled func * added fixtures and tests for getDiscordIds func * updated type def for user backend * removed public key from .toml and removed kick command from baseHandler * removed public key * added error message in getDiscordIds.ts * added fixture for taskOverdue, written test for taskOverDue, restructured the code of scheduleEventHandler, altered tests for getDiscordIds, refactored code for taskOverDueDiscordMembers, refactored code for getDiscordIds * forgot to add updated typeDefinitions for taskOverdue * extracted url to url.ts * resolved comments by:bharti * resolving comments * keeping schedule event under comments * error handling in scheduledEventHandler * resolved comments by:bharti * resolving comments by:dipayan * replaced staging url with production * inserted tracking channel id in url * extracted super user discord id to constants * BugFix: /listening (RealDevSquad#126) * bug fix * refactored te code * major fixes * resolved the comments --------- Co-authored-by: Prakash Choudhary <[email protected]> * FEATURE: Add /task command (RealDevSquad#127) * FEATURE: add code changes for /task command * feat(utils): add formatDate and formatTask utility * feat(message): update response format * FEATURE: add code changes for /task command * FEAT: add TASK command to register * Update src/utils/getNickName.ts * REFACTOR: change var name and add try-catch to getNickName fun * TEST: add tet for formatDate and formatStatusToTitleCase function * REFACTOR: extract fetchTasks fun * TEST: add test for fetchTasks * TEST(FIX): fix formatDate test * TEST: improve test coverage of fetchTasks function * TEST: add test for formatTasks function * TEST: fix failed test * TEST: fix failed test * remove console from getNickName.ts * TEST: add test for fetchTask funtion * REFACTOR: changes position of status var * REFACTOR: add try catch to fetchTasks function * FIX: lint * Making Auto fetch Overdue task live (RealDevSquad#134) * Refactor : Task command (RealDevSquad#137) * REFACTOR: add function to get RDS user data and replace formatDate function * TEST: add test for changes * TEST: add more test * REFACTOR: change date format * REFACTOR: change type file extension * FORMAT: fix prettier --------- Co-authored-by: FMK2312 <[email protected]> Co-authored-by: Fakhruddin KW <[email protected]> Co-authored-by: Vinit khandal <[email protected]> Co-authored-by: SHREYA MISHRA <[email protected]> Co-authored-by: Prakash Choudhary <[email protected]>
1 parent cff00f5 commit 0a9df25

22 files changed

+517
-13
lines changed

src/constants/commands.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,16 @@ export const LISTENING = {
4040
},
4141
],
4242
};
43+
44+
export const TASK = {
45+
name: "task",
46+
description: "display the task of the user",
47+
options: [
48+
{
49+
name: "username",
50+
description: "Nickname of the user",
51+
type: 6,
52+
required: true,
53+
},
54+
],
55+
};

src/constants/responses.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,9 @@ export const LISTENING_SUCCESS_MESSAGE =
4747
export const REMOVED_LISTENING_MESSAGE = "Listening nickname removed.";
4848
export const ALREADY_LISTENING = "You are already set to listen.";
4949
export const NOTHING_CHANGED = "Your nickname remains unchanged.";
50+
51+
export const INVALID_NICKNAME_ERROR =
52+
"Please set your discord server nickname or contact the Moderators.";
53+
export const NO_TASKS_FOUND = `No tasks found for **{{username}}**.`;
54+
export const TASKS_FETCH_FAILED = "An error occurred while fetching tasks.";
55+
export const FAILED_TO_FETCH_TASKS = `Failed to fetch tasks for **{{assignee}}**.`;

src/constants/urls.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,5 @@ export const STAGING_VERIFICATION_SITE_URL =
1212

1313
export const RDS_TRACKING_CHANNEL_URL =
1414
"https://discord.com/api/v10/channels/1136650427878871090/messages";
15+
16+
export const RDS_STATUS_SITE_URL = "https://status.realdevsquad.com";

src/controllers/baseHandler.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { commandNotFound } from "./commandNotFound";
22
import { helloCommand } from "./helloCommand";
33
import { verifyCommand } from "./verifyCommand";
44
import { mentionEachUser } from "./mentionEachUser";
5+
import { taskCommand } from "./taskCommand";
56

67
import { getCommandName } from "../utils/getCommandName";
78
import JSONResponse from "../utils/JsonResponse";
@@ -13,7 +14,13 @@ import {
1314
messageRequestDataOptions,
1415
} from "../typeDefinitions/discordMessage.types";
1516

16-
import { HELLO, LISTENING, MENTION_EACH, VERIFY } from "../constants/commands";
17+
import {
18+
HELLO,
19+
LISTENING,
20+
MENTION_EACH,
21+
VERIFY,
22+
TASK,
23+
} from "../constants/commands";
1724
import { updateNickName } from "../utils/updateNickname";
1825
import { discordEphemeralResponse } from "../utils/discordEphemeralResponse";
1926
import { removeListening } from "../utils/removeListening";
@@ -105,6 +112,10 @@ export async function baseHandler(
105112
return discordEphemeralResponse(RETRY_COMMAND);
106113
}
107114
}
115+
case getCommandName(TASK): {
116+
const data = message.data?.options as Array<messageRequestDataOptions>;
117+
return await taskCommand(data[0].value, env);
118+
}
108119
default: {
109120
return commandNotFound();
110121
}

src/controllers/taskCommand.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { task } from "../typeDefinitions/task.types";
2+
import { formatTask, generateTaskResponseMessage } from "../utils/formatTask";
3+
import { discordTextResponse } from "../utils/discordResponse";
4+
import { fetchTasks } from "../utils/fetchTasks";
5+
import { getUserDetails } from "../utils/getUserDetails";
6+
import {
7+
INVALID_NICKNAME_ERROR,
8+
NO_TASKS_FOUND,
9+
TASKS_FETCH_FAILED,
10+
} from "../constants/responses";
11+
12+
export async function taskCommand(userId: string) {
13+
const status = "IN_PROGRESS";
14+
try {
15+
const userData = await getUserDetails(userId);
16+
if (!userData.user) {
17+
return discordTextResponse(INVALID_NICKNAME_ERROR);
18+
}
19+
const username = userData.user.username;
20+
const tasksData = await fetchTasks(username, status);
21+
22+
if (!tasksData.tasks) {
23+
const errorMessage = NO_TASKS_FOUND.replace("{{username}}", username);
24+
return discordTextResponse(errorMessage);
25+
}
26+
27+
const formattedTasks = tasksData.tasks.map((task: task) =>
28+
formatTask(task)
29+
);
30+
31+
const responseMessage = generateTaskResponseMessage(
32+
username,
33+
formattedTasks,
34+
status
35+
);
36+
37+
return discordTextResponse(responseMessage);
38+
} catch (error) {
39+
console.error(error);
40+
return discordTextResponse(TASKS_FETCH_FAILED);
41+
}
42+
}

src/index.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,7 @@ export default {
8181
return router.handle(request, env);
8282
},
8383

84-
//keeping these code under comment until we patch the pagination
85-
//of overdue tasks API
86-
87-
// async scheduled(req: Request, env: env, ctx: ExecutionContext) {
88-
// ctx.waitUntil(send(env));
89-
// },
84+
async scheduled(req: Request, env: env, ctx: ExecutionContext) {
85+
ctx.waitUntil(send(env));
86+
},
9087
};

src/register.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import { HELLO, LISTENING, MENTION_EACH, VERIFY } from "./constants/commands";
1+
import {
2+
HELLO,
3+
MENTION_EACH,
4+
VERIFY,
5+
LISTENING,
6+
TASK,
7+
} from "./constants/commands";
28
import { config } from "dotenv";
39
import { DISCORD_BASE_URL } from "./constants/urls";
410
import { registerCommands } from "./utils/registerCommands";
@@ -17,7 +23,7 @@ async function registerGuildCommands(
1723
discordApplicationId?: string,
1824
discordGuildId?: string
1925
) {
20-
const commands = [HELLO, VERIFY, MENTION_EACH, LISTENING];
26+
const commands = [HELLO, VERIFY, MENTION_EACH, LISTENING, TASK];
2127

2228
try {
2329
if (!discordBotToken) throw new Error("Please provide a BOT TOKEN");
@@ -28,7 +34,7 @@ async function registerGuildCommands(
2834
const registrationUrl = `${DISCORD_BASE_URL}/applications/${discordApplicationId}/guilds/${discordGuildId}/commands`;
2935
await registerCommands(registrationUrl, discordBotToken, commands);
3036
} catch (e) {
31-
console.log(e);
37+
console.error(e);
3238
}
3339
}
3440

src/typeDefinitions/rdsUser.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
export type UserType = {
2+
id: string;
3+
profileURL?: string;
4+
discordJoinedAt?: string;
5+
roles: {
6+
archived: boolean;
7+
in_discord: boolean;
8+
member?: boolean;
9+
};
10+
created_at?: number;
11+
yoe?: number;
12+
github_created_at?: number;
13+
updated_at: number;
14+
company?: string;
15+
twitter_id?: string;
16+
first_name: string;
17+
incompleteUserDetails: boolean;
18+
website?: string;
19+
discordId?: string;
20+
last_name: string;
21+
linkedin_id?: string;
22+
picture?: {
23+
publicId: string;
24+
url: string;
25+
};
26+
instagram_id?: string;
27+
github_display_name?: string;
28+
github_id?: string;
29+
designation?: string;
30+
username: string;
31+
profileStatus?: string;
32+
};
33+
34+
export type UserResponseType = {
35+
message: string;
36+
users?: UserType[];
37+
user?: UserType;
38+
};
39+
40+
export type UserListResponseType = {
41+
message: string;
42+
count: number;
43+
users: string[];
44+
};
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
export type task = {
2+
id: string;
3+
title: string;
4+
type: string;
5+
endsOn: number;
6+
status: string;
7+
assignee?: string;
8+
percentCompleted: number;
9+
dependsOn: string[];
10+
participants?: string[];
11+
createdBy: string;
12+
github?: {
13+
issue: {
14+
assignee?: string;
15+
status: string;
16+
id: number;
17+
closedAt?: string;
18+
assigneeRdsInfo?: {
19+
firstName: string | null | undefined;
20+
lastName: string | null | undefined;
21+
username: string;
22+
};
23+
};
24+
};
25+
};
26+
27+
export type TasksResponseType = {
28+
message?: string;
29+
tasks: task[];
30+
next?: string;
31+
prev?: string;
32+
};

src/utils/fetchTasks.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { FAILED_TO_FETCH_TASKS } from "../constants/responses";
2+
import { RDS_BASE_API_URL } from "../constants/urls";
3+
import { TasksResponseType } from "../typeDefinitions/task.types";
4+
5+
async function fetchTasks(assignee: string, status: string) {
6+
try {
7+
const url = `${RDS_BASE_API_URL}/tasks?status=${status}&assignee=${assignee}&dev=true`;
8+
const response = await fetch(url);
9+
10+
if (!response.ok) {
11+
throw new Error(FAILED_TO_FETCH_TASKS.replace("{{assignee}}", assignee));
12+
}
13+
14+
const responseData: TasksResponseType = await response.json();
15+
return responseData;
16+
} catch (error) {
17+
console.error("An error occurred while fetching tasks:", error);
18+
throw error;
19+
}
20+
}
21+
22+
export { fetchTasks };

0 commit comments

Comments
 (0)