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

bugfix: remove user data re-creation with every login #171

Merged

Conversation

dmitry-sidorov
Copy link
Collaborator

@dmitry-sidorov dmitry-sidorov commented Jun 8, 2019

Есть небольшой баг, который пока не доставляет больших проблем, но потом может:
при логине существующего пользователя все его поля перезаписываются, потому что используется метод, в котором прописаны настройки по умолчанию:

  const createNewUser = user => {
    nullAndUndefinedValidation(user);
    const newUser = {
      firstName: user.first_name,
      lastName: user.last_name,
      telegramId: user.id,
      avatar: user.photo_url,
      username: user.username,
      events: [],
      created: Date.now(),
      banned: { status: false, expired: 0 },
      admin: { permission: 0, password: null }
    };
    return Users.findOneAndUpdate(
      { telegramId: newUser.telegramId },
      { $set: newUser },
      { upsert: true, new: true }
    );
  };

В исправленной версии, если пользователь существует, используем метод:

  const updateUserInfoByUserId = (_id, info) => {
    nullAndUndefinedValidation(_id, info);
    return Users.findOneAndUpdate(
      { _id },
      {
        $set: {
          firstName: info.first_name,
          lastName: info.last_name,
          avatar: info.photo_url,
          username: info.username
        }
      }
    );
  };

который подтягивает изменения, если пользователь изменил поля в аккаунте Телеграма, но при этом не трогает служебные поля а-ля created, admin, banned.

@dmitry-sidorov dmitry-sidorov added the bug Something isn't working label Jun 8, 2019
Copy link
Collaborator

@saniok017 saniok017 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good job! 🚀
tests show that the problem is fixed ㊗️

Copy link
Collaborator

@dasnein dasnein left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Problem solved 🥳

Copy link
Member

@PavloKovalov PavloKovalov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀

@dmitry-sidorov dmitry-sidorov merged commit f59b7bc into share-n-coffee:db-refactor Jun 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants