|
|
@@ -1,14 +1,14 @@
|
|
|
const _ = require('lodash')
|
|
|
const moment = require('moment-immutable')
|
|
|
const { getWeeks, formatDate, parseDate } = require('../dates')
|
|
|
-const { StaffMember, Workday, Location, Labor, sequelize } = require('../database')
|
|
|
+const { StaffMember, Workday, Terminal, Labor, sequelize } = require('../database')
|
|
|
const { Op } = require('sequelize')
|
|
|
|
|
|
const list = async (req, res) => {
|
|
|
- const locationKey = req.params.location
|
|
|
- const location = await Location.findOne({where: {key: locationKey}})
|
|
|
- if (!location) return res.status(404).end()
|
|
|
- const workdays = await Workday.findAll({where: { locationId: location.id }})
|
|
|
+ const terminalKey = req.params.terminal
|
|
|
+ const terminal = await Terminal.findOne({where: {key: terminalKey}})
|
|
|
+ if (!terminal) return res.status(404).end()
|
|
|
+ const workdays = await Workday.findAll({where: { terminalId: terminal.id }})
|
|
|
|
|
|
let workweeks = _.groupBy(workdays, d => formatDate(moment(d.date).startOf('week')))
|
|
|
|
|
|
@@ -47,12 +47,12 @@ const list = async (req, res) => {
|
|
|
}
|
|
|
|
|
|
const get = async (req, res) => {
|
|
|
- const locationKey = req.params.location
|
|
|
- const location = await Location.findOne({where: {key: locationKey}})
|
|
|
+ const terminalKey = req.params.terminal
|
|
|
+ const terminal = await Terminal.findOne({where: {key: terminalKey}})
|
|
|
const week = parseDate(req.params.week)
|
|
|
const workdays = await Workday.findAll({
|
|
|
where: {
|
|
|
- locationId: location.id,
|
|
|
+ terminalId: terminal.id,
|
|
|
date: {
|
|
|
[Op.gte]: week,
|
|
|
[Op.lte]: moment(week).endOf('week')
|
|
|
@@ -76,7 +76,7 @@ const get = async (req, res) => {
|
|
|
const staffMembers = await StaffMember.findAll({
|
|
|
where: {
|
|
|
[Op.or]: [
|
|
|
- { locationId: location.id },
|
|
|
+ { terminalId: terminal.id },
|
|
|
{
|
|
|
id: {
|
|
|
[Op.in]: extraStaffMembers
|
|
|
@@ -107,7 +107,7 @@ const get = async (req, res) => {
|
|
|
staffMemberId: sm.staffMemberId,
|
|
|
hours: (sm.regularHours + sm.overtimeHours) || null
|
|
|
}))
|
|
|
- // Restore any staffMembers that are no longer assigned this location
|
|
|
+ // Restore any staffMembers that are no longer assigned this terminal
|
|
|
labor.forEach(l => {
|
|
|
if (!staffMembersById[l.staffMemberId]) {
|
|
|
wd.labor.push(l)
|
|
|
@@ -124,12 +124,12 @@ const get = async (req, res) => {
|
|
|
const patch = async (req, res) => {
|
|
|
const transaction = await sequelize.transaction()
|
|
|
try {
|
|
|
- const locationKey = req.params.location
|
|
|
- const location = await Location.findOne({where: {key: locationKey}})
|
|
|
+ const terminalKey = req.params.terminal
|
|
|
+ const terminal = await Terminal.findOne({where: {key: terminalKey}})
|
|
|
const week = parseDate(req.params.week)
|
|
|
const workdays = await Workday.findAll({
|
|
|
where: {
|
|
|
- locationId: location.id,
|
|
|
+ terminalId: terminal.id,
|
|
|
date: {
|
|
|
[Op.gte]: week,
|
|
|
[Op.lt]: moment(week).endOf('week')
|
|
|
@@ -153,7 +153,7 @@ const patch = async (req, res) => {
|
|
|
const staffMembers = await StaffMember.findAll({
|
|
|
where: {
|
|
|
[Op.or]: [
|
|
|
- { locationId: location.id },
|
|
|
+ { terminalId: terminal.id },
|
|
|
{
|
|
|
id: {
|
|
|
[Op.in]: extraStaffMembers
|
|
|
@@ -171,7 +171,7 @@ const patch = async (req, res) => {
|
|
|
for (let day = week, i = 0; i < 7; i++, day = day.add(1, 'day')) {
|
|
|
const wd = workdaysByKey[formatDate(day)]
|
|
|
allWorkdays[i] = wd || Workday.build({
|
|
|
- locationId: location.id,
|
|
|
+ terminalId: terminal.id,
|
|
|
date: week.add(i, 'day')
|
|
|
})
|
|
|
}
|
|
|
@@ -181,7 +181,7 @@ const patch = async (req, res) => {
|
|
|
const laborByStaffMember = _.chain(labor).map(l => [l.staffMemberId, l]).fromPairs().value()
|
|
|
// Map from staffMembers to preserve sorting
|
|
|
wd.labor = staffMembers.map(sm => laborByStaffMember[sm.id] || Labor.build({staffMemberId: sm.id, workdayId: wd.id}))
|
|
|
- // Restore any staffMembers that are no longer assigned this location
|
|
|
+ // Restore any staffMembers that are no longer assigned this terminal
|
|
|
labor.forEach(l => {
|
|
|
if (!staffMembersById[l.staffMemberId]) {
|
|
|
wd.labor.push(l)
|