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

updates to bulk ops scripts for august 22 #93

Open
wants to merge 5 commits 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
48 changes: 30 additions & 18 deletions functions/airtable.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,10 @@ async function getBulkOrder(records) {
failedToLookup.push(item);
return 0;
}
if (!_.has(itemsByHouseholdSize[item], householdSize)) {
failedToLookup.push([item, householdSize]);
return 0;
}
return itemsByHouseholdSize[item][householdSize];
};

Expand All @@ -318,12 +322,12 @@ async function getBulkOrder(records) {
);

if (failedToLookup.length !== 0) {
// throw Error(`Failed to get item by household size for: ${_.join(_.uniq(failedToLookup))}`);
console.error(
`Failed to get item by household size for: ${_.join(
_.uniq(failedToLookup)
)}`
);
throw Error(`Failed to get item by household size for: ${_.join(_.uniq(failedToLookup))}`);
}

return itemToNumRequested;
Expand All @@ -350,6 +354,8 @@ async function getItemToNumAvailable(deliveryDate) {
*/
async function getAllRoutes(deliveryDate) {
const allRoutes = await getRecordsWithFilter(BULK_DELIVERY_ROUTES_TABLE, { deliveryDate });
// We are no longer using separate shopping volunteers!
/*
const routesWithoutShopper = _.filter(allRoutes, ([, fields]) => {
return (
fields.shoppingVolunteer === null || fields.shoppingVolunteer.length !== 1
Expand All @@ -362,6 +368,7 @@ async function getAllRoutes(deliveryDate) {
}));
throw new Error(msg);
}
*/
return allRoutes;
}

Expand All @@ -381,12 +388,7 @@ function getTicketsForRoute([, fields]) {
* @param {[string, Object, Object][]} allRoutes Bulk delivery route records.
*/
async function getTicketsForRoutes(allRoutes) {
return _.sortBy(
await Promise.all(_.flatMap(allRoutes, getTicketsForRoute)),
([, fields]) => {
return fields.ticketID;
}
);
return await Promise.all(_.flatMap(allRoutes, getTicketsForRoute));
}

class ReconciledOrder {
Expand Down Expand Up @@ -420,18 +422,9 @@ class ReconciledOrder {
}

bulkPurchasedItemsByGroup() {
const groups = _.groupBy(_.toPairs(this.provided), ([item]) => {
return _.groupBy(_.toPairs(this.provided), ([item]) => {
return this.itemToCategory[item].category;
});
const sorted = _.fromPairs(_.map(_.toPairs(groups), ([category, items]) => {
return [
category,
_.sortBy(items, (item) => {
return _.toNumber(this.itemToCategory[item[0]].order);
})
];
}));
return sorted;
}

/**
Expand Down Expand Up @@ -467,6 +460,20 @@ class ReconciledOrder {
// @ts-ignore eslint doesn't understand array structures I guess?
return missingItems.concat(customItems);
}

getWarehouseItems() {
const fields = this.intakeRecord[1];
const warehouseItems = fields.warehouseSpecialtyItems;
if (!warehouseItems) {
return [];
}
return _.map(
_.split(warehouseItems, ','),
(item) => {
return { item: _.trim(item), quantity: null };
}
);
}
}

/**
Expand All @@ -485,7 +492,12 @@ async function reconcileOrders(deliveryDate, allRoutes) {
})
);

const intakeRecords = await getTicketsForRoutes(allRoutes);
const intakeRecords = _.sortBy(
await getTicketsForRoutes(allRoutes),
([, fields]) => {
return fields.ticketID;
}
);

const itemToNumAvailable = await getItemToNumAvailable(deliveryDate);

Expand Down
2 changes: 2 additions & 0 deletions functions/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const INTAKE_SCHEMA = {
costCategory: 'cost_category',
foodOptions: 'Food Options',
otherItems: 'Other Items',
warehouseSpecialtyItems: 'Warehouse Specialty Items',
bulkRoute: 'Bulk Delivery Route',
deliveryVolunteerRecordID: 'Delivery Volunteer Record ID',
};
Expand Down Expand Up @@ -92,6 +93,7 @@ const ITEMS_BY_HOUSEHOLD_SIZE_SCHEMA = {
6: '6 Person(s)',
7: '7 Person(s)',
8: '8 Person(s)',
9: '9 Person(s)',
Copy link
Member

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

};

const BULK_ORDER_SCHEMA = {
Expand Down
103 changes: 92 additions & 11 deletions functions/scripts/email-bulk-delivery-volunteers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
};
}

Expand All @@ -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]',
Expand All @@ -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);
}
}
);
Loading