{"size": 762, "ext": "js", "max_stars_repo_path": "src/services/RegistrationService.js", "max_stars_repo_name": "elefher/fin-api", "max_stars_repo_stars_event_min_datetime": "2020-10-17T08:10:41.000Z", "max_stars_repo_stars_event_max_datetime": "2020-10-17T08:10:41.000Z", "max_issues_repo_path": "src/services/RegistrationService.js", "max_issues_repo_name": "elefher/fin-api", "max_issues_repo_issues_event_min_datetime": "2020-09-12T19:36:09.000Z", "max_issues_repo_issues_event_max_datetime": "2021-08-02T07:17:47.000Z", "max_forks_repo_path": "src/services/RegistrationService.js", "max_forks_repo_name": "elefher/fin-api", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": 125, "max_forks_count": null, "avg_line_length": 28.2222222222, "max_line_length": 79, "alphanum_fraction": 0.6824146982}
const User = require('@db/models/UserModel')

const registerOrUpdateUser = async (accessToken, profile) => {
  const userData = {
    familyName: profile.name.familyName
      ? profile.name.familyName
      : profile.family_name,
    givenName: profile.name.givenName
      ? profile.name.givenName
      : profile.given_name,
    providerId: profile.id,
    email:
      profile.emails !== undefined ? profile.emails[0].value : profile.email,
    picture:
      profile.photos !== undefined ? profile.photos[0].value : profile.picture,
    provider: profile.provider,
    token: accessToken,
  }

  return User.findOneAndUpdate({ providerId: userData.providerId }, userData, {
    new: true,
    upsert: true,
  })
}

module.exports = { registerOrUpdateUser }
