Adding new user.addNotification method with Mongodb Update (#8272)

* Adding new method to user schema that pushes a new notification to the database with an update operation instead of a save. fixes #8264

* fixing test text

* changing the addUserNotificationUpdate method to a static method as requested.

* Renaming to push notification

* fixing comment documentation based on pr comments.

* Changed the update statement to do a multi update and added a validation step. Added tests for both these cases.

* Updating pushNotification method to allow a query to be passed instead of an array of userIds to make it more flexible.

* Removing createdAt field from tests as it's no longer used.

* Removing only from test suite
This commit is contained in:
Travis
2017-01-04 08:27:54 -06:00
committed by Matteo Pagliazzi
parent 514d35c0be
commit 6cc359a935
2 changed files with 102 additions and 3 deletions

View File

@@ -1,5 +1,6 @@
import { model as User } from '../../../../../website/server/models/user'; import { model as User } from '../../../../../website/server/models/user';
import common from '../../../../../website/common'; import common from '../../../../../website/common';
import Bluebird from 'bluebird';
describe('User Model', () => { describe('User Model', () => {
it('keeps user._tmp when calling .toJSON', () => { it('keeps user._tmp when calling .toJSON', () => {
@@ -48,7 +49,7 @@ describe('User Model', () => {
}); });
context('notifications', () => { context('notifications', () => {
it('can add notifications with data', () => { it('can add notifications without data', () => {
let user = new User(); let user = new User();
user.addNotification('CRON'); user.addNotification('CRON');
@@ -60,7 +61,7 @@ describe('User Model', () => {
expect(userToJSON.notifications[0].data).to.eql({}); expect(userToJSON.notifications[0].data).to.eql({});
}); });
it('can add notifications without data', () => { it('can add notifications with data', () => {
let user = new User(); let user = new User();
user.addNotification('CRON', {field: 1}); user.addNotification('CRON', {field: 1});
@@ -71,5 +72,77 @@ describe('User Model', () => {
expect(userToJSON.notifications[0].type).to.equal('CRON'); expect(userToJSON.notifications[0].type).to.equal('CRON');
expect(userToJSON.notifications[0].data).to.eql({field: 1}); expect(userToJSON.notifications[0].data).to.eql({field: 1});
}); });
context('static push method', () => {
it('adds notifications for a single member via static method', async() => {
let user = new User();
await user.save();
await User.pushNotification({_id: user._id}, 'CRON');
user = await User.findOne({_id: user._id}).exec();
let userToJSON = user.toJSON();
expect(user.notifications.length).to.equal(1);
expect(userToJSON.notifications[0]).to.have.all.keys(['data', 'id', 'type']);
expect(userToJSON.notifications[0].type).to.equal('CRON');
expect(userToJSON.notifications[0].data).to.eql({});
});
it('validates notifications via static method', async() => {
let user = new User();
await user.save();
expect(User.pushNotification({_id: user._id}, 'BAD_TYPE')).to.eventually.be.rejected;
});
it('adds notifications without data for all given users via static method', async() => {
let user = new User();
let otherUser = new User();
await Bluebird.all([user.save(), otherUser.save()]);
await User.pushNotification({_id: {$in: [user._id, otherUser._id]}}, 'CRON');
user = await User.findOne({_id: user._id}).exec();
let userToJSON = user.toJSON();
expect(user.notifications.length).to.equal(1);
expect(userToJSON.notifications[0]).to.have.all.keys(['data', 'id', 'type']);
expect(userToJSON.notifications[0].type).to.equal('CRON');
expect(userToJSON.notifications[0].data).to.eql({});
user = await User.findOne({_id: otherUser._id}).exec();
userToJSON = user.toJSON();
expect(user.notifications.length).to.equal(1);
expect(userToJSON.notifications[0]).to.have.all.keys(['data', 'id', 'type']);
expect(userToJSON.notifications[0].type).to.equal('CRON');
expect(userToJSON.notifications[0].data).to.eql({});
});
it('adds notifications with data for all given users via static method', async() => {
let user = new User();
let otherUser = new User();
await Bluebird.all([user.save(), otherUser.save()]);
await User.pushNotification({_id: {$in: [user._id, otherUser._id]}}, 'CRON', {field: 1});
user = await User.findOne({_id: user._id}).exec();
let userToJSON = user.toJSON();
expect(user.notifications.length).to.equal(1);
expect(userToJSON.notifications[0]).to.have.all.keys(['data', 'id', 'type']);
expect(userToJSON.notifications[0].type).to.equal('CRON');
expect(userToJSON.notifications[0].data).to.eql({field: 1});
user = await User.findOne({_id: otherUser._id}).exec();
userToJSON = user.toJSON();
expect(user.notifications.length).to.equal(1);
expect(userToJSON.notifications[0]).to.have.all.keys(['data', 'id', 'type']);
expect(userToJSON.notifications[0].type).to.equal('CRON');
expect(userToJSON.notifications[0].data).to.eql({field: 1});
});
});
}); });
}); });

View File

@@ -5,7 +5,7 @@ import {
TAVERN_ID, TAVERN_ID,
} from '../group'; } from '../group';
import { defaults } from 'lodash'; import { defaults } from 'lodash';
import { model as UserNotification } from '../userNotification';
import schema from './schema'; import schema from './schema';
schema.methods.isSubscribed = function isSubscribed () { schema.methods.isSubscribed = function isSubscribed () {
@@ -46,6 +46,13 @@ schema.methods.sendMessage = async function sendMessage (userToReceiveMessage, o
await Bluebird.all(promises); await Bluebird.all(promises);
}; };
/**
* Creates a notification based on the input parameters and adds it to the local user notifications array.
* This does not save the notification to the database or interact with the database in any way.
*
* @param type The type of notification to add to the user. Possible values are defined in the UserNotificaiton Schema
* @param data The data to add to the notification
*/
schema.methods.addNotification = function addUserNotification (type, data = {}) { schema.methods.addNotification = function addUserNotification (type, data = {}) {
this.notifications.push({ this.notifications.push({
type, type,
@@ -53,6 +60,25 @@ schema.methods.addNotification = function addUserNotification (type, data = {})
}); });
}; };
/**
* Creates a notification based on the type and data input parameters and saves that new notification
* to the database directly using an update statement. The local copy of these users are not updated by
* this operation. Use this function when you want to add a notification to a user(s), but do not have
* the user document(s) opened.
*
* @param query A Mongoose query defining the users to add the notification to.
* @param type The type of notification to add to the user. Possible values are defined in the UserNotificaiton Schema
* @param data The data to add to the notification
*/
schema.statics.pushNotification = async function pushNotification (query, type, data = {}) {
let newNotification = new UserNotification({type, data});
let validationResult = newNotification.validateSync();
if (validationResult) {
throw validationResult;
}
await this.update(query, {$push: {notifications: newNotification}}, {multi: true}).exec();
};
// Add stats.toNextLevel, stats.maxMP and stats.maxHealth // Add stats.toNextLevel, stats.maxMP and stats.maxHealth
// to a JSONified User stats object // to a JSONified User stats object
schema.methods.addComputedStatsToJSONObj = function addComputedStatsToUserJSONObj (statsObject) { schema.methods.addComputedStatsToJSONObj = function addComputedStatsToUserJSONObj (statsObject) {