removed IOS worker-time-controls endpoints
gitea/salix/dev This commit has test failures Details

This commit is contained in:
Joan Sanchez 2019-10-23 07:14:12 +02:00
parent 137f701434
commit b6d666d223
4 changed files with 0 additions and 250 deletions

View File

@ -1,48 +0,0 @@
/*
Author : Enrique Blasco BLanquer
Date: 27 de mayo de 2019
*/
module.exports = Self => {
Self.remoteMethodCtx('addAutoTime', {
description: 'Adds a new hour registry by app in manual 0',
accessType: 'WRITE',
accepts: [{
arg: 'data',
type: 'object',
required: true,
description: 'timed',
http: {source: 'body'}
}],
returns: [{
type: 'Object',
root: true
}],
http: {
path: `/addAutoTime`,
verb: 'POST'
}
});
Self.addAutoTime = async(ctx, data) => {
const myUserId = ctx.req.accessToken.userId;
// get all worked time control, needed to calculate order
let hours = await Self.rawSql(`SELECT * FROM vn.workerTimeControl
WHERE userFk = ?
AND DATE(timed) = CURDATE()
ORDER BY timed DESC LIMIT 1`, [myUserId]);
// 1 get next order
let order = 0;
if (hours.length > 0)
order = hours[hours.length - 1].order;
// 2 create element in db
return Self.create({
userFk: myUserId,
timed: data.timed,
order: order + 1,
manual: 0
});
};
};

View File

@ -1,121 +0,0 @@
/*
Author : Enrique Blasco BLanquer
Date: 28 de mayo de 2019
*/
module.exports = Self => {
Self.remoteMethodCtx('getHoursWorked', {
description: 'Get worked hours in current week, month and year',
accessType: 'WRITE',
returns: [{
type: 'Object',
root: true
}],
http: {
path: `/getHoursWorked`,
verb: 'GET'
}
});
Self.getHoursWorked = async(ctx, data) => {
let totalHours = 0; // total hours worked in one year
let totalMinutes = 0; // total minutes worked in one year
let totalHoursMonth = 0; // total hours worked in one month
let totalMinutesMonth = 0; // total minutes worked in one month
let totalHoursWeek = 0; // total hours worked in one week
let totalMinutesWeek = 0; // total minutes worked in one week
const myUserId = ctx.req.accessToken.userId; // user id
let today = new Date(); // needed to calculate total hours worked to current date
let fromDate = today.getFullYear() + '-01-01'; // from date, current year
let toDate = today.getFullYear() + '-12-31'; // to date, current year
// 1 hours worked in a year
let hoursYear = await Self.rawSql(`SELECT wtc.userFk, DATE(wtc.timed) dated,
UNIX_TIMESTAMP(MIN(timed))timedStart,
SEC_TO_TIME(SUM(if( mod(wtc.order,2)=1,
UNIX_TIMESTAMP(timed) *-1,
UNIX_TIMESTAMP(timed)))) timeWorkDay
FROM vn.workerTimeControl wtc
WHERE wtc.timed BETWEEN ? AND ? AND userFk = ?
GROUP BY wtc.userFk,dated ORDER BY dated DESC`, [fromDate, toDate, myUserId]);
// 2 Get days of week
let week = [];
// Starting Monday not Sunday
let current = new Date();
current.setDate((current.getDate() - current.getDay() + 1));
for (let i = 0; i < 7; i++) {
week.push(
new Date(current)
);
current.setDate(current.getDate() + 1);
}
// 3 I have all timed control for one year... NOW I CALCULATE TOTAL HOURS IN YEAR, MONTH, WEEK, Let's GO!
for (hour of hoursYear) {
if (parseInt(hour.timeWorkDay.split(':')[0]) > 0) {
// YEAR
totalHours += parseInt(hour.timeWorkDay.split(':')[0]);
totalMinutes += parseInt(hour.timeWorkDay.split(':')[1]);
// If it exceeds 5 hours we add 20 minutes of breakfast.
if (parseInt(hour.timeWorkDay.split(':')[0]) >= 5)
totalMinutes += 20;
// MONTH
if ((new Date(hour.dated)).getMonth() == today.getMonth()) {
totalHoursMonth += parseInt(hour.timeWorkDay.split(':')[0]);
totalMinutesMonth += parseInt(hour.timeWorkDay.split(':')[1]);
// If it exceeds 5 hours we add 20 minutes of breakfast.
if (parseInt(hour.timeWorkDay.split(':')[0]) >= 5)
totalMinutesMonth += 20;
}
// WEEK
for (day of week) {
let dayOfWeek = new Date(day);
let dayOfCurrentWeek = new Date(hour.dated);
if (dayOfWeek.getMonth() == dayOfCurrentWeek.getMonth() && dayOfWeek.getDate() == dayOfCurrentWeek.getDate()) {
totalHoursWeek += parseInt(hour.timeWorkDay.split(':')[0]);
totalMinutesWeek += parseInt(hour.timeWorkDay.split(':')[1]);
// If it exceeds 5 hours we add 20 minutes of breakfast.
if (parseInt(hour.timeWorkDay.split(':')[0]) >= 5)
totalMinutesWeek += 20;
break;
}
}
}
}
// TOTAL WORKED HOURS IN THE YEAR
totalHours += totalMinutes / 60;
totalHours = decimalToHour(totalHours);
// TOTAL WORKED HOURS IN THE MONTH
totalHoursMonth += totalMinutesMonth / 60;
totalHoursMonth = decimalToHour(totalHoursMonth);
// TOTAL WORKED HOURS IN THE WEEK
totalHoursWeek += totalMinutesWeek / 60;
totalHoursWeek = decimalToHour(totalHoursWeek);
return {
'totalWorekdYear': totalHours,
'totalWorekdMonth': totalHoursMonth,
'totalWorkedWeek': totalHoursWeek
};
};
};
/*
function to calculate hours and minutes from decimal value
*/
function decimalToHour(value) {
let decimalTime = parseFloat(value);
decimalTime = decimalTime * 60 * 60;
let hoursDay = Math.floor((decimalTime / (60 * 60)));
decimalTime = decimalTime - (hoursDay * 60 * 60);
let minutesDay = Math.floor((decimalTime / 60));
return hoursDay + ':' + minutesDay;
}

