const client = require("../graphql-client/graphql-client").client;
const queries = require("../graphql-client/queries");
const path = require("path");
const logger = require("../utils/logger");

require("dotenv").config({
  path: path.resolve(process.cwd(), `.env.${process.env.NODE_ENV || "development"}`)
});

exports.default = async (req, res) => {
  const { useremail, bodyshopid, operationName, variables, env, time, dbevent, user } = req.body;

  try {
    await client.request(queries.INSERT_IOEVENT, {
      event: {
        operationname: operationName,
        time,
        dbevent,
        env,
        variables,
        bodyshopid,
        useremail
      }
    });
    res.sendStatus(200);
  } catch (error) {
    logger.log("ioevent-error", "trace", user, null, {
      operationname: operationName,
      time,
      dbevent,
      env,
      variables,
      bodyshopid,
      useremail
    });
    res.sendStatus(200);
  }
};
