Skip to content
This repository was archived by the owner on Jan 23, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions app/src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ const {
getUsernameToIdMap,
getUsersInfo,
confirmationMessage,
formatAttachmentsForStatusMessage
formatAttachmentsForStatusMessage,
membersHaveScheduleConflict
} = require('./helpers')(
fetch,
url,
Expand Down Expand Up @@ -217,9 +218,10 @@ async function executeCommand({ team_id, user_id, command, token }) {
// TODO: a new state out of the reducer should already include the team_id
nextState.team_id = team_id;

await saveState(nextState);
switch (action.type) {
case 'SCHEDULE':
const allEvents = getEventsFor({ team_id })
membersHaveScheduleConflict(allEvents, action)
scheduler.add([nextState]);
break;
case 'HALT':
Expand All @@ -231,6 +233,7 @@ async function executeCommand({ team_id, user_id, command, token }) {
scheduler.reschedule([nextState]);
break;
}
await saveState(nextState);

return {
action,
Expand Down
35 changes: 34 additions & 1 deletion app/src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,38 @@ module.exports = (
}))
}])
}

function membersHaveScheduleConflict(events, { userInfos, time, frequency}, standupTimeInMinutes = 15) {
let weekends = new Set(["SATERDAYS", "SUNDAYS", "WEEKENDS"])
let weekdays = new Set(["MONDAYS", "TUESDAYS", "WEDNESDAYS", "THURSDAYS", "FRIDAYS"])

const membersOfNextEvent = new Set(userInfos.map((user) => user.user_id))
let timeOfNextEvent = (time.hh * 100) + time.mm;
events.forEach(event => {
let timeOfScheduledEvent = (event.time.hh * 100) + event.time.mm;
event.members.forEach(member => {
if (Math.abs(timeOfScheduledEvent - timeOfNextEvent) <= standupTimeInMinutes) {
if (membersOfNextEvent.has(member.user_id)) {
if (frequency === event.frequency) {
throw new customError('schedule conflict', EH1000)
}
switch (event.frequency) {
case 'EVERYDAY':
throw new customError('schedule conflict', EH1000)
case 'WEEKENDS':
if (weekends.has(frequency)) {
throw new customError('schedule conflict', EH1000)
}
case 'WEEKDAYS':
if (weekdays.has(frequency)) {
throw new customError('schedule conflict', EH1000)
}
}
}
}
})
})
}

return {
notifyUsers,
Expand All @@ -287,6 +319,7 @@ module.exports = (
getUsernameToIdMap,
getSecretsAndSave,
confirmationMessage,
formatAttachmentsForStatusMessage
formatAttachmentsForStatusMessage,
membersHaveScheduleConflict
};
};