Skip to content

Commit

Permalink
Add message of cancelled quest in party chat (#11106)
Browse files Browse the repository at this point in the history
* Add message of cancelled quest in party chat

Issue #11093

* Delete trailing spaces

For successful passing the test

* Add test of cancelled quest's message

Also, added an explanation that partyMembers[1] hasn't accepted the invitation in the 'cancels a quest' test

* Fix: import Group

Import Group to pass Lint syntax test

* Move save function to Promise.all

* Fix moving save to Promise.all
  • Loading branch information
HydeHunter2 authored and paglias committed Apr 14, 2019
1 parent 0bfd709 commit 7a5a856
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
generateUser,
} from '../../../../helpers/api-integration/v3';
import { v4 as generateUUID } from 'uuid';
import { model as Group } from '../../../../../website/server/models/group';

describe('POST /groups/:groupId/quests/cancel', () => {
let questingGroup;
Expand Down Expand Up @@ -99,6 +100,10 @@ describe('POST /groups/:groupId/quests/cancel', () => {
it('cancels a quest', async () => {
await leader.post(`/groups/${questingGroup._id}/quests/invite/${PET_QUEST}`);
await partyMembers[0].post(`/groups/${questingGroup._id}/quests/accept`);
// partyMembers[1] hasn't accepted the invitation, because if he accepts, invitation phase ends.
// The cancel command can be done only in the invitation phase.

let stub = sandbox.spy(Group.prototype, 'sendChat');

let res = await leader.post(`/groups/${questingGroup._id}/quests/cancel`);

Expand Down Expand Up @@ -135,5 +140,9 @@ describe('POST /groups/:groupId/quests/cancel', () => {
},
members: {},
});
expect(Group.prototype.sendChat).to.be.calledOnce;
expect(Group.prototype.sendChat).to.be.calledWithMatch(/cancelled the party quest Wail of the Whale.`/);

stub.restore();
});
});
7 changes: 6 additions & 1 deletion website/server/controllers/api-v3/quests.js
Original file line number Diff line number Diff line change
Expand Up @@ -363,17 +363,22 @@ api.cancelQuest = {
if (validationErrors) throw validationErrors;

let group = await Group.getGroup({user, groupId, fields: basicGroupFields.concat(' quest')});

if (!group) throw new NotFound(res.t('groupNotFound'));
if (group.type !== 'party') throw new NotAuthorized(res.t('guildQuestsNotSupported'));
if (!group.quest.key) throw new NotFound(res.t('questInvitationDoesNotExist'));
if (user._id !== group.leader && group.quest.leader !== user._id) throw new NotAuthorized(res.t('onlyLeaderCancelQuest'));
if (group.quest.active) throw new NotAuthorized(res.t('cantCancelActiveQuest'));

let questName = questScrolls[group.quest.key].text('en');
const newChatMessage = group.sendChat(`\`${user.profile.name} cancelled the party quest ${questName}.\``);

group.quest = Group.cleanGroupQuest();
group.markModified('quest');

let [savedGroup] = await Promise.all([
group.save(),
newChatMessage.save(),
User.update(
{'party._id': groupId},
Group.cleanQuestParty(),
Expand Down Expand Up @@ -405,7 +410,7 @@ api.abortQuest = {
url: '/groups/:groupId/quests/abort',
middlewares: [authWithHeaders()],
async handler (req, res) {
// Abort a quest AFTER it has begun (see questCancel for BEFORE)
// Abort a quest AFTER it has begun
let user = res.locals.user;
let groupId = req.params.groupId;

Expand Down

0 comments on commit 7a5a856

Please sign in to comment.