Files
freeCodeCamp/api-server/src/common/models/User-Credential.js
Oliver Eyton-Williams e118dda13a fix: order imports and remove circular dependencies (#41824)
* fix: remove circular dependency

redux depended on templates/Challenges/redux and vice versa.  This
meant that import order mattered and confusing bugs could arise.

(cherry picked from commit 7d67a4e70922bbb3051f2f9982dcc69e240d43dc)

* feat: require imports to be in alphabetical order

Import order generally does not matter, but there are edge cases
(circular  imports and css imports, for example) where changing order
changes behaviour

(cherry picked from commit b8d1393a91ec6e068caf8e8498a5c95df68c2b2c)

* chore: order imports

* fix: lift up challenge description + title comps

This brings the classic Show closer to the others as they
now all create the description and title components

* fix: remove donation-saga/index circular import

(cherry picked from commit 51a44ca668a700786d2744feffeae4fdba5fd207)

* refactor: extract action-types from settings

(cherry picked from commit 25e26124d691c84a0d0827d41dafb761c686fadd)

* fix: lint errors

* feat: prevent useless renames
2021-08-02 08:39:40 -05:00

95 lines
2.6 KiB
JavaScript

import debug from 'debug';
import { Observable } from 'rx';
import {
createUserUpdatesFromProfile,
getSocialProvider
} from '../../server/utils/auth';
import { observeMethod, observeQuery } from '../../server/utils/rx';
const log = debug('fcc:models:UserCredential');
module.exports = function (UserCredential) {
UserCredential.link = function (
userId,
_provider,
authScheme,
profile,
credentials,
options = {},
cb
) {
if (typeof options === 'function' && !cb) {
cb = options;
options = {};
}
const User = UserCredential.app.models.User;
const findCred = observeMethod(UserCredential, 'findOne');
const createCred = observeMethod(UserCredential, 'create');
const provider = getSocialProvider(_provider);
const query = {
where: {
provider: provider,
externalId: profile.id
}
};
// find createCred if they exist
// if not create it
// if yes, update credentials
// also if github
// update profile
// update username
// update picture
log('link query', query);
return findCred(query)
.flatMap(_credentials => {
const modified = new Date();
const updateUser = new Promise((resolve, reject) => {
User.find({ id: userId }, (err, user) => {
if (err) {
return reject(err);
}
return user.updateAttributes(
createUserUpdatesFromProfile(provider, profile),
updateErr => {
if (updateErr) {
return reject(updateErr);
}
return resolve();
}
);
});
});
let updateCredentials;
if (!_credentials) {
updateCredentials = createCred({
provider,
externalId: profile.id,
authScheme,
// we no longer want to keep the profile
// this is information we do not need or use
profile: null,
credentials,
userId,
created: modified,
modified
});
} else {
_credentials.credentials = credentials;
updateCredentials = observeQuery(_credentials, 'updateAttributes', {
profile: null,
credentials,
modified
});
}
return Observable.combineLatest(
Observable.fromPromise(updateUser),
updateCredentials,
(_, credentials) => credentials
);
})
.subscribe(credentials => cb(null, credentials), cb);
};
};