MOON
Server: Apache
System: Linux vps.erhabenn.com.br 3.10.0-1160.119.1.el7.tuxcare.els2.x86_64 #1 SMP Mon Jul 15 12:09:18 UTC 2024 x86_64
User: sonne (1011)
PHP: 8.2.31
Disabled: NONE
Upload Files
File: //matrixSwot/backend/apis/sendgrid/post/inbound.js
/************************INFO******************************************************/
/**
 * @author Cristofer Marinho
 * @date 2019-05
 * @name sendgrid/inbound // Service to inbound Mail in ideas@matrixswot.ai ||
 * @method POST
 * @param dev true||false (When True: Use sandbox domain)
 *
 */
/************************RELEASES**************************************************/
/**
 * @version 1.0
 * @description Verifing entries mail from sendGrid Server, and validating db mail. Return parent(id) for "mailAlias" node
 */

const dev = process.env.LOCAL === "DEV" ? true : false;
const api = require("./../../../database/conf");
const axios = require("axios");

const request = async (req, res) => {
  /**
   * @checkSender from
   * @version 1.0
   */
  var fromAdress = req.fields.from.replace('"', "");
  fromAdress = fromAdress.replace("<", "");
  fromAdress = fromAdress.replace(">", "");
  var adress = fromAdress.split(" ");
  var fromValue = adress[adress.length - 1];
  req.mail = req.fields;
  req.mail.from = fromValue;

  var Query = `MATCH (u:User) WHERE u.email='${fromValue}'
                RETURN u
                UNION
                MATCH (a:mailAlias) WHERE a.name='${fromValue}'
                OPTIONAL MATCH (u)-[r:CONTAINS]->(a)
                RETURN (u)
    `;
  let execQuery = await api.session
    .run(Query)
    .then(result => {
      const user = result.records[0].get("u");
      //axios.post("http://");
      return result.records;
    })
    .catch(err => {
      console.log(err);
    });
  if (execQuery.length > 0) {
    /**
     * @checkFunctionAlias email.to (ALIAS-HASH@DOMAIN) ALIAS = Function | HASH = Unique Code mail Alias to NODE
     * @version 1.0
     */
    var toAdress = req.fields.to.replace('"', "");
    toAdress = toAdress.replace("<", "");
    toAdress = toAdress.replace(">", "");
    toAdress = toAdress.replace('"', "");
    var adress = toAdress.split(" ");
    var toName = adress[0].substring(0, adress[0].lastIndexOf("@"));
    var aliasName = toName.split(".");
    var toDomain = adress[0].substring(adress[0].lastIndexOf("@") + 1);

    if (!dev && toDomain.indexOf("matrixswot.ai") === -1) {
      return { code: 405, message: "Invalid Mail Domain" };
    } else if (
      dev &&
      toDomain.indexOf("technology.sonne-consulting.com") === -1
    ) {
      return { code: 405, message: "Invalid Mail Domain" };
    }

    /**CHECK HASH ***\/\/\/\/\/\/\/\/\/\/***/
    const hashVerify = async hash => {
      var QueryHash = `MATCH MATCH (n)-[r:CONTAINS]->(a:mailHash) WHERE a.value=$hash RETURN (n)`;
      let execQueryHash = await api.session
        .run(QueryHash, { hash })
        .then(result => {
          return result.records;
        })
        .catch(err => {
          console.log("error na linha 81 =====>>>>");
          console.log(err);
        });
      if (execQueryHash.length > 0) {
        return execQueryHash[0].get(0).identity.low;
      } else {
        return null;
      }
    };

    try {
      if (aliasName[1]) {
        req.nodeId = await hashVerify(aliasName[1]);
      }
      req.idUser = execQuery[0].get(0).identity.low;
      req.userData = execQuery[0].get(0).properties;
      var reqMod = await require(`./${aliasName[0]}.js`);
      var response = await reqMod.request(req, res);
      return response;
    } catch (err) {
      console.log("error na linha 100 ===>>>");
      return { code: 200, message: "NODULE NOT EXIST" };
    }
  } else {
    return { code: 401, message: "UNAUTHORIZED" };
  }
};
module.exports = { request };