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

Add _updateUser() function to update a user object in the database #10

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

amineHorseman
Copy link

Why this change ?

When _saveQuestion() is invoked, it only updates the questions object. However, the users object should also be updated to add the new question's id to the user.questions array.
This Pull Request will include a new _udapteUser() function, that can be used to update a particular user object.
The goal of creating a dedicated function instead of modifying the _saveQuestion() code directly is to insure backward compatibility with projects already using this file without requiring a change, and also, to make the function available for other user updates when necessary such as the name, password, avatarURL...etc.

Changes Made

  • Added function _updateUser(user) that takes a complete user object, validates presence of required fields, returns a Promise resolving to the updated user, and maintain 500ms delay to keep consistancy with other functions.
  • Throws rejection if any required field is missing:
    • id
    • password
    • name
    • avatarURL
    • answers
    • questions

Example Usage

import { _updateUser } from "_DATA";
const user = {
            id: 'mtsamis',
            password:'xyz',
            name: 'Mike',
            avatarURL: null,
            answers: { "xj352vofupe1dqz9emx13r": 'optionOne' },
            questions: ['6ni6ok3ym7mf1p33lnez'] 
};
const updatedUser = await _updateUser(user);
console.log(updatedUser);

Related Documentation

Updated README.md with new _updateUser function documentation in the same format as the existing functions including required parameters.

Testing:

Tested successfully using the following jest functions (see code bellow).

These tests cover the verification of:

  • Successful updates with valid user objects.
  • Rejection when required fields are missing.
import { _updateUser } from '../utils/_DATA';

describe("Test for _updateUser() functions", () => {
    test("_updateUser() returns updated user when correct data is provided", async () => {
        const user = {
            id: 'mtsamis',
            password:'xyz',
            name: 'Mike',
            avatarURL: null,
            answers: { "xj352vofupe1dqz9emx13r": 'optionOne', },
            questions: ['6ni6ok3ym7mf1p33lnez'], }
        const updatedUser = await _updateUser(user);

        expect(updatedUser).toBeDefined();
        expect(updatedUser.id).toEqual(user.id);
        expect(updatedUser.password).toEqual(user.password);
        expect(updatedUser.name).toEqual(user.name);
        expect(updatedUser.avatarURL).toEqual(user.avatarURL);
        expect(updatedUser.answers.lenght).toEqual(user.answers.lenght);
        expect(updatedUser.questions.lenght).toEqual(user.questions.lenght);
    });

    test("_updateUser() returns an error when incorrect data is provided", async () => {
        const user = {
            id: 'mtsamis',
          }
        const keys = ["id", "password", "name", "avatarURL", "answers", "questions"];
        await expect(_updateUser(user)).rejects.toMatch(`Please provide all User fields (${keys})`);
    });
});

The console output for these tests is shown bellow:

$ npm test update.test.js
 PASS  src/tests/update.test.js
  Test for _updateUser() functions
    √ _updateUser() returns updated user when correct data is provided (515 ms)
    √ _updateUser() returns an error when incorrect data is provided (1 ms)

Test Suites: 1 passed, 1 total
Tests:       2 passed, 2 total

@amineHorseman amineHorseman requested a review from a team as a code owner January 14, 2025 09:36
@amineHorseman amineHorseman requested review from SudKul and removed request for a team January 14, 2025 09:36
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

Successfully merging this pull request may close these issues.

1 participant