Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
1.7 kB
5
Indexable
Never
// DISCLAMER! That is not taken from the production code :)


/*
Implement functionality that allows retrieving user data in the required format. 
The input will be a userId provided as a string. 
The data is located at the endpoint `/api/users/{userId}`. 
Depending on the user's role, different data should be returned. 
There are currently two formats:

If the role is `admin`, return the fields `name`, `role`, `password`, and `permissions`.
If the role is `regular`, return name and role only.
The formats may change in the future.

The permissions field should be in `pascal-case` and should only include valid values: `reportStats`, `validate`.

These fields may also change in the future.
*/

export async function getUserData(userId) {
  
    const user = await fetch(`/api/users/${userId}`); 
    
    const rights = { 
        reportStats: true,
        validate: true
    }; 
    let d;
    
    switch (true) { 
        case 'admin':
            const d = {
                name: user.name,
                role: user.role,
                password: user.password,
                permissions: [],
            };
            Object.keys(rights).forEach(permission => {
                if (rights[permission]) {
                    if (permission === 'reportStats') {
                        d.permissions.push('report-stats');
                    } else {
                        d.permissions.push(permission);
                    }
                }
            });
            break;
        case 'regular':
            return {
                name: user.name,
                role: user.role,
            };
      
    };

    return d;
}
Leave a Comment