-
Notifications
You must be signed in to change notification settings - Fork 5
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
updates to bulk ops scripts for august 22 #93
Open
leifwalsh
wants to merge
5
commits into
bedstuystrong:master
Choose a base branch
from
leifwalsh:bulk-categories
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
fb4d19f
handle general category in code rather than airtable, and check for m…
leifwalsh ee31511
updates to bulk ops scripts for august 22
leifwalsh 5b82100
bulk script changes for august 29
leifwalsh fe985db
split warehouse specialty items, two coordinators
leifwalsh b09cb6f
recent updates to bulk scripts
leifwalsh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,30 +5,96 @@ const moment = require('moment'); | |
const Mustache = require('mustache'); | ||
const yargs = require('yargs'); | ||
|
||
const { getAllRoutes, getTicketsForRoutes, getRecordsWithFilter, BULK_DELIVERY_ROUTES_TABLE } = require('../airtable'); | ||
// eslint-disable-next-line no-unused-vars | ||
const { getAllRoutes, getTicketsForRoutes, getRecordsWithFilter, reconcileOrders, BULK_DELIVERY_ROUTES_TABLE, ReconciledOrder } = require('../airtable'); | ||
const { googleMapsUrl, Email } = require('../messages'); | ||
|
||
function getEmailTemplateParameters(route, tickets) { | ||
const ticketParameterMaps = tickets.map((ticket) => { | ||
// Each week we might need custom shoppers to do some "bulk purchases" if we | ||
// couldn't procure everything we needed. The "no route" section lets us ask | ||
// every custom shopper to get some bulk items and not sort them by tickets. | ||
const noRouteSection = { | ||
items: [] | ||
}; | ||
|
||
/** | ||
* Sometimes, we have "custom items" we have on hand and don't need shoppers | ||
* to purchase, but they aren't represented in the Bulk Order table. | ||
* @param {{ item: string, quantity: number | null }} param0 Custom item. | ||
*/ | ||
const itemNeedsCustomShopping = ({ item }) => { | ||
const lowered = _.lowerCase(item); | ||
return !( | ||
_.endsWith(lowered, 'art kit') | ||
|| _.endsWith(lowered, 'art kits') | ||
|| lowered.match(/\d\s*books?\s/) !== null | ||
|| _.endsWith(lowered, 'helmet') | ||
|| _.endsWith(lowered, 'helmets') | ||
); | ||
}; | ||
|
||
const getShoppingListTemplateParameters = (route, orders) => { | ||
const allTickets = _.map(orders, (order) => { | ||
const { ticketID, vulnerability, householdSize } = order.intakeRecord[1]; | ||
const conditions = _.concat([`household size ${householdSize}`], vulnerability); | ||
const items = _.filter(order.getAdditionalItems(), itemNeedsCustomShopping); | ||
return { ticketID, conditions: _.join(conditions, ', '), items }; | ||
}); | ||
const tickets = _.filter(allTickets, ({ items }) => { | ||
return items.length > 0; | ||
}); | ||
const params = { | ||
tickets | ||
}; | ||
if (!_.isEmpty(noRouteSection.items)) { | ||
params.noRouteSection = noRouteSection; | ||
} | ||
return params; | ||
}; | ||
|
||
/** | ||
* Construct the mustache template parameter map. | ||
* @param {Object} route route fields | ||
* @param {ReconciledOrder[]} orders list of orders | ||
*/ | ||
function getEmailTemplateParameters(route, orders) { | ||
const ticketParameterMaps = orders.map((order) => { | ||
const { intakeRecord: [, ticket,] } = order; | ||
const additionalItems = order.getAdditionalItems(); | ||
const shoppingItems = _.map( | ||
additionalItems, | ||
({ item, quantity }) => { | ||
return `${quantity || ''} ${item}`.trim(); | ||
} | ||
); | ||
const warehouseSpecialtyItems = _.map( | ||
order.getWarehouseItems(), | ||
({ item, quantity }) => { | ||
return `${quantity || ''} ${item}`.trim(); | ||
} | ||
); | ||
return Object.assign({}, ticket, { | ||
phoneNumberNumbersOnly: _.replace(ticket.phoneNumber, /[^0-9]/g, ''), | ||
mapsUrl: googleMapsUrl(ticket.address), | ||
vulnerabilities: _.join(ticket.vulnerability, ', '), | ||
groceryList: _.join(ticket.foodOptions, ', '), | ||
otherItems: _.join(shoppingItems, ', '), | ||
warehouseSpecialtyItems: _.join(warehouseSpecialtyItems, ', '), | ||
}); | ||
}); | ||
return { | ||
to: route.deliveryVolunteerEmail, | ||
deliveryDateString: moment(route.deliveryDate).utc().format('MMMM Do'), | ||
firstName: route.deliveryVolunteerName[0].split(' ')[0], | ||
routeName: route.name, | ||
ticketIDs: _.join(_.map(tickets, (fields) => { | ||
ticketIDs: _.join(_.map(orders, ({ intakeRecord: [, fields,]}) => { | ||
return fields.ticketID; | ||
}), ', '), | ||
warehouseMapsUrl: googleMapsUrl('221 Glenmore Ave'), | ||
arrivalTime: _.trim(route.arrivalTime), | ||
warehouseCoordinatorPhone: functions.config().bulk_ops_team.warehouse_coordinator.phone_number, | ||
warehouseCoordinatorPhone1: functions.config().bulk_ops_team.warehouse_coordinator1.phone_number, | ||
warehouseCoordinatorPhone2: functions.config().bulk_ops_team.warehouse_coordinator2.phone_number, | ||
tickets: ticketParameterMaps, | ||
shoppingList: getShoppingListTemplateParameters(route, orders), | ||
}; | ||
} | ||
|
||
|
@@ -51,18 +117,28 @@ async function main() { | |
await getRecordsWithFilter(BULK_DELIVERY_ROUTES_TABLE, { deliveryDate: argv.deliveryDate, name: argv.route }) | ||
) : await getAllRoutes(argv.deliveryDate); | ||
|
||
const orders = await reconcileOrders(argv.deliveryDate, routes); | ||
|
||
const ordersByKey = _.fromPairs( | ||
_.map(orders, (order) => { | ||
return [order.intakeRecord[0], order]; | ||
}) | ||
); | ||
|
||
const templateParameterMaps = await Promise.all(_.map(routes, async (route) => { | ||
const ticketRecords = await getTicketsForRoutes([route]); | ||
const ticketsFields = _.map(ticketRecords, ([, fields,]) => fields); | ||
const [, routeFields,] = route; | ||
return getEmailTemplateParameters(routeFields, ticketsFields); | ||
const orders = _.map(ticketRecords, ([ticketKey,]) => { | ||
return ordersByKey[ticketKey]; | ||
}); | ||
return getEmailTemplateParameters(routeFields, orders); | ||
})); | ||
|
||
const templateFilename = 'functions/templates/bulk-delivery-volunteer-email.md.mustache'; | ||
const template = (await fs.promises.readFile(templateFilename)).toString('utf-8'); | ||
const emailTemplateFilename = 'functions/templates/bulk-delivery-volunteer-email.md.mustache'; | ||
const emailTemplate = (await fs.promises.readFile(emailTemplateFilename)).toString('utf-8'); | ||
|
||
const emails = _.map(templateParameterMaps, (view) => { | ||
const markdown = Mustache.render(template, view); | ||
const markdown = Mustache.render(emailTemplate, view); | ||
return new Email(markdown, { | ||
to: view.to, | ||
cc: '[email protected]', | ||
|
@@ -86,5 +162,10 @@ async function main() { | |
main().then( | ||
() => console.log('done') | ||
).catch( | ||
(e) => console.error(e) | ||
(e) => { | ||
console.error(e); | ||
if (e.response && e.response.body && e.response.body.errors) { | ||
console.error(e.response.body.errors); | ||
} | ||
} | ||
); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks like this has since updated to 10 lol