feat(worker_calendar): add restrictions to add absence and to add hours recorded
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Vicent Llopis 2021-11-30 09:11:23 +01:00
parent df337360de
commit 4c064f8a80
6 changed files with 74 additions and 2 deletions

View File

@ -214,5 +214,7 @@
"You can't change the credit set to zero from a manager": "No puedes cambiar el cŕedito establecido a cero por un gerente", "You can't change the credit set to zero from a manager": "No puedes cambiar el cŕedito establecido a cero por un gerente",
"The PDF document does not exists": "El documento PDF no existe. Prueba a regenerarlo desde la opción 'Regenerar PDF factura'", "The PDF document does not exists": "El documento PDF no existe. Prueba a regenerarlo desde la opción 'Regenerar PDF factura'",
"The type of business must be filled in basic data": "El tipo de negocio debe estar rellenado en datos básicos", "The type of business must be filled in basic data": "El tipo de negocio debe estar rellenado en datos básicos",
"You can't create a claim from a ticket delivered more than seven days ago": "No puedes crear una reclamación de un ticket entregado hace más de siete días" "You can't create a claim from a ticket delivered more than seven days ago": "No puedes crear una reclamación de un ticket entregado hace más de siete días",
"The worker has hours recorded that day": "El trabajador tiene horas fichadas ese día",
"The worker has a marked absence that day": "El trabajador tiene marcada una ausencia ese día"
} }

View File

@ -47,11 +47,24 @@ module.exports = Self => {
throw new UserError(`You don't have enough privileges`); throw new UserError(`You don't have enough privileges`);
const timed = new Date(args.timed); const timed = new Date(args.timed);
timed.setHours(0, 0, 0, 0);
query = `SELECT * FROM vn.workerLabour WHERE workerFk = ? AND (ended >= ? OR ended IS NULL);`;
const [workerLabour] = await Self.rawSql(query, [workerId, timed]);
const hasAbsence = await models.Calendar.findOne({
where: {
businessFk: workerLabour.businessFk,
dated: timed
}
});
if (hasAbsence)
throw new UserError(`The worker has a marked absence that day`);
return models.WorkerTimeControl.create({ return models.WorkerTimeControl.create({
userFk: workerId, userFk: workerId,
direction: args.direction, direction: args.direction,
timed: timed, timed: args.timed,
manual: true manual: true
}, myOptions); }, myOptions);
}; };

View File

@ -77,6 +77,23 @@ describe('workerTimeControl add/delete timeEntry()', () => {
} }
}); });
it('should fail to add a time entry if the target user has absent that day', async() => {
activeCtx.accessToken.userId = salesBossId;
const workerId = salesPersonId;
let error;
let calendar = await app.models.Calendar.findById(3);
try {
ctx.args = {timed: new Date(calendar.dated), direction: 'in'};
await models.WorkerTimeControl.addTimeEntry(ctx, workerId);
} catch (e) {
error = e;
}
expect(error.message).toBe(`The worker has a marked absence that day`);
});
it('should try but fail to delete his own time entry', async() => { it('should try but fail to delete his own time entry', async() => {
activeCtx.accessToken.userId = salesBossId; activeCtx.accessToken.userId = salesBossId;
const workerId = salesBossId; const workerId = salesBossId;

View File

@ -65,6 +65,15 @@ module.exports = Self => {
if (args.dated < labour.started || (labour.ended != null && args.dated > labour.ended)) if (args.dated < labour.started || (labour.ended != null && args.dated > labour.ended))
throw new UserError(`The contract was not active during the selected date`); throw new UserError(`The contract was not active during the selected date`);
query = `SELECT *
FROM vn.workerTimeControl
WHERE userFk = ? AND timed BETWEEN DATE(?) AND CONCAT(DATE(?), ' 23:59:59')
LIMIT 1;`;
const [hasHoursRecorded] = await Self.rawSql(query, [args.id, args.dated, args.dated]);
if (hasHoursRecorded)
throw new UserError(`The worker has hours recorded that day`);
const result = await Self.rawSql( const result = await Self.rawSql(
`SELECT COUNT(*) halfHolidayCounter `SELECT COUNT(*) halfHolidayCounter
FROM vn.calendar c FROM vn.calendar c

View File

@ -102,4 +102,32 @@ describe('Worker createAbsence()', () => {
expect(error.message).toEqual(`Cannot add more than one '1/2 day vacation'`); expect(error.message).toEqual(`Cannot add more than one '1/2 day vacation'`);
}); });
it(`should throw an error when adding a absence if the worker has hours recorded that day`, async() => {
const ctx = {
req: {accessToken: {userId: 19}},
args: {
id: 1106,
businessFk: 1106,
absenceTypeId: 6,
dated: new Date()
}
};
const tx = await app.models.Calendar.beginTransaction({});
let error;
try {
const options = {transaction: tx};
await app.models.Worker.createAbsence(ctx, workerId, options);
await tx.rollback();
} catch (e) {
await tx.rollback();
error = e;
}
expect(error.message).toEqual(`The worker has hours recorded that day`);
});
}); });

View File

@ -11,6 +11,9 @@
"id": true, "id": true,
"type": "Number" "type": "Number"
}, },
"workerFk": {
"type": "Number"
},
"started": { "started": {
"type": "date" "type": "date"
}, },