Skip to content

Commit

Permalink
Deployment Updates (#537)
Browse files Browse the repository at this point in the history
* temp updates

* updates

* updates

* updates

* wip update

* updates

* fixture update

* updates

* updates

* update

* fix for render speed

* lint fixes
  • Loading branch information
sjmcculloch authored Jul 9, 2018
1 parent 1454724 commit 1a0bd3a
Show file tree
Hide file tree
Showing 67 changed files with 2,569 additions and 901 deletions.
136 changes: 82 additions & 54 deletions server/src/creators.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,21 @@ import { DISTANT_FUTURE } from './constants';
// returns a set of creators bound to the given models
export const getCreators = (models) => {
const {
Organisation, Group, User, Tag, Device, Event,
Schedule, TimeSegment, EventResponse, EventLocation, Message,
Organisation,
Group,
User,
Tag,
Device,
Event,
Schedule,
TimeSegment,
EventResponse,
EventLocation,
Message,
} = models;

return {
organisation: ({ name }) =>
Organisation.create({ name }),
organisation: ({ name }) => Organisation.create({ name }),

schedule: ({ name, details, type, priority, startTime, endTime, group, tags }) => {
if (!group || !group.id) {
Expand All @@ -31,13 +39,16 @@ export const getCreators = (models) => {
startTime,
endTime,
groupId: group.id,
}).then(schedule => Promise.all([
tags && tags.map(
t => Tag.findById(t.id).then((foundTag) => {
foundTag.addSchedule(schedule);
}),
),
]).then(() => schedule.reload()));
}).then(schedule =>
Promise.all([
tags &&
tags.map(t =>
Tag.findById(t.id).then((foundTag) => {
foundTag.addSchedule(schedule);
}),
),
]).then(() => schedule.reload()),
);
},

event: ({ name, details, sourceIdentifier, permalink, priority, group }) => {
Expand All @@ -51,7 +62,7 @@ export const getCreators = (models) => {
permalink,
priority,
groupId: group.id,
startTime: Math.floor((new Date()).getTime() / 1000),
startTime: Math.floor(new Date().getTime() / 1000),
endTime: DISTANT_FUTURE, // no end time
});
},
Expand Down Expand Up @@ -91,12 +102,12 @@ export const getCreators = (models) => {
name,
icon: icon || 'group',
organisationId: organisation.id,
}).then(group => Promise.all([
group.addUsers(users),
tags && tags.map(
t => Tag.findById(t.id).then(foundTag => foundTag.addGroup(group)),
),
]).then(() => group.reload()));
}).then(group =>
Promise.all([
group.addUsers(users),
tags && tags.map(t => Tag.findById(t.id).then(foundTag => foundTag.addGroup(group))),
]).then(() => group.reload()),
);
},

user: ({ id, username, password, email, displayName, version, organisation, tags }) => {
Expand All @@ -113,16 +124,15 @@ export const getCreators = (models) => {
email,
version,
organisationId: organisation.id,
}).then(user => Promise.all([
tags && tags.map(
t => Tag.findById(t.id).then(foundTag => foundTag.addUser(user)),
),
]).then(() => user.reload()),
}).then(user =>
Promise.all([
tags && tags.map(t => Tag.findById(t.id).then(foundTag => foundTag.addUser(user))),
]).then(() => user.reload()),
),
);
},

timeSegment: ({ status, startTime, endTime, schedule, user }) => {
timeSegment: ({ type, status, startTime, endTime, schedule, user, note, tags }) => {
if (!user || !user.id) {
return Promise.reject(Error('Must pass user'));
}
Expand All @@ -135,13 +145,23 @@ export const getCreators = (models) => {
if (endTime < startTime) {
return Promise.reject(Error('endTime must be greater than startTime'));
}

return TimeSegment.create({
type,
status,
startTime,
endTime,
scheduleId: schedule.id,
userId: user.id,
});
note,
}).then(timeSegment =>
Promise.all([
tags &&
tags.map(t =>
Tag.findById(t.id).then(foundTag => foundTag.addTimesegment(timeSegment)),
),
]).then(() => timeSegment.reload()),
);
},

