From ebcff4d95e378f46fc0b1b693e9bb8250a86a490 Mon Sep 17 00:00:00 2001 From: Wyatt Johnson Date: Thu, 17 May 2018 18:58:01 -0600 Subject: [PATCH] patched migration bugs --- models/schema/user.js | 10 +++++++++ services/actions.js | 49 +++++++++++++++++++------------------------ 2 files changed, 32 insertions(+), 27 deletions(-) diff --git a/models/schema/user.js b/models/schema/user.js index a4ccfd185..a27b8dc39 100644 --- a/models/schema/user.js +++ b/models/schema/user.js @@ -339,6 +339,11 @@ User.virtual('banned') }) .set(function(status) { this.status.banned.status = status; + + if (!this.status.banned.history) { + this.status.banned.history = []; + } + this.status.banned.history.push({ status, created_at: new Date(), @@ -357,6 +362,11 @@ User.virtual('suspended') }) .set(function(until) { this.status.suspension.until = until; + + if (!this.status.suspension.history) { + this.status.suspension.history = []; + } + this.status.suspension.history.push({ until, created_at: new Date(), diff --git a/services/actions.js b/services/actions.js index 1c95e5128..790adc6a0 100644 --- a/services/actions.js +++ b/services/actions.js @@ -2,7 +2,7 @@ const ActionModel = require('../models/action'); const CommentModel = require('../models/comment'); const UserModel = require('../models/user'); const _ = require('lodash'); -const errors = require('../errors'); +const { ErrAlreadyExists } = require('../errors'); const incrActionCounts = async (action, value) => { const ACTION_TYPE = action.action_type.toLowerCase(); @@ -41,35 +41,30 @@ const incrActionCounts = async (action, value) => { * @param {object} update the update operation for the mongo findOneAndUpdate op * @param {object} options the options operation for the mongo findOneAndUpdate op */ -const findOnlyOneAndUpdate = async (query, update, options = {}) => - new Promise((resolve, reject) => { - ActionModel.findOneAndUpdate( - query, - update, - Object.assign({}, options, { - // Use raw result to get `updatedExisting`. - passRawResult: true, +const findOnlyOneAndUpdate = async (query, update, options = {}) => { + const raw = await ActionModel.findOneAndUpdate( + query, + update, + Object.assign({}, options, { + // Use raw result to get `updatedExisting`. + rawResult: true, - // Ensure that if it's new, we return the new object created. - new: true, + // Ensure that if it's new, we return the new object created. + new: true, - // Perform an upsert in the event that this doesn't exist. - upsert: true, + // Perform an upsert in the event that this doesn't exist. + upsert: true, - // Set the default values if not provided based on the mongoose models. - setDefaultsOnInsert: true, - }), - (err, doc, raw) => { - if (err) { - return reject(err); - } - if (raw.lastErrorObject.updatedExisting) { - return reject(new errors.ErrAlreadyExists(raw.value)); - } - return resolve(raw.value); - } - ); - }); + // Set the default values if not provided based on the mongoose models. + setDefaultsOnInsert: true, + }) + ); + if (raw.lastErrorObject.updatedExisting) { + throw new ErrAlreadyExists(raw.value); + } + + return raw.value; +}; module.exports = class ActionsService { /**