2020-03-04 14:12:36 +00:00
|
|
|
const app = require('vn-loopback/server/server');
|
|
|
|
const ParameterizedSQL = require('loopback-connector').ParameterizedSQL;
|
|
|
|
|
|
|
|
describe('timeControl_calculateByUser()', () => {
|
|
|
|
it(`should return today's worked hours`, async() => {
|
2023-01-16 14:18:24 +00:00
|
|
|
let start = Date.vnNew();
|
2020-03-04 14:12:36 +00:00
|
|
|
start.setHours(0, 0, 0, 0);
|
|
|
|
start.setDate(start.getDate() - 1);
|
|
|
|
|
2023-01-16 14:18:24 +00:00
|
|
|
let end = Date.vnNew();
|
2020-03-04 14:12:36 +00:00
|
|
|
end.setHours(0, 0, 0, 0);
|
|
|
|
end.setDate(end.getDate() + 1);
|
|
|
|
|
|
|
|
let stmts = [];
|
|
|
|
let stmt;
|
|
|
|
|
|
|
|
stmts.push('START TRANSACTION');
|
|
|
|
|
|
|
|
stmts.push(`
|
2023-01-16 14:18:24 +00:00
|
|
|
DROP TEMPORARY TABLE IF EXISTS
|
2020-03-04 14:12:36 +00:00
|
|
|
tmp.timeControlCalculate,
|
|
|
|
tmp.timeBusinessCalculate
|
|
|
|
`);
|
|
|
|
|
|
|
|
let params = {
|
2021-06-23 11:24:23 +00:00
|
|
|
workerID: 1106,
|
2020-03-04 14:12:36 +00:00
|
|
|
start: start,
|
|
|
|
end: end
|
|
|
|
};
|
|
|
|
|
|
|
|
stmt = new ParameterizedSQL('CALL vn.timeControl_calculateByUser(?, ?, ?)', [
|
|
|
|
params.workerID,
|
|
|
|
params.start,
|
|
|
|
params.end
|
|
|
|
]);
|
|
|
|
stmts.push(stmt);
|
|
|
|
|
|
|
|
let tableIndex = stmts.push('SELECT * FROM tmp.timeControlCalculate') - 1;
|
|
|
|
|
|
|
|
stmts.push('ROLLBACK');
|
|
|
|
|
|
|
|
let sql = ParameterizedSQL.join(stmts, ';');
|
|
|
|
let result = await app.models.Ticket.rawStmt(sql);
|
|
|
|
|
|
|
|
let [timeControlCalculateTable] = result[tableIndex];
|
|
|
|
|
|
|
|
expect(timeControlCalculateTable.timeWorkSeconds).toEqual(29400);
|
|
|
|
});
|
|
|
|
|
|
|
|
it(`should return the worked hours between last sunday and monday`, async() => {
|
2023-01-16 14:18:24 +00:00
|
|
|
let lastSunday = Date.vnNew();
|
2020-03-04 14:12:36 +00:00
|
|
|
let daysSinceSunday = lastSunday.getDay();
|
|
|
|
if (daysSinceSunday === 0) // this means today is sunday but you need the previous sunday :)
|
|
|
|
daysSinceSunday = 7;
|
|
|
|
lastSunday.setHours(23, 0, 0, 0);
|
|
|
|
lastSunday.setDate(lastSunday.getDate() - daysSinceSunday);
|
|
|
|
|
2023-01-16 14:18:24 +00:00
|
|
|
let monday = Date.vnNew();
|
2020-03-04 14:12:36 +00:00
|
|
|
let daysSinceMonday = daysSinceSunday - 1; // aiming for monday (today could be monday)
|
|
|
|
monday.setHours(7, 0, 0, 0);
|
|
|
|
monday.setDate(monday.getDate() - daysSinceMonday);
|
|
|
|
|
|
|
|
let stmts = [];
|
|
|
|
let stmt;
|
|
|
|
|
|
|
|
stmts.push('START TRANSACTION');
|
|
|
|
|
|
|
|
stmts.push(`
|
2023-01-16 14:18:24 +00:00
|
|
|
DROP TEMPORARY TABLE IF EXISTS
|
2020-03-04 14:12:36 +00:00
|
|
|
tmp.timeControlCalculate,
|
|
|
|
tmp.timeBusinessCalculate
|
|
|
|
`);
|
|
|
|
|
2021-06-23 11:24:23 +00:00
|
|
|
const workerID = 1107;
|
2020-03-04 14:12:36 +00:00
|
|
|
|
|
|
|
stmt = new ParameterizedSQL(`
|
|
|
|
INSERT INTO vn.workerTimeControl(userFk, timed, manual, direction)
|
|
|
|
VALUES
|
|
|
|
(?, ?, 1, 'in'),
|
|
|
|
(?, ?, 1, 'out')
|
|
|
|
`, [
|
|
|
|
workerID,
|
|
|
|
lastSunday,
|
|
|
|
workerID,
|
|
|
|
monday
|
|
|
|
]);
|
|
|
|
stmts.push(stmt);
|
|
|
|
|
|
|
|
stmt = new ParameterizedSQL('CALL vn.timeControl_calculateByUser(?, ?, ?)', [
|
|
|
|
workerID,
|
|
|
|
lastSunday,
|
|
|
|
monday
|
|
|
|
]);
|
|
|
|
stmts.push(stmt);
|
|
|
|
|
|
|
|
let tableIndex = stmts.push('SELECT * FROM tmp.timeControlCalculate') - 1;
|
|
|
|
|
|
|
|
stmts.push('ROLLBACK');
|
|
|
|
|
|
|
|
let sql = ParameterizedSQL.join(stmts, ';');
|
|
|
|
let result = await app.models.Ticket.rawStmt(sql);
|
|
|
|
|
|
|
|
let [timeControlCalculateTable] = result[tableIndex];
|
|
|
|
|
|
|
|
expect(timeControlCalculateTable.timeWorkSeconds).toEqual(30000);
|
|
|
|
});
|
|
|
|
});
|