Last active
December 15, 2021 20:10
-
-
Save EvanBurbidge/e5dfb252b80d5005f6ce8992b79101e0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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