<commit_before>const express = require('express');
const { graphqlExpress, graphiqlExpress } = require('graphql-server-express');
const { makeExecutableSchema } = require('graphql-tools');
const bodyParser = require('body-parser');
const cors = require('cors');
const Logger = require('coa-node-logging');
require('dotenv').config();
const pg = require('pg');
const Pool = pg.Pool;

pg.defaults.poolSize = 1;
const logFile = process.env.logfile ? process.env.logfile : null;
const logger = new Logger('simplicity', logFile);
const typeDefs = require('./schema');
const resolvers = require('./resolvers');
const executableSchema = makeExecutableSchema({
  typeDefs,
  resolvers,
});
// Import Firebase - for now (8/25/16), the use of require and import of individual
// submodules is needed to avoid problems with webpack (import seems to require
// beta version of webpack 2).
const firebase = require('firebase');
logger.info('Initialize firebase');
firebase.initializeApp({
  serviceAccount: './SimpliCityII-284f9d0ebb83.json',
  databaseURL: 'https://simplicityii-878be.firebaseio.com',
});
logger.info('Firebase initialized');

const dbConfig = {
  host: process.env.dbhost,
  user: process.env.dbuser,
  password: <PASSWORD>,
  database: process.env.database,
  port: 5432,
  ssl: false,
};

logger.info('Connect to database');

const pool = new Pool(dbConfig);
logger.info('Database connection initialized');

const GRAPHQL_PORT = process.env.PORT || 8080;

const graphQLServer = express();
graphQLServer.use('*', cors());
graphQLServer.use((req, res, next) => {
  // This is middleware in prep for new authentication
  // console.log('I am in this middleware');
  return next();
});
graphQLServer.use(bodyParser.json());
logger.info('Initialize graphql server');

graphQLServer.use('/graphql', graphqlExpress((req, res) => {
  const baseConfig = {
    schema: executableSchema,
    context: {
      pool,
      logger,
      loggedin: false,
      token: null,
      uid: null,
      name: null,
      email: null,
      employee_id: 0,
      department: null,
      division: null,
      groups: [],
    },
  };
  logger.info('New client connection');
  if (!req.headers.authorization || req.headers.authorization === 'null') {
    return baseConfig;
  }
  logger.info('Attempt login verification');
  return firebase.auth().verifyIdToken(req.headers.authorization)
  .then((decodedToken) => {
    logger.info(`Logging in ${decodedToken.email}`);
    const decodedEmail = decodedToken.email.toLowerCase();
    const query = `SELECT emp_id, ad_memberships from amd.ad_info where email_city = '${decodedEmail}'`;
    if (!decodedEmail.endsWith('@ashevillenc.gov')) return baseConfig;
    return pool.query(query)
    .then(eres => {
      if (eres.rows.length > 0) {
        const employee = eres.rows[0];
        const config = {
          schema: executableSchema,
          context: {
            pool,
            logger,
            loggedin: true,
            token: req.headers.authorization,
            uid: decodedToken.uid,
            name: decodedToken.name,
            email: decodedToken.email,
            employee_id: employee.emp_id,
            department: null,
            division: null,
            groups: employee.ad_memberships.split(','),
          },
        };
        return config;
      }
      logger.error(`Unable to match employee by email ${decodedEmail}`);
      throw new Error('Unable to find employee by email.');
    })
    .catch(error => {
      logger.error(`Error on employee lookup: ${error}`);
    });
  }).catch((error) => {
    if (req.headers.authorization !== 'null') {
      logger.error(`Error decoding authentication token: ${error}`);
    }
    return baseConfig;
  });
}));

