Good evening, it was necessary to write a function that increases the salary of the employee with the lowest salary. It should: receive data on all employees, find the employee with the lowest salary, send a request for a salary increase to this employee by 20%, if the request was successful - send a notification about the salary increase to the employee with the text: Hello, ! Congratulations, your new salary is ! If the request fails, send error data to the administrator. Should always return a resolved promise with a boolean value: true if the increase was successful, false otherwise. All functions for getting/changing data are asynchronous and return promises. I can not pass the test “in case of an error in increasing the salary, it should send a notification to the administrator, but not to the user”, how can I do it?
My code:
My code:
JavaScript:
function increaseSalary() {
return api.getEmployees()
.then(employeeData => {
const [minSalaryEmployee] = employeeData.reduce(([minEmployee, minSalary], employee) => {
const {salary} = employee;
return (salary < minSalary
? [employee, salary]
: [minEmployee, minSalary]
);
}, [null, Infinity]);
const {id, salary: oldSalary} = minSalaryEmployee;
const newSalary = oldSalary * 1.2;
return {id, salary: newSalary};
})
.then(({id, salary}) => api.setEmployeeSalary(id, salary))
.then(({name, id, salary}) => api.notifyEmployee(id, `Hello, ${name}! Congratulations, your new salary is ${salary}!`))
.catch(e => api.notifyAdmin(e));
}
const api = {
_employees: [
{ id: 1, name: 'Alex', salary: 120000 },
{ id: 2, name: 'Fred', salary: 110000 },
{ id: 3, name: 'Bob', salary: 80000 },
],
getEmployees() {
return new Promise((resolve) => {
resolve(this._employees.slice());
});
},
setEmployeeSalary(employeeId, newSalary) {
return new Promise((resolve) => {
this._employees = this._employees.map((employee) =>
employee.id !== employeeId
? employee
: {
...employee,
salary: newSalary,
}
);
resolve(this._employees.find(({ id }) => id === employeeId));
});
},
notifyEmployee(employeeId, text) {
return new Promise((resolve) => {
resolve(true);
});
},
notifyAdmin(error) {
return new Promise((resolve) => {
resolve(true);
});
},
setEmployees(newEmployees) {
return new Promise((resolve) => {
this._employees = newEmployees;
resolve();
});
},
};