mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-19 15:48:04 +01:00
@@ -1,4 +1,5 @@
|
|||||||
import mongoose from 'mongoose';
|
import mongoose from 'mongoose';
|
||||||
|
import autoinc from 'mongoose-id-autoinc';
|
||||||
import logger from '../website/server/libs/logger';
|
import logger from '../website/server/libs/logger';
|
||||||
import nconf from 'nconf';
|
import nconf from 'nconf';
|
||||||
import repl from 'repl';
|
import repl from 'repl';
|
||||||
@@ -27,13 +28,15 @@ let improveRepl = (context) => {
|
|||||||
replset: { socketOptions: { keepAlive: 1, connectTimeoutMS: 30000 } },
|
replset: { socketOptions: { keepAlive: 1, connectTimeoutMS: 30000 } },
|
||||||
server: { socketOptions: { keepAlive: 1, connectTimeoutMS: 30000 } },
|
server: { socketOptions: { keepAlive: 1, connectTimeoutMS: 30000 } },
|
||||||
};
|
};
|
||||||
mongoose.connect(
|
autoinc.init(
|
||||||
nconf.get('NODE_DB_URI'),
|
mongoose.connect(
|
||||||
mongooseOptions,
|
nconf.get('NODE_DB_URI'),
|
||||||
(err) => {
|
mongooseOptions,
|
||||||
if (err) throw err;
|
(err) => {
|
||||||
logger.info('Connected with Mongoose');
|
if (err) throw err;
|
||||||
}
|
logger.info('Connected with Mongoose');
|
||||||
|
}
|
||||||
|
)
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
806
package-lock.json
generated
806
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -67,7 +67,8 @@
|
|||||||
"method-override": "^2.3.5",
|
"method-override": "^2.3.5",
|
||||||
"moment": "^2.13.0",
|
"moment": "^2.13.0",
|
||||||
"moment-recur": "git://github.com/habitrpg/moment-recur.git#f147ef27bbc26ca67638385f3db4a44084c76626",
|
"moment-recur": "git://github.com/habitrpg/moment-recur.git#f147ef27bbc26ca67638385f3db4a44084c76626",
|
||||||
"mongoose": "^4.13.10",
|
"mongoose": "^4.8.6",
|
||||||
|
"mongoose-id-autoinc": "~2013.7.14-4",
|
||||||
"morgan": "^1.7.0",
|
"morgan": "^1.7.0",
|
||||||
"nconf": "~0.8.2",
|
"nconf": "~0.8.2",
|
||||||
"node-gcm": "^0.14.4",
|
"node-gcm": "^0.14.4",
|
||||||
|
|||||||
@@ -131,7 +131,7 @@ describe('errorHandler', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('handle Mongoose Validation errors', () => {
|
it('handle Mongoose Validation errors', () => {
|
||||||
let error = new Error('User validation failed');
|
let error = new Error('User validation failed.');
|
||||||
error.name = 'ValidationError';
|
error.name = 'ValidationError';
|
||||||
|
|
||||||
error.errors = {
|
error.errors = {
|
||||||
@@ -151,7 +151,7 @@ describe('errorHandler', () => {
|
|||||||
expect(res.json).to.be.calledWith({
|
expect(res.json).to.be.calledWith({
|
||||||
success: false,
|
success: false,
|
||||||
error: 'BadRequest',
|
error: 'BadRequest',
|
||||||
message: 'User validation failed',
|
message: 'User validation failed.',
|
||||||
errors: [
|
errors: [
|
||||||
{ path: 'auth.local.email', message: 'Invalid email.', value: 'not an email' },
|
{ path: 'auth.local.email', message: 'Invalid email.', value: 'not an email' },
|
||||||
],
|
],
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import nconf from 'nconf';
|
import nconf from 'nconf';
|
||||||
import logger from './logger';
|
import logger from './logger';
|
||||||
|
import autoinc from 'mongoose-id-autoinc';
|
||||||
import mongoose from 'mongoose';
|
import mongoose from 'mongoose';
|
||||||
import Bluebird from 'bluebird';
|
import Bluebird from 'bluebird';
|
||||||
|
|
||||||
@@ -18,8 +19,10 @@ if (MAINTENANCE_MODE !== 'true') {
|
|||||||
|
|
||||||
const NODE_DB_URI = nconf.get('IS_TEST') ? nconf.get('TEST_DB_URI') : nconf.get('NODE_DB_URI');
|
const NODE_DB_URI = nconf.get('IS_TEST') ? nconf.get('TEST_DB_URI') : nconf.get('NODE_DB_URI');
|
||||||
|
|
||||||
mongoose.connect(NODE_DB_URI, mongooseOptions, (err) => {
|
let db = mongoose.connect(NODE_DB_URI, mongooseOptions, (err) => {
|
||||||
if (err) throw err;
|
if (err) throw err;
|
||||||
logger.info('Connected with Mongoose.');
|
logger.info('Connected with Mongoose.');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
autoinc.init(db);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -39,8 +39,7 @@ module.exports = function errorHandler (err, req, res, next) { // eslint-disable
|
|||||||
|
|
||||||
// Handle mongoose validation errors
|
// Handle mongoose validation errors
|
||||||
if (err.name === 'ValidationError') {
|
if (err.name === 'ValidationError') {
|
||||||
const model = err.message.split(' ')[0];
|
responseErr = new BadRequest(err.message); // TODO standard message? translate?
|
||||||
responseErr = new BadRequest(`${model} validation failed`);
|
|
||||||
responseErr.errors = map(err.errors, (mongooseErr) => {
|
responseErr.errors = map(err.errors, (mongooseErr) => {
|
||||||
return {
|
return {
|
||||||
message: mongooseErr.message,
|
message: mongooseErr.message,
|
||||||
|
|||||||
@@ -736,7 +736,7 @@ async function _updateUserWithRetries (userId, updates, numTry = 1, query = {})
|
|||||||
return raw;
|
return raw;
|
||||||
}).catch((err) => {
|
}).catch((err) => {
|
||||||
if (numTry < MAX_UPDATE_RETRIES) {
|
if (numTry < MAX_UPDATE_RETRIES) {
|
||||||
return _updateUserWithRetries(userId, updates, ++numTry, query);
|
return _updateUserWithRetries(userId, updates, ++numTry);
|
||||||
} else {
|
} else {
|
||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -54,23 +54,6 @@ export let TaskSchema = new Schema({
|
|||||||
return !validator.isUUID(val);
|
return !validator.isUUID(val);
|
||||||
},
|
},
|
||||||
msg: 'Task short names cannot be uuids.',
|
msg: 'Task short names cannot be uuids.',
|
||||||
}, {
|
|
||||||
validator (alias) {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
Task.findOne({ // eslint-disable-line no-use-before-define
|
|
||||||
_id: { $ne: this._id },
|
|
||||||
userId: this.userId,
|
|
||||||
alias,
|
|
||||||
}).exec().then((task) => {
|
|
||||||
let aliasAvailable = !task;
|
|
||||||
|
|
||||||
return aliasAvailable ? resolve() : reject();
|
|
||||||
}).catch(() => {
|
|
||||||
reject();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
},
|
|
||||||
msg: 'Task alias already used on another task.',
|
|
||||||
}],
|
}],
|
||||||
},
|
},
|
||||||
tags: [{
|
tags: [{
|
||||||
@@ -210,6 +193,20 @@ TaskSchema.methods.scoreChallengeTask = async function scoreChallengeTask (delta
|
|||||||
|
|
||||||
export let Task = mongoose.model('Task', TaskSchema);
|
export let Task = mongoose.model('Task', TaskSchema);
|
||||||
|
|
||||||
|
Task.schema.path('alias').validate(function valiateAliasNotTaken (alias, respond) {
|
||||||
|
Task.findOne({
|
||||||
|
_id: { $ne: this._id },
|
||||||
|
userId: this.userId,
|
||||||
|
alias,
|
||||||
|
}).exec().then((task) => {
|
||||||
|
let aliasAvailable = !task;
|
||||||
|
|
||||||
|
respond(aliasAvailable);
|
||||||
|
}).catch(() => {
|
||||||
|
respond(false);
|
||||||
|
});
|
||||||
|
}, 'Task alias already used on another task.');
|
||||||
|
|
||||||
// habits and dailies shared fields
|
// habits and dailies shared fields
|
||||||
let habitDailySchema = () => {
|
let habitDailySchema = () => {
|
||||||
return {history: Array}; // [{date:Date, value:Number}], // this causes major performance problems
|
return {history: Array}; // [{date:Date, value:Number}], // this causes major performance problems
|
||||||
|
|||||||
Reference in New Issue
Block a user