fix user schema definition and add missing schema

This commit is contained in:
Matteo Pagliazzi
2016-02-27 17:46:00 +01:00
parent 432d27fab2
commit 6c8da9a53a
3 changed files with 30 additions and 27 deletions

View File

@@ -7,6 +7,8 @@ import {
import { v4 as generateUUID } from 'uuid'; import { v4 as generateUUID } from 'uuid';
describe('POST /group/:groupId/join', () => { describe('POST /group/:groupId/join', () => {
const PET_QUEST = 'whale';
it('returns error when groupId is not for a valid group', async () => { it('returns error when groupId is not for a valid group', async () => {
let joiningUser = await generateUser(); let joiningUser = await generateUser();
@@ -135,6 +137,7 @@ describe('POST /group/:groupId/join', () => {
name: 'Test Party', name: 'Test Party',
type: 'party', type: 'party',
}, },
members: 2,
invites: 1, invites: 1,
}); });
@@ -205,18 +208,20 @@ describe('POST /group/:groupId/join', () => {
await expect(checkExistence('groups', oldParty._id)).to.eventually.equal(false); await expect(checkExistence('groups', oldParty._id)).to.eventually.equal(false);
}); });
xit('invites joining member to active quest', async () => { it('invites joining member to active quest', async () => {
// TODO start quest await user.update({
[`items.quests.${PET_QUEST}`]: 1,
});
await user.post(`/groups/${party._id}/quests/invite/${PET_QUEST}`);
await invitedUser.post(`/groups/${party._id}/join`); await invitedUser.post(`/groups/${party._id}/join`);
invitedUser = await user.get('/user'); await invitedUser.sync();
party = await user.get(`/groups/${party._id}`); await party.sync();
expect(user).to.have.deep.property('party.quest.RSVPNeeded', true); expect(invitedUser).to.have.deep.property('party.quest.RSVPNeeded', true);
expect(user).to.have.deep.property('party.quest.key', party.quest.key); expect(invitedUser).to.have.deep.property('party.quest.key', party.quest.key);
expect(party.quest.members[invitedUser._id]).to.be.null;
expect(party.quest.members[invitedUser._id]).to.be.undefined;
}); });
}); });
}); });

View File

@@ -251,16 +251,16 @@ api.joinGroup = {
let isUserInvited = false; let isUserInvited = false;
if (group.type === 'party' && group._id === (user.invitations.party && user.invitations.party.id)) { if (group.type === 'party' && group._id === user.invitations.party.id) {
inviter = user.invitations.party.inviter; inviter = user.invitations.party.inviter;
user.invitations.party = {}; // Clear invite TODO mark modified? user.invitations.party = {}; // Clear invite
user.markModified('invitations.party');
// invite new user to pending quest // invite new user to pending quest
if (group.quest.key && !group.quest.active) { if (group.quest.key && !group.quest.active) {
user.party.quest.RSVPNeeded = true; user.party.quest.RSVPNeeded = true;
user.party.quest.key = group.quest.key; user.party.quest.key = group.quest.key;
user.party.quest.progress = undefined; // Make sure to reset progress from ay previous quest group.quest.members[user._id] = null;
group.quest.members[user._id] = undefined;
group.markModified('quest.members'); group.markModified('quest.members');
} }
@@ -459,7 +459,6 @@ api.removeGroupMember = {
}; };
async function _inviteByUUID (uuid, group, inviter, req, res) { async function _inviteByUUID (uuid, group, inviter, req, res) {
// TODO: Add Push Notifications
let userToInvite = await User.findById(uuid).exec(); let userToInvite = await User.findById(uuid).exec();
if (!userToInvite) { if (!userToInvite) {
@@ -475,7 +474,7 @@ async function _inviteByUUID (uuid, group, inviter, req, res) {
} }
userToInvite.invitations.guilds.push({id: group._id, name: group.name, inviter: inviter._id}); userToInvite.invitations.guilds.push({id: group._id, name: group.name, inviter: inviter._id});
} else if (group.type === 'party') { } else if (group.type === 'party') {
if (!_.isEmpty(userToInvite.invitations.party)) { if (userToInvite.invitations.party.id) {
throw new NotAuthorized(res.t('userAlreadyPendingInvitation')); throw new NotAuthorized(res.t('userAlreadyPendingInvitation'));
} }

View File

@@ -136,7 +136,7 @@ export let schema = new Schema({
dateUpdated: Date, dateUpdated: Date,
extraMonths: {type: Number, default: 0}, extraMonths: {type: Number, default: 0},
gemsBought: {type: Number, default: 0}, gemsBought: {type: Number, default: 0},
mysteryItems: {type: Array, default: []}, mysteryItems: {type: Array, default: () => []},
lastBillingDate: Date, // Used only for Amazon Payments to keep track of billing date lastBillingDate: Date, // Used only for Amazon Payments to keep track of billing date
consecutive: { consecutive: {
count: {type: Number, default: 0}, count: {type: Number, default: 0},
@@ -348,16 +348,15 @@ export let schema = new Schema({
challenges: [{type: String, ref: 'Challenge', validate: [validator.isUUID, 'Invalid uuid.']}], challenges: [{type: String, ref: 'Challenge', validate: [validator.isUUID, 'Invalid uuid.']}],
invitations: { invitations: {
guilds: [{ // Using an array without validation because otherwise mongoose treat this as a subdocument and applies _id by default
id: {type: String, ref: 'Group', validate: [validator.isUUID, 'Invalid uuid.']}, // Schema is (id, name, inviter)
name: String, // TODO one way to fix is http://mongoosejs.com/docs/guide.html#_id
inviter: {type: String, ref: 'User', validate: [validator.isUUID, 'Invalid uuid.']}, guilds: {type: Array, default: () => []},
}], // Using a Mixed type because otherwise user.invitations.party = {} // to reset invitation, causes validation to fail TODO
party: { // schema is the same as for guild invitations (id, name, inviter)
id: {type: String, ref: 'Group', validate: [validator.isUUID, 'Invalid uuid.']}, party: {type: Schema.Types.Mixed, default: () => {
name: String, return {};
inviter: {type: String, ref: 'User', validate: [validator.isUUID, 'Invalid uuid.']}, }},
},
}, },
guilds: [{type: String, ref: 'Group', validate: [validator.isUUID, 'Invalid uuid.']}], guilds: [{type: String, ref: 'Group', validate: [validator.isUUID, 'Invalid uuid.']}],
@@ -483,7 +482,7 @@ export let schema = new Schema({
inbox: { inbox: {
newMessages: {type: Number, default: 0}, newMessages: {type: Number, default: 0},
blocks: {type: Array, default: []}, blocks: {type: Array, default: () => []},
messages: {type: Schema.Types.Mixed, default: () => { messages: {type: Schema.Types.Mixed, default: () => {
return {}; return {};
}}, }},
@@ -504,7 +503,7 @@ export let schema = new Schema({
regId: {type: String}, regId: {type: String},
type: {type: String}, type: {type: String},
}], }],
default: [], default: () => [],
}, },
}, { }, {
strict: true, strict: true,