refactors

This commit is contained in:
Dave Richer
2023-11-22 17:14:52 -05:00
parent e6df079431
commit 8670f386dc
3 changed files with 86 additions and 106 deletions

View File

@@ -1,59 +1,17 @@
const Dinero = require("dinero.js");
//const client = require("../graphql-client/graphql-client").client;
const _ = require("lodash");
const GraphQLClient = require("graphql-request").GraphQLClient;
const logger = require("./server/utils/logger");
const path = require("path");
const client = require("./server/graphql-client/graphql-client").client;
require("dotenv").config({
path: path.resolve(
process.cwd(),
`.env.${process.env.NODE_ENV || "development"}`
path: require("path").resolve(
process.cwd(),
`.env.${process.env.NODE_ENV || "development"}`
),
});
const { Client, Connection } = require("@opensearch-project/opensearch");
const { defaultProvider } = require("@aws-sdk/credential-provider-node");
const aws4 = require("aws4");
const { gql } = require("graphql-request");
const gqlclient = require("./server/graphql-client/graphql-client").client;
// const osClient = new Client({
// node: `https://imex:Wl0d8k@!@search-imexonline-search-ixp2stfvwp6qocjsowzjzyreoy.ca-central-1.es.amazonaws.com/`,
// });
var host = process.env.OPEN_SEARCH_HOST; // e.g. https://my-domain.region.es.amazonaws.com
const createAwsConnector = (credentials, region) => {
class AmazonConnection extends Connection {
buildRequestObject(params) {
const request = super.buildRequestObject(params);
request.service = "es";
request.region = region;
request.headers = request.headers || {};
request.headers["host"] = request.hostname;
return aws4.sign(request, credentials);
}
}
return {
Connection: AmazonConnection,
};
};
const getClient = async () => {
const credentials = await defaultProvider()();
return new Client({
...createAwsConnector(credentials, "ca-central-1"),
node: host,
});
};
const {omit} = require("lodash");
const gqlClient = require("./server/graphql-client/graphql-client").client;
const getClient = require('./libs/awsUtils');
async function OpenSearchUpdateHandler(req, res) {
try {
var osClient = await getClient();
// const osClient = new Client({
// node: `https://imex:password@search-imexonline-search-ixp2stfvwp6qocjsowzjzyreoy.ca-central-1.es.amazonaws.com`,
// });
const osClient = await getClient();
//Clear out all current documents
// const deleteResult = await osClient.deleteByQuery({
@@ -67,11 +25,11 @@ async function OpenSearchUpdateHandler(req, res) {
// return;
var batchSize = 1000;
var promiseQueue = [];
const batchSize = 1000;
const promiseQueue = [];
//Jobs Load.
const jobsData = await gqlclient.request(`query{jobs{
const jobsData = await gqlClient.request(`query{jobs{
id
bodyshopid:shopid
clm_no
@@ -105,7 +63,7 @@ async function OpenSearchUpdateHandler(req, res) {
}
//Owner Load
const ownersData = await gqlclient.request(`{
const ownersData = await gqlClient.request(`{
owners {
id
bodyshopid: shopid
@@ -131,7 +89,7 @@ async function OpenSearchUpdateHandler(req, res) {
}
//Vehicles
const vehiclesData = await gqlclient.request(`{
const vehiclesData = await gqlClient.request(`{
vehicles {
id
bodyshopid: shopid
@@ -158,7 +116,7 @@ async function OpenSearchUpdateHandler(req, res) {
}
//payments
const paymentsData = await gqlclient.request(`{
const paymentsData = await gqlClient.request(`{
payments {
id
amount
@@ -198,7 +156,7 @@ async function OpenSearchUpdateHandler(req, res) {
slicedArray.forEach((payment) => {
bulkOperation.push({ index: { _index: "payments", _id: payment.id } });
bulkOperation.push({
..._.omit(payment, ["job"]),
...omit(payment, ["job"]),
bodyshopid: payment.job.bodyshopid,
});
});
@@ -206,7 +164,7 @@ async function OpenSearchUpdateHandler(req, res) {
}
//bills
const billsData = await gqlclient.request(`{
const billsData = await gqlClient.request(`{
bills {
id
date
@@ -235,7 +193,7 @@ async function OpenSearchUpdateHandler(req, res) {
slicedArray.forEach((bill) => {
bulkOperation.push({ index: { _index: "bills", _id: bill.id } });
bulkOperation.push({
..._.omit(bill, ["job"]),
...omit(bill, ["job"]),
bodyshopid: bill.job.bodyshopid,
});
});