The exercise has two parts, A and B.
A is simple, it requires to check the modules that each user makes use of and return an object with the following format. This part is solved.
{
'auth_module': {
'authn.provider_1': ['./u1.json', './u2.json']
'authn.provider_2': ['./u3.json', './u4.json', './u5.json']
},
'content_module': {
'authz.provider_1': ['./u1.json', './u3.json'],
'authz.provider_2': ['./u2.json', './u4.json'],
'authz.provider_3': ['./u5.json']
}
}
Now, in part B I need help to solve this:
"Determine a group of users (taken from part A) that together use all the available modules"
The example output is in this format:
['./u1.json', './u4.json', './u5.json']
I genuinely have no clue on how to solve this, so any kind of help will be really appreciated.
Object#entries and Array#reduce, iterate over the auth_module pairs while updating a list of target users
Array#forEach, iterate over the current auth users and update the target list with common ones between the twoObject#entries and Array#forEach, iterate over the content_module pairs
auth_module to add its usersSetconst _getUsers = modules => {
const authIdPrefix = 'authn.', contentIdPrefix = 'authz.';
const { 'auth_module': authModule, 'content_module': contentModule } = modules;
const users = Object.entries(authModule)
.reduce((list, [authProvider, authUsers]) => {
const providerId = authProvider.substring(authIdPrefix.length);
const contentUsers = contentModule[`${contentIdPrefix}${providerId}`] ?? [];
authUsers.forEach(user => {
if(contentUsers.includes(user)) {
list.push(user);
}
});
return list;
}, []);
Object.entries(contentModule)
.forEach(([contentProvider, contentUsers]) => {
const providerId = contentProvider.substring(contentIdPrefix.length);
if(!authModule[`${authIdPrefix}${providerId}`]) {
users.push(...contentUsers);
}
});
return [...new Set(users)];
}
const modules = {
'auth_module': {
'authn.provider_1': ['./u1.json', './u2.json'],
'authn.provider_2': ['./u3.json', './u4.json', './u5.json']
},
'content_module': {
'authz.provider_1': ['./u1.json', './u3.json'],
'authz.provider_2': ['./u2.json', './u4.json'],
'authz.provider_3': ['./u5.json']
}
};
console.log( _getUsers(modules) );