Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Menu changed warning #142

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
24 changes: 24 additions & 0 deletions src/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
prettyPrint,
processMessage,
getMomentForMenu,
haveMenusChanged,
} from './utils'
import { getUsersInChannel, getTodaysMessages, addPost } from './slack'
import { TEXTS } from './texts'
Expand Down Expand Up @@ -36,6 +37,18 @@ export async function loadUsers() {
}
}

export async function loadMorningMenus(office) {
if (config.dev) {
return
}

logger.devLog(`Making first menu load (${office.id})`)

const menus = await getAllMenus(office)

office.restaurants.forEach((restaurant, index) => restaurant.morningMenu = menus[index])
}

export async function makeLastCall(office) {
if (config.dev) {
return
Expand All @@ -51,12 +64,23 @@ export async function makeLastCall(office) {

const message = `${office.getText(TEXTS.LAST_CALL)}\n${menus}`

const changedMenus = haveMenusChanged(office.restaurants, menus)

const changedMenusMessage = `Pozor! Evidujeme zmeny v menu (oproti ${office.firstMenuLoad.hour}:${office.firstMenuLoad.minute}) v nasledujucich restauraciach: ${changedMenus.map(r => r.name).join(', ')}`

for (let user of users) {
if (!messages.some(({ user: userId }) => userId === user.user_id)) {
addPost(
user.channel_id,
message,
)
} else {
if (changedMenus.length) {
addPost(
user.channel_id,
changedMenusMessage
)
}
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions src/offices.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ const officesData = [
hour: 9,
minute: 15,
},
firstMenuLoad: {
hour: 6,
minute: 0,
},
morningMenu: null,
lang: LANG.SK,
help: '',
},
Expand All @@ -43,6 +48,10 @@ const officesData = [
hour: 9,
minute: 30,
},
firstMenuLoad: {
hour: 6,
minute: 0,
},
lang: LANG.SK,
help: '',
},
Expand Down
10 changes: 5 additions & 5 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import moment from 'moment-timezone'

import { slack, logger } from './resources'
import { startExpress } from './routes'
import { loadUsers, messageReceived, processTodaysOrders, endOfOrders, makeLastCall } from './actions'
import { loadUsers, messageReceived, processTodaysOrders, endOfOrders, makeLastCall, loadMorningMenus } from './actions'
import offices from './offices'

let wasDST = null
Expand All @@ -30,10 +30,10 @@ function reschedule() {
jobs = [
getJob({ hour: 8, minute: 30 }, loadUsers),
offices.map(office => [
office.lastCall && getJob(office.lastCall, makeLastCall.bind(null, office)),
office.restaurants.map(
restaurant => restaurant.endOfOrders &&
getJob(restaurant.endOfOrders, endOfOrders.bind(null, office, restaurant)),
office.firstMenuLoad && getJob(office.firstMenuLoad, loadMorningMenus.bind(null, office)),
office.lastCall && getJob(office.lastCall, makeLastCall.bind(null, office)), // TODO: send warnings if menus changes from 6:00 menus
office.restaurants.filter(r => !!r.endOfOrders).map(
restaurant => getJob(restaurant.endOfOrders, endOfOrders.bind(null, office, restaurant)),
),
]),
].flat(Infinity).filter(Boolean)
Expand Down
12 changes: 12 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,3 +284,15 @@ export async function getAllMenus(office) {

return menus.filter(Boolean).join('\n\n')
}

export function haveMenusChanged(restaurants, currentMenus) {
const changedRestaurantMenus = []

restaurants.forEach((restaurant, index) => {
if (restaurant.morningMenu !== currentMenus[index]) {
changedRestaurantMenus.push(restaurant)
}
})

return changedRestaurantMenus
}