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

Commit ec9f50e

Browse files
committed
feat: first cut of implementing create and read for events
1 parent 293e4a2 commit ec9f50e

File tree

4 files changed

+139
-76
lines changed

4 files changed

+139
-76
lines changed

src/dataLayer/model/communityEvent.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const Schema = mongoose.Schema;
55
const communityEventSchema = new Schema({
66
id: {
77
type: 'string',
8-
description: 'Unique ID to refere to events externally',
8+
description: 'Unique ID to refers to events externally',
99
required: true
1010
},
1111
title: {

src/dataLayer/mongo/communityEvent.js

Lines changed: 131 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// import validator from 'validator';
1+
import validator from 'validator';
22
import CommunityEventModel from '../model/communityEvent.js';
33
import debug from 'debug';
44
import uuid from 'uuid/v4';
@@ -13,90 +13,148 @@ function doesExist(Model, options) {
1313
return Model.find(options).exec();
1414
}
1515

16-
// // Get one event (TODO: what do we allow searching on?)
17-
// export function getCommunityEvent(root, { title }) {
18-
// // return new Promise(async function getCommunityEventPromise(resolve, reject) {
19-
// // if (!isString(title)) {
20-
// // reject(
21-
// // new TypeError('Expected an event title')
22-
// // );
23-
// // return null;
24-
// // }
25-
// // log(`finding event for ${title}`);
26-
// // const found = await CommunityEventModel.findOne({ title }).exec();
27-
// // log(`found? ${!!found}`);
28-
// // if (isEmpty(found)) {
29-
// // reject(new Error(`No event found for ${title}`));
30-
// // return null;
31-
// // }
32-
// // return resolve(found);
33-
// // });
34-
// }
16+
// Get one event (TODO: what do we allow searching on?)
17+
// should allow to search on events owned by "me" or a user..
18+
export function getCommunityEvent(root, vars) {
19+
return new Promise(async function getCommunityEventPromise(resolve, reject) {
20+
const query = {};
21+
// TODO: can be re-used
22+
if ('id' in vars && 'title in vars' in vars) {
23+
console.log(
24+
'both title and ID in query, preferring ID as ' + 'it is more specific'
25+
);
26+
if (!isString(vars.id) || !validator.isUUID(vars.id)) {
27+
reject(new TypeError('Not a valid UUID'));
28+
return null;
29+
}
30+
query.id = vars.id;
31+
} else if ('id' in vars) {
32+
console.log('searching by ID ' + vars.id);
33+
if (!isString(vars.id) || !validator.isUUID(vars.id)) {
34+
reject(new TypeError('Expected UUID'));
35+
return null;
36+
}
37+
query.id = vars.id;
38+
} else if ('title' in vars) {
39+
console.log('searching by title');
40+
if (!isString(vars.title)) {
41+
reject(new TypeError('Expected string'));
42+
return null;
43+
}
44+
query.title = vars.title;
45+
} else {
46+
reject(new TypeError('Expected an event ID or title'));
47+
return null;
48+
}
3549

36-
// Delete event, should only be allowed by creator and / or admin
37-
export async function getCommunityEvent(root, vars, ctx) {
38-
// const { decoded } = verifyWebToken(ctx);
39-
// const { accountLinkId } = vars;
40-
// const loggedInId = decoded[namespace + 'accountLinkId'];
41-
// if (loggedInId !== accountLinkId) {
42-
// throw new Error('You can delete only your account');
43-
// }
44-
// const removedUser = await EventModel.findOneAndRemove({ accountLinkId });
45-
// if (!removedUser) {
46-
// throw new Error(
47-
// 'There is no account with this accountLinkId ' + accountLinkId
48-
// );
49-
// }
50-
// return removedUser;
50+
const found = await CommunityEventModel.findOne(query).exec();
51+
log(`found? ${!!found}`);
52+
if (isEmpty(found)) {
53+
reject(new Error(`No event found for ${JSON.stringify(query)}`));
54+
return null;
55+
}
56+
return resolve(found);
57+
});
5158
}
5259

53-
// // Get all events (TODO: what do we allow searching on? Needs pagination)
54-
// export function getEvents(root, { email }) {
55-
// return new Promise(async function getUserPromise(resolve, reject) {
56-
// if (!isString(email) || !validator.isEmail(email)) {
57-
// reject(
58-
// new TypeError(`Expected a valid email, got ${JSON.stringify(email)}`)
59-
// );
60-
// return null;
61-
// }
62-
// log(`finding user for ${email}`);
63-
// const found = await UserModel.findOne({ email }).exec();
64-
// log(`found? ${!!found}`);
65-
// if (isEmpty(found)) {
66-
// reject(new Error(`No user found for ${email}`));
67-
// return null;
68-
// }
69-
// return resolve(found);
70-
// });
71-
// }
60+
//TODO: Pagination)
61+
export function getCommunityEvents(root, vars) {
62+
return new Promise(async function getCommunityEventsPromise(resolve, reject) {
63+
const query = {};
64+
if ('id' in vars && 'title in vars' in vars) {
65+
console.log(
66+
'both title and ID in query, preferring ID as ' + 'it is more specific'
67+
);
68+
if (!isString(vars.id) || !validator.isUUID(vars.id)) {
69+
reject(new TypeError('Not a valid UUID'));
70+
return null;
71+
}
72+
query.id = vars.id;
73+
} else if ('id' in vars) {
74+
console.log('searching by ID ' + vars.id);
75+
if (!isString(vars.id) || !validator.isUUID(vars.id)) {
76+
reject(new TypeError('Expected UUID'));
77+
return null;
78+
}
79+
query.id = vars.id;
80+
} else if ('title' in vars) {
81+
console.log('searching by title');
82+
if (!isString(vars.title)) {
83+
reject(new TypeError('Expected string'));
84+
return null;
85+
}
86+
query.title = vars.title;
87+
}
88+
89+
const found = await CommunityEventModel.find(query).exec();
90+
log(`found? ${!!found}`);
91+
if (isEmpty(found)) {
92+
reject(new Error(`No events found for ${JSON.stringify(query)}`));
93+
return null;
94+
}
95+
return resolve(found);
96+
});
97+
}
7298

7399
export async function createCommunityEvent(root, vars, ctx) {
74-
// const { decoded } = verifyWebToken(ctx);
100+
const { decoded } = verifyWebToken(ctx);
101+
// TODO:
102+
// * authorization
103+
// * validation
104+
// * verification
105+
// * creation
106+
75107
// const { email, name, sub: id } = decoded;
76108
// if (!isString(email) || !validator.isEmail(email)) {
77109
// throw new Error('You must provide a valid email');
78110
// }
79-
// const newUser = { name, email };
80-
// let accountLinkId = decoded[namespace + 'accountLinkId'];
81-
// if (accountLinkId) {
82-
// newUser.accountLinkId = accountLinkId;
83-
// } else {
84-
// accountLinkId = uuid();
85-
// newUser.accountLinkId = accountLinkId;
86-
// updateAppMetaData(id, { accountLinkId });
87-
// }
111+
112+
// All mandatory, but still need validation.. (build pattern anyone?)
113+
const event = {
114+
title: vars.title,
115+
description: vars.description,
116+
owner: vars.owner,
117+
date: vars.date,
118+
id: uuid()
119+
};
120+
121+
// Optional
122+
if ('imageUrl' in vars) {
123+
if (!vars.imageUrl.isString || validator.isURL(vars.imageUrl)) {
124+
// Use promise instead? / validate if URL too?
125+
// new TypeError(`Expected valid URL string, got ${JSON.stringify(vars.imageUrl)}`);
126+
console.log(
127+
`Expected valid URL string, got ${JSON.stringify(vars.imageUrl)}`
128+
);
129+
return null;
130+
}
131+
132+
event.imageUrl = vars.imageUrl;
133+
}
134+
if ('isLocked' in vars) {
135+
if (!vars.isLocked.isString || validator.isBoolean(vars.isLocked)) {
136+
// Use promise instead? / check for Bool?
137+
console.log(
138+
`Expected valid URL string, got ${JSON.stringify(vars.imageUrl)}`
139+
);
140+
// new TypeError(`Expected valid URL string, got ${JSON.stringify(vars.imageUrl)}`);
141+
return null;
142+
}
143+
144+
event.isLocked = vars.isLocked;
145+
}
146+
147+
// Does it exist? We'll have too... check if the ID is there,
148+
// and.. if the title is there. What "is" an event, really? :)
88149
// const exists = await asyncErrorHandler(
89150
// doesExist(CommunityEventModel, { accountLinkId })
90151
// );
91-
// if (isEmpty(exists)) {
92-
// const newAccount = new CommunityEventModel(newUser);
93-
// return await asyncErrorHandler(
94-
// newAccount.save(),
95-
// 'Something went wrong creating your account, please try again'
96-
// );
97-
// } else {
98-
// throw new Error('Account already in use');
99-
// }
152+
// If not, create it
153+
const e = new CommunityEventModel(event);
154+
return await asyncErrorHandler(
155+
e.save(),
156+
'Something went wrong creating your event, please try again'
157+
);
100158
}
101159

102160
// Update an event

src/graphql/resolvers/communityEvent.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import {
22
getCommunityEvent,
3+
getCommunityEvents,
34
createCommunityEvent,
45
deleteCommunityEvent
56
} from '../../dataLayer/mongo/communityEvent';
67

78
export const communityEventResolvers = {
89
Query: {
9-
getCommunityEvent
10+
getCommunityEvent,
11+
getCommunityEvents
1012
},
1113
Mutation: {
1214
createCommunityEvent,
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
export default `
22
type Query {
3-
getCommunityEvent(title: String!): CommunityEvent
3+
getCommunityEvent(id: String
4+
title: String): CommunityEvent,
5+
getCommunityEvents(id: String
6+
title: String): [CommunityEvent]
47
}
58
`;

0 commit comments

Comments
 (0)