/* eslint max-len: ["error", { "code": 150 }]*/ const models = require('vn-loopback/server/server').models; const LoopBackContext = require('loopback-context'); describe('workerTimeControl add/delete timeEntry()', () => { const employeeId = 1; const salesBossId = 19; const hankPymId = 1107; const monday = 1; const activeCtx = { accessToken: {userId: 50}, }; const ctx = {req: activeCtx}; beforeAll(() => { spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({ active: activeCtx }); }); describe('as Role errors', () => { it('should fail to add a time entry if the target user is not a subordinate', async() => { activeCtx.accessToken.userId = employeeId; const workerId = 2; let error; try { ctx.args = {timed: Date.vnNew(), direction: 'in'}; await models.WorkerTimeControl.addTimeEntry(ctx, workerId); } catch (e) { error = e; } expect(error).toBeDefined(); expect(error.statusCode).toBe(400); expect(error.message).toBe(`You don't have enough privileges`); }); it('should fail to add if the current and the target user are the same and is not team boss', async() => { activeCtx.accessToken.userId = employeeId; const workerId = employeeId; let error; try { ctx.args = {timed: Date.vnNew(), direction: 'in'}; await models.WorkerTimeControl.addTimeEntry(ctx, workerId); } catch (e) { error = e; } expect(error).toBeDefined(); expect(error.statusCode).toBe(400); expect(error.message).toBe(`You don't have enough privileges`); }); }); describe('WorkerTimeControl_clockIn calls', () => { beforeEach(() => activeCtx.accessToken.userId = salesBossId); describe('WorkerTimeControl_calculate calls', () => { let dated = Date.vnNew(); dated.setDate(dated.getDate() - 7); dated = new Date(weekDay(dated, monday)); const end = new Date(dated); end.setDate(end.getDate() + 1); it(`should return today's worked 8 hours without break`, async() => { const tx = await models.WorkerTimeControl.beginTransaction({}); const options = {transaction: tx}; try { await populateWeek(dated, monday, monday, ctx, hankPymId, options); const start = new Date(dated - 1); start.setHours(0, 0, 0); await models.WorkerTimeControl.rawSql('CALL vn.timeControl_calculateByUser(?, ?, ?)', [ hankPymId, start, end ], options); let [timeControlCalculateTable] = await models.WorkerTimeControl.rawSql('SELECT * FROM tmp.timeControlCalculate', null, options); expect(timeControlCalculateTable.timeWorkSeconds).toEqual(28800); await tx.rollback(); } catch (e) { await tx.rollback(); throw e; } }); it(`should return today's worked hours with 15min break and work time consecutive less than 5h`, async() => { const tx = await models.WorkerTimeControl.beginTransaction({}); const options = {transaction: tx}; try { await populateWeek(dated, monday, monday, ctx, hankPymId, options); dated.setHours(14, 59, 0); await addTimeEntry(ctx, dated, 'middle', hankPymId, options); dated.setHours(15, 14, 0); await addTimeEntry(ctx, dated, 'middle', hankPymId, options); const start = new Date(dated - 1); start.setHours(0, 0, 0); await models.WorkerTimeControl.rawSql('CALL vn.timeControl_calculateByUser(?, ?, ?)', [ hankPymId, start, end ], options); let [timeControlCalculateTable] = await models.WorkerTimeControl.rawSql('SELECT * FROM tmp.timeControlCalculate', null, options); expect(timeControlCalculateTable.timeWorkSeconds).toEqual(28800); await tx.rollback(); } catch (e) { await tx.rollback(); throw e; } }); it(`should return today's worked hours with 15min break and work time consecutive greater than 5h`, async() => { const tx = await models.WorkerTimeControl.beginTransaction({}); const options = {transaction: tx}; try { await populateWeek(dated, monday, monday, ctx, hankPymId, options); dated.setHours(15, 0, 0); await addTimeEntry(ctx, dated, 'middle', hankPymId, options); dated.setHours(15, 15, 0); await addTimeEntry(ctx, dated, 'middle', hankPymId, options); const start = new Date(dated - 1); start.setHours(0, 0, 0); await models.WorkerTimeControl.rawSql('CALL vn.timeControl_calculateByUser(?, ?, ?)', [ hankPymId, start, end ], options); let [timeControlCalculateTable] = await models.WorkerTimeControl.rawSql('SELECT * FROM tmp.timeControlCalculate', null, options); expect(timeControlCalculateTable.timeWorkSeconds).toEqual(28800); await tx.rollback(); } catch (e) { await tx.rollback(); throw e; } }); it(`should return today's worked hours with 25min break and work time consecutive less than 5h`, async() => { const tx = await models.WorkerTimeControl.beginTransaction({}); const options = {transaction: tx}; try { await populateWeek(dated, monday, monday, ctx, hankPymId, options); dated.setHours(14, 59, 0); await addTimeEntry(ctx, dated, 'middle', hankPymId, options); dated.setHours(15, 24, 0); await addTimeEntry(ctx, dated, 'middle', hankPymId, options); const start = new Date(dated - 1); start.setHours(0, 0, 0); await models.WorkerTimeControl.rawSql('CALL vn.timeControl_calculateByUser(?, ?, ?)', [ hankPymId, start, end ], options); let [timeControlCalculateTable] = await models.WorkerTimeControl.rawSql('SELECT * FROM tmp.timeControlCalculate', null, options); expect(timeControlCalculateTable.timeWorkSeconds).toEqual(28500); await tx.rollback(); } catch (e) { await tx.rollback(); throw e; } }); it(`should return today's worked hours with 25min break and work time consecutive greater than 5h`, async() => { const tx = await models.WorkerTimeControl.beginTransaction({}); const options = {transaction: tx}; try { await populateWeek(dated, monday, monday, ctx, hankPymId, options); dated.setHours(15, 0, 0); await addTimeEntry(ctx, dated, 'middle', hankPymId, options); dated.setHours(15, 25, 0); await addTimeEntry(ctx, dated, 'middle', hankPymId, options); const start = new Date(dated - 1); start.setHours(0, 0, 0); await models.WorkerTimeControl.rawSql('CALL vn.timeControl_calculateByUser(?, ?, ?)', [ hankPymId, start, end ], options); let [timeControlCalculateTable] = await models.WorkerTimeControl.rawSql('SELECT * FROM tmp.timeControlCalculate', null, options); expect(timeControlCalculateTable.timeWorkSeconds).toEqual(28500); await tx.rollback(); } catch (e) { await tx.rollback(); throw e; } }); it(`should return today's worked hours with 60min break and work time consecutive less than 5h`, async() => { const tx = await models.WorkerTimeControl.beginTransaction({}); const options = {transaction: tx}; try { await populateWeek(dated, monday, monday, ctx, hankPymId, options); dated.setHours(14, 59, 0); await addTimeEntry(ctx, dated, 'middle', hankPymId, options); dated.setHours(15, 59, 0); await addTimeEntry(ctx, dated, 'middle', hankPymId, options); const start = new Date(dated - 1); start.setHours(0, 0, 0); await models.WorkerTimeControl.rawSql('CALL vn.timeControl_calculateByUser(?, ?, ?)', [ hankPymId, start, end ], options); let [timeControlCalculateTable] = await models.WorkerTimeControl.rawSql('SELECT * FROM tmp.timeControlCalculate', null, options); expect(timeControlCalculateTable.timeWorkSeconds).toEqual(25200); await tx.rollback(); } catch (e) { await tx.rollback(); throw e; } }); it(`should return today's worked hours with 60min break and work time consecutive greater than 5h`, async() => { const tx = await models.WorkerTimeControl.beginTransaction({}); const options = {transaction: tx}; try { await populateWeek(dated, monday, monday, ctx, hankPymId, options); dated.setHours(15, 0, 0); await addTimeEntry(ctx, dated, 'middle', hankPymId, options); dated.setHours(16, 0, 0); await addTimeEntry(ctx, dated, 'middle', hankPymId, options); const start = new Date(dated - 1); start.setHours(0, 0, 0); await models.WorkerTimeControl.rawSql('CALL vn.timeControl_calculateByUser(?, ?, ?)', [ hankPymId, start, end ], options); let [timeControlCalculateTable] = await models.WorkerTimeControl.rawSql('SELECT * FROM tmp.timeControlCalculate', null, options); expect(timeControlCalculateTable.timeWorkSeconds).toEqual(26400); await tx.rollback(); } catch (e) { await tx.rollback(); throw e; } }); }); }); }); async function addTimeEntry(ctx, dated, direction, userId, option) { ctx.args = {timed: dated, direction}; await models.WorkerTimeControl.addTimeEntry(ctx, userId, option); } function weekDay(date, dayToSet) { const currentDay = date.getDay(); const distance = dayToSet - currentDay; date.setDate(date.getDate() + distance); return date; } async function populateWeek(date, dayStart, dayEnd, ctx, workerId, options) { const dateStart = new Date(weekDay(date, dayStart)); const dateEnd = new Date(dateStart); dateEnd.setDate(dateStart.getDate() + dayEnd); for (let i = dayStart; i <= dayEnd; i++) { dateStart.setHours(10, 0, 0); ctx.args = {timed: dateStart, direction: 'in'}; await models.WorkerTimeControl.addTimeEntry(ctx, workerId, options); dateStart.setHours(18, 0, 0); ctx.args = {timed: dateStart, direction: 'out'}; await models.WorkerTimeControl.addTimeEntry(ctx, workerId, options); dateStart.setDate(dateStart.getDate() + 1); } }