Skip to content

Commit

Permalink
Refactor the calculation of DateDiff with moment
Browse files Browse the repository at this point in the history
  • Loading branch information
lomamech committed Feb 28, 2024
1 parent cbdc5ba commit 8bc4253
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 13 deletions.
20 changes: 8 additions & 12 deletions server/controllers/payroll/configuration/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ function create(req, res, next) {
db.exec(sql, [data])
.then((row) => {
insertedId = row.insertId;
data.dateTo = moment(data.dateTo).format('YYYY-MM-DD');
return updateEmployeesBasicIndice(insertedId, data.dateTo);
}).then(() => {
res.status(201).json({ id : insertedId });
Expand Down Expand Up @@ -250,10 +251,8 @@ async function updateEmployeesBasicIndice(idPeriod, dateTo) {

// Processing of new employee data
newEmployees.forEach(employee => {
const diff = moment(dateTo).diff(moment(employee.date_embauche));
const duration = moment.duration(diff, 'milliseconds');
// Here we calculate in years the number of years of seniority
const yearOfSeniority = parseInt(duration.asYears(), 10);
employee.date_embauche = moment(employee.date_embauche).format('YYYY-MM-DD');
const yearOfSeniority = parseInt(moment(dateTo).diff(employee.date_embauche, 'years'), 10);

// Here we increment the base index based on the number of years
for (let i = 0; i < yearOfSeniority; i++) {
Expand All @@ -273,18 +272,15 @@ async function updateEmployeesBasicIndice(idPeriod, dateTo) {
});

oldEmployees.forEach(employee => {
employee.date_embauche = moment(employee.date_embauche).format('YYYY-MM-DD');
employee.lastDateIncrease = moment(employee.lastDateIncrease).format('YYYY-MM-DD');
// For employees who have already been configured, we will compare the number of years of seniority
// and the difference in years between the date of the last increment of the base index,
// if this difference is greater than zero, the we will have to increment
// the base index in relation to this difference

const diffSeriority = moment(dateTo).diff(moment(employee.date_embauche));
const durationSeniority = moment.duration(diffSeriority, 'milliseconds');
const yearOfSeniority = parseInt(durationSeniority.asYears(), 10);

const diffLastIncrementation = moment(employee.lastDateIncrease).diff(moment(employee.date_embauche));
const durationLastIncrementation = moment.duration(diffLastIncrementation, 'milliseconds');
const yearLastIncrementation = parseInt(durationLastIncrementation.asYears(), 10);
const yearOfSeniority = parseInt(moment(dateTo).diff(employee.date_embauche, 'years'), 10);
const yearLastIncrementation = parseInt(moment(employee.lastDateIncrease).diff(employee.date_embauche, 'years'),
10);

const diffSeniorityIncrementation = yearOfSeniority - yearLastIncrementation;

Expand Down
2 changes: 1 addition & 1 deletion test/integration/staffingIndices.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ describe('test/integration/staffingIndices The staffing indices API', () => {
// The pay period is January 2026
// The system will first calculate their year of seniority in relation to the pay period
// Seniority = (2026-01-31) - (2022-02-27) is equal to 3 years (1)
// Last increment = (2024-02-27) - (2024-02-27) is equal to 2 years (2)
// Last increment = (2024-02-27) - (2022-02-27) is equal to 2 years (2)
// Base Index Growth Rate being 5%, the base index of this employee will be increased in
// accordance with the difference in years (1)-(2) => 3 years - 2 years = 1 year
// - Year 1: 138 + (138 x 0.05) = 144.9 which the system will round to 145
Expand Down

0 comments on commit 8bc4253

Please sign in to comment.