{"size": 3025, "ext": "js", "max_stars_repo_path": "src/lib/falcor/routes/teams/by-slug.js", "max_stars_repo_name": "dweggyness/gazelle-server", "max_stars_repo_stars_event_min_datetime": "2016-11-19T15:40:26.000Z", "max_stars_repo_stars_event_max_datetime": "2020-03-04T14:41:42.000Z", "max_issues_repo_path": "src/lib/falcor/routes/teams/by-slug.js", "max_issues_repo_name": "dweggyness/gazelle-server", "max_issues_repo_issues_event_min_datetime": "2016-09-21T11:52:57.000Z", "max_issues_repo_issues_event_max_datetime": "2022-01-19T08:36:11.000Z", "max_forks_repo_path": "src/lib/falcor/routes/teams/by-slug.js", "max_forks_repo_name": "dweggyness/gazelle-server", "max_forks_repo_forks_event_min_datetime": "2016-11-04T08:18:36.000Z", "max_forks_repo_forks_event_max_datetime": "2021-08-28T15:46:57.000Z"}
{"max_stars_count": 15, "max_issues_count": 393, "max_forks_count": 8, "avg_line_length": 29.6568627451, "max_line_length": 80, "alphanum_fraction": 0.4565289256}
import falcor from 'falcor';
import _ from 'lodash';

import * as db from 'lib/db';
import { has } from 'lib/utilities';

const $ref = falcor.Model.ref;

export default [
  {
    route: "teams['bySlug'][{keys:slugs}]['slug', 'id', 'name', 'description']",
    get: pathSet =>
      new Promise(resolve => {
        const requestedFields = pathSet[3];
        db.teamQuery(pathSet.slugs, requestedFields).then(data => {
          const results = [];
          data.forEach(team => {
            requestedFields.forEach(field => {
              results.push({
                path: ['teams', 'bySlug', team.slug, field],
                value: team[field],
              });
            });
          });
          resolve(results);
        });
      }),
    set: jsonGraphArg =>
      new Promise((resolve, reject) => {
        const teamsBySlug = jsonGraphArg.teams.bySlug;
        db.updateTeams(teamsBySlug)
          .then(flag => {
            if (!flag) {
              throw new Error(
                'For unknown reasons updateTeams returned a non-true flag',
              );
            }
            const results = [];
            _.forEach(teamsBySlug, (teamObject, slug) => {
              _.forEach(teamObject, (value, field) => {
                results.push({
                  path: ['teams', 'bySlug', slug, field],
                  value,
                });
              });
            });
            resolve(results);
          })
          .catch(e => {
            reject(e);
          });
      }),
  },
  {
    route: "teams['bySlug'][{keys:slugs}]['staff'][{integers:indices}]",
    get: pathSet =>
      new Promise(resolve => {
        db.teamStaffQuery(pathSet.slugs).then(data => {
          const results = [];
          _.forEach(data, (staffSlugArray, teamSlug) => {
            pathSet.indices.forEach(index => {
              if (index < staffSlugArray.length) {
                results.push({
                  path: ['teams', 'bySlug', teamSlug, 'staff', index],
                  value: $ref(['staff', 'bySlug', staffSlugArray[index]]),
                });
              }
            });
          });
          resolve(results);
        });
      }),
  },
  {
    route: "teams['bySlug']['createTeam']",
    call: (callPath, args) =>
      new Promise(resolve => {
        const teamObject = args[0];
        if (!(has.call(teamObject, 'slug') && has.call(teamObject, 'name'))) {
          throw new Error(
            'When creating a team you must provide both name and slug',
          );
        }
        db.addTeam(teamObject).then(flag => {
          if (!flag) {
            throw new Error(
              'For unknown reasons addTeam returned a non-true flag',
            );
          }
          const results = [];
          _.forEach(teamObject, (value, field) => {
            results.push({
              path: ['teams', 'bySlug', teamObject.slug, field],
              value,
            });
          });
          resolve(results);
        });
      }),
  },
];