eventResponse: ({
Expand Down Expand Up @@ -170,7 +190,7 @@ export const getCreators = (models) => {
locationTime,
userId: user.id,
eventId: event.id,
eventlocationId: (destination ? destination.id : null),
eventlocationId: destination ? destination.id : null,
});
},
eventLocation: ({
Expand All @@ -192,7 +212,7 @@ export const getCreators = (models) => {
icon,
locationLatitude,
locationLongitude,
locationTime: locationTime || (new Date()).getTime() / 1000,
locationTime: locationTime || new Date().getTime() / 1000,
eventId: event.id,
}).then((location) => {
if (primaryLocation) {
Expand All @@ -209,42 +229,50 @@ export const getCreators = (models) => {
}

if (groupId) {
futs.push(Group.findById(groupId).then((group) => {
if (!group) {
return Promise.reject(Error('Must pass valid group ID'));
}
return group;
}));
futs.push(
Group.findById(groupId).then((group) => {
if (!group) {
return Promise.reject(Error('Must pass valid group ID'));
}
return group;
}),
);
}
if (eventId) {
futs.push(Event.findById(eventId).then((event) => {
if (!event) {
return Promise.reject(Error('Must pass valid event ID'));
}
return event;
}));
futs.push(
Event.findById(eventId).then((event) => {
if (!event) {
return Promise.reject(Error('Must pass valid event ID'));
}
return event;
}),
);
}
if (scheduleId) {
futs.push(Schedule.findById(scheduleId).then((schedule) => {
if (!schedule) {
return Promise.reject(Error('Must pass valid schedule ID'));
}
return schedule;
}));
futs.push(
Schedule.findById(scheduleId).then((schedule) => {
if (!schedule) {
return Promise.reject(Error('Must pass valid schedule ID'));
}
return schedule;
}),
);
}
if (futs.length !== 1) {
return Promise.reject(Error('must pass exactly one of groupId, eventId, scheduleId'));
}

return Promise.all(futs).then(() => Message.create({
text,
image,
groupId,
eventId,
scheduleId,
userId: user && user.id,
edited: false,
}));
return Promise.all(futs).then(() =>
Message.create({
text,
image,
groupId,
eventId,
scheduleId,
userId: user && user.id,
edited: false,
}),
);
},
};
};
Expand Down
70 changes: 67 additions & 3 deletions server/src/logic.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,45 @@ export const getHandlers = ({ models, creators: Creators, push, pubsub }) => {
});
});
},
updateSchedule(_, args, ctx) {
const {
id, name, details, type, priority,
startTime, endTime, groupId, tags,
} = args.schedule;
return getAuthenticatedUser(ctx).then(() =>
Schedule.findById(id).then((schedule) => {
if (!schedule) {
return Promise.reject(Error('Invalid schedule!'));
}
return schedule
.update({
name,
details,
type,
priority,
startTime,
endTime,
groupId,
})
.then(() =>
Promise.all([
tags &&
schedule.getTags().then(ts =>
ts.forEach((t) => {
const tagRemove = tags.find(tag => tag.id === t.id);
if (tagRemove === undefined) {
t.removeSchedule(schedule);
}
}),
),
tags &&
tags.map(t => Tag.findById(t.id)
.then(foundTag => foundTag.addSchedule(schedule))),
]).then(() => schedule.reload()),
);
}),
);
},
messages(schedule) {
return schedule.getMessages({
order: [['createdAt', 'DESC']],
Expand All @@ -280,19 +319,27 @@ export const getHandlers = ({ models, creators: Creators, push, pubsub }) => {
user(timesegment) {
return timesegment.getUser();
},
tags(timesegment) {
return timesegment.getTags();
},
createTimeSegment(_, args, ctx) {
const { scheduleId, status, startTime, endTime, userId } = args.timeSegment;
const {
scheduleId, type, status, startTime, endTime, userId, note, tags,
} = args.timeSegment;
return getAuthenticatedUser(ctx).then(user =>
Schedule.findById(scheduleId).then((schedule) => {
if (!schedule) {
return Promise.reject(Error('Invalid schedule!'));
}
return Creators.timeSegment({
schedule,
type,
status,
startTime,
endTime,
user: userId === undefined ? user : { id: userId },
note,
tags,
});
}),
);
Expand All @@ -314,17 +361,34 @@ export const getHandlers = ({ models, creators: Creators, push, pubsub }) => {
);
},
updateTimeSegment(_, args, ctx) {
const { segmentId, status, startTime, endTime } = args.timeSegment;
const { segmentId, type, status, startTime, endTime, note, tags } = args.timeSegment;
return getAuthenticatedUser(ctx).then(() =>
TimeSegment.findById(segmentId).then((segment) => {
if (!segment) {
return Promise.reject(Error('Invalid segment!'));
}
return segment.update({
type,
status,
startTime,
endTime,
});
note,
}).then(() =>
Promise.all([
tags &&
segment.getTags().then(ts =>
ts.forEach((t) => {
const tagRemove = tags.find(tag => tag.id === t.id);
if (tagRemove === undefined) {
t.removeTimesegment(segment);
}
}),
),
tags &&
tags.map(t => Tag.findById(t.id)
.then(foundTag => foundTag.addTimesegment(segment))),
]).then(() => segment.reload()),
);
}),
);
},
Expand Down
5 changes: 5 additions & 0 deletions server/src/models-mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export const defineModels = () => {
});

const TimeSegmentModel = db.define('timesegment', {
type: 'availability',
status: 'available',
startTime: 1514860289,
endTime: 1514860289 + (60 * 60),
Expand Down Expand Up @@ -133,6 +134,10 @@ export const defineModels = () => {
GroupModel.belongsToMany(TagModel, { through: 'group_tag' });
TagModel.belongsToMany(GroupModel, { through: 'group_tag' });

// timeSegments > tags
TimeSegmentModel.belongsToMany(TagModel, { through: 'timesegment_tag' });
TagModel.belongsToMany(TimeSegmentModel, { through: 'timesegment_tag' });

// schedules -> tags
ScheduleModel.belongsToMany(TagModel, { through: 'schedule_tag' });
TagModel.belongsToMany(ScheduleModel, { through: 'schedule_tag' });
Expand Down
6 changes: 6 additions & 0 deletions server/src/models.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,12 @@ export const defineModels = (db) => {
});

const TimeSegmentModel = db.define('timesegment', {
type: { type: Sequelize.STRING },
status: { type: Sequelize.INTEGER },
startTime: { type: Sequelize.INTEGER },
endTime: { type: Sequelize.INTEGER },
lastUpdate: { type: Sequelize.INTEGER },
note: { type: Sequelize.STRING },
});

const EventResponseModel = db.define('eventresponse', {
Expand Down Expand Up @@ -140,6 +142,10 @@ export const defineModels = (db) => {
ScheduleModel.belongsToMany(TagModel, { through: 'schedule_tag' });
TagModel.belongsToMany(ScheduleModel, { through: 'schedule_tag' });

// timeSegments > tags
TimeSegmentModel.belongsToMany(TagModel, { through: 'timesegment_tag' });
TagModel.belongsToMany(TimeSegmentModel, { through: 'timesegment_tag' });

// events are created for a single group
EventModel.belongsTo(GroupModel);
GroupModel.hasMany(EventModel);
Expand Down
6 changes: 6 additions & 0 deletions server/src/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ export const getResolvers = (handlers) => {
},
},
TimeSegment: {
tags(timesegment, args, ctx) {
return timeSegmentHandler.tags(timesegment, args, ctx);
},
user(timesegment, args, ctx) {
return timeSegmentHandler.user(timesegment, args, ctx);
},
Expand Down Expand Up @@ -186,6 +189,9 @@ export const getResolvers = (handlers) => {
createSchedule(_, args, ctx) {
return scheduleHandler.createSchedule(_, args, ctx);
},
updateSchedule(_, args, ctx) {
return scheduleHandler.updateSchedule(_, args, ctx);
},
createTimeSegment(_, args, ctx) {
return timeSegmentHandler.createTimeSegment(_, args, ctx);
},
Expand Down
Loading

0 comments on commit 1a0bd3a

Please sign in to comment.