0

Please, help pass the last test. Need to send a notification to the Admin. await api.notifyAdmin(error) isn't working properly, I guess. Expected 0 number of calls - I recieve 1 :( my code:

async function increaseSalary() {
 try {
 let data = await api.getEmployees();
 let salary = 0;
 let id = '';
 let min = Math.min(...data.map((e) => e.salary)); 

data.forEach(async (e) => {
  if(e.salary === min) {
    e.salary *= 1.2;
    id = e.id;
    salary = e.salary;
    api.notifyEmployee(e.id, `Hello, ${e.name}! Congratulations, your new salary is ${e.salary}!`)
  }
});
await api.setEmployeeSalary(id, salary);
return true;
} catch (error) {
await api.notifyAdmin(error);
return false;
}
}

// All functions for receiving/changing data are asynchronous and return promises.

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();
    });
  },
};

I need to pass this test:
✕ in case of an increase salary error, you should send a notification to the administrator (not user)

successfully passed: ✓ the function must return a promise (5ms) ✓ must correctly increase the employee's salary with the lowest salary (9ms) ✓ must send a notification to the employee

nini
  • 1
  • 2
  • You're not properly waiting for the `api.notifyEmployee` call – Bergi Jul 26 '22 at 17:26
  • Also notice that if two employees have the same low salary that is the minimum, you're sending two notifications but increasing only one salary. Not sure if the tests would catch that, but it's still a bug. – Bergi Jul 26 '22 at 17:28
  • @Bergi thanks! still couldn't fix it. I didn't get: how is ```api.notifyEmployee``` related to sending a notification to the administrator (my exact problem). Could you pls explain? – nini Jul 26 '22 at 20:34
  • When `api.notifyEmployee` returns a rejected promise, the error is not caught in the code you've shown, and `api.notifyAdmin` won't be called. – Bergi Jul 27 '22 at 09:42

0 Answers0