View File

@ -1,78 +0,0 @@
/*
Author : Enrique Blasco BLanquer
Date: 29 de mayo de 2019
*/
module.exports = Self => {
Self.remoteMethodCtx('getWorkedWeek', {
description: 'get worked week info',
accessType: 'WRITE',
returns: [{
type: 'Object',
root: true
}],
http: {
path: `/getWorkedWeek`,
verb: 'GET'
}
});
Self.getWorkedWeek = async(ctx, data) => {
const myUserId = ctx.req.accessToken.userId; // user id
let lastDate = new Date('1986-09-24'); // reference date
let diff = 0; // difference of value between two dates
let total = 0; // total hours
// 1 Get days of week
let week = [];
// 2 Starting Monday not Sunday
let current = new Date();
current.setDate((current.getDate() - current.getDay() + 1));
for (let i = 0; i < 7; i++) {
week.push(
new Date(current)
);
current.setDate(current.getDate() + 1);
}
let fromDate = week[0].getFullYear() + '-' + (week[0].getMonth() + 1) + '-' + week[0].getDate();
let toDate = week[week.length - 1].getFullYear() + '-' + (week[week.length - 1].getMonth() + 1) + '-' + week[week.length - 1].getDate();
// 3 hours worked in a current week
let hoursWeek = await Self.rawSql(`SELECT wtc.timed ,wtc.order
FROM vn.workerTimeControl wtc
WHERE userFk = ?
AND DATE(timed) BETWEEN ? AND ? ORDER BY timed DESC;`, [myUserId, fromDate, toDate]);
// 4 treat data
let isFirst = true;
for (let i = hoursWeek.length - 1; i >= 0; i--) {
let d = new Date(hoursWeek[i].timed);
if (isFirst) {
lastDate = d;
isFirst = false;
} else {
if (lastDate.getDate() === d.getDate()) {
diff += Math.abs(d.getTime() - lastDate.getTime());
lastDate = d;
} else {
total += diff;
diff = 0;
lastDate = d;
}
}
}
total += diff;
// 5 calculate hours and minutes
let decimalTime = total / 1000 / 3600;
decimalTime = decimalTime * 60 * 60;
let hours = Math.floor((decimalTime / (60 * 60)));
decimalTime = decimalTime - (hours * 60 * 60);
let minutes = Math.floor((decimalTime / 60));
return {'timeds': hoursWeek, 'totalWorked': hours + ':' + minutes};
};
};

View File

@ -3,9 +3,6 @@ const UserError = require('vn-loopback/util/user-error');
module.exports = Self => {
require('../methods/worker-time-control/filter')(Self);
require('../methods/worker-time-control/addTime')(Self);
require('../methods/worker-time-control/addAutoTime')(Self);
require('../methods/worker-time-control/getHoursWorked')(Self);
require('../methods/worker-time-control/getWorkedWeek')(Self);
Self.rewriteDbError(function(err) {
if (err.code === 'ER_DUP_ENTRY')