graphQLServer.use('/graphiql', graphiqlExpress({
  endpointURL: '/graphql',<commit_msg>Using the new employee_main view for login<commit_after>const express = require('express');
const { graphqlExpress, graphiqlExpress } = require('graphql-server-express');
const { makeExecutableSchema } = require('graphql-tools');
const bodyParser = require('body-parser');
const cors = require('cors');
const Logger = require('coa-node-logging');
require('dotenv').config();
const pg = require('pg');
const Pool = pg.Pool;

pg.defaults.poolSize = 1;
const logFile = process.env.logfile ? process.env.logfile : null;
const logger = new Logger('simplicity', logFile);
const typeDefs = require('./schema');
const resolvers = require('./resolvers');
const executableSchema = makeExecutableSchema({
  typeDefs,
  resolvers,
});
// Import Firebase - for now (8/25/16), the use of require and import of individual
// submodules is needed to avoid problems with webpack (import seems to require
// beta version of webpack 2).
const firebase = require('firebase');
logger.info('Initialize firebase');
firebase.initializeApp({
  serviceAccount: './SimpliCityII-284f9d0ebb83.json',
  databaseURL: 'https://simplicityii-878be.firebaseio.com',
});
logger.info('Firebase initialized');

const dbConfig = {
  host: process.env.dbhost,
  user: process.env.dbuser,
  password: <PASSWORD>,
  database: process.env.database,
  port: 5432,
  ssl: false,
};

logger.info('Connect to database');

const pool = new Pool(dbConfig);
logger.info('Database connection initialized');

const GRAPHQL_PORT = process.env.PORT || 8080;

const graphQLServer = express();
graphQLServer.use('*', cors());
graphQLServer.use((req, res, next) => {
  // This is middleware in prep for new authentication
  // console.log('I am in this middleware');
  return next();
});
graphQLServer.use(bodyParser.json());
logger.info('Initialize graphql server');

graphQLServer.use('/graphql', graphqlExpress((req, res) => {
  const baseConfig = {
    schema: executableSchema,
    context: {
      pool,
      logger,
      loggedin: false,
      token: null,
      uid: null,
      name: null,
      email: null,
      employee_id: 0,
      department: null,
      division: null,
      groups: [],
    },
  };
  logger.info('New client connection');
  if (!req.headers.authorization || req.headers.authorization === 'null') {
    return baseConfig;
  }
  logger.info('Attempt login verification');
  return firebase.auth().verifyIdToken(req.headers.authorization)
  .then((decodedToken) => {
    logger.info(`Logging in ${decodedToken.email}`);
    const decodedEmail = decodedToken.email.toLowerCase();
    const query = `SELECT emp_id, ad_memberships, active, department, division from amd.employee_main where email_city = '${decodedEmail}'`;
    if (!decodedEmail.endsWith('@ashevillenc.gov')) return baseConfig;
    return pool.query(query)
    .then(eres => {
      if (eres.rows.length > 0) {
        const employee = eres.rows[0];
        if (employee.active !== 'A') {
          logger.warn(`Non-active employee ${decodedEmail} login attempt`);
          return baseConfig;
        }
        const config = {
          schema: executableSchema,
          context: {
            pool,
            logger,
            loggedin: true,
            token: req.headers.authorization,
            uid: decodedToken.uid,
            name: decodedToken.name,
            email: decodedToken.email,
            employee_id: employee.emp_id,
            department: employee.department,
            division: employee.division,
            groups: employee.ad_memberships.split(','),
          },
        };
        return config;
      }
      logger.error(`Unable to match employee by email ${decodedEmail}`);
      throw new Error('Unable to find employee by email.');
    })
    .catch(error => {
      logger.error(`Error on employee lookup: ${error}`);
    });
  }).catch((error) => {
    if (req.headers.authorization !== 'null') {
      logger.error(`Error decoding authentication token: ${error}`);
    }
    return baseConfig;
  });
}));

graphQLServer.use('/graphiql', graphiqlExpress({
  endpointURL: '/graphql',
{'fraction_non_alphanumeric': 0.14081296694947448, 'fraction_numerical': 0.007091300493858427, 'mean_word_length': 3.3443344334433442, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 19, 'https://': 2, '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': '7262242', 'n_tokens_mistral': 2363, 'n_tokens_neox': 2323, 'n_words': 578}