Worker calendar rework #645

Merged
carlosjr merged 15 commits from 2567-calendar_rework into dev 2021-06-14 10:04:45 +00:00
11 changed files with 278 additions and 346 deletions
Showing only changes of commit 8284702603 - Show all commits

View File

@ -11,7 +11,7 @@ import './style.scss';
* @property {String} valueField The data field name that should be used as value * @property {String} valueField The data field name that should be used as value
* @property {Array} data Static data for the autocomplete * @property {Array} data Static data for the autocomplete
* @property {Object} intialData An initial data to avoid the server request used to get the selection * @property {Object} intialData An initial data to avoid the server request used to get the selection
* @property {Boolean} multiple Wether to allow multiple selection * @property {Boolean} multiple Whether to allow multiple selection
* @property {Object} selection Current object selected * @property {Object} selection Current object selected
* *
* @event change Thrown when value is changed * @event change Thrown when value is changed

View File

@ -17,7 +17,7 @@ export default class Popup extends Component {
} }
/** /**
* @type {Boolean} Wether to show or hide the popup. * @type {Boolean} Whether to show or hide the popup.
*/ */
get shown() { get shown() {
return this._shown; return this._shown;

View File

@ -2,7 +2,7 @@ const UserError = require('vn-loopback/util/user-error');
module.exports = Self => { module.exports = Self => {
Self.remoteMethodCtx('absences', { Self.remoteMethodCtx('absences', {
description: 'Returns an array of absences from an specified worker', description: 'Returns an array of absences from an specified contract',
accepts: [{ accepts: [{
arg: 'businessFk', arg: 'businessFk',
type: 'number', type: 'number',
@ -14,9 +14,6 @@ module.exports = Self => {
required: true, required: true,
}], }],
returns: [{ returns: [{
arg: 'calendar'
},
{
arg: 'absences', arg: 'absences',
type: 'number' type: 'number'
}, },
@ -30,13 +27,8 @@ module.exports = Self => {
} }
}); });
Self.absences = async(ctx, businessFk, year) => { Self.absences = async(ctx, businessFk, year, options) => {
const models = Self.app.models; const models = Self.app.models;
const calendar = {totalHolidays: 0, holidaysEnjoyed: 0};
const holidays = [];
// Get active contracts on current year
// const year = yearStarted.getFullYear();
const started = new Date(); const started = new Date();
started.setFullYear(year); started.setFullYear(year);
@ -48,6 +40,11 @@ module.exports = Self => {
ended.setMonth(12); ended.setMonth(12);
ended.setDate(0); ended.setDate(0);
let myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
const contract = await models.WorkerLabour.findOne({ const contract = await models.WorkerLabour.findOne({
include: [{ include: [{
relation: 'holidays', relation: 'holidays',
@ -55,6 +52,17 @@ module.exports = Self => {
where: {year} where: {year}
} }
}, },
{
relation: 'absences',
scope: {
include: {
relation: 'absenceType',
},
where: {
dated: {between: [started, ended]}
}
}
},
{ {
relation: 'workCenter', relation: 'workCenter',
scope: { scope: {
@ -75,58 +83,25 @@ module.exports = Self => {
} }
}], }],
where: {businessFk} where: {businessFk}
}); }, myOptions);
if (!contract) return; if (!contract) return;
const isSubordinate = await models.Worker.isSubordinate(ctx, contract.workerFk); const isSubordinate = await models.Worker.isSubordinate(ctx, contract.workerFk, myOptions);
if (!isSubordinate) if (!isSubordinate)
throw new UserError(`You don't have enough privileges`); throw new UserError(`You don't have enough privileges`);
// Get absences of year const absences = [];
let absences = await Self.find({ for (let absence of contract.absences()) {
include: {
relation: 'absenceType'
},
where: {
businessFk: contract.businessFk,
dated: {between: [started, ended]}
}
});
let entitlementRate = 0;
absences.forEach(absence => {
const absenceType = absence.absenceType();
const isHoliday = absenceType.code === 'holiday';
const isHalfHoliday = absenceType.code === 'halfHoliday';
if (isHoliday)
calendar.holidaysEnjoyed += 1;
if (isHalfHoliday)
calendar.holidaysEnjoyed += 0.5;
entitlementRate += absenceType.holidayEntitlementRate;
absence.dated = new Date(absence.dated); absence.dated = new Date(absence.dated);
absence.dated.setHours(0, 0, 0, 0); absence.dated.setHours(0, 0, 0, 0);
});
// Get number of worked days absences.push(absence);
let workedDays = 0; }
const contractStarted = contract.started;
const contractEnded = contract.ended;
// esta mal, la fecha de inicio puede ser un año anterior...
const startedTime = contractStarted < started ? started.getTime() : contractStarted.getTime();
const endedTime = contractEnded && contractEnded.getTime() || ended;
const dayTimestamp = 1000 * 60 * 60 * 24;
workedDays += Math.floor((endedTime - startedTime) / dayTimestamp);
if (workedDays > daysInYear())
workedDays = daysInYear();
// Workcenter holidays // Workcenter holidays
let holidayList = contract.workCenter().holidays(); const holidays = [];
const holidayList = contract.workCenter().holidays();
for (let day of holidayList) { for (let day of holidayList) {
day.dated = new Date(day.dated); day.dated = new Date(day.dated);
day.dated.setHours(0, 0, 0, 0); day.dated.setHours(0, 0, 0, 0);
@ -134,24 +109,6 @@ module.exports = Self => {
holidays.push(day); holidays.push(day);
} }
const maxHolidays = contract.holidays() && contract.holidays().days; return [absences, holidays];
calendar.totalHolidays = maxHolidays;
workedDays -= entitlementRate;
if (workedDays < daysInYear())
calendar.totalHolidays = Math.round(2 * maxHolidays * (workedDays) / daysInYear()) / 2;
function daysInYear() {
const year = started.getFullYear();
return isLeapYear(year) ? 366 : 365;
}
return [calendar, absences, holidays];
}; };
function isLeapYear(year) {
return year % 400 === 0 || (year % 100 !== 0 && year % 4 === 0);
}
}; };

View File

@ -34,50 +34,70 @@ module.exports = Self => {
} }
}); });
Self.createAbsence = async(ctx, id, businessFk, absenceTypeId, dated) => { Self.createAbsence = async(ctx, id, options) => {
const models = Self.app.models; const models = Self.app.models;
const $t = ctx.req.__; // $translate const $t = ctx.req.__; // $translate
const args = ctx.args;
const userId = ctx.req.accessToken.userId; const userId = ctx.req.accessToken.userId;
const isSubordinate = await models.Worker.isSubordinate(ctx, id);
const isTeamBoss = await models.Account.hasRole(userId, 'teamBoss'); let tx;
let myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
if (!myOptions.transaction) {
tx = await Self.beginTransaction({});
myOptions.transaction = tx;
}
try {
const isSubordinate = await models.Worker.isSubordinate(ctx, id, myOptions);
const isTeamBoss = await models.Account.hasRole(userId, 'teamBoss', myOptions);
if (!isSubordinate || (isSubordinate && userId == id && !isTeamBoss)) if (!isSubordinate || (isSubordinate && userId == id && !isTeamBoss))
throw new UserError(`You don't have enough privileges`); throw new UserError(`You don't have enough privileges`);
const labour = await models.WorkerLabour.findById(businessFk, { const labour = await models.WorkerLabour.findById(args.businessFk, {
include: {relation: 'department'} include: {relation: 'department'}
}); }, myOptions);
if (dated < labour.started || (labour.ended != null && 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`);
const absence = await models.Calendar.create({ const absence = await models.Calendar.create({
businessFk: labour.businessFk, businessFk: labour.businessFk,
dayOffTypeFk: absenceTypeId, dayOffTypeFk: args.absenceTypeId,
dated: dated dated: args.dated
}); }, myOptions);
const department = labour.department(); const department = labour.department();
if (department && department.notificationEmail) { if (department && department.notificationEmail) {
const absenceType = await models.AbsenceType.findById(absenceTypeId); const absenceType = await models.AbsenceType.findById(args.absenceTypeId, null, myOptions);
const account = await models.Account.findById(userId); const account = await models.Account.findById(userId, null, myOptions);
const subordinated = await models.Account.findById(id); const subordinated = await models.Account.findById(id, null, myOptions);
const origin = ctx.req.headers.origin; const origin = ctx.req.headers.origin;
const body = $t('Created absence', { const body = $t('Created absence', {
author: account.nickname, author: account.nickname,
employee: subordinated.nickname, employee: subordinated.nickname,
absenceType: absenceType.name, absenceType: absenceType.name,
dated: formatDate(dated), dated: formatDate(args.dated),
workerUrl: `${origin}/#!/worker/${id}/calendar` workerUrl: `${origin}/#!/worker/${id}/calendar`
}); });
await models.Mail.create({ await models.Mail.create({
subject: $t('Absence change notification on the labour calendar'), subject: $t('Absence change notification on the labour calendar'),
body: body, body: body,
sender: department.notificationEmail sender: department.notificationEmail
}); }, myOptions);
} }
if (tx) await tx.commit();
return absence; return absence;
} catch (e) {
if (tx) await tx.rollback();
throw e;
}
}; };
function formatDate(date) { function formatDate(date) {

View File

@ -5,13 +5,13 @@ module.exports = Self => {
description: 'Deletes a worker absence', description: 'Deletes a worker absence',
accepts: [{ accepts: [{
arg: 'id', arg: 'id',
type: 'Number', type: 'number',
description: 'The worker id', description: 'The worker id',
http: {source: 'path'} http: {source: 'path'}
}, },
{ {
arg: 'absenceId', arg: 'absenceId',
type: 'Number', type: 'number',
required: true required: true
}], }],
returns: 'Object', returns: 'Object',
@ -21,31 +21,45 @@ module.exports = Self => {
} }
}); });
Self.deleteAbsence = async(ctx, id, absenceId) => { Self.deleteAbsence = async(ctx, id, options) => {
const models = Self.app.models; const models = Self.app.models;
const $t = ctx.req.__; // $translate const $t = ctx.req.__; // $translate
const args = ctx.args;
const userId = ctx.req.accessToken.userId; const userId = ctx.req.accessToken.userId;
const isSubordinate = await models.Worker.isSubordinate(ctx, id);
const isTeamBoss = await models.Account.hasRole(userId, 'teamBoss'); let tx;
let myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
if (!myOptions.transaction) {
tx = await Self.beginTransaction({});
myOptions.transaction = tx;
}
try {
const isSubordinate = await models.Worker.isSubordinate(ctx, id, myOptions);
const isTeamBoss = await models.Account.hasRole(userId, 'teamBoss', myOptions);
if (!isSubordinate || (isSubordinate && userId == id && !isTeamBoss)) if (!isSubordinate || (isSubordinate && userId == id && !isTeamBoss))
throw new UserError(`You don't have enough privileges`); throw new UserError(`You don't have enough privileges`);
const absence = await models.Calendar.findById(absenceId, { const absence = await models.Calendar.findById(args.absenceId, {
include: { include: {
relation: 'labour', relation: 'labour',
scope: { scope: {
include: {relation: 'department'} include: {relation: 'department'}
} }
} }
}); }, myOptions);
const result = await absence.destroy(); const result = await absence.destroy(myOptions);
const labour = absence.labour(); const labour = absence.labour();
const department = labour && labour.department(); const department = labour && labour.department();
if (department && department.notificationEmail) { if (department && department.notificationEmail) {
const absenceType = await models.AbsenceType.findById(absence.dayOffTypeFk); const absenceType = await models.AbsenceType.findById(absence.dayOffTypeFk, null, myOptions);
const account = await models.Account.findById(userId); const account = await models.Account.findById(userId, null, myOptions);
const subordinated = await models.Account.findById(labour.workerFk); const subordinated = await models.Account.findById(labour.workerFk, null, myOptions);
const origin = ctx.req.headers.origin; const origin = ctx.req.headers.origin;
const body = $t('Deleted absence', { const body = $t('Deleted absence', {
author: account.nickname, author: account.nickname,
@ -58,10 +72,16 @@ module.exports = Self => {
subject: $t('Absence change notification on the labour calendar'), subject: $t('Absence change notification on the labour calendar'),
body: body, body: body,
sender: department.notificationEmail sender: department.notificationEmail
}); }, myOptions);
} }
if (tx) await tx.commit();
return result; return result;
} catch (e) {
if (tx) await tx.rollback();
throw e;
}
}; };
function formatDate(date) { function formatDate(date) {

View File

@ -2,7 +2,7 @@ const UserError = require('vn-loopback/util/user-error');
module.exports = Self => { module.exports = Self => {
Self.remoteMethodCtx('holidays', { Self.remoteMethodCtx('holidays', {
description: 'Returns data - Change me', description: 'Returns the holidays available whitin a contract or a year',
accepts: [{ accepts: [{
arg: 'id', arg: 'id',
type: 'number', type: 'number',
@ -30,26 +30,27 @@ module.exports = Self => {
} }
}); });
Self.holidays = async(ctx, id, year, businessFk) => { Self.holidays = async(ctx, id, options) => {
const models = Self.app.models; const models = Self.app.models;
/* const calendar = {totalHolidays: 0, holidaysEnjoyed: 0}; const args = ctx.args;
const holidays = []; */
const isSubordinate = await models.Worker.isSubordinate(ctx, id); let myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
const isSubordinate = await models.Worker.isSubordinate(ctx, id, myOptions);
if (!isSubordinate) if (!isSubordinate)
throw new UserError(`You don't have enough privileges`); throw new UserError(`You don't have enough privileges`);
// Get active contracts on current year
// const year = yearStarted.getFullYear();
const started = new Date(); const started = new Date();
started.setFullYear(year); started.setFullYear(args.year);
started.setMonth(0); started.setMonth(0);
started.setDate(1); started.setDate(1);
started.setHours(0, 0, 0, 0); started.setHours(0, 0, 0, 0);
const ended = new Date(); const ended = new Date();
ended.setFullYear(year); ended.setFullYear(args.year);
ended.setMonth(12); ended.setMonth(12);
ended.setDate(0); ended.setDate(0);
ended.setHours(23, 59, 59, 59); ended.setHours(23, 59, 59, 59);
@ -58,7 +59,7 @@ module.exports = Self => {
include: [{ include: [{
relation: 'holidays', relation: 'holidays',
scope: { scope: {
where: {year} where: {year: args.year}
} }
}, },
{ {
@ -94,49 +95,26 @@ module.exports = Self => {
where: { where: {
and: [ and: [
{workerFk: id}, {workerFk: id},
{or: [{ {
ended: {gte: [started]} or: [
}, {ended: null}]} {started: {between: [started, ended]}},
{ended: {between: [started, ended]}},
{and: [{started: {lt: started}}, {ended: {gt: ended}}]},
{and: [{started: {lt: started}}, {ended: null}]}
]
}
], ],
} }
}; };
if (businessFk) if (args.businessFk)
filter.where = {businessFk}; filter.where.and.push({businessFk: args.businessFk});
const contracts = await models.WorkerLabour.find(filter);
if (!contracts.length) return; const contracts = await models.WorkerLabour.find(filter, myOptions);
// Contracts ids
const contractsId = contracts.map(contract => contract.businessFk);
// Get absences of year
const absences = await models.Calendar.find({
include: {
relation: 'absenceType'
},
where: {
businessFk: {inq: contractsId},
dated: {between: [started, ended]}
}
});
let totalHolidays = 0; let totalHolidays = 0;
let holidaysEnjoyed = 0; let holidaysEnjoyed = 0;
/* for (let absence of absences) {
const absenceType = absence.absenceType();
const isHoliday = absenceType.code === 'holiday';
const isHalfHoliday = absenceType.code === 'halfHoliday';
if (isHoliday) holidaysEnjoyed += 1;
if (isHalfHoliday) holidaysEnjoyed += 0.5;
entitlementRate += absenceType.holidayEntitlementRate;
} */
// Get number of worked days
// let totalWorkedDays = 0;
for (let contract of contracts) { for (let contract of contracts) {
const contractStarted = contract.started; const contractStarted = contract.started;
contractStarted.setHours(0, 0, 0, 0); contractStarted.setHours(0, 0, 0, 0);
@ -144,29 +122,23 @@ module.exports = Self => {
if (contractEnded) if (contractEnded)
contractEnded.setHours(23, 59, 59, 59); contractEnded.setHours(23, 59, 59, 59);
// esta mal, la fecha de inicio puede ser un año anterior... let startedTime;
const startedTime = contractStarted < started ? started.getTime() : contractStarted.getTime(); if (contractStarted < started)
const endedTime = !contractEnded || contractEnded && contractEnded > ended ? ended.getTime() : contractEnded.getTime(); startedTime = started.getTime();
// const endedTime = contractEnded && contractEnded.getTime() || ended.getTime(); else contractStarted.getTime();
let endedTime;
if (!contractEnded || (contractEnded && contractEnded > ended))
endedTime = ended.getTime();
else contractEnded.getTime();
const dayTimestamp = 1000 * 60 * 60 * 24; const dayTimestamp = 1000 * 60 * 60 * 24;
let workedDays = Math.floor((endedTime - startedTime) / dayTimestamp); // debería dar un computo de 366 dias // Get number of worked days between dates
let workedDays = Math.floor((endedTime - startedTime) / dayTimestamp);
workedDays += 1; // 1 day inclusion workedDays += 1; // 1 day inclusion
// workedDays -= entitlementRate;
/* if (workedDays > daysInYear()) // Calculates absences
workedDays = daysInYear(); */
// Workcenter holidays
/* let holidayList = contract.workCenter().holidays();
for (let day of holidayList) {
day.dated = new Date(day.dated);
day.dated.setHours(0, 0, 0, 0);
holidays.push(day);
}
*/
let entitlementRate = 0; let entitlementRate = 0;
for (let absence of contract.absences()) { for (let absence of contract.absences()) {
const absenceType = absence.absenceType(); const absenceType = absence.absenceType();
@ -177,22 +149,16 @@ module.exports = Self => {
if (isHalfHoliday) holidaysEnjoyed += 0.5; if (isHalfHoliday) holidaysEnjoyed += 0.5;
entitlementRate += absenceType.holidayEntitlementRate; entitlementRate += absenceType.holidayEntitlementRate;
// absence.dated = new Date(absence.dated); // not needed
// absence.dated.setHours(0, 0, 0, 0);
} }
workedDays -= entitlementRate; workedDays -= entitlementRate;
// Set max holidays // Max holidays for the selected year
// const maxHolidays = contract.holidays() && contract.holidays().days;
const maxHolidays = contract.holidays() && contract.holidays().days; const maxHolidays = contract.holidays() && contract.holidays().days;
if (workedDays < daysInYear()) if (workedDays < daysInYear())
totalHolidays += Math.round(2 * maxHolidays * (workedDays) / daysInYear()) / 2; totalHolidays += Math.round(2 * maxHolidays * (workedDays) / daysInYear()) / 2;
else totalHolidays = maxHolidays; else totalHolidays = maxHolidays;
// totalWorkedDays += workedDays;
} }
function daysInYear() { function daysInYear() {
@ -207,35 +173,4 @@ module.exports = Self => {
function isLeapYear(year) { function isLeapYear(year) {
return year % 400 === 0 || (year % 100 !== 0 && year % 4 === 0); return year % 400 === 0 || (year % 100 !== 0 && year % 4 === 0);
} }
/*
const contract = await models.WorkerLabour.findOne({
include: [{
relation: 'holidays',
scope: {
where: {year}
}
},
{
relation: 'workCenter',
scope: {
include: {
relation: 'holidays',
scope: {
include: [{
relation: 'detail'
},
{
relation: 'type'
}],
where: {
dated: {between: [started, ended]}
}
}
}
}
}],
where: {businessFk}
}); */
}; };

View File

@ -23,16 +23,21 @@ module.exports = Self => {
} }
}); });
Self.isSubordinate = async(ctx, id) => { Self.isSubordinate = async(ctx, id, options) => {
const models = Self.app.models; const models = Self.app.models;
const myUserId = ctx.req.accessToken.userId; const myUserId = ctx.req.accessToken.userId;
const mySubordinates = await Self.mySubordinates(ctx); let myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
const mySubordinates = await Self.mySubordinates(ctx, myOptions);
const isSubordinate = mySubordinates.find(subordinate => { const isSubordinate = mySubordinates.find(subordinate => {
return subordinate.workerFk == id; return subordinate.workerFk == id;
}); });
const isHr = await models.Account.hasRole(myUserId, 'hr'); const isHr = await models.Account.hasRole(myUserId, 'hr', myOptions);
if (isHr || isSubordinate) if (isHr || isSubordinate)
return true; return true;

View File

@ -20,17 +20,22 @@ module.exports = Self => {
} }
}); });
Self.mySubordinates = async ctx => { Self.mySubordinates = async(ctx, options) => {
const conn = Self.dataSource.connector; const conn = Self.dataSource.connector;
const userId = ctx.req.accessToken.userId; const userId = ctx.req.accessToken.userId;
const stmts = []; const stmts = [];
let myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
stmts.push(new ParameterizedSQL('CALL vn.subordinateGetList(?)', [userId])); stmts.push(new ParameterizedSQL('CALL vn.subordinateGetList(?)', [userId]));
const queryIndex = stmts.push('SELECT * FROM tmp.subordinate') - 1; const queryIndex = stmts.push('SELECT * FROM tmp.subordinate') - 1;
stmts.push('DROP TEMPORARY TABLE tmp.subordinate'); stmts.push('DROP TEMPORARY TABLE tmp.subordinate');
const sql = ParameterizedSQL.join(stmts, ';'); const sql = ParameterizedSQL.join(stmts, ';');
const result = await conn.executeStmt(sql); const result = await conn.executeStmt(sql, myOptions);
return result[queryIndex]; return result[queryIndex];
}; };

View File

@ -12,10 +12,10 @@
"type": "Number" "type": "Number"
}, },
"started": { "started": {
"type": "Date" "type": "date"
}, },
"ended": { "ended": {
"type": "Date" "type": "date"
} }
}, },
"relations": { "relations": {

View File

@ -26,6 +26,20 @@ class Controller extends Section {
.then(() => this.getYearHolidays()); .then(() => this.getYearHolidays());
} }
get businessId() {
return this._businessId;
}
set businessId(value) {
this._businessId = value;
if (value) {
this.refresh()
.then(() => this.repaint())
.then(() => this.getContractHolidays())
.then(() => this.getYearHolidays());
}
}
get date() { get date() {
return this._date; return this._date;
} }
@ -34,16 +48,6 @@ class Controller extends Section {
this._date = value; this._date = value;
value.setHours(0, 0, 0, 0); value.setHours(0, 0, 0, 0);
const started = new Date(value.getTime());
started.setMonth(0);
started.setDate(1);
this.started = started;
const ended = new Date(value.getTime());
ended.setMonth(12);
ended.setDate(0);
this.ended = ended;
this.months = new Array(12); this.months = new Array(12);
for (let i = 0; i < this.months.length; i++) { for (let i = 0; i < this.months.length; i++) {
@ -62,26 +66,11 @@ class Controller extends Section {
this._worker = value; this._worker = value;
if (value) { if (value) {
// this.refresh().then(() => this.repaint());
this.getIsSubordinate(); this.getIsSubordinate();
this.getActiveContract(); this.getActiveContract();
} }
} }
get businessId() {
return this._businessId;
}
set businessId(value) {
this._businessId = value;
if (value) {
this.refresh()
.then(() => this.repaint())
.then(() => this.getContractHolidays())
.then(() => this.getYearHolidays());
}
}
buildYearFilter() { buildYearFilter() {
const now = new Date(); const now = new Date();
now.setFullYear(now.getFullYear() + 1); now.setFullYear(now.getFullYear() + 1);
@ -114,9 +103,9 @@ class Controller extends Section {
} }
getYearHolidays() { getYearHolidays() {
/* this.getHolidays({ this.getHolidays({
year: this.year year: this.year
}, data => this.yearHolidays = data); */ }, data => this.yearHolidays = data);
} }
getHolidays(params, cb) { getHolidays(params, cb) {
@ -198,9 +187,6 @@ class Controller extends Section {
if (!this.absenceType) if (!this.absenceType)
return this.vnApp.showMessage(this.$t('Choose an absence type from the right menu')); return this.vnApp.showMessage(this.$t('Choose an absence type from the right menu'));
if (this.year != new Date().getFullYear())
return this.vnApp.showMessage(this.$t('You can just add absences within the current year'));
const day = $days[0]; const day = $days[0];
const stamp = day.getTime(); const stamp = day.getTime();
const event = this.events[stamp]; const event = this.events[stamp];
@ -234,7 +220,10 @@ class Controller extends Section {
}; };
this.repaintCanceller(() => this.repaintCanceller(() =>
this.refresh().then(calendar.repaint()) this.refresh()
.then(calendar.repaint())
.then(() => this.getContractHolidays())
.then(() => this.getYearHolidays())
); );
}); });
} }
@ -252,7 +241,10 @@ class Controller extends Section {
event.type = absenceType.code; event.type = absenceType.code;
this.repaintCanceller(() => this.repaintCanceller(() =>
this.refresh().then(calendar.repaint()) this.refresh()
.then(calendar.repaint())
.then(() => this.getContractHolidays())
.then(() => this.getYearHolidays())
); );
}); });
} }
@ -264,7 +256,10 @@ class Controller extends Section {
delete this.events[day.getTime()]; delete this.events[day.getTime()];
this.repaintCanceller(() => this.repaintCanceller(() =>
this.refresh().then(calendar.repaint()) this.refresh()
.then(calendar.repaint())
.then(() => this.getContractHolidays())
.then(() => this.getYearHolidays())
); );
}); });
} }

