Resolve missing items from merge conflicts.
This commit is contained in:
@@ -1743,3 +1743,11 @@ exports.UPDATE_PARTS_CRITICAL = `mutation UPDATE_PARTS_CRITICAL ($IdsToMarkCriti
|
||||
affected_rows
|
||||
}
|
||||
}`;
|
||||
|
||||
exports.ACTIVE_SHOP_BY_USER = `query ACTIVE_SHOP_BY_USER($user: String) {
|
||||
associations(where: {active: {_eq: true}, useremail: {_eq: $user}}) {
|
||||
id
|
||||
shopid
|
||||
}
|
||||
}
|
||||
`;
|
||||
|
||||
251
server/opensearch/os-handler.js
Normal file
251
server/opensearch/os-handler.js
Normal file
@@ -0,0 +1,251 @@
|
||||
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",
|
||||
"ro_number",
|
||||
"clm_no",
|
||||
"ownr_fn",
|
||||
"ownr_ln",
|
||||
"status",
|
||||
"ownr_co_nm",
|
||||
"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",
|
||||
"invoice_number",
|
||||
"date",
|
||||
]),
|
||||
...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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
`,
|
||||
{ paymentId: req.body.event.data.new.id }
|
||||
);
|
||||
document = {
|
||||
..._.pick(req.body.event.data.new, ["id", "invoice_number"]),
|
||||
...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 } = 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({
|
||||
body: {
|
||||
size: 100,
|
||||
query: {
|
||||
bool: {
|
||||
must: [
|
||||
{
|
||||
multi_match: {
|
||||
query: search,
|
||||
//fields: ["*"],
|
||||
fuzziness: "AUTO",
|
||||
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;
|
||||
Reference in New Issue
Block a user