diff --git a/test/api/v3/unit/models/user.test.js b/test/api/v3/unit/models/user.test.js index f9651c9224..1c2b55ce1c 100644 --- a/test/api/v3/unit/models/user.test.js +++ b/test/api/v3/unit/models/user.test.js @@ -1,5 +1,6 @@ import { model as User } from '../../../../../website/server/models/user'; import common from '../../../../../website/common'; +import Bluebird from 'bluebird'; describe('User Model', () => { it('keeps user._tmp when calling .toJSON', () => { @@ -48,7 +49,7 @@ describe('User Model', () => { }); context('notifications', () => { - it('can add notifications with data', () => { + it('can add notifications without data', () => { let user = new User(); user.addNotification('CRON'); @@ -60,7 +61,7 @@ describe('User Model', () => { expect(userToJSON.notifications[0].data).to.eql({}); }); - it('can add notifications without data', () => { + it('can add notifications with data', () => { let user = new User(); user.addNotification('CRON', {field: 1}); @@ -71,5 +72,77 @@ describe('User Model', () => { expect(userToJSON.notifications[0].type).to.equal('CRON'); 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}); + }); + }); }); }); diff --git a/website/server/models/user/methods.js b/website/server/models/user/methods.js index e3132af324..517be30c0c 100644 --- a/website/server/models/user/methods.js +++ b/website/server/models/user/methods.js @@ -5,7 +5,7 @@ import { TAVERN_ID, } from '../group'; import { defaults } from 'lodash'; - +import { model as UserNotification } from '../userNotification'; import schema from './schema'; schema.methods.isSubscribed = function isSubscribed () { @@ -46,6 +46,13 @@ schema.methods.sendMessage = async function sendMessage (userToReceiveMessage, o 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 = {}) { this.notifications.push({ 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 // to a JSONified User stats object schema.methods.addComputedStatsToJSONObj = function addComputedStatsToUserJSONObj (statsObject) {