Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix progress update form bugs #1079

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions __tests__/Utils/getTotalMissedTaskProgressUpdate.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { getTotalMissedTaskProgressUpdate } from '@/utils/getTotalMissedTaskProgressUpdate';

describe('Get total missed task progress update', () => {
test('should return 1 if progress is not updated in last 3 days', () => {
jest.useFakeTimers().setSystemTime(new Date('13 january 2024'));
const result = getTotalMissedTaskProgressUpdate(1704795225 * 1000);
expect(result).toBe(1);
});

test('should return 0 if last progress is updated in 3 days', () => {
jest.useFakeTimers().setSystemTime(new Date('11 january 2024'));
const result = getTotalMissedTaskProgressUpdate(1704795225 * 1000);
expect(result).toBe(0);
});

test('should return 0 if the last progress is not updated in 3 days but the date includes sunday', () => {
jest.useFakeTimers().setSystemTime(new Date('8 january 2024'));
const result = getTotalMissedTaskProgressUpdate(1704445747000);
expect(result).toBe(0);
});
});
2 changes: 1 addition & 1 deletion src/app/services/progressesApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { progressDetails } from '@/types/standup.type';

type queryParamsType = {
userId?: string;
taskId?: string;
taskId?: string | string[];
};

export const progressesApi = api.injectEndpoints({
Expand Down
5 changes: 4 additions & 1 deletion src/app/services/taskDetailsApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ const getTasksDependencyDetailsQueryFn = async (

export const taskDetailsApi = api.injectEndpoints({
endpoints: (build) => ({
getTaskDetails: build.query<taskDetailsDataType, string>({
getTaskDetails: build.query<
taskDetailsDataType,
string[] | undefined | string
>({
query: (taskId): string => `${TASKS_URL}/${taskId}/details`,
providesTags: ['Task_Details'],
}),
Expand Down
46 changes: 37 additions & 9 deletions src/components/ProgressForm/ProgressLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,51 @@ import ProgressForm from './ProgressForm';
import styles from '@/components/ProgressForm/ProgressForm.module.scss';

import { questions } from '@/constants/ProgressUpdates';
import { getTotalMissedUpdates } from '@/utils/getTotalMissedUpdate';
import { getTotalMissedTaskProgressUpdate } from '@/utils/getTotalMissedTaskProgressUpdate';
import { useGetProgressDetailsQuery } from '@/app/services/progressesApi';
import { useGetUserQuery } from '@/app/services/userApi';
import { useGetTaskDetailsQuery } from '@/app/services/taskDetailsApi';

const ProgressLayout: FC = () => {
const { data: user } = useGetUserQuery();
const { data: userStandupdata } = useGetProgressDetailsQuery({
userId: user?.id,
});
interface ProgressLayoutPropsType {
taskId: string[] | undefined;
}

const ProgressLayout: FC<ProgressLayoutPropsType> = ({ taskId }) => {
const { data: userStandupdata, isLoading: isProgressUpdateDetailsLoading } =
useGetProgressDetailsQuery(
{
taskId: taskId,
},
{ skip: taskId ? false : true }
);

const { data: taskDetailData, isLoading: isTaskDetailsLoading } =
useGetTaskDetailsQuery(taskId);

const taskStartedOn = taskDetailData?.taskData?.createdAt;
const standupDates = userStandupdata?.data?.map((element) => element.date);
const totalMissedUpdates = getTotalMissedUpdates(standupDates || []);

let totalProgressMissedUpdates;

if (!isTaskDetailsLoading && !isProgressUpdateDetailsLoading) {
if (standupDates) {
totalProgressMissedUpdates = getTotalMissedTaskProgressUpdate(
standupDates[0]
);
} else {
totalProgressMissedUpdates = getTotalMissedTaskProgressUpdate(
Number(taskStartedOn) * 1000
);
}
} else {
totalProgressMissedUpdates = 0;
}

return (
<>
<NavBar />
<div className={styles.progressUpdateContainer}>
<ProgressHeader
totalMissedUpdates={totalMissedUpdates}
totalMissedUpdates={totalProgressMissedUpdates}
updateType="Progress"
/>
<section className={styles.container}>
Expand Down
1 change: 1 addition & 0 deletions src/interfaces/task.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ type task = {
type: string;
links?: string[];
endsOn: number;
createdAt: string;
startedOn: string;
createdBy: string;
assignee?: string;
Expand Down
2 changes: 1 addition & 1 deletion src/pages/progress/[id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const ProgressUpdatesPage = () => {
return <PageNotFound />;
}

return <ProgressLayout />;
return <ProgressLayout taskId={id} />;
};

export default ProgressUpdatesPage;
47 changes: 47 additions & 0 deletions src/utils/getTotalMissedTaskProgressUpdate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { TOTAL_MILLISECONDS_IN_A_HOUR } from '@/constants/date';

export function getTotalMissedTaskProgressUpdate(lastUpdated: number) {
const presentDateInUtc = new Date().toUTCString();
const taskLastUpdatedOn = new Date(lastUpdated).toUTCString();

const presentDateMillisecond = new Date(presentDateInUtc).valueOf();
const taskStartedOnMillisecond = new Date(taskLastUpdatedOn).valueOf();

let count = 0;
const sundays = getSundays(lastUpdated);

const differenceInHours = Math.round(
(presentDateMillisecond - taskStartedOnMillisecond) /
TOTAL_MILLISECONDS_IN_A_HOUR
);

let missedProgressUpdate = Math.round(differenceInHours / 24);
if (sundays !== 0) {
missedProgressUpdate = missedProgressUpdate - sundays;
}

if (missedProgressUpdate >= 3) {
for (let i = 1; i <= missedProgressUpdate; i++) {
if (i % 3 === 0) {
count++;
}
}
return count;
} else {
return count;
}
}

function getSundays(taskStartedOn: number) {
const taskStartedOnDate = new Date(taskStartedOn);
const currentDate = new Date();
let sundays = 0;

while (taskStartedOnDate <= currentDate) {
if (taskStartedOnDate.getDay() === 0) {
sundays++;
}
taskStartedOnDate.setDate(taskStartedOnDate.getDate() + 1);
}
return sundays;
}
Loading