endpoint minor refactor
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Carlos Jimenez Ruiz 2021-06-07 14:26:34 +02:00
parent 589f9339a4
commit a46fef5295
1 changed files with 17 additions and 7 deletions

View File

@ -34,35 +34,45 @@ module.exports = Self => {
}); });
Self.getWorkedHours = async(id, started, ended) => { Self.getWorkedHours = async(id, started, ended) => {
const models = Self.app.models;
const conn = Self.dataSource.connector; const conn = Self.dataSource.connector;
const worker = await models.Worker.findById(id);
const userId = worker.userFk;
const stmts = []; const stmts = [];
const startedMinusOne = new Date(started); const startedMinusOne = new Date(started);
startedMinusOne.setDate(started.getDate() - 1);
const endedPlusOne = new Date(ended); const endedPlusOne = new Date(ended);
let worker = await Self.app.models.Worker.findById(id); endedPlusOne.setDate(ended.getDate() + 1);
let userId = worker.userFk;
stmts.push(` stmts.push(`
DROP TEMPORARY TABLE IF EXISTS DROP TEMPORARY TABLE IF EXISTS
tmp.timeControlCalculate, tmp.timeControlCalculate,
tmp.timeBusinessCalculate tmp.timeBusinessCalculate
`); `);
startedMinusOne.setDate(started.getDate() - 1);
endedPlusOne.setDate(ended.getDate() + 1);
stmts.push(new ParameterizedSQL('CALL vn.timeControl_calculateByUser(?, ?, ?)', [userId, startedMinusOne, endedPlusOne])); stmts.push(new ParameterizedSQL('CALL vn.timeControl_calculateByUser(?, ?, ?)', [userId, startedMinusOne, endedPlusOne]));
stmts.push(new ParameterizedSQL('CALL vn.timeBusiness_calculateByUser(?, ?, ?)', [userId, startedMinusOne, endedPlusOne])); stmts.push(new ParameterizedSQL('CALL vn.timeBusiness_calculateByUser(?, ?, ?)', [userId, startedMinusOne, endedPlusOne]));
let resultIndex = stmts.push(new ParameterizedSQL(`
const resultIndex = stmts.push(new ParameterizedSQL(`
SELECT tbc.dated, tbc.timeWorkSeconds expectedHours, tcc.timeWorkSeconds workedHours SELECT tbc.dated, tbc.timeWorkSeconds expectedHours, tcc.timeWorkSeconds workedHours
FROM tmp.timeBusinessCalculate tbc FROM tmp.timeBusinessCalculate tbc
LEFT JOIN tmp.timeControlCalculate tcc ON tcc.dated = tbc.dated LEFT JOIN tmp.timeControlCalculate tcc ON tcc.dated = tbc.dated
WHERE tbc.dated BETWEEN ? AND ? WHERE tbc.dated BETWEEN ? AND ?
`, [started, ended])) - 1; `, [started, ended])) - 1;
stmts.push(` stmts.push(`
DROP TEMPORARY TABLE IF EXISTS DROP TEMPORARY TABLE IF EXISTS
tmp.timeControlCalculate, tmp.timeControlCalculate,
tmp.timeBusinessCalculate tmp.timeBusinessCalculate
`); `);
let sql = ParameterizedSQL.join(stmts, ';');
let result = await conn.executeStmt(sql); const sql = ParameterizedSQL.join(stmts, ';');
const result = await conn.executeStmt(sql);
return result[resultIndex]; return result[resultIndex];
}; };