add exceprion to half day absence
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Vicent Llopis 2021-11-30 12:31:02 +01:00
parent 4c064f8a80
commit 21bd625b3b
3 changed files with 18 additions and 13 deletions

View File

@ -46,21 +46,24 @@ module.exports = Self => {
if (isSubordinate === false || (isSubordinate && isHimself && !isTeamBoss))
throw new UserError(`You don't have enough privileges`);
const timed = new Date(args.timed);
timed.setHours(0, 0, 0, 0);
const minTime = new Date(args.timed);
minTime.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({
const [workerLabour] = await Self.rawSql(query, [workerId, minTime]);
const absence = await models.Calendar.findOne({
where: {
businessFk: workerLabour.businessFk,
dated: timed
dated: minTime
}
});
if (absence) {
const absenceType = await models.AbsenceType.findById(absence.dayOffTypeFk, null, myOptions);
const isNotHalfAbsence = absenceType.id != 6 && absenceType.id != 15 && absenceType.id != 21;
if (hasAbsence)
throw new UserError(`The worker has a marked absence that day`);
if (isNotHalfAbsence)
throw new UserError(`The worker has a marked absence that day`);
}
return models.WorkerTimeControl.create({
userFk: workerId,
direction: args.direction,

View File

@ -71,10 +71,12 @@ module.exports = Self => {
LIMIT 1;`;
const [hasHoursRecorded] = await Self.rawSql(query, [args.id, args.dated, args.dated]);
if (hasHoursRecorded)
const isNotHalfAbsence = args.absenceTypeId != 6 && args.absenceTypeId != 15 && args.absenceTypeId != 21;
if (hasHoursRecorded && isNotHalfAbsence)
throw new UserError(`The worker has hours recorded that day`);
const result = await Self.rawSql(
const [result] = await Self.rawSql(
`SELECT COUNT(*) halfHolidayCounter
FROM vn.calendar c
JOIN postgresql.business b ON b.business_id = c.businessFk
@ -85,7 +87,7 @@ module.exports = Self => {
AND c.dated BETWEEN util.firstDayOfYear(CURDATE())
AND LAST_DAY(DATE_ADD(NOW(), INTERVAL 12-MONTH(NOW()) MONTH))`, [args.id]);
const hasHalfHoliday = result[0].halfHolidayCounter > 0;
const hasHalfHoliday = result.halfHolidayCounter > 0;
const isHalfHoliday = args.absenceTypeId == 6;
if (isHalfHoliday && hasHalfHoliday)

View File

@ -103,13 +103,13 @@ describe('Worker createAbsence()', () => {
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() => {
it(`should throw an error when adding a absence if the worker has hours recorded that day and not is a half absence`, async() => {
const ctx = {
req: {accessToken: {userId: 19}},
args: {
id: 1106,
businessFk: 1106,
absenceTypeId: 6,
absenceTypeId: 1,
dated: new Date()
}
};