281 lines
7.3 KiB
JavaScript
281 lines
7.3 KiB
JavaScript
const Dinero = require("dinero.js");
|
|
const queries = require("../graphql-client/queries");
|
|
//const client = require("../graphql-client/graphql-client").client;
|
|
const _ = require("lodash");
|
|
const GraphQLClient = require("graphql-request").GraphQLClient;
|
|
const logger = require("../utils/logger");
|
|
|
|
const path = require("path");
|
|
const client = require("../graphql-client/graphql-client").client;
|
|
require("dotenv").config({
|
|
path: 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 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) {
|
|
try {
|
|
var osClient = await getClient();
|
|
// const osClient = new Client({
|
|
// node: `https://imex:<password>@search-imexonline-search-ixp2stfvwp6qocjsowzjzyreoy.ca-central-1.es.amazonaws.com/`,
|
|
// });
|
|
|
|
if (req.body.event.op === "DELETE") {
|
|
let response;
|
|
response = await osClient.delete({
|
|
id: req.body.event.data.old.id,
|
|
index: req.body.table.name,
|
|
});
|
|
res.status(200).json(response.body);
|
|
} else {
|
|
let document;
|
|
|
|
switch (req.body.table.name) {
|
|
case "jobs":
|
|
document = _.pick(req.body.event.data.new, [
|
|
"id",
|
|
"bodyshopid",
|
|
"clm_no",
|
|
"clm_total",
|
|
"comment",
|
|
"ins_co_nm",
|
|
"ownr_co_nm",
|
|
"ownr_fn",
|
|
"ownr_ln",
|
|
"ownr_ph1",
|
|
"ownr_ph2",
|
|
"plate_no",
|
|
"ro_number",
|
|
"status",
|
|
"v_model_yr",
|
|
"v_make_desc",
|
|
"v_model_desc",
|
|
]);
|
|
document.bodyshopid = req.body.event.data.new.shopid;
|
|
break;
|
|
case "vehicles":
|
|
document = _.pick(req.body.event.data.new, [
|
|
"id",
|
|
"v_model_yr",
|
|
"v_model_desc",
|
|
"v_make_desc",
|
|
"v_color",
|
|
"v_vin",
|
|
"plate_no",
|
|
]);
|
|
document.bodyshopid = req.body.event.data.new.shopid;
|
|
break;
|
|
case "owners":
|
|
document = _.pick(req.body.event.data.new, [
|
|
"id",
|
|
"ownr_fn",
|
|
"ownr_ln",
|
|
"ownr_co_nm",
|
|
"ownr_ph1",
|
|
"ownr_ph2",
|
|
]);
|
|
document.bodyshopid = req.body.event.data.new.shopid;
|
|
break;
|
|
case "bills":
|
|
const bill = await client.request(
|
|
`query ADMIN_GET_BILL_BY_ID($billId: uuid!) {
|
|
bills_by_pk(id: $billId) {
|
|
id
|
|
job {
|
|
id
|
|
ro_number
|
|
shopid
|
|
}
|
|
vendor {
|
|
id
|
|
name
|
|
}
|
|
}
|
|
}
|
|
`,
|
|
{ billId: req.body.event.data.new.id }
|
|
);
|
|
|
|
document = {
|
|
..._.pick(req.body.event.data.new, [
|
|
"id",
|
|
"date",
|
|
"exported",
|
|
"invoice_number",
|
|
"is_credit_memo",
|
|
]),
|
|
...bill.bills_by_pk,
|
|
bodyshopid: bill.bills_by_pk.job.shopid,
|
|
};
|
|
|
|
break;
|
|
case "payments":
|
|
//Query to get the job and RO number
|
|
const payment = await client.request(
|
|
`query ADMIN_GET_PAYMENT_BY_ID($paymentId: uuid!) {
|
|
payments_by_pk(id: $paymentId) {
|
|
id
|
|
job {
|
|
id
|
|
ro_number
|
|
shopid
|
|
ownerid
|
|
ownr_co_nm
|
|
ownr_fn
|
|
ownr_ln
|
|
owner {
|
|
id
|
|
ownr_co_nm
|
|
ownr_fn
|
|
ownr_ln
|
|
}
|
|
}
|
|
}
|
|
}
|
|
`,
|
|
{ paymentId: req.body.event.data.new.id }
|
|
);
|
|
document = {
|
|
..._.pick(req.body.event.data.new, [
|
|
"id",
|
|
"amount",
|
|
"created_at",
|
|
"exportedat",
|
|
"memo",
|
|
"payer",
|
|
"paymentnum",
|
|
"transactionid",
|
|
"type",
|
|
]),
|
|
...payment.payments_by_pk,
|
|
bodyshopid: bill.payments_by_pk.job.shopid,
|
|
};
|
|
break;
|
|
}
|
|
|
|
const payload = {
|
|
id: req.body.event.data.new.id,
|
|
index: req.body.table.name,
|
|
body: document,
|
|
};
|
|
|
|
let response;
|
|
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 {
|
|
}
|
|
}
|
|
|
|
async function OpensearchSearchHandler(req, res) {
|
|
try {
|
|
const { search, bodyshopid, index } = req.body;
|
|
if (!req.user) {
|
|
res.sendStatus(401);
|
|
return;
|
|
}
|
|
logger.log("os-search", "DEBUG", req.user.email, null, {
|
|
search,
|
|
});
|
|
|
|
const BearerToken = req.headers.authorization;
|
|
const client = new GraphQLClient(process.env.GRAPHQL_ENDPOINT, {
|
|
headers: {
|
|
Authorization: BearerToken,
|
|
},
|
|
});
|
|
|
|
const assocs = await client
|
|
.setHeaders({ Authorization: BearerToken })
|
|
.request(queries.ACTIVE_SHOP_BY_USER, {
|
|
user: req.user.email,
|
|
});
|
|
|
|
if (assocs.length === 0) {
|
|
res.sendStatus(401);
|
|
}
|
|
|
|
var osClient = await getClient();
|
|
|
|
const { body } = await osClient.search({
|
|
...(index ? { index } : {}),
|
|
body: {
|
|
size: 100,
|
|
query: {
|
|
bool: {
|
|
must: [
|
|
{
|
|
multi_match: {
|
|
query: search,
|
|
type: "phrase_prefix",
|
|
//fields: ["*"],
|
|
// fuzziness: "5",
|
|
//prefix_length: 2,
|
|
},
|
|
},
|
|
{
|
|
match: {
|
|
bodyshopid: assocs.associations[0].shopid,
|
|
},
|
|
},
|
|
],
|
|
},
|
|
},
|
|
sort: [
|
|
{
|
|
_score: {
|
|
order: "desc",
|
|
},
|
|
},
|
|
],
|
|
},
|
|
});
|
|
|
|
res.json(body);
|
|
} catch (error) {
|
|
console.log(error);
|
|
logger.log("os-search-error", "ERROR", req.user.email, null, {
|
|
error: JSON.stringify(error),
|
|
});
|
|
res.status(400).json(error);
|
|
} finally {
|
|
}
|
|
}
|
|
|
|
exports.handler = OpenSearchUpdateHandler;
|
|
exports.search = OpensearchSearchHandler;
|