diff --git a/app/src/app.js b/app/src/app.js index 3a88822..ca5bf39 100644 --- a/app/src/app.js +++ b/app/src/app.js @@ -52,7 +52,8 @@ const { getUsernameToIdMap, getUsersInfo, confirmationMessage, - formatAttachmentsForStatusMessage + formatAttachmentsForStatusMessage, + membersHaveScheduleConflict } = require('./helpers')( fetch, url, @@ -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': @@ -231,6 +233,7 @@ async function executeCommand({ team_id, user_id, command, token }) { scheduler.reschedule([nextState]); break; } + await saveState(nextState); return { action, diff --git a/app/src/helpers.js b/app/src/helpers.js index 2d499b6..7c81e77 100644 --- a/app/src/helpers.js +++ b/app/src/helpers.js @@ -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, @@ -287,6 +319,7 @@ module.exports = ( getUsernameToIdMap, getSecretsAndSave, confirmationMessage, - formatAttachmentsForStatusMessage + formatAttachmentsForStatusMessage, + membersHaveScheduleConflict }; }; \ No newline at end of file