View File

@ -41,35 +41,31 @@ describe('Worker', () => {
}); });
}); });
describe('started property', () => { describe('businessId() setter', () => {
it(`should return first day and month of current year`, () => { it(`should set the contract id and then call to the refresh method`, () => {
let started = new Date(year, 0, 1); jest.spyOn(controller, 'refresh').mockReturnValue(Promise.resolve());
expect(controller.started).toEqual(started); controller.businessId = 106;
});
});
describe('ended property', () => { expect(controller.refresh).toHaveBeenCalledWith();
it(`should return last day and month of current year`, () => {
let ended = new Date(year, 11, 31);
expect(controller.ended).toEqual(ended);
}); });
}); });
describe('months property', () => { describe('months property', () => {
it(`should return an array of twelve months length`, () => { it(`should return an array of twelve months length`, () => {
const started = new Date(year, 0, 1);
const ended = new Date(year, 11, 1); const ended = new Date(year, 11, 1);
expect(controller.months.length).toEqual(12); expect(controller.months.length).toEqual(12);
expect(controller.months[0]).toEqual(controller.started); expect(controller.months[0]).toEqual(started);
expect(controller.months[11]).toEqual(ended); expect(controller.months[11]).toEqual(ended);
}); });
}); });
describe('worker() setter', () => { describe('worker() setter', () => {
it(`should perform a get query and set the reponse data on the model`, () => { it(`should perform a get query and set the reponse data on the model`, () => {
jest.spyOn(controller, 'getIsSubordinate').mockReturnValue(true); controller.getIsSubordinate = jest.fn();
controller.getActiveContract = jest.fn();
let today = new Date(); let today = new Date();
let tomorrow = new Date(today.getTime()); let tomorrow = new Date(today.getTime());
@ -78,49 +74,60 @@ describe('Worker', () => {
let yesterday = new Date(today.getTime()); let yesterday = new Date(today.getTime());
yesterday.setDate(yesterday.getDate() - 1); yesterday.setDate(yesterday.getDate() - 1);
$httpBackend.whenRoute('GET', 'Calendars/absences') controller.worker = {id: 107};
.respond({
holidays: [ expect(controller.getIsSubordinate).toHaveBeenCalledWith();
{dated: today, detail: {name: 'New year'}}, expect(controller.getActiveContract).toHaveBeenCalledWith();
{dated: tomorrow, detail: {name: 'Easter'}} });
],
absences: [
{dated: today, absenceType: {name: 'Holiday', rgb: '#aaa'}},
{dated: yesterday, absenceType: {name: 'Leave', rgb: '#bbb'}}
]
}); });
controller.worker = {id: 107}; describe('getIsSubordinate()', () => {
it(`should return whether the worker is a subordinate`, () => {
$httpBackend.expect('GET', `Workers/106/isSubordinate`).respond(true);
controller.getIsSubordinate();
$httpBackend.flush(); $httpBackend.flush();
let events = controller.events; expect(controller.isSubordinate).toBe(true);
});
});
expect(events[today.getTime()].name).toEqual('New year, Holiday'); describe('getActiveContract()', () => {
expect(events[tomorrow.getTime()].name).toEqual('Easter'); it(`should return the current contract and then set the businessId property`, () => {
expect(events[yesterday.getTime()].name).toEqual('Leave'); jest.spyOn(controller, 'refresh').mockReturnValue(Promise.resolve());
expect(events[yesterday.getTime()].color).toEqual('#bbb');
expect(controller.getIsSubordinate).toHaveBeenCalledWith(); $httpBackend.expect('GET', `Workers/106/activeContract`).respond({businessFk: 106});
controller.getActiveContract();
$httpBackend.flush();
expect(controller.businessId).toEqual(106);
});
});
describe('getContractHolidays()', () => {
it(`should return the worker holidays amount and then set the contractHolidays property`, () => {
const today = new Date();
const year = today.getFullYear();
const serializedParams = $httpParamSerializer({year});
$httpBackend.expect('GET', `Workers/106/holidays?${serializedParams}`).respond({totalHolidays: 28});
controller.getContractHolidays();
$httpBackend.flush();
expect(controller.contractHolidays).toEqual({totalHolidays: 28});
}); });
}); });
describe('formatDay()', () => { describe('formatDay()', () => {
it(`should set the day element style`, () => { it(`should set the day element style`, () => {
jest.spyOn(controller, 'getIsSubordinate').mockReturnThis(); const today = new Date();
let today = new Date(); controller.events[today.getTime()] = {
name: 'Holiday',
color: '#000'
};
$httpBackend.whenRoute('GET', 'Calendars/absences') const dayElement = angular.element('<div><section></section></div>')[0];
.respond({ const dayNumber = dayElement.firstElementChild;
absences: [
{dated: today, absenceType: {name: 'Holiday', rgb: '#000'}}
]
});
controller.worker = {id: 1};
$httpBackend.flush();
let dayElement = angular.element('<div><section></section></div>')[0];
let dayNumber = dayElement.firstElementChild;
controller.formatDay(today, dayElement); controller.formatDay(today, dayElement);
@ -160,7 +167,7 @@ describe('Worker', () => {
expect(controller.vnApp.showMessage).toHaveBeenCalledWith('Choose an absence type from the right menu'); expect(controller.vnApp.showMessage).toHaveBeenCalledWith('Choose an absence type from the right menu');
}); });
it(`should show an snackbar message if the selected day is not within the current year`, () => { /* it(`should show an snackbar message if the selected day is not within the current year`, () => {
jest.spyOn(controller.vnApp, 'showMessage').mockReturnThis(); jest.spyOn(controller.vnApp, 'showMessage').mockReturnThis();
const selectedDay = new Date(); const selectedDay = new Date();
@ -180,7 +187,7 @@ describe('Worker', () => {
controller.onSelection($event, $days); controller.onSelection($event, $days);
expect(controller.vnApp.showMessage).toHaveBeenCalledWith('You can just add absences within the current year'); expect(controller.vnApp.showMessage).toHaveBeenCalledWith('You can just add absences within the current year');
}); }); */
it(`should call to the create() method`, () => { it(`should call to the create() method`, () => {
jest.spyOn(controller, 'create').mockReturnThis(); jest.spyOn(controller, 'create').mockReturnThis();
@ -342,20 +349,8 @@ describe('Worker', () => {
it(`should make a HTTP GET query and then call to the onData() method`, () => { it(`should make a HTTP GET query and then call to the onData() method`, () => {
jest.spyOn(controller, 'onData').mockReturnThis(); jest.spyOn(controller, 'onData').mockReturnThis();
const dated = controller.date;
const started = new Date(dated.getTime());
started.setMonth(0);
started.setDate(1);
const ended = new Date(dated.getTime());
ended.setMonth(12);
ended.setDate(0);
controller.started = started;
controller.ended = ended;
const expecteResponse = [{id: 1}]; const expecteResponse = [{id: 1}];
const expectedParams = {workerFk: 106, started: started, ended: ended}; const expectedParams = {year: year};
const serializedParams = $httpParamSerializer(expectedParams); const serializedParams = $httpParamSerializer(expectedParams);
$httpBackend.expect('GET', `Calendars/absences?${serializedParams}`).respond(200, expecteResponse); $httpBackend.expect('GET', `Calendars/absences?${serializedParams}`).respond(200, expecteResponse);
controller.refresh(); controller.refresh();