Skip to content

Commit

Permalink
WIP: Edit Event Frontend (#118)
Browse files Browse the repository at this point in the history
* Mutation for edit events #21

* Fixing another bit of sample data

* Edit seminar mutation created

* Removing unused functions

* Fixed duplicates in newsfeed

* Posting seminar is works

* Updating EventUpdateInput to have required fields

* start of edit event page, store stuff should be finished.

* more stuff for edit event page

* Fixing EventUpdateInput

* edit events work now

* starting on editing seminar

* Fix to seminar type including #121

* fixed an error with merge conflict

* edit seminars should work now

* google location button on event and seminar works

* location, website, location links now is pushed to db and is called when editting

* changed the google maps link icon

* error messages are put in alert for create events and seminars

* capacity boxes only visible when the respective option is selected

* create event now has error messages, but seminar should already have it so nothing was changed there

* capacity boxes only visible when the respective option is selected now works for SEMINAR TOO

* capacity types are on their own line, cancel button gives the right message all this applies to both events and seminar in theory

* Fixing escaping of '?'s in event fields for edit

* added hover for google maps location link for both events and seminars

* location links are not viewable in edits for both events and seminar

* fixed createSeminar errors that unable you to create seminars
  • Loading branch information
VivianNg authored and alliyya committed Nov 28, 2018
1 parent 24e5c66 commit 189aa16
Show file tree
Hide file tree
Showing 19 changed files with 882 additions and 270 deletions.
16 changes: 3 additions & 13 deletions api/src/migrations/002_create_sample_data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,13 @@ INSERT INTO doom_user (first_name, last_name, email, privacy_settings, password_
INSERT INTO doom_user (first_name, last_name, email, privacy_settings, password_hash) VALUES ('Thor','Odinson','[email protected]', 'Private','$2b$12$rF.modokh7SuBpRHdg/xA.KN/zYvx0HmaxasTXmzWIiWcLPlhfWH2');
INSERT INTO doom_user (first_name, last_name, email, privacy_settings, password_hash) VALUES ('Loki','Laufeyson','[email protected]', 'Private','$2b$12$rF.modokh7SuBpRHdg/xA.KN/zYvx0HmaxasTXmzWIiWcLPlhfWH2');


-- 3 events
insert into Event (creator_id, name, start_time, end_time, capacity_type, max_capacity, current_capacity, website, location_link) values (3,'CUSEC',TIMESTAMP '2019-10-23 8:30:00+02', TIMESTAMP '2019-11-27 10:30:00+02', 'FCFS_E',6,0,'http://2018.cusec.net/','https://goo.gl/maps/w38KNq2BNLB2');
INSERT INTO Event (creator_id, name, start_time, end_time, capacity_type, max_capacity) VALUES (2,'Animefest',TIMESTAMP '2019-10-21 12:30:00+02', TIMESTAMP '2019-11-29 10:30:00+02', 'FCFS_P',3);
INSERT INTO Event (creator_id, name, start_time, end_time, capacity_type) VALUES (1,'Doomsday convention',TIMESTAMP '2018-10-30 1:00:00', TIMESTAMP '2018-11-02 10:30:00', 'FFA');
INSERT INTO Event (creator_id, name, start_time, end_time, capacity_type) VALUES (1,'Doomsday convention',TIMESTAMP '2018-09-04 1:00:00', TIMESTAMP '2018-12-13 10:30:00', 'FFA');
INSERT INTO Event (creator_id, name, start_time, end_time, capacity_type,max_capacity) VALUES (6,'Avenger Meetup',TIMESTAMP '2019-05-03 10:30:00', TIMESTAMP '2019-11-24 10:30:00', 'FCFS_E',5);
INSERT INTO Event (creator_id, name, start_time, end_time, capacity_type,max_capacity) VALUES (5,'Wizarding War',TIMESTAMP '2018-11-03 10:30:00', TIMESTAMP '2019-12-24 10:30:00', 'FCFS_P',10);


insert into Event_Organizer (user_id, event_id) values (3,1);
insert into Event_Organizer (user_id, event_id) values (2,2);
insert into Event_Organizer (user_id, event_id) values (1,3);
Expand All @@ -30,8 +28,7 @@ insert into Event_Organizer (user_id, event_id) values (3,4);
insert into Event_Organizer (user_id, event_id) values (5,5);



INSERT INTO Seminar (event_id, name, start_time, end_time, capacity_type) VALUES (3,'Preparing for your doom',TIMESTAMP '2019-10-20 10:30:00+02', TIMESTAMP '2019-11-20 12:30:00+02', 'FFA');
INSERT INTO Seminar (event_id, name, start_time, end_time, capacity_type) VALUES (3,'Preparing for your doom',TIMESTAMP '2018-09-10 10:30:00+02', TIMESTAMP '2018-10-22 12:30:00+02', 'FFA');
INSERT INTO Seminar (event_id, name, start_time, end_time, capacity_type, max_capacity) VALUES (2,'How to Weeb',TIMESTAMP '2019-10-22 12:30:00+02', TIMESTAMP '2019-11-22 13:30:00+02', 'FCFS_E',2);
INSERT INTO Seminar (event_id, name, start_time, end_time, capacity_type, max_capacity) VALUES (1,'Hackathon',TIMESTAMP '2019-10-24 8:30:00+02', TIMESTAMP '2019-11-24 10:30:00+02', 'FCFS_P',6);
INSERT INTO Seminar (event_id, name, start_time, end_time, capacity_type, max_capacity) VALUES (3,'Alpha Demo',TIMESTAMP '2018-10-31 14:50:00', TIMESTAMP '2018-10-31 15:00:00', 'FCFS_P',5);
Expand Down Expand Up @@ -64,7 +61,7 @@ INSERT INTO Event_Participation (user_id, event_id, attending) VALUES (9,4,True)
INSERT INTO Event_Participation (user_id, event_id, following) VALUES (10,4,True);

-- Adding users to seminars
INSERT INTO Seminar_Participation (user_id, seminar_id, attending) VALUES (2,2,True);
INSERT INTO Seminar_Participation (user_id, seminar_id, attending) VALUES (1,2,True);
INSERT INTO Seminar_Participation (user_id, seminar_id, following) VALUES (3,2,True);
INSERT INTO Seminar_Participation (user_id, seminar_id, attending) VALUES (5,2,True);

Expand All @@ -79,14 +76,12 @@ INSERT INTO Seminar_Participation (user_id, seminar_id, attending) VALUES (5,5,T
INSERT INTO Seminar_Wait_list (user_id, seminar_id,date_added) VALUES (3,2, TIMESTAMP '2019-10-23 8:30:00+02');
INSERT INTO Event_Wait_list (user_id, event_id,date_added) VALUES (10,2, TIMESTAMP '2019-10-29 8:30:00+02');


-- 2 Annoucements
INSERT INTO Event_Announcement (event_id, message, date_created, date_modified) VALUES (3,'The end is nigh, remember your wallet',TIMESTAMP '2018-10-31 8:30:00', TIMESTAMP '2018-10-31 19:00:00');
INSERT INTO Event_Announcement (event_id, message, date_created, date_modified) VALUES (3,'Remember your doom',TIMESTAMP '2018-10-31 8:30:00', TIMESTAMP '2018-11-01 8:45:00');
INSERT INTO Seminar_Announcement (seminar_id, message, date_created, date_modified) VALUES (4,'Bug Fixing Party',TIMESTAMP '2018-10-31 8:30:00', TIMESTAMP '2018-10-13 8:30:00');
INSERT INTO Seminar_Announcement (seminar_id, message, date_created, date_modified) VALUES (3,'Reminder to participants to bring laptops',TIMESTAMP '2018-10-23 8:30:00+02', TIMESTAMP '2018-10-23 8:30:00+02');


-- One user following
INSERT INTO User_Following (user_id, following_user_id) VALUES (4,1);

Expand All @@ -95,8 +90,6 @@ DO
$do$
BEGIN
FOR i IN 1..5 LOOP


UPDATE "event"
SET current_capacity =subquery.count
FROM
Expand All @@ -112,9 +105,6 @@ BEGIN
FROM seminar_participation
WHERE seminar_id = i AND attending = true) AS subquery
WHERE seminar.id=i;



END LOOP;
END
$do$;
Expand Down
165 changes: 162 additions & 3 deletions api/src/mutations/event.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { db } = require("../db");
const { queryEventByID } = require("../resolvers/event");
const { queryEventByID, getEventByID } = require("../resolvers/event");
const { querySeminarsByEventID } = require("../resolvers/seminar");
const { alreadyAttendingEvent } = require("../resolvers/user");
const {
Expand Down Expand Up @@ -102,6 +102,165 @@ async function insertNewEvent(eventInput) {
};
}

/*
NOTE: Remember she'll always give me the whole object
*/
async function updateEvent(eventid, event) {
let {
name,
description,
location,
picture_path,
website,
location_link,
start_time,
end_time,
capacity_type,
max_capacity,
organizer_ids
} = event;

let simpleEvent = {
name,
description,
location,
picture_path,
website,
location_link
};

if (Object.keys(simpleEvent).length === 0) return;

let queryString = `UPDATE event SET`;
let first = 1;
const vals = [];
// Adding basic properties of simpleEvent to queryString
for (var key in simpleEvent) {
if (simpleEvent[key] !== null && simpleEvent[key] !== undefined) {
if (!first) queryString = `${queryString}, `;
first = 0;
queryString = `${queryString} ${key} = ?`;
vals.push(simpleEvent[key]);
}
}
originalEvent = await getEventByID(eventid);

// Validating the event start time with its end time
const event_start_time = new Date(start_time);
const event_end_time = new Date(end_time);

if (event_start_time > event_end_time) {
console.log("Invalid Start Time: Event cannot start after Event ends");
return new Error(
"Unable to create a Event: Invalid Start Time: Event cannot start after Event ends"
);
}
// Validating the event start time/end time with its seminar's start/end
earliestStart = null;
latestEnd = null;
firstSem = null;
lastSem = null;
originalEvent["seminars"].forEach(seminar => {
if (
earliestStart == null ||
Date.parse(seminar.start_time) < earliestStart
) {
earliestStart = seminar.start_time;
firstSem = seminar.name;
}
if (latestEnd == null || Date.parse(seminar.end_time) > latestEnd) {
latestEnd = seminar.end_time;
lastSem = seminar.name;
}
});

if (!earliestStart || !latestEnd) {
console.log("Life is a scam");
} else if (event_start_time > Date.parse(earliestStart)) {
return new Error(
"Invalid Event start date: First Seminar: ",
firstSem,
" starts at ",
earliestStart,
", event must start prior"
);
} else if (event_end_time < Date.parse(latestEnd)) {
return new Error(
"Invalid Event end date: Last Seminar: " +
lastSem +
" ends at " +
latestEnd +
"+ event must end after"
);
}
if (max_capacity && max_capacity < originalEvent["current_capacity"]) {
return new Error("Max Capacity cannot be less than Current Capacity");
}
queryString = `${queryString}, start_time = '${event_start_time.toISOString()}'`;
queryString = `${queryString}, end_time = '${event_end_time.toISOString()}'`;
queryString = `${queryString}, capacity_type = '${capacity_type}'`;

if (capacity_type == "FFA") {
max_capacity = null;
queryString = `${queryString}, max_capacity = ${max_capacity}`;
} else {
queryString = `${queryString}, max_capacity = '${max_capacity}'`;
}
queryString = `${queryString} WHERE id = ? RETURNING *;`;
vals.push(eventid);
await db.raw(queryString, vals);

// Check if there are people on the waitlist and updates accordingly
if (
max_capacity == null ||
(max_capacity > originalEvent["max_capacity"] &&
originalEvent["max_capacity"] == originalEvent["current_capacity"])
) {
var top = await getWaitlistTop(eventid);
var newCapacity = await updateCurrentCapacity(eventid);
while (
(top && max_capacity == null) ||
(top && newCapacity < max_capacity)
) {
if (top) {
await updateEventWaitlist(top, eventid, false);
await updateEventParticipation({
userid: top,
participationType: "ATTENDING",
eventid: eventid
});
}
top = await getWaitlistTop(eventid);
newCapacity = await updateCurrentCapacity(eventid);
}
}
await updateCurrentCapacity(eventid);

var oldOrganizerIDs = [];
originalEvent["organizers"].forEach(organizer => {
oldOrganizerIDs.push(organizer.id);
});

// NOTE: revise this method to make use of set operations
// Simplest sol'n atm was remove all organizers then add them all back
// Cannot remove creator of event
for (var i = 0; i < oldOrganizerIDs.length; i++) {
const queryString = `delete from event_organizer where user_id = ? and event_id = ?; `;
if (originalEvent["creator_id"] != oldOrganizerIDs[i]) {
await db.raw(`${queryString}`, [oldOrganizerIDs[i], eventid]);
}
}

for (var i = 0; i < organizer_ids.length; i++) {
const queryString = `INSERT INTO Event_Organizer
(user_id,event_id) VALUES (?, ?); `;
if (originalEvent["creator_id"] != organizer_ids[i]) {
await db.raw(`${queryString}`, [organizer_ids[i], eventid]);
}
}
return;
}

async function updateCurrentCapacity(eventid) {
// Will recalculate and update the table's current capacity
var queryString = `UPDATE "event"
Expand All @@ -118,7 +277,6 @@ async function updateCurrentCapacity(eventid) {
async function getWaitlistTop(eventid) {
// Returns the id of the user who's at the top of the waitlist given an eventid
var queryString = null;
var userid = null;
queryString = `SELECT user_id FROM event_wait_list WHERE event_id=? ORDER BY date_added LIMIT 1;`;
vals = [eventid];
const res = await db.raw(`${queryString}`, vals);
Expand Down Expand Up @@ -245,5 +403,6 @@ async function updateEventParticipation(
module.exports = {
insertNewEvent,
updateEventParticipation,
updateEventWaitlist
updateEventWaitlist,
updateEvent
};
30 changes: 28 additions & 2 deletions api/src/mutations/index.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
const {
insertNewEvent,
updateEventParticipation,
updateEventWaitlist
updateEventWaitlist,
updateEvent
} = require("./event");
const {
insertNewSeminar,
updateSeminarParticipation,
updateSeminarWaitlist
updateSeminarWaitlist,
updateSeminar
} = require("./seminar");
const {
registerUser,
editUserProfile,
updateUserFollowing
} = require("./user");
const { insertNewAnnouncement } = require("./announcement");
const { getEventByID } = require("../resolvers/event");
const { getSeminarByID } = require("../resolvers/seminar");

const mutations = {
Mutation: {
Expand Down Expand Up @@ -151,6 +155,28 @@ const mutations = {
return new Error("Unable to update user profile");
}
},
async editEvent(_, args) {
const { eventID, event } = args;
try {
errors = await updateEvent(eventID, event);
if (errors) return errors;
return await getEventByID(eventID);
} catch (err) {
console.log(err);
return new Error("Unable to update event");
}
},
async editSeminar(_, args) {
const { seminarID, seminar } = args;
try {
errors = await updateSeminar(seminarID, seminar);
if (errors) return errors;
return await getSeminarByID(seminarID);
} catch (err) {
console.log(err);
return new Error("Unable to update seminar");
}
},
async followUser(_, args) {
try {
const { userID, followingID } = args;
Expand Down
Loading

0 comments on commit 189aa16

Please sign in to comment.