1
- // import validator from 'validator';
1
+ import validator from 'validator' ;
2
2
import CommunityEventModel from '../model/communityEvent.js' ;
3
3
import debug from 'debug' ;
4
4
import uuid from 'uuid/v4' ;
@@ -13,90 +13,148 @@ function doesExist(Model, options) {
13
13
return Model . find ( options ) . exec ( ) ;
14
14
}
15
15
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
+ }
35
49
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
+ } ) ;
51
58
}
52
59
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
+ }
72
98
73
99
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
+
75
107
// const { email, name, sub: id } = decoded;
76
108
// if (!isString(email) || !validator.isEmail(email)) {
77
109
// throw new Error('You must provide a valid email');
78
110
// }
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? :)
88
149
// const exists = await asyncErrorHandler(
89
150
// doesExist(CommunityEventModel, { accountLinkId })
90
151
// );
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
+ ) ;
100
158
}
101
159
102
160
// Update an event
0 commit comments