diff --git a/libs/awsUtils.js b/libs/awsUtils.js new file mode 100644 index 000000000..4f7ee9aa4 --- /dev/null +++ b/libs/awsUtils.js @@ -0,0 +1,56 @@ +require("dotenv").config({ + path: require("path").resolve( + process.cwd(), + `.env.${process.env.NODE_ENV || "development"}` + ), +}); +const {isNil} = require('lodash'); +const aws4 = require("aws4"); +const {Connection, Client} = require("@opensearch-project/opensearch"); +const {defaultProvider} = require("@aws-sdk/credential-provider-node"); + +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 () => { + + // We have manual configuration for OpenSearch, + // Return a client using these custom credentials + if ( + !isNil(process.env.OPEN_SEARCH_PASSWORD) && + !isNil(process.env.OPEN_SEARCH_USER) && + !isNil(process.env.OPEN_SEARCH_HOST) && + !isNil(process.env.OPEN_SEARCH_PROTOCOL) + ) { + // The URI is currently being stored in its entirety, so strip protocol prior to rebuilding it. + const hostUrl = process.env.OPEN_SEARCH_HOST.replace(/^https?:\/\//i, ''); + const node = `${process.env.OPEN_SEARCH_PROTOCOL}://${process.env.OPEN_SEARCH_USER}:${process.env.OPEN_SEARCH_PASSWORD}@${hostUrl}`; + + return new Client({ + node, + }); + } + + // Default to the AWS Credentials Provider. + const credentials = await defaultProvider()(); + return new Client({ + ...createAwsConnector(credentials, "ca-central-1"), + node: process.env.OPEN_SEARCH_HOST, + }); +}; + +module.exports = { getClient }; \ No newline at end of file diff --git a/os-loader.js b/os-loader.js index a7876187a..bdabb87ac 100644 --- a/os-loader.js +++ b/os-loader.js @@ -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, }); }); diff --git a/server/opensearch/os-handler.js b/server/opensearch/os-handler.js index b6e5fc490..7cb544400 100644 --- a/server/opensearch/os-handler.js +++ b/server/opensearch/os-handler.js @@ -1,49 +1,18 @@ -const queries = require("../graphql-client/queries"); -const {pick} = require("lodash"); -const GraphQLClient = require("graphql-request").GraphQLClient; -const logger = require("../utils/logger"); -//const client = require("../graphql-client/graphql-client").client; - -const path = require("path"); -const client = require("../graphql-client/graphql-client").client; require("dotenv").config({ - path: path.resolve( + 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"); -var host = process.env.OPEN_SEARCH_HOST; +const GraphQLClient = require("graphql-request").GraphQLClient; +//const client = require("../graphql-client/graphql-client").client; +const logger = require("../utils/logger"); +const queries = require("../graphql-client/queries"); +const client = require("../graphql-client/graphql-client").client; +const {pick, isNil} = require("lodash"); +const {getClient} = require('../../libs/awsUtils'); -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, - }); -}; async function OpenSearchUpdateHandler(req, res) { if (req.headers["event-secret"] !== process.env.EVENT_SECRET) { @@ -51,10 +20,8 @@ async function OpenSearchUpdateHandler(req, res) { return; } try { - var osClient = await getClient(); - // const osClient = new Client({ - // node: `https://imex:@search-imexonline-search-ixp2stfvwp6qocjsowzjzyreoy.ca-central-1.es.amazonaws.com/`, - // }); + + const osClient = await getClient(); if (req.body.event.op === "DELETE") { let response; @@ -197,14 +164,12 @@ async function OpenSearchUpdateHandler(req, res) { body: document, }; - let response; - response = await osClient.index(payload); + const response = await osClient.index(payload); console.log(response.body); res.status(200).json(response.body); } } catch (error) { res.status(400).json(JSON.stringify(error)); - } finally { } } @@ -240,6 +205,8 @@ async function OpenSearchSearchHandler(req, res) { const osClient = await getClient(); + const bodyShopIdMatchOverride = isNil(process.env.BODY_SHOP_ID_MATCH_OVERRIDE) ? assocs.associations[0].shopid : process.env.BODY_SHOP_ID_MATCH_OVERRIDE + const {body} = await osClient.search({ ...(index ? {index} : {}), body: { @@ -249,7 +216,7 @@ async function OpenSearchSearchHandler(req, res) { must: [ { match: { - bodyshopid: assocs.associations[0].shopid, + bodyshopid: bodyShopIdMatchOverride, }, }, { @@ -318,7 +285,6 @@ async function OpenSearchSearchHandler(req, res) { error: JSON.stringify(error), }); res.status(400).json(error); - } finally { } }