const moment = require('moment');

const plannedWork = require('./plannedWork');
const { baseURL, routes, imgMap } = require('./constants.js');

function buildLink({ route, datetime }) {
  const date = moment(datetime);
  return `${baseURL}?tag=${route}&date=${date.format('MM/DD/YYYY')}&time=&method=getstatus4`;
}

function plannedWorkByDate(datetime) {
  const pages = routes.map((route) => {
    const link = buildLink({ route, datetime });
    return plannedWork(link);
  });

  // each advisory can have multiple messages
  const messagesByRoute = {};
  return Promise.all(pages)
    .then((advisories) => {
      advisories.forEach((advisory, index) => {
        const route = routes[index];
        messagesByRoute[route] = advisory;
      });
      return messagesByRoute;
}

module.exports = plannedWorkByDate;

@@ -74,16 +74,58 @@
 Work');%0A
+const DateRange = require('./DateRange');%0A
 const %7B 
@@ -821,43 +821,786 @@
 te;%0A
-%7D%0A%0Amodule.exports = plannedWorkByDate
+    %7D);%0A%7D%0A%0A/**%0A * Scrapes service advisories across multiple date ranges%0A *%0A * @param Number num - How many results to return%0A * @param Date from - Which date to start with%0A * @return Array%5BObject%5D - Batches of work for a specified%0A *   date range%0A */%0Afunction getWorkBatches(num=3, from=Date.now()) %7B%0A  const batches = %5B%5D;%0A  const start = new DateRange(from);%0A%0A  for (let offset = 0; offset %3C num; offset++) %7B%0A    const dateRange = start.next(offset);%0A    const advisories = plannedWorkByDate(dateRange.start);%0A    batches.push(Promise.resolve(advisories)%0A      .then((plannedWork) =%3E (%7B%0A        type: dateRange.type,%0A        start: dateRange.start,%0A        end: dateRange.end,%0A        plannedWork,%0A      %7D)))%0A  %7D%0A%0A  return Promise.all(batches)%0A%7D%0A%0Amodule.exports = getWorkBatches
 ;%0A

Implement crawling by date range