refactors
This commit is contained in:
56
libs/awsUtils.js
Normal file
56
libs/awsUtils.js
Normal file
@@ -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 };
|
||||||
74
os-loader.js
74
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({
|
require("dotenv").config({
|
||||||
path: path.resolve(
|
path: require("path").resolve(
|
||||||
process.cwd(),
|
process.cwd(),
|
||||||
`.env.${process.env.NODE_ENV || "development"}`
|
`.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 {omit} = require("lodash");
|
||||||
const createAwsConnector = (credentials, region) => {
|
const gqlClient = require("./server/graphql-client/graphql-client").client;
|
||||||
class AmazonConnection extends Connection {
|
const getClient = require('./libs/awsUtils');
|
||||||
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) {
|
async function OpenSearchUpdateHandler(req, res) {
|
||||||
try {
|
try {
|
||||||
var osClient = await getClient();
|
const osClient = await getClient();
|
||||||
// const osClient = new Client({
|
|
||||||
// node: `https://imex:password@search-imexonline-search-ixp2stfvwp6qocjsowzjzyreoy.ca-central-1.es.amazonaws.com`,
|
|
||||||
// });
|
|
||||||
|
|
||||||
//Clear out all current documents
|
//Clear out all current documents
|
||||||
// const deleteResult = await osClient.deleteByQuery({
|
// const deleteResult = await osClient.deleteByQuery({
|
||||||
@@ -67,11 +25,11 @@ async function OpenSearchUpdateHandler(req, res) {
|
|||||||
|
|
||||||
// return;
|
// return;
|
||||||
|
|
||||||
var batchSize = 1000;
|
const batchSize = 1000;
|
||||||
var promiseQueue = [];
|
const promiseQueue = [];
|
||||||
|
|
||||||
//Jobs Load.
|
//Jobs Load.
|
||||||
const jobsData = await gqlclient.request(`query{jobs{
|
const jobsData = await gqlClient.request(`query{jobs{
|
||||||
id
|
id
|
||||||
bodyshopid:shopid
|
bodyshopid:shopid
|
||||||
clm_no
|
clm_no
|
||||||
@@ -105,7 +63,7 @@ async function OpenSearchUpdateHandler(req, res) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//Owner Load
|
//Owner Load
|
||||||
const ownersData = await gqlclient.request(`{
|
const ownersData = await gqlClient.request(`{
|
||||||
owners {
|
owners {
|
||||||
id
|
id
|
||||||
bodyshopid: shopid
|
bodyshopid: shopid
|
||||||
@@ -131,7 +89,7 @@ async function OpenSearchUpdateHandler(req, res) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//Vehicles
|
//Vehicles
|
||||||
const vehiclesData = await gqlclient.request(`{
|
const vehiclesData = await gqlClient.request(`{
|
||||||
vehicles {
|
vehicles {
|
||||||
id
|
id
|
||||||
bodyshopid: shopid
|
bodyshopid: shopid
|
||||||
@@ -158,7 +116,7 @@ async function OpenSearchUpdateHandler(req, res) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//payments
|
//payments
|
||||||
const paymentsData = await gqlclient.request(`{
|
const paymentsData = await gqlClient.request(`{
|
||||||
payments {
|
payments {
|
||||||
id
|
id
|
||||||
amount
|
amount
|
||||||
@@ -198,7 +156,7 @@ async function OpenSearchUpdateHandler(req, res) {
|
|||||||
slicedArray.forEach((payment) => {
|
slicedArray.forEach((payment) => {
|
||||||
bulkOperation.push({ index: { _index: "payments", _id: payment.id } });
|
bulkOperation.push({ index: { _index: "payments", _id: payment.id } });
|
||||||
bulkOperation.push({
|
bulkOperation.push({
|
||||||
..._.omit(payment, ["job"]),
|
...omit(payment, ["job"]),
|
||||||
bodyshopid: payment.job.bodyshopid,
|
bodyshopid: payment.job.bodyshopid,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -206,7 +164,7 @@ async function OpenSearchUpdateHandler(req, res) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//bills
|
//bills
|
||||||
const billsData = await gqlclient.request(`{
|
const billsData = await gqlClient.request(`{
|
||||||
bills {
|
bills {
|
||||||
id
|
id
|
||||||
date
|
date
|
||||||
@@ -235,7 +193,7 @@ async function OpenSearchUpdateHandler(req, res) {
|
|||||||
slicedArray.forEach((bill) => {
|
slicedArray.forEach((bill) => {
|
||||||
bulkOperation.push({ index: { _index: "bills", _id: bill.id } });
|
bulkOperation.push({ index: { _index: "bills", _id: bill.id } });
|
||||||
bulkOperation.push({
|
bulkOperation.push({
|
||||||
..._.omit(bill, ["job"]),
|
...omit(bill, ["job"]),
|
||||||
bodyshopid: bill.job.bodyshopid,
|
bodyshopid: bill.job.bodyshopid,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -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({
|
require("dotenv").config({
|
||||||
path: path.resolve(
|
path: require("path").resolve(
|
||||||
process.cwd(),
|
process.cwd(),
|
||||||
`.env.${process.env.NODE_ENV || "development"}`
|
`.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) {
|
async function OpenSearchUpdateHandler(req, res) {
|
||||||
if (req.headers["event-secret"] !== process.env.EVENT_SECRET) {
|
if (req.headers["event-secret"] !== process.env.EVENT_SECRET) {
|
||||||
@@ -51,10 +20,8 @@ async function OpenSearchUpdateHandler(req, res) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
var osClient = await getClient();
|
|
||||||
// const osClient = new Client({
|
const osClient = await getClient();
|
||||||
// node: `https://imex:<password>@search-imexonline-search-ixp2stfvwp6qocjsowzjzyreoy.ca-central-1.es.amazonaws.com/`,
|
|
||||||
// });
|
|
||||||
|
|
||||||
if (req.body.event.op === "DELETE") {
|
if (req.body.event.op === "DELETE") {
|
||||||
let response;
|
let response;
|
||||||
@@ -197,14 +164,12 @@ async function OpenSearchUpdateHandler(req, res) {
|
|||||||
body: document,
|
body: document,
|
||||||
};
|
};
|
||||||
|
|
||||||
let response;
|
const response = await osClient.index(payload);
|
||||||
response = await osClient.index(payload);
|
|
||||||
console.log(response.body);
|
console.log(response.body);
|
||||||
res.status(200).json(response.body);
|
res.status(200).json(response.body);
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
res.status(400).json(JSON.stringify(error));
|
res.status(400).json(JSON.stringify(error));
|
||||||
} finally {
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -240,6 +205,8 @@ async function OpenSearchSearchHandler(req, res) {
|
|||||||
|
|
||||||
const osClient = await getClient();
|
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({
|
const {body} = await osClient.search({
|
||||||
...(index ? {index} : {}),
|
...(index ? {index} : {}),
|
||||||
body: {
|
body: {
|
||||||
@@ -249,7 +216,7 @@ async function OpenSearchSearchHandler(req, res) {
|
|||||||
must: [
|
must: [
|
||||||
{
|
{
|
||||||
match: {
|
match: {
|
||||||
bodyshopid: assocs.associations[0].shopid,
|
bodyshopid: bodyShopIdMatchOverride,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -318,7 +285,6 @@ async function OpenSearchSearchHandler(req, res) {
|
|||||||
error: JSON.stringify(error),
|
error: JSON.stringify(error),
|
||||||
});
|
});
|
||||||
res.status(400).json(error);
|
res.status(400).json(error);
|
||||||
} finally {
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user