All files data.js

100% Statements 93/93
100% Branches 10/10
100% Functions 3/3
100% Lines 93/93

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 941x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 5x 5x 5x 1x 1x 1x 1x 1x 1x 1x 1x 1x 12x 1x 1x 11x 11x 10x 10x 10x 1x 1x 1x 11x 12x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 9x 9x 9x 9x 9x 9x 9x 3x 3x 3x 9x 9x 2x 2x 2x 2x 7x 7x 7x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
/**
 * Data connection functions
 *
 * Provides quick access via functions to common data entities
 *
 * @author michael whobrey <mwhobrey@idealss.com>
 */
 
const { MongoClient } = require("mongodb");
const { constants } = require('./constants');
 
// const uri = "mongodb://" + (process.env.IDEAL_ENV === "production" ? "10.132.59.171" : "10.132.224.79") + ":27017/";
// const uri = "mongodb://127.0.0.1:27017/";
// const client = new MongoClient(uri);
 
/**
 * Create a MongoClient.
 *
 * Create a mongo client connection in order to interact with mongo
 *
 * @param uri {string} the connection uri
 *
 * @return {MongoClient} client connection
 */
const createClient = async (uri) => {
  console.log(`Connecting mongo client to ${uri}`);
  const client = new MongoClient(uri);
  return await client.connect();
}
 
/**
 * Get the database name
 *
 * Return the correct database to use based the env var set
 *
 * @return {string} the database name
 */
const dbName = () => {
  if (constants.isProduction()) {
    return "api_admin_production";
  }
  else {
    if (constants.isTesting()) {
      // return "api_admin_development";
      return "idealnet_testing";
    }
    else {
      return "api_admin_development";
    }
  }
}
 
/**
 * Lookup the auth doc tied to a token
 *
 * Lookup and return the auth doc tied to a provided token.
 *
 * @param {string} token - the identifying token of the client
 * @param {MongoClient} dbClient - Client connection to use for auth doc access
 *
 * @return {Object} the auth doc for the token
 */
const authDoc = async (token, dbClient) => {
  const aDatabase = dbClient.db(dbName());
  const aCollection = aDatabase.collection('authorizations');
  const aTCollection = aDatabase.collection('temp_authorizations');
 
  let aDoc = await aCollection
    .findOne({ _id: token });
  if (aDoc === null) {
    aDoc = await aTCollection
      .findOne({ _id: token });
  }
 
  if (aDoc === null) {
    aDoc = await aCollection.find();
    console.log('No auth doc found for provided token: ', token);
    return {};
  }
 
  return aDoc;
}
 
module.exports = {
  authDoc,
  createClient
}
 
module.exports.testingExports = {
  authDoc,
  dbName,
  createClient
}