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

Add time entry range calculations #627

Merged
merged 1 commit into from
Sep 5, 2022
Merged
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
137 changes: 120 additions & 17 deletions frontend/src/pages/VacationPlanner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ import {
getUsersInGroups,
getGroups,
} from "../utils";
import { eachDayOfInterval, Interval, format as formatDate } from "date-fns";
import {
eachDayOfInterval,
Interval,
format as formatDate,
addDays,
} from "date-fns";
import LoadingOverlay from "react-loading-overlay-ts";
import ClimbingBoxLoader from "react-spinners/ClimbingBoxLoader";
import { HeaderUser } from "../components/HeaderUser";
Expand Down Expand Up @@ -99,6 +104,89 @@ export const VacationPlanner = () => {
return myReportedEntries.length;
};

const getVacationRanges = (entries: FetchedTimeEntry[]) => {
const dates: { dateRanges: [Date][]; userName: string } =
findConsecutiveDates(entries);
const vacationRanges: {
startDate: Date;
endDate: Date;
userName: string;
}[] = dates.dateRanges.map((range: Date[]) => {
if (range.length === 1) {
let date = range[range.length - 1];
return { startDate: date, endDate: date, userName: dates.userName };
} else if (range.length > 1) {
let fromDate = range[range.length - 1];
let toDate = range[0];
return {
startDate: fromDate,
endDate: toDate,
userName: dates.userName,
};
}
});
return vacationRanges;
};

const findConsecutiveDates = (entries: FetchedTimeEntry[]) => {
let rangesIndex: number = 0;
let userName: string | IdName = "";
const dateRanges: [Date][] = entries.reduce(
(
entryRanges: [Date][],
entry: FetchedTimeEntry,
index,
fetchedEntries: FetchedTimeEntry[]
) => {
// Find out the last date we last appended to a range array
userName = entry.user.name ? entry.user.name : entry.user;

let lastInRange: Date = entryRanges[rangesIndex]
? entryRanges[rangesIndex].slice(-1).pop()
: undefined;
// Use the last appended date as toDate or first next iteration value (first)
const toDate = lastInRange ? lastInRange : new Date(entry.spent_on);
// Use next element in fetchedEntries as fromDate, and if it does not exist, use toDate
const fromDate = fetchedEntries[index + 1]
? new Date(fetchedEntries[index + 1].spent_on)
: toDate;

// Initialise the entryRanges with the first entry date
if (index === 0) {
entryRanges.push([toDate]);
}
// If it's friday, the next reportable day is 3 days away, otherwise 1 day
let daysToNextReportableDay: number = fromDate.getDay() === 5 ? 3 : 1;
// If dates are consecutive ...
if (addDays(fromDate, daysToNextReportableDay) - toDate === 0) {
// And the date is not aalready present
if (
!entryRanges
.flat()
.find((date) => date.getTime() === fromDate.getTime())
) {
// Add date to entry ranges
entryRanges[rangesIndex].push(fromDate);
}
} else {
// Otherwise add a new range array, if the date does not already exist
if (
!entryRanges
.flat()
.find((date) => date.getTime() === fromDate.getTime())
) {
entryRanges.push([fromDate]);
rangesIndex++;
}
}

return entryRanges;
},
[]
);
return { dateRanges: dateRanges, userName: userName };
};

React.useEffect(() => {
toggleLoadingPage(true);
const fetchTimeEntriesFromGroups = async () => {
Expand Down Expand Up @@ -142,24 +230,39 @@ export const VacationPlanner = () => {
{ type: "date", id: "End" },
]);

for await (let group of sliced_users) {
for await (let user of sliced_users) {
let entries = await getVacationTimeEntries(
new Date(2021, 11, 20),
new Date(2021, 12, 30),
group.toString()
new Date(2021, 10, 20),
new Date(2022, 2, 30),
user.toString()
);
const vacationRanges: {
startDate: Date;
endDate: Date;
userName: string;
}[] = getVacationRanges(entries);

vacationRanges.map(
(range: { startDate: Date; endDate: Date; userName: string }) => {
range.group = groupId;
}
);

vacationRanges.map(
(range: {
startDate: Date;
endDate: Date;
userName: string;
group: string;
}) => {
vacationRows.push([
range.userName,
"test",
range.startDate,
range.endDate,
]);
}
);
entries.map((entry) => {
entry.group = groupId;
});
entries.map((entry: FetchedTimeEntry) => {
vacationRows.push([
entry.user.name ? entry.user.name : entry.user,
"test",
new Date(entry.spent_on),
// One day later
new Date(entry.spent_on).getTime() + 86400000,
]);
});
}
toggleLoadingPage(false);

Expand Down