fix(update$): Remove User.update$, refactor to use native loopback interfaces
This commit is contained in:
committed by
mrugesh mohapatra
parent
97f7d53bee
commit
0f73fdbd9c
@ -45,10 +45,22 @@ module.exports = function(UserCredential) {
|
|||||||
return findCred(query)
|
return findCred(query)
|
||||||
.flatMap(_credentials => {
|
.flatMap(_credentials => {
|
||||||
const modified = new Date();
|
const modified = new Date();
|
||||||
const updateUser = User.update$(
|
const updateUser = new Promise((resolve, reject) => {
|
||||||
{ id: userId },
|
User.find({ id: userId }, (err, user) => {
|
||||||
createUserUpdatesFromProfile(provider, profile)
|
if (err) {
|
||||||
);
|
return reject(err);
|
||||||
|
}
|
||||||
|
return user.updateAttributes(
|
||||||
|
createUserUpdatesFromProfile(provider, profile),
|
||||||
|
updateErr => {
|
||||||
|
if (updateErr) {
|
||||||
|
return reject(updateErr);
|
||||||
|
}
|
||||||
|
return resolve();
|
||||||
|
}
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
let updateCredentials;
|
let updateCredentials;
|
||||||
if (!_credentials) {
|
if (!_credentials) {
|
||||||
updateCredentials = createCred({
|
updateCredentials = createCred({
|
||||||
@ -65,25 +77,18 @@ module.exports = function(UserCredential) {
|
|||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
_credentials.credentials = credentials;
|
_credentials.credentials = credentials;
|
||||||
updateCredentials = observeQuery(
|
updateCredentials = observeQuery(_credentials, 'updateAttributes', {
|
||||||
_credentials,
|
profile: null,
|
||||||
'updateAttributes',
|
credentials,
|
||||||
{
|
modified
|
||||||
profile: null,
|
});
|
||||||
credentials,
|
|
||||||
modified
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
return Observable.combineLatest(
|
return Observable.combineLatest(
|
||||||
updateUser,
|
Observable.fromPromise(updateUser),
|
||||||
updateCredentials,
|
updateCredentials,
|
||||||
(_, credentials) => credentials
|
(_, credentials) => credentials
|
||||||
);
|
);
|
||||||
})
|
})
|
||||||
.subscribe(
|
.subscribe(credentials => cb(null, credentials), cb);
|
||||||
credentials => cb(null, credentials),
|
|
||||||
cb
|
|
||||||
);
|
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
@ -126,16 +126,26 @@ export default function(UserIdent) {
|
|||||||
created: new Date(),
|
created: new Date(),
|
||||||
ttl: user.constructor.settings.ttl
|
ttl: user.constructor.settings.ttl
|
||||||
});
|
});
|
||||||
const updateUser = user.update$({
|
const updateUser = new Promise((resolve, reject) =>
|
||||||
email: email,
|
user.updateAttributes(
|
||||||
emailVerified: true,
|
{
|
||||||
emailAuthLinkTTL: null,
|
email: email,
|
||||||
emailVerifyTTL: null
|
emailVerified: true,
|
||||||
});
|
emailAuthLinkTTL: null,
|
||||||
|
emailVerifyTTL: null
|
||||||
|
},
|
||||||
|
err => {
|
||||||
|
if (err) {
|
||||||
|
return reject(err);
|
||||||
|
}
|
||||||
|
return resolve();
|
||||||
|
}
|
||||||
|
)
|
||||||
|
);
|
||||||
return Observable.combineLatest(
|
return Observable.combineLatest(
|
||||||
Observable.of(user),
|
Observable.of(user),
|
||||||
createToken,
|
createToken,
|
||||||
updateUser,
|
Observable.fromPromise(updateUser),
|
||||||
(user, token) => ({ user, token })
|
(user, token) => ({ user, token })
|
||||||
);
|
);
|
||||||
})
|
})
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -272,9 +272,17 @@ function createVerifyCert(certTypeIds, app) {
|
|||||||
// set here so sendCertifiedEmail works properly
|
// set here so sendCertifiedEmail works properly
|
||||||
// not used otherwise
|
// not used otherwise
|
||||||
user[certType] = true;
|
user[certType] = true;
|
||||||
|
const updatePromise = new Promise((resolve, reject) =>
|
||||||
|
user.updateAttributes(updateData, err => {
|
||||||
|
if (err) {
|
||||||
|
return reject(err);
|
||||||
|
}
|
||||||
|
return resolve();
|
||||||
|
})
|
||||||
|
);
|
||||||
return Observable.combineLatest(
|
return Observable.combineLatest(
|
||||||
// update user data
|
// update user data
|
||||||
user.update$(updateData),
|
Observable.fromPromise(updatePromise),
|
||||||
// If user has committed to nonprofit,
|
// If user has committed to nonprofit,
|
||||||
// this will complete their pledge
|
// this will complete their pledge
|
||||||
completeCommitment$(user),
|
completeCommitment$(user),
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
* a db migration to fix all completedChallenges
|
* a db migration to fix all completedChallenges
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
import { Observable } from 'rx';
|
||||||
import _ from 'lodash';
|
import _ from 'lodash';
|
||||||
import debug from 'debug';
|
import debug from 'debug';
|
||||||
import accepts from 'accepts';
|
import accepts from 'accepts';
|
||||||
@ -87,9 +87,6 @@ function buildUserUpdate(user, challengeId, _completedChallenge, timezone) {
|
|||||||
timezone: userTimezone
|
timezone: userTimezone
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
log('user update data', updateData);
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
alreadyCompleted,
|
alreadyCompleted,
|
||||||
updateData,
|
updateData,
|
||||||
@ -215,21 +212,24 @@ export default async function bootChallenge(app, done) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const points = alreadyCompleted ? user.points : user.points + 1;
|
const points = alreadyCompleted ? user.points : user.points + 1;
|
||||||
|
const updatePromise = new Promise((resolve, reject) =>
|
||||||
return user
|
user.updateAttributes(updateData, err => {
|
||||||
.update$(updateData)
|
if (err) {
|
||||||
.doOnNext(() => user.manualReload())
|
return reject(err);
|
||||||
.doOnNext(({ count }) => log('%s documents updated', count))
|
|
||||||
.map(() => {
|
|
||||||
if (type === 'json') {
|
|
||||||
return res.json({
|
|
||||||
points,
|
|
||||||
alreadyCompleted,
|
|
||||||
completedDate
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
return res.sendStatus(200);
|
return resolve();
|
||||||
});
|
})
|
||||||
|
);
|
||||||
|
return Observable.fromPromise(updatePromise).map(() => {
|
||||||
|
if (type === 'json') {
|
||||||
|
return res.json({
|
||||||
|
points,
|
||||||
|
alreadyCompleted,
|
||||||
|
completedDate
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return res.sendStatus(200);
|
||||||
|
});
|
||||||
})
|
})
|
||||||
.subscribe(() => {}, next);
|
.subscribe(() => {}, next);
|
||||||
}
|
}
|
||||||
@ -239,6 +239,8 @@ export default async function bootChallenge(app, done) {
|
|||||||
const type = accepts(req).type('html', 'json', 'text');
|
const type = accepts(req).type('html', 'json', 'text');
|
||||||
const errors = req.validationErrors(true);
|
const errors = req.validationErrors(true);
|
||||||
|
|
||||||
|
const { user } = req;
|
||||||
|
|
||||||
if (errors) {
|
if (errors) {
|
||||||
if (type === 'json') {
|
if (type === 'json') {
|
||||||
return res.status(403).send({ errors });
|
return res.status(403).send({ errors });
|
||||||
@ -248,35 +250,39 @@ export default async function bootChallenge(app, done) {
|
|||||||
return res.sendStatus(403);
|
return res.sendStatus(403);
|
||||||
}
|
}
|
||||||
|
|
||||||
return req.user
|
return user
|
||||||
.getCompletedChallenges$()
|
.getCompletedChallenges$()
|
||||||
.flatMap(() => {
|
.flatMap(() => {
|
||||||
const completedDate = Date.now();
|
const completedDate = Date.now();
|
||||||
const { id, solution, timezone, files } = req.body;
|
const { id, solution, timezone, files } = req.body;
|
||||||
|
|
||||||
const { alreadyCompleted, updateData } = buildUserUpdate(
|
const { alreadyCompleted, updateData } = buildUserUpdate(
|
||||||
req.user,
|
user,
|
||||||
id,
|
id,
|
||||||
{ id, solution, completedDate, files },
|
{ id, solution, completedDate, files },
|
||||||
timezone
|
timezone
|
||||||
);
|
);
|
||||||
|
|
||||||
const user = req.user;
|
|
||||||
const points = alreadyCompleted ? user.points : user.points + 1;
|
const points = alreadyCompleted ? user.points : user.points + 1;
|
||||||
|
|
||||||
return user
|
const updatePromise = new Promise((resolve, reject) =>
|
||||||
.update$(updateData)
|
user.updateAttributes(updateData, err => {
|
||||||
.doOnNext(({ count }) => log('%s documents updated', count))
|
if (err) {
|
||||||
.map(() => {
|
return reject(err);
|
||||||
if (type === 'json') {
|
|
||||||
return res.json({
|
|
||||||
points,
|
|
||||||
alreadyCompleted,
|
|
||||||
completedDate
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
return res.sendStatus(200);
|
return resolve();
|
||||||
});
|
})
|
||||||
|
);
|
||||||
|
return Observable.fromPromise(updatePromise).map(() => {
|
||||||
|
if (type === 'json') {
|
||||||
|
return res.json({
|
||||||
|
points,
|
||||||
|
alreadyCompleted,
|
||||||
|
completedDate
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return res.sendStatus(200);
|
||||||
|
});
|
||||||
})
|
})
|
||||||
.subscribe(() => {}, next);
|
.subscribe(() => {}, next);
|
||||||
}
|
}
|
||||||
@ -329,20 +335,24 @@ export default async function bootChallenge(app, done) {
|
|||||||
completedChallenge
|
completedChallenge
|
||||||
);
|
);
|
||||||
|
|
||||||
return user
|
const updatePromise = new Promise((resolve, reject) =>
|
||||||
.update$(updateData)
|
user.updateAttributes(updateData, err => {
|
||||||
.doOnNext(() => user.manualReload())
|
if (err) {
|
||||||
.doOnNext(({ count }) => log('%s documents updated', count))
|
return reject(err);
|
||||||
.doOnNext(() => {
|
|
||||||
if (type === 'json') {
|
|
||||||
return res.send({
|
|
||||||
alreadyCompleted,
|
|
||||||
points: alreadyCompleted ? user.points : user.points + 1,
|
|
||||||
completedDate: completedChallenge.completedDate
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
return res.status(200).send(true);
|
return resolve();
|
||||||
});
|
})
|
||||||
|
);
|
||||||
|
return Observable.fromPromise(updatePromise).doOnNext(() => {
|
||||||
|
if (type === 'json') {
|
||||||
|
return res.send({
|
||||||
|
alreadyCompleted,
|
||||||
|
points: alreadyCompleted ? user.points : user.points + 1,
|
||||||
|
completedDate: completedChallenge.completedDate
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return res.status(200).send(true);
|
||||||
|
});
|
||||||
})
|
})
|
||||||
.subscribe(() => {}, next);
|
.subscribe(() => {}, next);
|
||||||
}
|
}
|
||||||
@ -376,19 +386,24 @@ export default async function bootChallenge(app, done) {
|
|||||||
completedChallenge
|
completedChallenge
|
||||||
);
|
);
|
||||||
|
|
||||||
return user
|
const updatePromise = new Promise((resolve, reject) =>
|
||||||
.update$(updateData)
|
user.updateAttributes(updateData, err => {
|
||||||
.doOnNext(({ count }) => log('%s documents updated', count))
|
if (err) {
|
||||||
.doOnNext(() => {
|
return reject(err);
|
||||||
if (type === 'json') {
|
|
||||||
return res.send({
|
|
||||||
alreadyCompleted,
|
|
||||||
points: alreadyCompleted ? user.points : user.points + 1,
|
|
||||||
completedDate: completedChallenge.completedDate
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
return res.status(200).send(true);
|
return resolve();
|
||||||
});
|
})
|
||||||
|
);
|
||||||
|
return Observable.fromPromise(updatePromise).doOnNext(() => {
|
||||||
|
if (type === 'json') {
|
||||||
|
return res.send({
|
||||||
|
alreadyCompleted,
|
||||||
|
points: alreadyCompleted ? user.points : user.points + 1,
|
||||||
|
completedDate: completedChallenge.completedDate
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return res.status(200).send(true);
|
||||||
|
});
|
||||||
})
|
})
|
||||||
.subscribe(() => {}, next);
|
.subscribe(() => {}, next);
|
||||||
}
|
}
|
||||||
|
@ -120,12 +120,15 @@ function getUnlinkSocial(req, res, next) {
|
|||||||
|
|
||||||
const updateData = { [social]: null };
|
const updateData = { [social]: null };
|
||||||
|
|
||||||
return user.update$(updateData).subscribe(() => {
|
return user.updateAttributes(updateData, err => {
|
||||||
|
if (err) {
|
||||||
|
return next(err);
|
||||||
|
}
|
||||||
log(`${social} has been unlinked successfully`);
|
log(`${social} has been unlinked successfully`);
|
||||||
|
|
||||||
req.flash('info', `You've successfully unlinked your ${social}.`);
|
req.flash('info', `You've successfully unlinked your ${social}.`);
|
||||||
return res.redirect('/' + username);
|
return res.redirectWithFlash(`${homeLocation}/${username}`);
|
||||||
}, next);
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
{
|
{
|
||||||
"db": {
|
"db": {
|
||||||
"name": "db",
|
"name": "db",
|
||||||
"connector": "mongodb"
|
"connector": "mongodb",
|
||||||
|
"allowExtendedOperators": true
|
||||||
},
|
},
|
||||||
"mail": {
|
"mail": {
|
||||||
"name": "mail",
|
"name": "mail",
|
||||||
|
Reference in New Issue
Block a user