diff --git a/front/core/components/autocomplete/index.js b/front/core/components/autocomplete/index.js index 56b30667e..52491f7e0 100755 --- a/front/core/components/autocomplete/index.js +++ b/front/core/components/autocomplete/index.js @@ -11,7 +11,7 @@ import './style.scss'; * @property {String} valueField The data field name that should be used as value * @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 {Boolean} multiple Wether to allow multiple selection + * @property {Boolean} multiple Whether to allow multiple selection * @property {Object} selection Current object selected * * @event change Thrown when value is changed diff --git a/front/core/components/popup/index.js b/front/core/components/popup/index.js index 2a2433770..3743b9f41 100644 --- a/front/core/components/popup/index.js +++ b/front/core/components/popup/index.js @@ -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() { return this._shown; diff --git a/modules/worker/back/methods/calendar/absences.js b/modules/worker/back/methods/calendar/absences.js index 83761242f..96293c931 100644 --- a/modules/worker/back/methods/calendar/absences.js +++ b/modules/worker/back/methods/calendar/absences.js @@ -2,7 +2,7 @@ const UserError = require('vn-loopback/util/user-error'); module.exports = Self => { Self.remoteMethodCtx('absences', { - description: 'Returns an array of absences from an specified worker', + description: 'Returns an array of absences from an specified contract', accepts: [{ arg: 'businessFk', type: 'number', @@ -14,9 +14,6 @@ module.exports = Self => { required: true, }], returns: [{ - arg: 'calendar' - }, - { arg: 'absences', 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 calendar = {totalHolidays: 0, holidaysEnjoyed: 0}; - const holidays = []; - - // Get active contracts on current year - // const year = yearStarted.getFullYear(); const started = new Date(); started.setFullYear(year); @@ -48,6 +40,11 @@ module.exports = Self => { ended.setMonth(12); ended.setDate(0); + let myOptions = {}; + + if (typeof options == 'object') + Object.assign(myOptions, options); + const contract = await models.WorkerLabour.findOne({ include: [{ relation: 'holidays', @@ -55,6 +52,17 @@ module.exports = Self => { where: {year} } }, + { + relation: 'absences', + scope: { + include: { + relation: 'absenceType', + }, + where: { + dated: {between: [started, ended]} + } + } + }, { relation: 'workCenter', scope: { @@ -75,58 +83,25 @@ module.exports = Self => { } }], where: {businessFk} - }); + }, myOptions); if (!contract) return; - const isSubordinate = await models.Worker.isSubordinate(ctx, contract.workerFk); + const isSubordinate = await models.Worker.isSubordinate(ctx, contract.workerFk, myOptions); if (!isSubordinate) throw new UserError(`You don't have enough privileges`); - // Get absences of year - let absences = await Self.find({ - 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; - + const absences = []; + for (let absence of contract.absences()) { absence.dated = new Date(absence.dated); absence.dated.setHours(0, 0, 0, 0); - }); - // Get number of worked days - 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(); + absences.push(absence); + } // Workcenter holidays - let holidayList = contract.workCenter().holidays(); + const holidays = []; + const holidayList = contract.workCenter().holidays(); for (let day of holidayList) { day.dated = new Date(day.dated); day.dated.setHours(0, 0, 0, 0); @@ -134,24 +109,6 @@ module.exports = Self => { holidays.push(day); } - const maxHolidays = contract.holidays() && contract.holidays().days; - 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]; + return [absences, holidays]; }; - - function isLeapYear(year) { - return year % 400 === 0 || (year % 100 !== 0 && year % 4 === 0); - } }; diff --git a/modules/worker/back/methods/worker/createAbsence.js b/modules/worker/back/methods/worker/createAbsence.js index e327701b3..da64c413f 100644 --- a/modules/worker/back/methods/worker/createAbsence.js +++ b/modules/worker/back/methods/worker/createAbsence.js @@ -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 $t = ctx.req.__; // $translate + const args = ctx.args; const userId = ctx.req.accessToken.userId; - const isSubordinate = await models.Worker.isSubordinate(ctx, id); - const isTeamBoss = await models.Account.hasRole(userId, 'teamBoss'); - if (!isSubordinate || (isSubordinate && userId == id && !isTeamBoss)) - throw new UserError(`You don't have enough privileges`); + let tx; + let myOptions = {}; - const labour = await models.WorkerLabour.findById(businessFk, { - include: {relation: 'department'} - }); + if (typeof options == 'object') + Object.assign(myOptions, options); - if (dated < labour.started || (labour.ended != null && dated > labour.ended)) - throw new UserError(`The contract was not active during the selected date`); - - const absence = await models.Calendar.create({ - businessFk: labour.businessFk, - dayOffTypeFk: absenceTypeId, - dated: dated - }); - - const department = labour.department(); - if (department && department.notificationEmail) { - const absenceType = await models.AbsenceType.findById(absenceTypeId); - const account = await models.Account.findById(userId); - const subordinated = await models.Account.findById(id); - const origin = ctx.req.headers.origin; - const body = $t('Created absence', { - author: account.nickname, - employee: subordinated.nickname, - absenceType: absenceType.name, - dated: formatDate(dated), - workerUrl: `${origin}/#!/worker/${id}/calendar` - }); - await models.Mail.create({ - subject: $t('Absence change notification on the labour calendar'), - body: body, - sender: department.notificationEmail - }); + if (!myOptions.transaction) { + tx = await Self.beginTransaction({}); + myOptions.transaction = tx; } - return absence; + 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)) + throw new UserError(`You don't have enough privileges`); + + const labour = await models.WorkerLabour.findById(args.businessFk, { + include: {relation: 'department'} + }, myOptions); + + if (args.dated < labour.started || (labour.ended != null && args.dated > labour.ended)) + throw new UserError(`The contract was not active during the selected date`); + + const absence = await models.Calendar.create({ + businessFk: labour.businessFk, + dayOffTypeFk: args.absenceTypeId, + dated: args.dated + }, myOptions); + + const department = labour.department(); + if (department && department.notificationEmail) { + const absenceType = await models.AbsenceType.findById(args.absenceTypeId, null, myOptions); + const account = await models.Account.findById(userId, null, myOptions); + const subordinated = await models.Account.findById(id, null, myOptions); + const origin = ctx.req.headers.origin; + const body = $t('Created absence', { + author: account.nickname, + employee: subordinated.nickname, + absenceType: absenceType.name, + dated: formatDate(args.dated), + workerUrl: `${origin}/#!/worker/${id}/calendar` + }); + await models.Mail.create({ + subject: $t('Absence change notification on the labour calendar'), + body: body, + sender: department.notificationEmail + }, myOptions); + } + + if (tx) await tx.commit(); + + return absence; + } catch (e) { + if (tx) await tx.rollback(); + throw e; + } }; function formatDate(date) { diff --git a/modules/worker/back/methods/worker/deleteAbsence.js b/modules/worker/back/methods/worker/deleteAbsence.js index c7e24fa58..e322288d2 100644 --- a/modules/worker/back/methods/worker/deleteAbsence.js +++ b/modules/worker/back/methods/worker/deleteAbsence.js @@ -5,13 +5,13 @@ module.exports = Self => { description: 'Deletes a worker absence', accepts: [{ arg: 'id', - type: 'Number', + type: 'number', description: 'The worker id', http: {source: 'path'} }, { arg: 'absenceId', - type: 'Number', + type: 'number', required: true }], returns: 'Object', @@ -21,47 +21,67 @@ module.exports = Self => { } }); - Self.deleteAbsence = async(ctx, id, absenceId) => { + Self.deleteAbsence = async(ctx, id, options) => { const models = Self.app.models; const $t = ctx.req.__; // $translate + const args = ctx.args; const userId = ctx.req.accessToken.userId; - const isSubordinate = await models.Worker.isSubordinate(ctx, id); - const isTeamBoss = await models.Account.hasRole(userId, 'teamBoss'); - if (!isSubordinate || (isSubordinate && userId == id && !isTeamBoss)) - throw new UserError(`You don't have enough privileges`); + let tx; + let myOptions = {}; - const absence = await models.Calendar.findById(absenceId, { - include: { - relation: 'labour', - scope: { - include: {relation: 'department'} - } - } - }); - const result = await absence.destroy(); - const labour = absence.labour(); - const department = labour && labour.department(); - if (department && department.notificationEmail) { - const absenceType = await models.AbsenceType.findById(absence.dayOffTypeFk); - const account = await models.Account.findById(userId); - const subordinated = await models.Account.findById(labour.workerFk); - const origin = ctx.req.headers.origin; - const body = $t('Deleted absence', { - author: account.nickname, - employee: subordinated.nickname, - absenceType: absenceType.name, - dated: formatDate(absence.dated), - workerUrl: `${origin}/#!/worker/${id}/calendar` - }); - await models.Mail.create({ - subject: $t('Absence change notification on the labour calendar'), - body: body, - sender: department.notificationEmail - }); + if (typeof options == 'object') + Object.assign(myOptions, options); + + if (!myOptions.transaction) { + tx = await Self.beginTransaction({}); + myOptions.transaction = tx; } - return result; + 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)) + throw new UserError(`You don't have enough privileges`); + + const absence = await models.Calendar.findById(args.absenceId, { + include: { + relation: 'labour', + scope: { + include: {relation: 'department'} + } + } + }, myOptions); + const result = await absence.destroy(myOptions); + const labour = absence.labour(); + const department = labour && labour.department(); + if (department && department.notificationEmail) { + const absenceType = await models.AbsenceType.findById(absence.dayOffTypeFk, null, myOptions); + const account = await models.Account.findById(userId, null, myOptions); + const subordinated = await models.Account.findById(labour.workerFk, null, myOptions); + const origin = ctx.req.headers.origin; + const body = $t('Deleted absence', { + author: account.nickname, + employee: subordinated.nickname, + absenceType: absenceType.name, + dated: formatDate(absence.dated), + workerUrl: `${origin}/#!/worker/${id}/calendar` + }); + await models.Mail.create({ + subject: $t('Absence change notification on the labour calendar'), + body: body, + sender: department.notificationEmail + }, myOptions); + } + + if (tx) await tx.commit(); + + return result; + } catch (e) { + if (tx) await tx.rollback(); + throw e; + } }; function formatDate(date) { diff --git a/modules/worker/back/methods/worker/holidays.js b/modules/worker/back/methods/worker/holidays.js index c6f299a73..704f4d389 100644 --- a/modules/worker/back/methods/worker/holidays.js +++ b/modules/worker/back/methods/worker/holidays.js @@ -2,7 +2,7 @@ const UserError = require('vn-loopback/util/user-error'); module.exports = Self => { Self.remoteMethodCtx('holidays', { - description: 'Returns data - Change me', + description: 'Returns the holidays available whitin a contract or a year', accepts: [{ arg: 'id', 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 calendar = {totalHolidays: 0, holidaysEnjoyed: 0}; - const holidays = []; */ + const args = ctx.args; - 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) throw new UserError(`You don't have enough privileges`); - // Get active contracts on current year - // const year = yearStarted.getFullYear(); - const started = new Date(); - started.setFullYear(year); + started.setFullYear(args.year); started.setMonth(0); started.setDate(1); started.setHours(0, 0, 0, 0); const ended = new Date(); - ended.setFullYear(year); + ended.setFullYear(args.year); ended.setMonth(12); ended.setDate(0); ended.setHours(23, 59, 59, 59); @@ -58,7 +59,7 @@ module.exports = Self => { include: [{ relation: 'holidays', scope: { - where: {year} + where: {year: args.year} } }, { @@ -94,49 +95,26 @@ module.exports = Self => { where: { and: [ {workerFk: id}, - {or: [{ - ended: {gte: [started]} - }, {ended: null}]} + { + or: [ + {started: {between: [started, ended]}}, + {ended: {between: [started, ended]}}, + {and: [{started: {lt: started}}, {ended: {gt: ended}}]}, + {and: [{started: {lt: started}}, {ended: null}]} + ] + } ], } }; - if (businessFk) - filter.where = {businessFk}; - const contracts = await models.WorkerLabour.find(filter); + if (args.businessFk) + filter.where.and.push({businessFk: args.businessFk}); - if (!contracts.length) return; - - // 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]} - } - }); + const contracts = await models.WorkerLabour.find(filter, myOptions); let totalHolidays = 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) { const contractStarted = contract.started; contractStarted.setHours(0, 0, 0, 0); @@ -144,29 +122,23 @@ module.exports = Self => { if (contractEnded) contractEnded.setHours(23, 59, 59, 59); - // esta mal, la fecha de inicio puede ser un año anterior... - const startedTime = contractStarted < started ? started.getTime() : contractStarted.getTime(); - const endedTime = !contractEnded || contractEnded && contractEnded > ended ? ended.getTime() : contractEnded.getTime(); - // const endedTime = contractEnded && contractEnded.getTime() || ended.getTime(); + let startedTime; + if (contractStarted < started) + startedTime = started.getTime(); + else contractStarted.getTime(); + + let endedTime; + if (!contractEnded || (contractEnded && contractEnded > ended)) + endedTime = ended.getTime(); + else contractEnded.getTime(); + 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 -= entitlementRate; - /* if (workedDays > daysInYear()) - 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); - } - - */ + // Calculates absences let entitlementRate = 0; for (let absence of contract.absences()) { const absenceType = absence.absenceType(); @@ -177,22 +149,16 @@ module.exports = Self => { if (isHalfHoliday) holidaysEnjoyed += 0.5; entitlementRate += absenceType.holidayEntitlementRate; - - // absence.dated = new Date(absence.dated); // not needed - // absence.dated.setHours(0, 0, 0, 0); } workedDays -= entitlementRate; - // Set max holidays - // const maxHolidays = contract.holidays() && contract.holidays().days; + // Max holidays for the selected year const maxHolidays = contract.holidays() && contract.holidays().days; if (workedDays < daysInYear()) totalHolidays += Math.round(2 * maxHolidays * (workedDays) / daysInYear()) / 2; else totalHolidays = maxHolidays; - - // totalWorkedDays += workedDays; } function daysInYear() { @@ -207,35 +173,4 @@ module.exports = Self => { function isLeapYear(year) { 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} - }); */ }; diff --git a/modules/worker/back/methods/worker/isSubordinate.js b/modules/worker/back/methods/worker/isSubordinate.js index 18c7df17e..13cc365c6 100644 --- a/modules/worker/back/methods/worker/isSubordinate.js +++ b/modules/worker/back/methods/worker/isSubordinate.js @@ -23,16 +23,21 @@ module.exports = Self => { } }); - Self.isSubordinate = async(ctx, id) => { + Self.isSubordinate = async(ctx, id, options) => { const models = Self.app.models; 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 => { return subordinate.workerFk == id; }); - const isHr = await models.Account.hasRole(myUserId, 'hr'); + const isHr = await models.Account.hasRole(myUserId, 'hr', myOptions); if (isHr || isSubordinate) return true; diff --git a/modules/worker/back/methods/worker/mySubordinates.js b/modules/worker/back/methods/worker/mySubordinates.js index 956374964..07a22291d 100644 --- a/modules/worker/back/methods/worker/mySubordinates.js +++ b/modules/worker/back/methods/worker/mySubordinates.js @@ -20,17 +20,22 @@ module.exports = Self => { } }); - Self.mySubordinates = async ctx => { + Self.mySubordinates = async(ctx, options) => { const conn = Self.dataSource.connector; const userId = ctx.req.accessToken.userId; const stmts = []; + let myOptions = {}; + + if (typeof options == 'object') + Object.assign(myOptions, options); + stmts.push(new ParameterizedSQL('CALL vn.subordinateGetList(?)', [userId])); const queryIndex = stmts.push('SELECT * FROM tmp.subordinate') - 1; stmts.push('DROP TEMPORARY TABLE tmp.subordinate'); const sql = ParameterizedSQL.join(stmts, ';'); - const result = await conn.executeStmt(sql); + const result = await conn.executeStmt(sql, myOptions); return result[queryIndex]; }; diff --git a/modules/worker/back/models/worker-labour.json b/modules/worker/back/models/worker-labour.json index 62764961a..8ad7bf41e 100644 --- a/modules/worker/back/models/worker-labour.json +++ b/modules/worker/back/models/worker-labour.json @@ -12,10 +12,10 @@ "type": "Number" }, "started": { - "type": "Date" + "type": "date" }, "ended": { - "type": "Date" + "type": "date" } }, "relations": { diff --git a/modules/worker/front/calendar/index.js b/modules/worker/front/calendar/index.js index ab172f597..d60fa0647 100644 --- a/modules/worker/front/calendar/index.js +++ b/modules/worker/front/calendar/index.js @@ -26,6 +26,20 @@ class Controller extends Section { .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() { return this._date; } @@ -34,16 +48,6 @@ class Controller extends Section { this._date = value; 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); for (let i = 0; i < this.months.length; i++) { @@ -62,26 +66,11 @@ class Controller extends Section { this._worker = value; if (value) { - // this.refresh().then(() => this.repaint()); this.getIsSubordinate(); 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() { const now = new Date(); now.setFullYear(now.getFullYear() + 1); @@ -114,9 +103,9 @@ class Controller extends Section { } getYearHolidays() { - /* this.getHolidays({ + this.getHolidays({ year: this.year - }, data => this.yearHolidays = data); */ + }, data => this.yearHolidays = data); } getHolidays(params, cb) { @@ -198,9 +187,6 @@ class Controller extends Section { if (!this.absenceType) 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 stamp = day.getTime(); const event = this.events[stamp]; @@ -234,7 +220,10 @@ class Controller extends Section { }; 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; 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()]; this.repaintCanceller(() => - this.refresh().then(calendar.repaint()) + this.refresh() + .then(calendar.repaint()) + .then(() => this.getContractHolidays()) + .then(() => this.getYearHolidays()) ); }); } diff --git a/modules/worker/front/calendar/index.spec.js b/modules/worker/front/calendar/index.spec.js index 14a5e2e0d..ec2f5dd8a 100644 --- a/modules/worker/front/calendar/index.spec.js +++ b/modules/worker/front/calendar/index.spec.js @@ -41,35 +41,31 @@ describe('Worker', () => { }); }); - describe('started property', () => { - it(`should return first day and month of current year`, () => { - let started = new Date(year, 0, 1); + describe('businessId() setter', () => { + it(`should set the contract id and then call to the refresh method`, () => { + jest.spyOn(controller, 'refresh').mockReturnValue(Promise.resolve()); - expect(controller.started).toEqual(started); - }); - }); + controller.businessId = 106; - describe('ended property', () => { - it(`should return last day and month of current year`, () => { - let ended = new Date(year, 11, 31); - - expect(controller.ended).toEqual(ended); + expect(controller.refresh).toHaveBeenCalledWith(); }); }); describe('months property', () => { it(`should return an array of twelve months length`, () => { + const started = new Date(year, 0, 1); const ended = new Date(year, 11, 1); 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); }); }); describe('worker() setter', () => { 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 tomorrow = new Date(today.getTime()); @@ -78,49 +74,60 @@ describe('Worker', () => { let yesterday = new Date(today.getTime()); yesterday.setDate(yesterday.getDate() - 1); - $httpBackend.whenRoute('GET', 'Calendars/absences') - .respond({ - holidays: [ - {dated: today, detail: {name: 'New year'}}, - {dated: tomorrow, detail: {name: 'Easter'}} - ], - absences: [ - {dated: today, absenceType: {name: 'Holiday', rgb: '#aaa'}}, - {dated: yesterday, absenceType: {name: 'Leave', rgb: '#bbb'}} - ] - }); - controller.worker = {id: 107}; + + expect(controller.getIsSubordinate).toHaveBeenCalledWith(); + expect(controller.getActiveContract).toHaveBeenCalledWith(); + }); + }); + + describe('getIsSubordinate()', () => { + it(`should return whether the worker is a subordinate`, () => { + $httpBackend.expect('GET', `Workers/106/isSubordinate`).respond(true); + controller.getIsSubordinate(); $httpBackend.flush(); - let events = controller.events; + expect(controller.isSubordinate).toBe(true); + }); + }); - expect(events[today.getTime()].name).toEqual('New year, Holiday'); - expect(events[tomorrow.getTime()].name).toEqual('Easter'); - expect(events[yesterday.getTime()].name).toEqual('Leave'); - expect(events[yesterday.getTime()].color).toEqual('#bbb'); - expect(controller.getIsSubordinate).toHaveBeenCalledWith(); + describe('getActiveContract()', () => { + it(`should return the current contract and then set the businessId property`, () => { + jest.spyOn(controller, 'refresh').mockReturnValue(Promise.resolve()); + + $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()', () => { 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') - .respond({ - absences: [ - {dated: today, absenceType: {name: 'Holiday', rgb: '#000'}} - ] - }); - - controller.worker = {id: 1}; - $httpBackend.flush(); - - let dayElement = angular.element('
')[0]; - let dayNumber = dayElement.firstElementChild; + const dayElement = angular.element('
')[0]; + const dayNumber = dayElement.firstElementChild; controller.formatDay(today, dayElement); @@ -160,7 +167,7 @@ describe('Worker', () => { 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(); const selectedDay = new Date(); @@ -180,7 +187,7 @@ describe('Worker', () => { controller.onSelection($event, $days); expect(controller.vnApp.showMessage).toHaveBeenCalledWith('You can just add absences within the current year'); - }); + }); */ it(`should call to the create() method`, () => { 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`, () => { 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 expectedParams = {workerFk: 106, started: started, ended: ended}; + const expectedParams = {year: year}; const serializedParams = $httpParamSerializer(expectedParams); $httpBackend.expect('GET', `Calendars/absences?${serializedParams}`).respond(200, expecteResponse); controller.refresh();