Skip to content

Commit

Permalink
Merge pull request #220 from Real-Dev-Squad/issue-219-online-users-fi…
Browse files Browse the repository at this point in the history
…lter
  • Loading branch information
iamitprakash authored Apr 17, 2024
2 parents 3d82b14 + b0f91cd commit bdef426
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 51 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
20 changes: 11 additions & 9 deletions src/handlers/scheduledEventHandler.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
try {
const assigneeIds: string[] | string = await taskOverDueDiscordMembers();
const discordIds: 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)];
const superUsers = [SUPER_USER_ONE, SUPER_USER_TWO];
const filteredDiscordIds = discordIds.filter(
(id) => !superUsers.includes(id)
);

if (filteredDiscordIds.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) => {
filteredDiscordIds.forEach((id: string) => {
const discordUser = `<@${id}> `;
stringToBeSent += discordUser;
forFormatting++;
Expand All @@ -35,7 +37,7 @@ export async function send(env: env): Promise<void> {

const url = config(env).TRACKING_CHANNEL_URL;

const res = await fetch(url, {
await fetch(url, {
method: "POST",
body: JSON.stringify(bodyObj),
headers: {
Expand Down
20 changes: 7 additions & 13 deletions src/utils/taskOverDueDiscordMembers.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
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
> => {
export const taskOverDueDiscordMembers = async (): Promise<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;
Expand Down
66 changes: 42 additions & 24 deletions tests/fixtures/fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
},
],
};
11 changes: 6 additions & 5 deletions tests/unit/utils/taskOverDueDiscordMembers.test.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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`
);
});
});

0 comments on commit bdef426

Please sign in to comment.