Skip to content

Instantly share code, notes, and snippets.

@EvanBurbidge
Last active December 15, 2021 20:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EvanBurbidge/e5dfb252b80d5005f6ce8992b79101e0 to your computer and use it in GitHub Desktop.
Save EvanBurbidge/e5dfb252b80d5005f6ce8992b79101e0 to your computer and use it in GitHub Desktop.
const cache = {};
function changePhonesStatus(phones = {}, unavailable = []) {
const key = JSON.stringify(unavailable);
// generates a string array e.g. "[1,2,3]"
if (cache[key]) {
return cache[key];
}
// we need to look at each department and update the phones for that department.
const updatedPhones = Object.keys(phones.departments).reduce((acc, key) => {
const departmentPhones = phones.departments[key];
return [
...acc,
...departmentPhones.map((phone) => ({
...phone,
status: unavailable.includes(phone.id) ? "IN_CALL" : "AVAILABLE"
}))
];
}, []);
// once we've updated it we want to ensure we set that in our cache for later use
cache[key] = updatedPhones;
return cache[key];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment