Files
jandl-taskmanager-app/app/src/plugins/tm@dashboard/tasks/_utils/timeline.utils.ts
T
2023-02-22 14:43:44 +01:00

88 lines
2.3 KiB
TypeScript

import { setHours, setMinutes, addDays, differenceInCalendarDays } from 'date-fns'
import moment from 'moment'
export default {
generateTimelineTasks(subtasks, date, employee) {
return [
...this.generateTimelineEvents(subtasks, employee, date),
...this.generateTimelineNonWorkingHours(date, employee)
]
},
getUnscheduledTime(subtasks) {
let unscheduledTime = 0
subtasks.forEach(subtask => unscheduledTime += (subtask.duration - subtask.spent_time))
return unscheduledTime
},
isWeekend(date) {
return moment(date).day() % 6 == 0
},
generateTimelineEvents(subtasks, employee, date) {
const events = []
subtasks.forEach(subtask => {
subtask.timeblocks?.forEach((timeblock) => {
events.push(this._composeTimelineEvent(subtask, employee, date, timeblock))
})
})
return events
},
_composeTimelineEvent(subtask, employee, date, timeblock) {
const diffInDays = differenceInCalendarDays(date, new Date(timeblock.start))
return {
id: subtask.id,
start: addDays(new Date(timeblock.start), diffInDays),
end: addDays(new Date(timeblock.end), diffInDays),
master_task: subtask.master_task,
date: new Date(timeblock.start),
isWeekend: this.isWeekend(timeblock.start),
initials: employee.name.charAt(0).toUpperCase() + employee.surname.charAt(0).toUpperCase(),
split: this._getSplitFromDate(date, timeblock.start),
project_code: subtask.project_code,
subtask: subtask,
_timeblock_id: timeblock.id
}
},
generateTimelineNonWorkingHours(date, employee) {
const nonWorkingHours = []
for (let i = 0; i < 7; i++) {
nonWorkingHours.push({
title: "Non working hours",
start: setMinutes(setHours(date, 0), 0),
end: setMinutes(setHours(date, employee.work_start), 0),
nonWorking: true,
split: i + 1,
draggable: false
})
nonWorkingHours.push({
title: "Non working hours",
start: setMinutes(setHours(date, employee.work_end), 0),
end: setMinutes(setHours(date, 23), 59),
nonWorking: true,
split: i + 1,
draggable: false
})
}
return nonWorkingHours
},
_getSplitFromDate(date, start) {
return differenceInCalendarDays(new Date(date), new Date(start)) * (-1) + 1
},
_getDifferenceInHours(date1, date2) {
let diff =(date1.getTime() - date2.getTime()) / 1000;
diff /= (60 * 60)
return Math.abs(Math.round(diff))
}
}