Merge pull request 'ref #6065 replace then by async/await' (!1766) from 6065-fixWorkerTimeControl into dev
gitea/salix/pipeline/head This commit looks good Details

Reviewed-on: #1766
Reviewed-by: Alex Moreno <alexm@verdnatura.es>
This commit is contained in:
Jorge Penadés 2023-10-02 07:28:20 +00:00
commit a04167f3f1
2 changed files with 90 additions and 88 deletions

View File

@ -52,6 +52,28 @@ class Controller extends Section {
set worker(value) {
this._worker = value;
this.fetchHours();
}
/**
* Worker hours data
*/
get hours() {
return this._hours;
}
set hours(value) {
this._hours = value;
for (const weekDay of this.weekDays) {
if (value) {
let day = weekDay.dated.getDay();
weekDay.hours = value
.filter(hour => new Date(hour.timed).getDay() == day)
.sort((a, b) => new Date(a.timed) - new Date(b.timed));
} else
weekDay.hours = null;
}
}
/**
@ -87,10 +109,18 @@ class Controller extends Section {
dayIndex.setDate(dayIndex.getDate() + 1);
}
this.fetchHours();
if (!this.weekTotalHours) this.fetchHours();
this.getWeekData();
}
set weekTotalHours(totalHours) {
this._weekTotalHours = this.formatHours(totalHours);
}
get weekTotalHours() {
return this._weekTotalHours;
}
getWeekData() {
const filter = {
where: {
@ -101,38 +131,19 @@ class Controller extends Section {
};
this.$http.get('WorkerTimeControlMails', {filter})
.then(res => {
const workerTimeControlMail = res.data;
if (!workerTimeControlMail.length) {
const mail = res.data;
if (!mail.length) {
this.state = null;
return;
}
this.state = workerTimeControlMail[0].state;
this.reason = workerTimeControlMail[0].reason;
this.state = mail[0].state;
this.reason = mail[0].reason;
});
}
/**
* Worker hours data
*/
get hours() {
return this._hours;
}
set hours(value) {
this._hours = value;
for (const weekDay of this.weekDays) {
if (value) {
let day = weekDay.dated.getDay();
weekDay.hours = value
.filter(hour => new Date(hour.timed).getDay() == day)
.sort((a, b) => new Date(a.timed) - new Date(b.timed));
} else
weekDay.hours = null;
}
}
fetchHours() {
if (!this.worker || !this.date) return;
const params = {workerFk: this.$params.id};
const filter = {
where: {and: [
@ -148,58 +159,6 @@ class Controller extends Section {
});
}
hasEvents(day) {
return day >= this.started && day < this.ended;
}
getAbsences() {
const fullYear = this.started.getFullYear();
let params = {
workerFk: this.$params.id,
businessFk: null,
year: fullYear
};
return this.$http.get(`Calendars/absences`, {params})
.then(res => this.onData(res.data));
}
onData(data) {
const events = {};
const addEvent = (day, event) => {
events[new Date(day).getTime()] = event;
};
if (data.holidays) {
data.holidays.forEach(holiday => {
const holidayDetail = holiday.detail && holiday.detail.description;
const holidayType = holiday.type && holiday.type.name;
const holidayName = holidayDetail || holidayType;
addEvent(holiday.dated, {
name: holidayName,
color: '#ff0'
});
});
}
if (data.absences) {
data.absences.forEach(absence => {
const type = absence.absenceType;
addEvent(absence.dated, {
name: type.name,
color: type.rgb
});
});
}
this.weekDays.forEach(day => {
const timestamp = day.dated.getTime();
if (events[timestamp])
day.event = events[timestamp];
});
}
getWorkedHours(from, to) {
this.weekTotalHours = null;
let weekTotalHours = 0;
@ -239,6 +198,58 @@ class Controller extends Section {
});
}
getAbsences() {
const fullYear = this.started.getFullYear();
let params = {
workerFk: this.$params.id,
businessFk: null,
year: fullYear
};
return this.$http.get(`Calendars/absences`, {params})
.then(res => this.onData(res.data));
}
hasEvents(day) {
return day >= this.started && day < this.ended;
}
onData(data) {
const events = {};
const addEvent = (day, event) => {
events[new Date(day).getTime()] = event;
};
if (data.holidays) {
data.holidays.forEach(holiday => {
const holidayDetail = holiday.detail && holiday.detail.description;
const holidayType = holiday.type && holiday.type.name;
const holidayName = holidayDetail || holidayType;
addEvent(holiday.dated, {
name: holidayName,
color: '#ff0'
});
});
}
if (data.absences) {
data.absences.forEach(absence => {
const type = absence.absenceType;
addEvent(absence.dated, {
name: type.name,
color: type.rgb
});
});
}
this.weekDays.forEach(day => {
const timestamp = day.dated.getTime();
if (events[timestamp])
day.event = events[timestamp];
});
}
getFinishTime() {
if (!this.weekDays) return;
@ -267,14 +278,6 @@ class Controller extends Section {
}
}
set weekTotalHours(totalHours) {
this._weekTotalHours = this.formatHours(totalHours);
}
get weekTotalHours() {
return this._weekTotalHours;
}
formatHours(timestamp = 0) {
let hour = Math.floor(timestamp / 3600);
let min = Math.floor(timestamp / 60 - 60 * hour);

View File

@ -22,7 +22,7 @@ describe('Component vnWorkerTimeControl', () => {
}));
describe('date() setter', () => {
it(`should set the weekDays, the date in the controller and call fetchHours`, () => {
it(`should set the weekDays and the date in the controller`, () => {
let today = Date.vnNew();
jest.spyOn(controller, 'fetchHours').mockReturnThis();
@ -32,7 +32,6 @@ describe('Component vnWorkerTimeControl', () => {
expect(controller.started).toBeDefined();
expect(controller.ended).toBeDefined();
expect(controller.weekDays.length).toEqual(7);
expect(controller.fetchHours).toHaveBeenCalledWith();
});
});