-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Visa Pollari
authored and
Visa Pollari
committed
Sep 3, 2020
1 parent
39ec294
commit 5b05974
Showing
173 changed files
with
2,880 additions
and
855 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,12 @@ | ||
{ | ||
"printWidth": 80, | ||
"tabWidth": 2, | ||
"semi": true, | ||
"tabWidth": 4, | ||
"semi": false, | ||
"singleQuote": true, | ||
"trailingComma": "all", | ||
"bracketSpacing": true, | ||
"jsxBracketSameLine": false, | ||
"endOfLine":"lf" | ||
"endOfLine":"lf", | ||
"arrowParens": "avoid" | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
{ | ||
"extends": ["airbnb-base", "plugin:prettier/recommended"], | ||
"env": { | ||
"jest": true, | ||
"browser": true | ||
}, | ||
"rules": { | ||
"import/no-extraneous-dependencies": "off", | ||
"import/prefer-default-export": "off", | ||
"no-confusing-arrow": "off", | ||
"linebreak-style": "off", | ||
"arrow-parens": ["error", "as-needed"], | ||
"comma-dangle": [ | ||
"error", | ||
{ | ||
"arrays": "always-multiline", | ||
"objects": "always-multiline", | ||
"imports": "always-multiline", | ||
"exports": "always-multiline", | ||
"functions": "ignore" | ||
} | ||
], | ||
"no-plusplus": "off" | ||
}, | ||
"globals": { | ||
"browser": true, | ||
"$": true, | ||
"before": true, | ||
"document": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,151 +1,165 @@ | ||
const mongoose = require('mongoose') | ||
const { Auth } = require('@hackjunction/shared') | ||
const DataLoader = require('dataloader') | ||
const Event = require('./model') | ||
const PermissionUtils = require('../../utils/permissions') | ||
const mongoose = require('mongoose'); | ||
const { Auth } = require('@hackjunction/shared'); | ||
const DataLoader = require('dataloader'); | ||
const Event = require('./model'); | ||
const PermissionUtils = require('../../utils/permissions'); | ||
|
||
async function batchGetEventsByIds(eventIds) { | ||
const objectIds = eventIds.map(id => new mongoose.Types.ObjectId(id)) | ||
const results = await Event.find({ | ||
_id: { | ||
$in: objectIds, | ||
}, | ||
}).lean() | ||
const resultsMap = results.reduce((map, current) => { | ||
map[current._id.toString()] = current | ||
return map | ||
}, {}) | ||
return eventIds.map(_id => resultsMap[_id] || null) | ||
const objectIds = eventIds.map(id => new mongoose.Types.ObjectId(id)); | ||
const results = await Event.find({ | ||
_id: { | ||
$in: objectIds, | ||
}, | ||
}).lean(); | ||
const resultsMap = results.reduce((map, current) => { | ||
map[current._id.toString()] = current; | ||
return map; | ||
}, {}); | ||
return eventIds.map(_id => resultsMap[_id] || null); | ||
} | ||
|
||
async function batchGetEventsBySlugs(eventSlugs) { | ||
const results = await Event.find({ | ||
slug: { | ||
$in: eventSlugs, | ||
}, | ||
}).lean() | ||
const resultsMap = results.reduce((map, current) => { | ||
map[current.slug] = current | ||
return map | ||
}, {}) | ||
return eventSlugs.map(slug => resultsMap[slug] || null) | ||
const results = await Event.find({ | ||
slug: { | ||
$in: eventSlugs, | ||
}, | ||
}).lean(); | ||
const resultsMap = results.reduce((map, current) => { | ||
map[current.slug] = current; | ||
return map; | ||
}, {}); | ||
return eventSlugs.map(slug => resultsMap[slug] || null); | ||
} | ||
|
||
class EventController { | ||
constructor(requestingUser, overrideChecks) { | ||
this.requestingUser = requestingUser | ||
this.overrideChecks = overrideChecks | ||
this.isAdmin = | ||
overrideChecks || | ||
PermissionUtils.userHasPermission( | ||
requestingUser, | ||
Auth.Permissions.MANAGE_EVENT | ||
) | ||
|
||
this.eventIdLoader = new DataLoader(batchGetEventsByIds) | ||
this.eventSlugLoader = new DataLoader(batchGetEventsBySlugs) | ||
} | ||
|
||
getById(eventId) { | ||
return this._clean(this.eventIdLoader.load(eventId)) | ||
} | ||
|
||
getBySlug(eventSlug) { | ||
return this._clean(this.eventSlugLoader.load(eventSlug)) | ||
} | ||
|
||
getByOrganiser(userId) { | ||
return this._clean( | ||
Event.find() | ||
.or([{ owner: userId }, { organisers: userId }]) | ||
.lean() | ||
) | ||
} | ||
|
||
getByOrganization(slug) { | ||
return this._clean(Event.find({ organizations: slug }).lean()) | ||
} | ||
|
||
getHighlighted() { | ||
return this._clean( | ||
Event.find({ | ||
published: true, | ||
startTime: { | ||
$gte: new Date(), | ||
}, | ||
}) | ||
.sort([['startTime', 1]]) | ||
.lean() | ||
) | ||
constructor(requestingUser, overrideChecks) { | ||
this.requestingUser = requestingUser; | ||
this.overrideChecks = overrideChecks; | ||
this.isAdmin = | ||
overrideChecks || | ||
PermissionUtils.userHasPermission( | ||
requestingUser, | ||
Auth.Permissions.MANAGE_EVENT, | ||
); | ||
|
||
this.eventIdLoader = new DataLoader(batchGetEventsByIds); | ||
this.eventSlugLoader = new DataLoader(batchGetEventsBySlugs); | ||
} | ||
|
||
getById(eventId) { | ||
return this._clean(this.eventIdLoader.load(eventId)); | ||
} | ||
|
||
getBySlug(eventSlug) { | ||
return this._clean(this.eventSlugLoader.load(eventSlug)); | ||
} | ||
|
||
getByOrganiser(userId) { | ||
return this._clean( | ||
Event.find() | ||
.or([{ owner: userId }, { organisers: userId }]) | ||
.lean(), | ||
); | ||
} | ||
|
||
getByOrganization(slug) { | ||
return this._clean(Event.find({ organizations: slug }).lean()); | ||
} | ||
|
||
getHighlighted() { | ||
// TODO remove this once connected is done | ||
return this._clean( | ||
Event.find({ | ||
name: 'Junction 2020 Connected', | ||
published: true, | ||
startTime: { | ||
$gte: new Date(), | ||
}, | ||
}) | ||
.sort([['startTime', 1]]) | ||
.lean(), | ||
); | ||
/* | ||
return this._clean( | ||
Event.find({ | ||
published: true, | ||
startTime: { | ||
$gte: new Date(), | ||
}, | ||
}) | ||
.sort([['startTime', 1]]) | ||
.lean(), | ||
); | ||
*/ | ||
} | ||
|
||
getActive() { | ||
return this._clean( | ||
Event.find({ | ||
published: true, | ||
endTime: { | ||
$gte: new Date(), | ||
}, | ||
}) | ||
.sort([['startTime', 1]]) | ||
.lean(), | ||
); | ||
} | ||
|
||
getPast() { | ||
return this._clean( | ||
Event.find({ | ||
published: true, | ||
endTime: { | ||
$lt: new Date(), | ||
}, | ||
}) | ||
.sort([['endTime', -1]]) | ||
.lean(), | ||
); | ||
} | ||
|
||
async getAll() { | ||
return this._clean(Event.find().lean()); | ||
} | ||
|
||
async _clean(promise) { | ||
const result = await promise; | ||
if (Array.isArray(result)) { | ||
const resulitemesult.map((item) => { | ||
return this._cleanOne(item); | ||
}); | ||
returnitemts.filter((item) => item !== null); | ||
} | ||
return this._cleanOne(result); | ||
} | ||
|
||
getActive() { | ||
return this._clean( | ||
Event.find({ | ||
published: true, | ||
endTime: { | ||
$gte: new Date(), | ||
}, | ||
}) | ||
.sort([['startTime', 1]]) | ||
.lean() | ||
) | ||
} | ||
_cleanOne(event) { | ||
if (!event) return null; | ||
|
||
getPast() { | ||
return this._clean( | ||
Event.find({ | ||
published: true, | ||
endTime: { | ||
$lt: new Date(), | ||
}, | ||
}) | ||
.sort([['endTime', -1]]) | ||
.lean() | ||
) | ||
/** If it's a public event, anyone can see it */ | ||
if (event.published) { | ||
// TODO: Maybe strip some fields from the response if necessary? | ||
return event; | ||
} | ||
|
||
async getAll() { | ||
return this._clean(Event.find().lean()) | ||
if (this.isAdmin) { | ||
return event; | ||
} | ||
|
||
async _clean(promise) { | ||
const result = await promise | ||
if (Array.isArray(result)) { | ||
const results = result.map(item => { | ||
return this._cleanOne(item) | ||
}) | ||
return results.filter(item => item !== null) | ||
} | ||
return this._cleanOne(result) | ||
/** If the user is an organiser or admin, they can see it */ | ||
const isOwner = | ||
this.requestingUser && this.requestingUser.sub === event.owner; | ||
const isOrganiser = | ||
this.requestingUser && | ||
event.organisers.indexOf(this.requestingUser.sub) !== -1; | ||
if (isOwner || isOrganiser) { | ||
return event; | ||
} | ||
|
||
_cleanOne(event) { | ||
if (!event) return null | ||
|
||
/** If it's a public event, anyone can see it */ | ||
if (event.published) { | ||
// TODO: Maybe strip some fields from the response if necessary? | ||
return event | ||
} | ||
|
||
if (this.isAdmin) { | ||
return event | ||
} | ||
|
||
/** If the user is an organiser or admin, they can see it */ | ||
const isOwner = | ||
this.requestingUser && this.requestingUser.sub === event.owner | ||
const isOrganiser = | ||
this.requestingUser && | ||
event.organisers.indexOf(this.requestingUser.sub) !== -1 | ||
if (isOwner || isOrganiser) { | ||
return event | ||
} | ||
|
||
/** Otherwise return null for now */ | ||
return null | ||
} | ||
/** Otherwise return null for now */ | ||
return null; | ||
} | ||
} | ||
|
||
module.exports = EventController | ||
module.exports = EventController; |
Oops, something went wrong.