IO-233 Mixdata schema updates and API.
This commit is contained in:
@@ -2,6 +2,8 @@ const path = require("path");
|
||||
const _ = require("lodash");
|
||||
const logger = require("../utils/logger");
|
||||
const xml2js = require("xml2js");
|
||||
const GraphQLClient = require("graphql-request").GraphQLClient;
|
||||
const queries = require("../graphql-client/queries");
|
||||
|
||||
require("dotenv").config({
|
||||
path: path.resolve(
|
||||
@@ -12,7 +14,7 @@ require("dotenv").config({
|
||||
|
||||
exports.mixdataUpload = async (req, res) => {
|
||||
const { bodyshopid } = req.body;
|
||||
logger.log("media-bulk-download", "DEBUG", req.user.email, ids, null);
|
||||
|
||||
const BearerToken = req.headers.authorization;
|
||||
logger.log("job-mixdata-upload", "DEBUG", req.user.email, null, null);
|
||||
const client = new GraphQLClient(process.env.GRAPHQL_ENDPOINT, {
|
||||
@@ -31,15 +33,50 @@ exports.mixdataUpload = async (req, res) => {
|
||||
});
|
||||
|
||||
const ScaleType = DetermineScaleType(inboundRequest);
|
||||
const ROList = GetListOfRos(inboundRequest, ScaleType);
|
||||
const RoNumbersFromInboundRequest = GetListOfRos(
|
||||
inboundRequest,
|
||||
ScaleType
|
||||
);
|
||||
|
||||
//Query the list of ROs based on the RO number.
|
||||
if (RoNumbersFromInboundRequest.length > 0) {
|
||||
//Query the list of ROs based on the RO number.
|
||||
const { jobs } = await client.request(queries.QUERY_JOB_ID_MIXDATA, {
|
||||
roNumbers: RoNumbersFromInboundRequest,
|
||||
});
|
||||
|
||||
console.log(ROList);
|
||||
//Create the hash for faster processing for inserts/updates.
|
||||
const jobHash = {};
|
||||
jobs.forEach((j) => {
|
||||
jobHash[j.ro_number] = {
|
||||
jobid: j.id,
|
||||
mixdataid: j.mixdata.length > 0 ? j.mixdata[0].id : null,
|
||||
};
|
||||
});
|
||||
const MixDataArray = GenerateMixDataArray(
|
||||
inboundRequest,
|
||||
ScaleType,
|
||||
jobHash
|
||||
);
|
||||
|
||||
const MixDataQuery = `
|
||||
mutation UPSERT_MIXDATA{
|
||||
${MixDataArray.map((md, idx) =>
|
||||
GenerateGqlForMixData(md, idx)
|
||||
).join(" ")}
|
||||
}
|
||||
`;
|
||||
|
||||
const resp = await client.request(MixDataQuery);
|
||||
|
||||
//Process the list of ROs and return an object to generate the queries.
|
||||
}
|
||||
});
|
||||
res.sendStatus(200);
|
||||
} catch (error) {
|
||||
res.status(500).JSON(error);
|
||||
logger.log("job-mixdata-upload-error", "ERROR", null, null, {
|
||||
error: error.message,
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
@@ -63,3 +100,45 @@ function GetListOfRos(inboundRequest, ScaleType) {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
function GenerateMixDataArray(inboundRequest, ScaleType, jobHash) {
|
||||
if (ScaleType.company === "PPG" && ScaleType.version === "1.3.0") {
|
||||
return inboundRequest.PPG.DataExportInterface.ROData.RepairOrders.RO.map(
|
||||
(r) => {
|
||||
return {
|
||||
jobid: jobHash[r.RONumber].jobid,
|
||||
id: jobHash[r.RONumber].mixdataid,
|
||||
mixdata: r,
|
||||
totalliquidcost: r.TotalLiquidCost,
|
||||
totalsundrycost: r.TotalSundryCost,
|
||||
company: ScaleType.company,
|
||||
version: ScaleType.version,
|
||||
};
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
function GenerateGqlForMixData(mixdata, key) {
|
||||
const { id, ...restMixData } = mixdata;
|
||||
|
||||
if (id) {
|
||||
//Update.
|
||||
return `
|
||||
update${key}: update_mixdata_by_pk(pk_columns:{id: "${id}"}, _set: ${JSON.stringify(
|
||||
restMixData
|
||||
).replace(/"(\w+)"\s*:/g, "$1:")}){
|
||||
id
|
||||
}
|
||||
`;
|
||||
} else {
|
||||
//Insert
|
||||
return `
|
||||
insert${key}: insert_mixdata_one(object: ${JSON.stringify(
|
||||
restMixData
|
||||
).replace(/"(\w+)"\s*:/g, "$1:")}){
|
||||
id
|
||||
}
|
||||
`;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user