refactors
This commit is contained in:
@@ -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:<password>@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 {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user