Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] The “score” property on the mocked users do not have the same name #17

Open
LePhenix47 opened this issue Jan 12, 2023 · 1 comment

Comments

@LePhenix47
Copy link

When making a call to the API using the endpoint: /user/:userId

Like: http://localhost:3000/user/18

The score value for the user “Karl” of ID 12 is stored inside a property of data named todayScore:
image

But the score value for the user “Cecilia” of ID 18 is stored inside a property of data named score:
image

So when we try accessing the value of the user's objective score, the property for either one of these users is undefined

For example:

//For Karl with an ID of 12 
const karlData = data;

console.log(karlData?.todayScore);  //→ Outputs 0.12 in the console
console.log(karlData?.score);  //→ Outputs undefined in the console

[...]

//For Cecicilia with an ID of 18
const ceciliaData = data;

console.log(ceciliaData?.todayScore);  //→ Outputs undefined in the console
console.log(ceciliaData?.score);  //→ Outputs 0.3 in the console

A fix would be to simply change the name property of either one of the mocked users

@dubar-jeremy
Copy link

dubar-jeremy commented Mar 27, 2023

As in real world you might not have an access to the api (ex: external api), a possible solution would be to create a single "todayScore" key and use the Nullish Coalescing Operator to set its value based on the available data:

Ignore typescript if you don't use it

src/data/model/userModel.ts

export class UserModel {
    static getUser(data: User): User {
        const { id, userInfos, score, todayScore, keyData } = data;
        return {
            id,
            userInfos,
            todayScore: todayScore ?? score
            keyData,
        };
    }
}

The API mock

src/data/mock/user.ts

export const users: User[] = [
   {
       id: 12,
       userInfos: {
           firstName: 'Karl',
           lastName: 'Dovineau',
           age: 31,
       },
       todayScore: 0.12,
       keyData: {
           calorieCount: 1930,
           proteinCount: 155,
           carbohydrateCount: 290,
           lipidCount: 50
       }
   },
   {
       id: 18,
       userInfos: {
           firstName: 'Cecilia',
           lastName: 'Ratorez',
           age: 34,
       },
       score: 0.3,
       keyData: {
           calorieCount: 2500,
           proteinCount: 90,
           carbohydrateCount: 150,
           lipidCount: 120
       }
   }
]

Use it :

const userstest: User[] = users.map(rawUser => UserModel.getUser(rawUser));

Output :

[
    {
        "id": 12,
        "userInfos": {
            "firstName": "Karl",
            "lastName": "Dovineau",
            "age": 31
        },
        "todayScore": 0.12,
        "keyData": {
            "calorieCount": 1930,
            "proteinCount": 155,
            "carbohydrateCount": 290,
            "lipidCount": 50
        }
    },
    {
        "id": 18,
        "userInfos": {
            "firstName": "Cecilia",
            "lastName": "Ratorez",
            "age": 34
        },
        "todayScore": 0.3,
        "keyData": {
            "calorieCount": 2500,
            "proteinCount": 90,
            "carbohydrateCount": 150,
            "lipidCount": 120
        }
    }
]

In case you want to use typescript, you can just set todayScore and score keys as optional in your interface.

 todayScore?: number,
 score?: number,

No idea if they did it on purpose or if it's just a typo that has never been fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants