{"size": 1835, "ext": "js", "max_stars_repo_path": "app/imports/startup/server/Mongo.js", "max_stars_repo_name": "geostudy/geostudy-source", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "app/imports/startup/server/Mongo.js", "max_issues_repo_name": "geostudy/geostudy-source", "max_issues_repo_issues_event_min_datetime": "2020-04-09T07:10:44.000Z", "max_issues_repo_issues_event_max_datetime": "2020-05-13T13:51:42.000Z", "max_forks_repo_path": "app/imports/startup/server/Mongo.js", "max_forks_repo_name": "geostudy/geostudy-source", "max_forks_repo_forks_event_min_datetime": "2020-05-14T00:17:26.000Z", "max_forks_repo_forks_event_max_datetime": "2020-05-14T00:17:26.000Z"}
{"max_stars_count": null, "max_issues_count": 49, "max_forks_count": 1, "avg_line_length": 35.9803921569, "max_line_length": 105, "alphanum_fraction": 0.6474114441}
import { Meteor } from 'meteor/meteor';
import { _ } from 'meteor/underscore';
import { Spots } from '../../api/spot/Spots.js';
import { Tags } from '../../api/tag/Tags.js';
import { Ratings } from '../../api/rating/Ratings.js';

/* eslint-disable no-console */

/** Initialize the database with a default data document. */
function addSpots({ image, name, location, description, owner }) {
  console.log(`  Adding: ${name} (${owner})`);
  Spots.insert({ image, name, location, description, owner });
}

function addTags({ name, spot }) {
  const spotId = _.pluck(_.flatten(_.map(spot,
      (tag) => Spots.find({ name: tag }).fetch()), true), '_id');
  console.log(`  Adding: ${name}`);
  Tags.insert({ name, spotId });
}

function addRatings({ score, owner, spot }) {
  const spotId = _.reduce(_.pluck(Spots.find({ name: spot }).fetch(), '_id'), (memo, num) => memo + num);
  console.log(`  Adding: ${spot} (${score})`);
  Ratings.insert({ score, owner, spotId });
}

/** Initialize the collection if empty. */
if (Meteor.settings.loadAssetsFile) {
  if (Spots.find().count() === 0) {
    const assetsFileName = 'data.json';
    console.log(`Loading spots from private/${assetsFileName}`);
    const jsonData = JSON.parse(Assets.getText(assetsFileName));
    jsonData.spots.map(spot => addSpots(spot));
  }

  if (Tags.find().count() === 0) {
    const assetsFileName = 'data.json';
    console.log(`Loading tags from private/${assetsFileName}`);
    const jsonData = JSON.parse(Assets.getText(assetsFileName));
    jsonData.tags.map(tag => addTags(tag));
  }

  if (Ratings.find().count() === 0) {
    const assetsFileName = 'data.json';
    console.log(`Loading ratings from private/${assetsFileName}`);
    const jsonData = JSON.parse(Assets.getText(assetsFileName));
    jsonData.ratings.map(rating => addRatings(rating));
  }
}
