function doFixAllEmptyValues() {
  return Center.find()
    .then(centers =>
      centers.map(center => doFixCenterEmptyValues(center, schema))
    )
    .then(centers =>
      Promise.all(
        centers.map(center => {
          if (center.isModified()) {
            if (dryRun) {
              // eslint-disable-next-line no-console
              console.log(
                chalk.cyan('DRY RUN: Fixed empty values but NOT saving %s'),
                center.id
              );
            } else {
              // eslint-disable-next-line no-console
              console.log(
                chalk.green('Fixed empty values, saving… %s'),
                center.id
              );
              return center.save();
            }
          }
          return center;
        })
      )
    );
}
function saveToMongo(cleanedCenter) {
  const save = obj => {
    if (dryRun) {
      return Promise.resolve(obj);
    }
    return obj.save();
  };

  const create = () => {
    console.log('Creating… %s', cleanedCenter.id); // eslint-disable-line no-console
    return save(new Center(cleanedCenter));
  };

  const update = found => {
    // Remove 'id' from subdocuments when comparing
    const json = found.toJSON();
    const picked =
      argv.update.length > 0 ? pick(cleanedCenter, argv.update) : cleanedCenter;
    let updates = {};
    for (let key in picked) {
      if (!isSloppyEqual(cleanedCenter[key], json[key])) {
        updates[key] = cleanedCenter[key];
      }
    }
    if (Object.keys(updates).length === 0) {
      if (verbose) {
        console.log(chalk.dim('VERBOSE: not updating %s'), cleanedCenter.id); // eslint-disable-line no-console
      }
      return null;
    }
    // eslint-disable-next-line no-console
    console.log(
      'Updating… %s (%d fields: %s)',
      cleanedCenter.id,
      Object.keys(updates).length,
      Object.keys(updates).join(', ')
    );
    for (let key in updates) {
      if (verbose) {
        // eslint-disable-next-line no-console
        console.log(
          chalk.dim('VERBOSE: %s: %s => %s'),
          key,
          JSON.stringify(json[key]),
          JSON.stringify(updates[key])
        );
      }
      found[key] = updates[key];
    }
    return save(found);
  };

  return updateCenters
    ? Center.findOne({ id: cleanedCenter.id }).then(found =>
      found ? update(found) : create()
    )
    : create();
}
#!/usr/bin/env node

// This script generates data.json from the `centers` collection

const fs = require('fs');
const path = require('path');

const DATA = path.join(__dirname, '../app/data/data.json');
const data = require(DATA);

const { Center } = require(path.join(__dirname, '../server/models'));

// TODO
Center.find().then(centers => {
  data.allCenters = centers;
  fs.writeFile(
    path.join(__dirname, '../app/data/data2.json'),
    JSON.stringify(data),
    () => process.exit()
  );
});
              );
              return center.save();
            }
          }
          return center;
        })
      )
    );
}

// let's go

if (clearCenters) {
  if (!dryRun) {
    Center.remove(
      {},
      (err, { result }) => console.log(`${result.n} centers deleted`) // eslint-disable-line no-console
    );
  } else {
    console.log(chalk.cyan('DRY RUN: 0 centers deleted')); // eslint-disable-line no-console
  }
}

Promise.all(
  Object.values(centers)
    .map(sanitize)
    .map(saveToMongo)
)
  .then(updated =>
    fixEmptyValues ? doFixAllEmptyValues().then(() => updated) : updated
  )
  .then(updated => {