74 lines
2.3 KiB
JavaScript
74 lines
2.3 KiB
JavaScript
const ParameterizedSQL = require('loopback-connector').ParameterizedSQL;
|
|
|
|
module.exports = Self => {
|
|
Self.remoteMethod('getWorkedHours', {
|
|
description: 'returns the total worked hours per day for a given range of dates in format YYYY-mm-dd hh:mm:ss',
|
|
accessType: 'READ',
|
|
accepts: [{
|
|
arg: 'id',
|
|
type: 'number',
|
|
required: true,
|
|
description: 'The worker id',
|
|
http: {source: 'path'}
|
|
},
|
|
{
|
|
arg: 'from',
|
|
type: 'Date',
|
|
required: true,
|
|
description: `The from date`
|
|
},
|
|
{
|
|
arg: 'to',
|
|
type: 'Date',
|
|
required: true,
|
|
description: `The to date`
|
|
}],
|
|
returns: {
|
|
type: ['Object'],
|
|
root: true
|
|
},
|
|
http: {
|
|
path: `/:id/getWorkedHours`,
|
|
verb: 'GET'
|
|
}
|
|
});
|
|
|
|
Self.getWorkedHours = async(id, started, ended) => {
|
|
const models = Self.app.models;
|
|
const conn = Self.dataSource.connector;
|
|
|
|
const worker = await models.Worker.findById(id);
|
|
const userId = worker.id;
|
|
|
|
const stmts = [];
|
|
|
|
stmts.push(`
|
|
DROP TEMPORARY TABLE IF EXISTS
|
|
tmp.timeControlCalculate,
|
|
tmp.timeBusinessCalculate
|
|
`);
|
|
|
|
stmts.push(new ParameterizedSQL('CALL vn.timeControl_calculateByUser(?, ?, ?)', [userId, started, ended]));
|
|
|
|
stmts.push(new ParameterizedSQL('CALL vn.timeBusiness_calculateByUser(?, ?, ?)', [userId, started, ended]));
|
|
|
|
const resultIndex = stmts.push(new ParameterizedSQL(`
|
|
SELECT tcc.dated, tbc.timeWorkSeconds expectedHours, tcc.timeWorkSeconds workedHours
|
|
FROM tmp.timeControlCalculate tcc
|
|
LEFT JOIN tmp.timeBusinessCalculate tbc ON tcc.dated = tbc.dated
|
|
WHERE tcc.dated BETWEEN DATE(?) AND DATE(?)
|
|
`, [started, ended])) - 1;
|
|
|
|
stmts.push(`
|
|
DROP TEMPORARY TABLE IF EXISTS
|
|
tmp.timeControlCalculate,
|
|
tmp.timeBusinessCalculate
|
|
`);
|
|
|
|
const sql = ParameterizedSQL.join(stmts, ';');
|
|
const result = await conn.executeStmt(sql);
|
|
|
|
return result[resultIndex];
|
|
};
|
|
};
|