feat(worker): absences can return all absences of worker

This commit is contained in:
Alex Moreno 2022-02-25 10:33:47 +01:00
parent c3e480b2e7
commit bf712ba5af
6 changed files with 54 additions and 39 deletions

View File

@ -1851,6 +1851,15 @@ INSERT INTO `postgresql`.`business_labour`(`business_id`, `notes`, `department_i
SELECT b.business_id, NULL, 23, 1, 0, 1, 1, 1, 1
FROM `postgresql`.`business` `b`;
INSERT INTO `postgresql`.`business` (`client_id`, `provider_id`, `date_start`, `date_end`, `workerBusiness`, `reasonEndFk`)
SELECT p.profile_id, 1000, CONCAT(YEAR(DATE_ADD(CURDATE(), INTERVAL -2 YEAR)), '-12-25'), CONCAT(YEAR(DATE_ADD(CURDATE(), INTERVAL -1 YEAR)), '-12-24'), CONCAT('E-46-',RPAD(CONCAT(p.profile_id,9),8,p.profile_id)), NULL
FROM `postgresql`.`profile` `p`
WHERE `p`.`profile_id` = 1109;
INSERT INTO `postgresql`.`business_labour` (`business_id`, `notes`, `department_id`, `professional_category_id`, `incentivo`, `calendar_labour_type_id`, `porhoras`, `labour_agreement_id`, `workcenter_id`)
VALUES
(1111, NULL, 23, 1, 0.0, 1, 1, 1, 1);
UPDATE `postgresql`.`business_labour` bl
JOIN `postgresql`.`business` b ON b.business_id = bl.business_id
JOIN `postgresql`.`profile` pr ON pr.profile_id = b.client_id

View File

@ -4,10 +4,15 @@ module.exports = Self => {
Self.remoteMethodCtx('absences', {
description: 'Returns an array of absences from an specified contract',
accepts: [{
arg: 'businessFk',
arg: 'workerFk',
type: 'number',
required: true,
},
{
arg: 'businessFk',
type: 'number',
required: false,
},
{
arg: 'year',
type: 'date',
@ -27,7 +32,7 @@ module.exports = Self => {
}
});
Self.absences = async(ctx, businessFk, year, options) => {
Self.absences = async(ctx, workerFk, businessFk, year, options) => {
const models = Self.app.models;
const started = new Date();
@ -45,7 +50,17 @@ module.exports = Self => {
if (typeof options == 'object')
Object.assign(myOptions, options);
const contract = await models.WorkerLabour.findOne({
let condition = {
and: [
{workerFk: workerFk},
{businessFk: businessFk}
]
};
if (businessFk)
condition.and.push({workerFk: workerFk});
const contracts = await models.WorkerLabour.find({
include: [{
relation: 'holidays',
scope: {
@ -82,16 +97,19 @@ module.exports = Self => {
}
}
}],
where: {businessFk}
where: condition
}, myOptions);
if (!contract) return;
if (!contracts) return;
const isSubordinate = await models.Worker.isSubordinate(ctx, contract.workerFk, myOptions);
const isSubordinate = await models.Worker.isSubordinate(ctx, workerFk, myOptions);
if (!isSubordinate)
throw new UserError(`You don't have enough privileges`);
const absences = [];
const holidays = [];
for (let contract of contracts) {
for (let absence of contract.absences()) {
absence.dated = new Date(absence.dated);
absence.dated.setHours(0, 0, 0, 0);
@ -99,15 +117,13 @@ module.exports = Self => {
absences.push(absence);
}
// Workcenter holidays
const holidays = [];
const holidayList = contract.workCenter().holidays();
for (let day of holidayList) {
for (let day of contract.workCenter().holidays()) {
day.dated = new Date(day.dated);
day.dated.setHours(0, 0, 0, 0);
holidays.push(day);
}
}
return [absences, holidays];
};

View File

@ -3,12 +3,13 @@ const app = require('vn-loopback/server/server');
describe('Worker absences()', () => {
it('should get the absence calendar for a full year contract', async() => {
const ctx = {req: {accessToken: {userId: 1106}}};
const workerId = 1106;
const businessId = 1106;
const now = new Date();
const year = now.getFullYear();
const [absences] = await app.models.Calendar.absences(ctx, businessId, year);
const [absences] = await app.models.Calendar.absences(ctx, workerId, businessId, year);
const firstType = absences[0].absenceType().name;
const sixthType = absences[5].absenceType().name;
@ -35,7 +36,7 @@ describe('Worker absences()', () => {
`UPDATE postgresql.business SET date_end = ? WHERE business_id = ?`,
[null, worker.businessFk], options);
const [absences] = await app.models.Calendar.absences(ctx, businessId, year, options);
const [absences] = await app.models.Calendar.absences(ctx, worker.id, businessId, year, options);
let firstType = absences[0].absenceType().name;
let sixthType = absences[5].absenceType().name;
@ -51,6 +52,8 @@ describe('Worker absences()', () => {
it('should give the same holidays as worked days since the holidays amount matches the amount of days in a year', async() => {
const businessId = 1106;
const workerId = 1106;
const userId = 1106;
const today = new Date();
@ -101,7 +104,7 @@ describe('Worker absences()', () => {
const ctx = {req: {accessToken: {userId: userId}}};
const [absences] = await app.models.Calendar.absences(ctx, businessId, currentYear);
const [absences] = await app.models.Calendar.absences(ctx, workerId, businessId, currentYear);
const firstType = absences[0].absenceType().name;
const sixthType = absences[5].absenceType().name;

View File

@ -282,6 +282,7 @@ class Controller extends Section {
refresh() {
const params = {
workerFk: this.$params.id,
businessFk: this.businessId,
year: this.year
};

View File

@ -328,7 +328,7 @@ describe('Worker', () => {
jest.spyOn(controller, 'onData').mockReturnThis();
const expecteResponse = [{id: 1}];
const expectedParams = {year: year};
const expectedParams = {workerFk: controller.worker.id, year: year};
const serializedParams = $httpParamSerializer(expectedParams);
$httpBackend.expect('GET', `Calendars/absences?${serializedParams}`).respond(200, expecteResponse);
controller.refresh();

View File

@ -32,11 +32,6 @@ class Controller extends Section {
set worker(value) {
this._worker = value;
if (value) {
this.getActiveContract()
.then(() => this.getAbsences());
}
}
/**
@ -96,14 +91,6 @@ class Controller extends Section {
}
}
getActiveContract() {
return this.$http.get(`Workers/${this.worker.id}/activeContract`)
.then(res => {
if (res.data)
this.businessId = res.data.businessFk;
});
}
fetchHours() {
const params = {workerFk: this.$params.id};
const filter = {
@ -123,11 +110,10 @@ class Controller extends Section {
}
getAbsences() {
if (!this.businessId) return;
const fullYear = this.started.getFullYear();
let params = {
businessFk: this.businessId,
workerFk: this.$params.id,
businessFk: null,
year: fullYear
};