<commit_before>/* eslint-env node */
/* global Promise */

const express = require('express');

const { AppError, buildURL, getLogger, performGETRequest } = require('./utils');
const { fetchAllRepresentatives, fetchAllSenators, fetchDistrictsForZip } = require('./api-cache');

const log = getLogger();
const app = express();

const DEFAULT_PORT = 3000;

const AT_LARGE_DISTRICT_NAME = '(at Large)';
const AT_LARGE_DISTRICT_NUMBER = 0;

const GEOGRAPHY_BASE_URL = 'https://geocoding.geo.census.gov/geocoder/geographies/address';

// Geography layer that includes information on the 115th Congressional Districts
// as defined: https://tigerweb.geo.census.gov/arcgis/rest/services/TIGERweb/tigerWMS_Current/MapServer/54
const CONGRESSIONAL_DISTRICTS_LAYER = 54;

function getPartyName(shorthand) {
  switch(shorthand) {
    case 'D': return 'Democrat';
    case 'R': return 'Republican';
    default: return shorthand;
  }
}

function getDistricts(geography) {
  // Be careful not to log actual street here, to protect privacy of users
  log.info(`[app] getDistricts, zip=${geography.zip}`);

  const params = {
    benchmark: 'Public_AR_Current',
    vintage: 'Current_Current',
    format: 'json',
    layers: CONGRESSIONAL_DISTRICTS_LAYER,
    street: geography.street,
    zip: geography.zip
  };

  return performGETRequest({ url: buildURL(GEOGRAPHY_BASE_URL, params) }, result => {
    if (result.result.addressMatches.length === 0) {
      throw new AppError('INVALID_ADDRESS');
    }

    const address = result.result.addressMatches[0];
    let number = address.geographies['115th Congressional Districts'][0].BASENAME;
    const state = address.addressComponents.state;

    if (state && number.match(AT_LARGE_DISTRICT_NAME)) {
      number = AT_LARGE_DISTRICT_NUMBER;
    } else {
      number = Number(number);
    }

    const id = `${state}-${number}`;

    return {
      districts: [
        { number, state, id }
      ]
    };
  });
}

function getDistrictsZipOnly(geography) {
  log.info(`[app] getDistrictsZipOnly, zip=${geography.zip}`);
<commit_msg>Ensure server handles most recent congressional name without hardcoding<commit_after>/* eslint-env node */
/* global Promise */

const express = require('express');

const { AppError, buildURL, getLogger, performGETRequest } = require('./utils');
const { fetchAllRepresentatives, fetchAllSenators, fetchDistrictsForZip } = require('./api-cache');

const log = getLogger();
const app = express();

const DEFAULT_PORT = 3000;

const AT_LARGE_DISTRICT_NAME = '(at Large)';
const AT_LARGE_DISTRICT_NUMBER = 0;

const GEOGRAPHY_BASE_URL = 'https://geocoding.geo.census.gov/geocoder/geographies/address';

// Geography layer that includes information on the most recent congressional districts
// as defined: https://tigerweb.geo.census.gov/arcgis/rest/services/TIGERweb/tigerWMS_Current/MapServer/54
const CONGRESSIONAL_DISTRICTS_LAYER = 54;

function getPartyName(shorthand) {
  switch(shorthand) {
    case 'D': return 'Democrat';
    case 'R': return 'Republican';
    default: return shorthand;
  }
}

function getDistricts(geography) {
  // Be careful not to log actual street here, to protect privacy of users
  log.info(`[app] getDistricts, zip=${geography.zip}`);

  const params = {
    benchmark: 'Public_AR_Current',
    vintage: 'Current_Current',
    format: 'json',
    layers: CONGRESSIONAL_DISTRICTS_LAYER,
    street: geography.street,
    zip: geography.zip
  };

  return performGETRequest({ url: buildURL(GEOGRAPHY_BASE_URL, params) }, result => {
    if (result.result.addressMatches.length === 0) {
      throw new AppError('INVALID_ADDRESS');
    }

    const address = result.result.addressMatches[0];

    const allCongressNames = Object.getOwnPropertyNames(address.geographies).sort();

    const mostRecentCongressName = allCongressNames.slice(-1);
    const mostRecentCongress = address.geographies[mostRecentCongressName];

    let number = mostRecentCongress[0].BASENAME;
    const state = address.addressComponents.state;

    if (state && number.match(AT_LARGE_DISTRICT_NAME)) {
      number = AT_LARGE_DISTRICT_NUMBER;
    } else {
      number = Number(number);
    }

    const id = `${state}-${number}`;

    return {
      districts: [
        { number, state, id }
      ]
    };
  });
}

function getDistrictsZipOnly(geography) {
  log.info(`[app] getDistrictsZipOnly, zip=${geography.zip}`);

{'fraction_non_alphanumeric': 0.12394814646349785, 'fraction_numerical': 0.007050261541960427, 'mean_word_length': 4.198581560283688, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6138007', 'n_tokens_mistral': 1486, 'n_tokens_neox': 1382, 'n_words': 304}