Junior — Senior
Detect bugs in the user data retrieval function
livecode
Task condition
In this task, you need to analyze the function and find errors in it. Pay attention to working with asynchronous requests, conditional structures, and forming the array of permissions.
export async function getUserData(userId) {
const fetchedUser = await fetch(`/api/users/${userId}`);
const permissionsMap = { reportStats: true, validate: true };
let result;
switch (true) {
case fetchedUser.role == 'admin':
result = {
name: fetchedUser.name,
role: fetchedUser.role,
password: fetchedUser.password,
permissions: [],
};
Object.keys(permissionsMap).forEach(perm => {
if (permissionsMap[perm]) {
if (perm === 'reportStats') {
result.permissions.push('report-stats');
} else {
result.permissions.push(perm);
}
}
});
break;
case fetchedUser.role == 'regular':
result = {
name: fetchedUser.name,
role: fetchedUser.role,
};
}
return result;
}