Skip to content

Commit 40cd98b

Browse files
j24mfakhruddinkwsahsisunnyvinit717shreya-mishra
authored
Dev to main sync (RealDevSquad#143)
* 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 * Discord bot command called user that takes a username as an option and provides user details in response * Registered /user command for a bot in a specific guild * /OOO command to get user OOO details (RealDevSquad#129) * Added utility functions to get and return user details * Test : For utility functions * Refactor : time stamp conversion --------- Co-authored-by: FMK2312 <[email protected]> Co-authored-by: Sunny Sahsi <[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]> Co-authored-by: Vinit <[email protected]> Co-authored-by: Dash Deipayan <[email protected]>
1 parent cdf3f54 commit 40cd98b

File tree

8 files changed

+99
-1
lines changed

8 files changed

+99
-1
lines changed

src/constants/commands.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,16 @@ export const OOO = {
6666
},
6767
],
6868
};
69+
70+
export const USER = {
71+
name: "user",
72+
description: "Replies with specific user details in the channel",
73+
options: [
74+
{
75+
name: "username",
76+
description: "Takes username and shows user details",
77+
type: 6,
78+
required: true,
79+
},
80+
],
81+
};

src/controllers/baseHandler.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { verifyCommand } from "./verifyCommand";
44
import { mentionEachUser } from "./mentionEachUser";
55
import { taskCommand } from "./taskCommand";
66
import { oooCommand } from "./oooCommand";
7+
import { userCommand } from "./userCommand";
78

89
import { getCommandName } from "../utils/getCommandName";
910
import JSONResponse from "../utils/JsonResponse";
@@ -22,6 +23,7 @@ import {
2223
VERIFY,
2324
TASK,
2425
OOO,
26+
USER,
2527
} from "../constants/commands";
2628
import { updateNickName } from "../utils/updateNickname";
2729
import { discordEphemeralResponse } from "../utils/discordEphemeralResponse";
@@ -122,6 +124,11 @@ export async function baseHandler(
122124
const data = message.data?.options as Array<messageRequestDataOptions>;
123125
return await oooCommand(data[0].value);
124126
}
127+
128+
case getCommandName(USER): {
129+
const data = message.data?.options as Array<messageRequestDataOptions>;
130+
return await userCommand(data[0].value, env);
131+
}
125132
default: {
126133
return commandNotFound();
127134
}

src/controllers/userCommand.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { env } from "../typeDefinitions/default.types";
2+
import { discordTextResponse } from "../utils/discordResponse";
3+
import { getUserDetails } from "../utils/getUserDetails";
4+
import { formatUserDetails } from "../utils/formatUserDetails";
5+
6+
export async function userCommand(discordId: string, env: env) {
7+
try {
8+
const userDetails = await getUserDetails(discordId);
9+
const formattedUserDetails = formatUserDetails(userDetails);
10+
return discordTextResponse(formattedUserDetails);
11+
} catch (error) {
12+
return discordTextResponse("Trouble in fetching user details.");
13+
}
14+
}

src/register.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
LISTENING,
66
TASK,
77
OOO,
8+
USER,
89
} from "./constants/commands";
910
import { config } from "dotenv";
1011
import { DISCORD_BASE_URL } from "./constants/urls";
@@ -24,7 +25,7 @@ async function registerGuildCommands(
2425
discordApplicationId?: string,
2526
discordGuildId?: string
2627
) {
27-
const commands = [HELLO, VERIFY, MENTION_EACH, LISTENING, TASK, OOO];
28+
const commands = [HELLO, VERIFY, MENTION_EACH, LISTENING, TASK, OOO, USER];
2829

2930
try {
3031
if (!discordBotToken) throw new Error("Please provide a BOT TOKEN");

src/typeDefinitions/rdsUser.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export type UserType = {
2929
designation?: string;
3030
username: string;
3131
profileStatus?: string;
32+
state?: string;
3233
};
3334

3435
export type UserResponseType = {

src/utils/formatUserDetails.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { UserResponseType } from "../typeDefinitions/rdsUser";
2+
3+
export function convertTimeStamp(userDetails: UserResponseType) {
4+
const timestamp = userDetails.user?.discordJoinedAt ?? "";
5+
const date = new Date(timestamp);
6+
7+
const day = String(date.getDate()).padStart(2, "0");
8+
const month = String(date.getMonth() + 1).padStart(2, "0");
9+
const year = date.getFullYear();
10+
11+
const hours = String(date.getHours()).padStart(2, "0");
12+
const minutes = String(date.getMinutes()).padStart(2, "0");
13+
const seconds = String(date.getSeconds()).padStart(2, "0");
14+
15+
const formattedDate = `${day}/${month}/${year} ${hours}:${minutes}:${seconds}`;
16+
return formattedDate;
17+
}
18+
19+
export function formatUserDetails(userDetails: UserResponseType) {
20+
const convertedTimestamp = convertTimeStamp(userDetails);
21+
return `
22+
## User Details
23+
**Full Name :** ${userDetails.user?.first_name} ${userDetails.user?.last_name}
24+
**RDS Discord Joined At :** ${convertedTimestamp}
25+
**State :** ${userDetails.user?.state}
26+
`;
27+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { UserResponseType } from "../../../src/typeDefinitions/rdsUser";
2+
import { formatUserDetails } from "../../../src/utils/formatUserDetails";
3+
import { userResponse } from "../../fixtures/user";
4+
import { convertTimeStamp } from "../../../src/utils/formatUserDetails";
5+
6+
describe("formatUserDetails function", () => {
7+
it("Should return a string", () => {
8+
const userData: UserResponseType = userResponse;
9+
const formattedUserDetails = formatUserDetails(userData);
10+
expect(typeof formattedUserDetails).toBe("string");
11+
});
12+
13+
it("should format user details correctly", () => {
14+
const formattedDetails = formatUserDetails(userResponse).trim();
15+
console.log(formattedDetails);
16+
const expectedFormattedDetails = `
17+
## User Details
18+
**Full Name :** Sunny Sahsi
19+
**RDS Discord Joined At :** ${convertTimeStamp(userResponse)}
20+
**State :** ACTIVE
21+
`.trim();
22+
expect(formattedDetails).toEqual(expectedFormattedDetails);
23+
});
24+
});

tests/unit/utils/getUserDetails.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,15 @@ describe("Test getUserDetails function", () => {
4848
"https://api.realdevsquad.com/users?discordId=1234567890&dev=true"
4949
);
5050
});
51+
52+
it("Should include state property", async () => {
53+
const mockResponse = userResponse;
54+
jest
55+
.spyOn(global, "fetch")
56+
.mockImplementation(() =>
57+
Promise.resolve(new JSONResponse(mockResponse))
58+
);
59+
const user = await getUserDetails(discordID);
60+
expect(userResponse.user).toHaveProperty("state");
61+
});
5162
});

0 commit comments

Comments
 (0)