|
@@ -1,7 +1,7 @@
|
|
|
const _ = require('lodash')
|
|
const _ = require('lodash')
|
|
|
const moment = require('moment-immutable')
|
|
const moment = require('moment-immutable')
|
|
|
const { getWeeks, formatDate, parseDate } = require('../dates')
|
|
const { getWeeks, formatDate, parseDate } = require('../dates')
|
|
|
-const { Retailer, Workday, Terminal, Service, sequelize } = require('../database')
|
|
|
|
|
|
|
+const { Client, Workday, Terminal, Service, sequelize } = require('../database')
|
|
|
const { Op } = require('sequelize')
|
|
const { Op } = require('sequelize')
|
|
|
|
|
|
|
|
const list = async (req, res) => {
|
|
const list = async (req, res) => {
|
|
@@ -66,25 +66,25 @@ const get = async (req, res) => {
|
|
|
}
|
|
}
|
|
|
})
|
|
})
|
|
|
const servicesByWorkday = _.groupBy(services, x => x.workdayId)
|
|
const servicesByWorkday = _.groupBy(services, x => x.workdayId)
|
|
|
- const extraRetailers = _.chain(services)
|
|
|
|
|
- .map(x => x.retailerId)
|
|
|
|
|
|
|
+ const extraClients = _.chain(services)
|
|
|
|
|
+ .map(x => x.clientId)
|
|
|
.filter(x => x)
|
|
.filter(x => x)
|
|
|
.uniq()
|
|
.uniq()
|
|
|
.value()
|
|
.value()
|
|
|
- const retailers = await Retailer.findAll({
|
|
|
|
|
|
|
+ const clients = await Client.findAll({
|
|
|
where: {
|
|
where: {
|
|
|
[Op.or]: [
|
|
[Op.or]: [
|
|
|
{ terminalId: terminal.id },
|
|
{ terminalId: terminal.id },
|
|
|
{
|
|
{
|
|
|
id: {
|
|
id: {
|
|
|
- [Op.in]: extraRetailers
|
|
|
|
|
|
|
+ [Op.in]: extraClients
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
]
|
|
]
|
|
|
},
|
|
},
|
|
|
order: [ 'name' ]
|
|
order: [ 'name' ]
|
|
|
})
|
|
})
|
|
|
- const retailersById = _.chain(retailers).map(x => [x.id, x]).fromPairs().value()
|
|
|
|
|
|
|
+ const clientsById = _.chain(clients).map(x => [x.id, x]).fromPairs().value()
|
|
|
|
|
|
|
|
// Fill in empty days
|
|
// Fill in empty days
|
|
|
const workdaysByKey = _.chain(workdays).map(wd => [formatDate(wd.date), wd]).fromPairs().value()
|
|
const workdaysByKey = _.chain(workdays).map(wd => [formatDate(wd.date), wd]).fromPairs().value()
|
|
@@ -96,17 +96,17 @@ const get = async (req, res) => {
|
|
|
|
|
|
|
|
const workdaysWithServices = allWorkdays.map(wd => {
|
|
const workdaysWithServices = allWorkdays.map(wd => {
|
|
|
const services = servicesByWorkday[wd.id] || []
|
|
const services = servicesByWorkday[wd.id] || []
|
|
|
- const serviceByRetailer = _.chain(services).map(l => [l.retailerId, l]).fromPairs().value()
|
|
|
|
|
|
|
+ const serviceByClient = _.chain(services).map(l => [l.clientId, l]).fromPairs().value()
|
|
|
// Map from staffMembers to preserve sorting
|
|
// Map from staffMembers to preserve sorting
|
|
|
- wd.services = retailers
|
|
|
|
|
- .map(sm => serviceByRetailer[sm.id] || {retailerId: sm.id})
|
|
|
|
|
|
|
+ wd.services = clients
|
|
|
|
|
+ .map(sm => serviceByClient[sm.id] || {clientId: sm.id})
|
|
|
.map(sm => ({
|
|
.map(sm => ({
|
|
|
- retailerId: sm.retailerId,
|
|
|
|
|
|
|
+ clientId: sm.clientId,
|
|
|
cartons: sm.cartons || null
|
|
cartons: sm.cartons || null
|
|
|
}))
|
|
}))
|
|
|
// Restore any staffMembers that are no longer assigned this terminal
|
|
// Restore any staffMembers that are no longer assigned this terminal
|
|
|
services.forEach(l => {
|
|
services.forEach(l => {
|
|
|
- if (!serviceByRetailer[l.retailerId]) {
|
|
|
|
|
|
|
+ if (!serviceByClient[l.clientId]) {
|
|
|
wd.services.push(l)
|
|
wd.services.push(l)
|
|
|
}
|
|
}
|
|
|
})
|
|
})
|
|
@@ -142,25 +142,25 @@ const patch = async (req, res) => {
|
|
|
}
|
|
}
|
|
|
})
|
|
})
|
|
|
const servicesByWorkday = _.groupBy(services, x => x.workdayId)
|
|
const servicesByWorkday = _.groupBy(services, x => x.workdayId)
|
|
|
- const extraRetailers = _.chain(services)
|
|
|
|
|
- .map(x => x.retailerId)
|
|
|
|
|
|
|
+ const extraClients = _.chain(services)
|
|
|
|
|
+ .map(x => x.clientId)
|
|
|
.filter(x => x)
|
|
.filter(x => x)
|
|
|
.uniq()
|
|
.uniq()
|
|
|
.value()
|
|
.value()
|
|
|
- const retailers = await Retailer.findAll({
|
|
|
|
|
|
|
+ const clients = await Client.findAll({
|
|
|
where: {
|
|
where: {
|
|
|
[Op.or]: [
|
|
[Op.or]: [
|
|
|
{ terminalId: terminal.id },
|
|
{ terminalId: terminal.id },
|
|
|
{
|
|
{
|
|
|
id: {
|
|
id: {
|
|
|
- [Op.in]: extraRetailers
|
|
|
|
|
|
|
+ [Op.in]: extraClients
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
]
|
|
]
|
|
|
},
|
|
},
|
|
|
order: [ 'name' ]
|
|
order: [ 'name' ]
|
|
|
})
|
|
})
|
|
|
- const retailersById = _.chain(retailers).map(x => [x.id, x]).fromPairs().value()
|
|
|
|
|
|
|
+ const clientsById = _.chain(clients).map(x => [x.id, x]).fromPairs().value()
|
|
|
|
|
|
|
|
// Fill in empty days
|
|
// Fill in empty days
|
|
|
const workdaysByKey = _.chain(workdays).map(wd => [formatDate(wd.date), wd]).fromPairs().value()
|
|
const workdaysByKey = _.chain(workdays).map(wd => [formatDate(wd.date), wd]).fromPairs().value()
|
|
@@ -175,12 +175,12 @@ const patch = async (req, res) => {
|
|
|
|
|
|
|
|
const workdaysWithServices = allWorkdays.map(wd => {
|
|
const workdaysWithServices = allWorkdays.map(wd => {
|
|
|
const services = servicesByWorkday[wd.id] || []
|
|
const services = servicesByWorkday[wd.id] || []
|
|
|
- const servicesByRetailer = _.chain(services).map(l => [l.retailerId, l]).fromPairs().value()
|
|
|
|
|
|
|
+ const servicesByClient = _.chain(services).map(l => [l.clientId, l]).fromPairs().value()
|
|
|
// Map from staffMembers to preserve sorting
|
|
// Map from staffMembers to preserve sorting
|
|
|
- wd.services = retailers.map(sm => servicesByRetailer[sm.id] || Service.build({retailerId: sm.id, workdayId: wd.id}))
|
|
|
|
|
|
|
+ wd.services = clients.map(sm => servicesByClient[sm.id] || Service.build({clientId: sm.id, workdayId: wd.id}))
|
|
|
// Restore any staffMembers that are no longer assigned this terminal
|
|
// Restore any staffMembers that are no longer assigned this terminal
|
|
|
services.forEach(l => {
|
|
services.forEach(l => {
|
|
|
- if (!retailersById[l.retailerId]) {
|
|
|
|
|
|
|
+ if (!clientsById[l.clientId]) {
|
|
|
wd.services.push(l)
|
|
wd.services.push(l)
|
|
|
}
|
|
}
|
|
|
})
|
|
})
|
|
@@ -190,11 +190,11 @@ const patch = async (req, res) => {
|
|
|
// Update with model
|
|
// Update with model
|
|
|
const model = req.body
|
|
const model = req.body
|
|
|
await Promise.all(model.workdays.map(async (modelWorkday, i) => {
|
|
await Promise.all(model.workdays.map(async (modelWorkday, i) => {
|
|
|
- const modelServicesById = _.chain(modelWorkday.services).map(l => [l.retailerId, l]).fromPairs().value()
|
|
|
|
|
|
|
+ const modelServicesById = _.chain(modelWorkday.services).map(l => [l.clientId, l]).fromPairs().value()
|
|
|
const workday = allWorkdays[i]
|
|
const workday = allWorkdays[i]
|
|
|
|
|
|
|
|
workday.services.forEach(service => {
|
|
workday.services.forEach(service => {
|
|
|
- const modelService = modelServicesById[service.retailerId]
|
|
|
|
|
|
|
+ const modelService = modelServicesById[service.clientId]
|
|
|
|
|
|
|
|
service.cartons = modelService.cartons || 0
|
|
service.cartons = modelService.cartons || 0
|
|
|
})
|
|
})
|