const app = require('vn-loopback/server/server'); const ParameterizedSQL = require('loopback-connector').ParameterizedSQL; describe('timeControl_calculateByUser()', () => { it(`should return today's worked hours`, async() => { let start = new Date(); start.setHours(0, 0, 0, 0); start.setDate(start.getDate() - 1); let end = new Date(); end.setHours(0, 0, 0, 0); end.setDate(end.getDate() + 1); let stmts = []; let stmt; stmts.push('START TRANSACTION'); stmts.push(` DROP TEMPORARY TABLE IF EXISTS tmp.timeControlCalculate, tmp.timeBusinessCalculate `); let params = { workerID: 106, 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() => { let lastSunday = new Date(); 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); let monday = new Date(); 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(` DROP TEMPORARY TABLE IF EXISTS tmp.timeControlCalculate, tmp.timeBusinessCalculate `); const workerID = 107; 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); }); });