Skip to content
This repository was archived by the owner on Oct 21, 2020. It is now read-only.

Commit dde63c8

Browse files
committed
feat: implement deletion, add more tests
1 parent 8b06f4c commit dde63c8

File tree

4 files changed

+287
-30
lines changed

4 files changed

+287
-30
lines changed

src/dataLayer/mongo/communityEvent.js

Lines changed: 58 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import uuid from 'uuid/v4';
66
import { isEmpty, isString } from 'lodash';
77

88
import { asyncErrorHandler } from '../../utils';
9-
import { verifyWebToken, namespace, updateAppMetaData } from '../../auth';
9+
import { verifyWebToken } from '../../auth';
1010
import { isArray } from 'util';
1111

1212
const log = debug('fcc:dataLayer:mongo:event');
@@ -35,22 +35,18 @@ function resolveAttendees(attendees) {
3535
return null;
3636
}
3737
*/
38-
// const attendee = await UserModel.findOne({externalId: attendeeUuid.externalId}, '_id');
38+
3939
const attendee = await UserModel.findOne(attendeeUuid, '_id');
40-
// const attendee = await UserModel.findOne(
41-
// { email: 'otto@a20.net' },
42-
// '_id'
43-
// );
4440

4541
if (attendee && !attendee.isEmpty) {
4642
resolvedAttendeesResult.push(attendee._id);
4743
} else {
48-
reject(`Cant find attendee ${attendeeUuid}`);
44+
reject(`Unable to find attendee: ${JSON.stringify(attendeeUuid)}`);
4945
return null;
5046
}
5147
}
5248

53-
return resolve(resolvedAttendeesResult);
49+
resolve(resolvedAttendeesResult);
5450
});
5551
}
5652

@@ -87,25 +83,25 @@ export async function getCommunityEvent(root, vars) {
8783
* consider builder pattern
8884
*/
8985
if ('externalId' in vars && 'title in vars' in vars) {
86+
if (!isString(vars.externalId) || !validator.isUUID(vars.externalId)) {
87+
throw new TypeError(`Expected a valid UUID: ${vars.externalId}`);
88+
}
9089
log(
9190
'both title and externalId in query, preferring externalId as ' +
9291
'it is more specific'
9392
);
94-
if (!isString(vars.externalId) || !validator.isUUID(vars.externalId)) {
95-
throw new TypeError('Not a valid UUID');
96-
}
9793
query.externalId = vars.externalId;
9894
} else if ('externalId' in vars) {
99-
log('searching by ID ' + vars.externalId);
10095
if (!isString(vars.externalId) || !validator.isUUID(vars.externalId)) {
101-
throw new TypeError('Expected UUID');
96+
throw new TypeError(`Expected a valid UUID: ${vars.externalId}`);
10297
}
98+
log('searching by ID ' + vars.externalId);
10399
query.externalId = vars.externalId;
104100
} else if ('title' in vars) {
105-
log('searching by title');
106101
if (!isString(vars.title)) {
107-
throw new TypeError('Expected string');
102+
throw new TypeError(`Expected a valid String: ${vars.title}`);
108103
}
104+
log('searching by title');
109105
query.title = vars.title;
110106
} else {
111107
throw new TypeError('Expected an event ID or title');
@@ -127,7 +123,7 @@ export async function getCommunityEvents(root, vars) {
127123
* no console.log
128124
* consider builder pattern
129125
130-
Pagination
126+
* Implement pagination
131127
*/
132128
if ('externalId' in vars && 'title in vars' in vars) {
133129
console.log(
@@ -203,7 +199,7 @@ export async function createCommunityEvent(root, vars, ctx) {
203199
if ('isLocked' in vars) {
204200
if (!vars.isLocked.isString || validator.isBoolean(vars.isLocked)) {
205201
reject(
206-
`Expected valid URL string, got ${JSON.stringify(vars.imageUrl)}`
202+
`Expected a Boolean value, got ${JSON.stringify(vars.isLocked)}`
207203
);
208204
return null;
209205
}
@@ -213,14 +209,15 @@ export async function createCommunityEvent(root, vars, ctx) {
213209

214210
if ('attendees' in vars) {
215211
event.attendees = await resolveAttendees(vars.attendees).catch(err => {
216-
console.log('Oh oh could not resolve users, I should stop now..');
217212
reject(err);
218213
return null;
219214
});
215+
216+
if (!event.attendees) {
217+
return null;
218+
}
220219
}
221220

222-
// BUG: code below will even though our previous if statement returned null
223-
// how does that work?
224221
const e = new CommunityEventModel(event);
225222

226223
await e.save(err => {
@@ -229,8 +226,7 @@ export async function createCommunityEvent(root, vars, ctx) {
229226
return null;
230227
}
231228
/* Will have attendees populated at this point
232-
Note that it interestingly this will *not* have an ID:
233-
229+
Note that it interestingly this will *not* have an ID:
234230
{
235231
"title":"another new event",
236232
"description":"cool event IV",
@@ -248,9 +244,47 @@ export async function createCommunityEvent(root, vars, ctx) {
248244
});
249245
}
250246

251-
// Update an event
247+
// TODO: Update an event
252248
// export async function updateEvent(root, vars, ctx) {
253249
// }
254250

255251
// Delete event, should only be allowed by creator and / or admin
256-
export async function deleteCommunityEvent(root, vars, ctx) {}
252+
export async function deleteCommunityEvent(root, vars, ctx) {
253+
const { decoded } = verifyWebToken(ctx);
254+
const { email } = decoded;
255+
256+
if (!isString(email) || !validator.isEmail(email)) {
257+
throw new TypeError('You must provide a valid email');
258+
}
259+
260+
// TODO: error handling
261+
const user = await asyncErrorHandler(
262+
UserModel.findOne({ email: email }, '_id').exec()
263+
);
264+
265+
if ('externalId' in vars) {
266+
if (!isString(vars.externalId) || !validator.isUUID(vars.externalId)) {
267+
throw new TypeError('Not a valid UUID');
268+
}
269+
}
270+
271+
const event = await CommunityEventModel.findOne({
272+
externalId: vars.externalId
273+
}).exec();
274+
275+
if (isEmpty(event)) {
276+
throw new Error('Event not found');
277+
}
278+
279+
if (event.owner.externalId !== user.externalId) {
280+
throw new Error('Only allowed to delete events you own');
281+
}
282+
283+
const removedEvent = await CommunityEventModel.findOneAndRemove({
284+
externalId: vars.externalId
285+
});
286+
if (!removedEvent) {
287+
throw new Error(`No event with externalId ${vars.externalId}`);
288+
}
289+
return removedEvent;
290+
}

0 commit comments

Comments
 (0)