{"size": 2362, "ext": "js", "max_stars_repo_path": "edit/proser_guestApi/proser_reports_old/src/sync/etl/extract/inv/extract_agent.js", "max_stars_repo_name": "Maprotel/Proser2-Sync", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "edit/proser_guestApi/proser_reports_old/src/sync/etl/extract/inv/extract_agent.js", "max_issues_repo_name": "Maprotel/Proser2-Sync", "max_issues_repo_issues_event_min_datetime": "2021-05-08T18:12:05.000Z", "max_issues_repo_issues_event_max_datetime": "2021-05-11T01:53:28.000Z", "max_forks_repo_path": "edit/proser_guestApi/proser_reports_old/src/sync/etl/extract/inv/extract_agent.js", "max_forks_repo_name": "Maprotel/Proser2-Release", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 5, "max_forks_count": null, "avg_line_length": 25.6739130435, "max_line_length": 80, "alphanum_fraction": 0.5728196444}
import * as pool from "../../../../connectors/pool";
import { removeRowDataPacket } from "../../../helpers/mysql-helper.js";
import {
  writeDestiny,
  readOriginByDate,
  readOriginByStatus,
  previousDate,
  minDate,
  startDate
} from "../extract_functions";
import moment from "moment";

const destinyTable = "InvAgent";
const destinyDateField = "";
const destinyStatusField = "inv_agent_status";

const originTable = "call_center.agent";
const originDateField = "";
const originStatusField = "estatus";

let first_pass = true;
let incoming_date = process.argv[2];

async function extractInvAgent(start_date) {
  console.log(`/*************/ Extracting ${destinyTable} /*************/ `);

  console.log("start_date", start_date);

  let val = true;

  let result = await readOriginByStatus(
    start_date,
    originTable,
    originStatusField
  ).catch(err =>
    console.log(`${destinyTable} caught it - readOriginByStatus`, err)
  );

  if (Array.isArray(result) && result.length > 0) {
    let extendedResult = result
      .map(x => {
        x.inv_agent_id = x.id;
        x.inv_agent_type = x.type;
        x.inv_agent_extension = x.number;
        x.inv_agent_name = x.name;
        x.inv_agent_status = x.estatus;

        return x;
      })
      .map(y => {
        delete y.id;
        delete y.type;
        delete y.number;
        delete y.name;
        delete y.password;
        delete y.estatus;
        delete y.eccp_password;

        return y;
      });

    let validation =
      Array.isArray(extendedResult) && extendedResult.length > 0 ? true : false;

    if (validation) {
      try {
        let written = await writeDestiny(extendedResult, destinyTable).catch(
          err => console.log("extractInvAgent caught it - writeDestiny", err)
        );
        return "extractInvAgent end";
      } catch (e) {
        console.log("e", e);
        return e;
      }
    } else {
      console.log(`********************************************`);
      console.log(`El resultado está vacío en ${originTable}`);
    }
  } else {
    console.log(`********************************************`);
    console.log(`No hay registros nuevos por actualizar en ${destinyTable}`);
  }

  return "extractInvAgent end";
}

/************************************************************************ */

module.exports = {
  extractInvAgent
};
