From 6fac2a029b2d0ffd2576ade63370ac3b6c841370 Mon Sep 17 00:00:00 2001 From: Sunny Sahsi Date: Sat, 13 Apr 2024 07:47:05 +0530 Subject: [PATCH 1/4] refactor: change API endpoint and related logic to get overdue task users who are in server --- src/handlers/scheduledEventHandler.ts | 20 +++++++++++--------- src/utils/taskOverDueDiscordMembers.ts | 16 ++++++---------- 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/src/handlers/scheduledEventHandler.ts b/src/handlers/scheduledEventHandler.ts index dc2ff21c..a21bdf5f 100644 --- a/src/handlers/scheduledEventHandler.ts +++ b/src/handlers/scheduledEventHandler.ts @@ -1,24 +1,26 @@ import { env } from "../typeDefinitions/default.types"; import { taskOverDueDiscordMembers } from "../utils/taskOverDueDiscordMembers"; -import * as error from "../constants/responses"; -import { getDiscordIds } from "../utils/getDiscordIds"; import config from "../../config/config"; import { SUPER_USER_ONE, SUPER_USER_TWO } from "../constants/variables"; export async function send(env: env): Promise { try { - const assigneeIds: string[] | string = await taskOverDueDiscordMembers(); + let discordIds: string[] | string = await taskOverDueDiscordMembers(); - //A user might have more than one task which are running red - //so to mention them just once, we are using Set to filter out - const discordIds: string[] | string = await getDiscordIds(assigneeIds); - const uniqueDiscordIds = [...new Set(discordIds)]; + if (!Array.isArray(discordIds)) { + // If it's not an array, convert it to an array with a single element + discordIds = [discordIds]; + } + + if (discordIds.length === 0) { + return; + } //notifying the two users with the authority. let stringToBeSent = `<@${SUPER_USER_ONE}> <@${SUPER_USER_TWO}>\nThese people have their task running red:\n`; let forFormatting = 0; - uniqueDiscordIds.forEach((id) => { + discordIds.forEach((id: string) => { const discordUser = `<@${id}> `; stringToBeSent += discordUser; forFormatting++; @@ -35,7 +37,7 @@ export async function send(env: env): Promise { const url = config(env).TRACKING_CHANNEL_URL; - const res = await fetch(url, { + await fetch(url, { method: "POST", body: JSON.stringify(bodyObj), headers: { diff --git a/src/utils/taskOverDueDiscordMembers.ts b/src/utils/taskOverDueDiscordMembers.ts index c391bca3..3d915bc0 100644 --- a/src/utils/taskOverDueDiscordMembers.ts +++ b/src/utils/taskOverDueDiscordMembers.ts @@ -1,24 +1,20 @@ import { RDS_BASE_API_URL } from "../constants/urls"; -import { - TaskOverdue, - TaskOverdueResponse, -} from "../typeDefinitions/taskOverdue.types"; -import * as errors from "../constants/responses"; +import { UserOverdueTaskResponseType } from "../typeDefinitions/rdsUser"; export const taskOverDueDiscordMembers = async (): Promise< string[] | string > => { try { - const overDueUrl = `${RDS_BASE_API_URL}/tasks?dev=true&status=overdue&size=100`; + const overDueUrl = `${RDS_BASE_API_URL}/users?query=filterBy:overdue_tasks`; const response: Response = await fetch(overDueUrl); - const responseObj: TaskOverdueResponse = await response.json(); + const responseObj: UserOverdueTaskResponseType = await response.json(); - const assigneeIds: string[] = responseObj.tasks.map( - (task: TaskOverdue) => task.assigneeId + const discordIds: string[] = responseObj.users.map( + (user) => user.discordId ); - return assigneeIds; + return discordIds; } catch (e) { console.log(e); throw e; From 516bb0038dcd5ba4ebe49dc72b79b453b92f58d2 Mon Sep 17 00:00:00 2001 From: Sunny Sahsi Date: Sat, 13 Apr 2024 07:48:21 +0530 Subject: [PATCH 2/4] test: change test fixtures data and expect logic based on previous commit changes --- tests/fixtures/fixture.ts | 66 ++++++++++++------- .../utils/taskOverDueDiscordMembers.test.ts | 11 ++-- 2 files changed, 48 insertions(+), 29 deletions(-) diff --git a/tests/fixtures/fixture.ts b/tests/fixtures/fixture.ts index ea16d7dc..ecc0453c 100644 --- a/tests/fixtures/fixture.ts +++ b/tests/fixtures/fixture.ts @@ -245,30 +245,6 @@ export const userBackendMock: UserBackend = { }, }; -export const taskOverdueMock: TaskOverdueResponse = { - message: "Tasks returned Successfully", - tasks: [ - { - id: "1234567", - percentCompleted: 60, - endsOn: "1686528000", - isNoteworthy: false, - createdBy: "random", - assignee: "vineeeet", - title: "task dependsOn", - type: "feature", - status: "SMOKE_TESTING", - assigneeId: "12345", - dependsOn: [ - "1NtgKz4lzyiGMHSyBWje", - " HdRj3T603v0L5Pn80GUq", - " C29GgTuPWK32UqcyImsA", - ], - startedOn: "1686527000", - }, - ], -}; - export const userStatusMock: UserStatus = { id: "someId", userId: "someUserId", @@ -327,3 +303,45 @@ export const memberGroupRoleResponseList = [ { userid: "YYYY", roleid: "YYYY", success: true }, { userid: "ZZZZ", roleid: "ZZZZ", success: true }, ]; + +export const overdueTaskUsers = { + message: "Users returned successfully!", + count: 7, + users: [ + { + id: "XAF7rSUvk4p0d098qWYS", + discordId: "154585730465660929", + username: "ankush", + }, + { + id: "C2XUDSTtDooWLJ44iBYr", + discordId: "875289457589379102", + username: "vinayak-g", + }, + { + id: "DU3gRW3xQY8lRt3DmF6i", + discordId: "987654321098765432", + username: "random_user1", + }, + { + id: "wH5f1kuYp3vRZydF70sA", + discordId: "123456789012345678", + username: "random_user2", + }, + { + id: "jV8d5ZbEDJc7Mnq6a2g9", + discordId: "567890123456789012", + username: "random_user3", + }, + { + id: "qX2zOyB1n7PmIv6TcR8w", + discordId: "890123456789012345", + username: "random_user4", + }, + { + id: "pY6cVuA2b5sHgNq9fJ3m", + discordId: "345678901234567890", + username: "random_user5", + }, + ], +}; diff --git a/tests/unit/utils/taskOverDueDiscordMembers.test.ts b/tests/unit/utils/taskOverDueDiscordMembers.test.ts index 0bd962c3..cf97442c 100644 --- a/tests/unit/utils/taskOverDueDiscordMembers.test.ts +++ b/tests/unit/utils/taskOverDueDiscordMembers.test.ts @@ -1,22 +1,23 @@ import { taskOverDueDiscordMembers } from "../../../src/utils/taskOverDueDiscordMembers"; -import { taskOverdueMock } from "../../fixtures/fixture"; +import { overdueTaskUsers } from "../../fixtures/fixture"; import JSONResponse from "../../../src/utils/JsonResponse"; import { RDS_BASE_API_URL } from "../../../src/constants/urls"; describe("taskOverDueDiscordMembers()", () => { test("should return all the tasks which are overdue", async () => { - const mockResponse = taskOverdueMock; + const mockResponse = overdueTaskUsers; jest .spyOn(global, "fetch") .mockImplementation(() => Promise.resolve(new JSONResponse(mockResponse)) ); const result = await taskOverDueDiscordMembers(); + const discordIds = mockResponse.users.map((user) => user.discordId); - expect(result).toEqual([taskOverdueMock.tasks[0].assigneeId]); + expect(result).toEqual(discordIds); expect(global.fetch).toHaveBeenCalledWith( - `${RDS_BASE_API_URL}/tasks?dev=true&status=overdue&size=100` + `${RDS_BASE_API_URL}/users?query=filterBy:overdue_tasks` ); expect(global.fetch).toBeCalledTimes(1); @@ -34,7 +35,7 @@ describe("taskOverDueDiscordMembers()", () => { } expect(global.fetch).toHaveBeenCalledWith( - `${RDS_BASE_API_URL}/tasks?dev=true&status=overdue&size=100` + `${RDS_BASE_API_URL}/users?query=filterBy:overdue_tasks` ); }); }); From 9a116d2ddcc204712052d926a7957c6763bdb165 Mon Sep 17 00:00:00 2001 From: Sunny Sahsi Date: Sat, 13 Apr 2024 07:51:11 +0530 Subject: [PATCH 3/4] NPM Script : Introduce new command for automated code style correction: 'npm run lint-fix' for linting and 'npm run format-fix' for formatting, ensuring consistent code style across the project. --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index f7b190e9..ed1a11d8 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ "lint-fix": "eslint --fix .", "format-check": "echo 'Checking the formatting of your code 👨‍💻' && prettier --check . && echo '✅ code matches prettier formatting'", "format-fix": "prettier --write .", + "fix": "npm run lint-fix && npm run format-fix", "ngrok": "ngrok http 8787", "register": "ts-node-esm src/register.ts" }, From b0f91cd27be8506ea85c56062950cb50e5d1cb0e Mon Sep 17 00:00:00 2001 From: Sunny Sahsi Date: Tue, 16 Apr 2024 05:33:07 +0530 Subject: [PATCH 4/4] refactor: refactor and filtred discord ids --- src/handlers/scheduledEventHandler.ts | 14 +++++++------- src/utils/taskOverDueDiscordMembers.ts | 4 +--- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/src/handlers/scheduledEventHandler.ts b/src/handlers/scheduledEventHandler.ts index a21bdf5f..acddabb4 100644 --- a/src/handlers/scheduledEventHandler.ts +++ b/src/handlers/scheduledEventHandler.ts @@ -5,14 +5,14 @@ import { SUPER_USER_ONE, SUPER_USER_TWO } from "../constants/variables"; export async function send(env: env): Promise { try { - let discordIds: string[] | string = await taskOverDueDiscordMembers(); + const discordIds: string[] = await taskOverDueDiscordMembers(); - if (!Array.isArray(discordIds)) { - // If it's not an array, convert it to an array with a single element - discordIds = [discordIds]; - } + const superUsers = [SUPER_USER_ONE, SUPER_USER_TWO]; + const filteredDiscordIds = discordIds.filter( + (id) => !superUsers.includes(id) + ); - if (discordIds.length === 0) { + if (filteredDiscordIds.length === 0) { return; } @@ -20,7 +20,7 @@ export async function send(env: env): Promise { let stringToBeSent = `<@${SUPER_USER_ONE}> <@${SUPER_USER_TWO}>\nThese people have their task running red:\n`; let forFormatting = 0; - discordIds.forEach((id: string) => { + filteredDiscordIds.forEach((id: string) => { const discordUser = `<@${id}> `; stringToBeSent += discordUser; forFormatting++; diff --git a/src/utils/taskOverDueDiscordMembers.ts b/src/utils/taskOverDueDiscordMembers.ts index 3d915bc0..11938358 100644 --- a/src/utils/taskOverDueDiscordMembers.ts +++ b/src/utils/taskOverDueDiscordMembers.ts @@ -1,9 +1,7 @@ import { RDS_BASE_API_URL } from "../constants/urls"; import { UserOverdueTaskResponseType } from "../typeDefinitions/rdsUser"; -export const taskOverDueDiscordMembers = async (): Promise< - string[] | string -> => { +export const taskOverDueDiscordMembers = async (): Promise => { try { const overDueUrl = `${RDS_BASE_API_URL}/users?query=filterBy:overdue_tasks`;