IO-233 Base websocket setup for CDK.
This commit is contained in:
34
server/cdk/cdk-job-export.js
Normal file
34
server/cdk/cdk-job-export.js
Normal file
@@ -0,0 +1,34 @@
|
||||
const path = require("path");
|
||||
require("dotenv").config({
|
||||
path: path.resolve(
|
||||
process.cwd(),
|
||||
`.env.${process.env.NODE_ENV || "development"}`
|
||||
),
|
||||
});
|
||||
const GraphQLClient = require("graphql-request").GraphQLClient;
|
||||
const queries = require("../graphql-client/queries");
|
||||
|
||||
const CdkBase = require("./cdk");
|
||||
|
||||
exports.default = async function (socket, jobid) {
|
||||
CdkBase.createLogEvent(
|
||||
socket,
|
||||
"DEBUG",
|
||||
`Received Job export request for id ${jobid}`
|
||||
);
|
||||
const JobData = await QueryJobData(socket, jobid);
|
||||
};
|
||||
|
||||
async function QueryJobData(socket, jobid) {
|
||||
CdkBase.createLogEvent(socket, "DEBUG", `Querying job data for id ${jobid}`);
|
||||
const client = new GraphQLClient(process.env.GRAPHQL_ENDPOINT, {});
|
||||
const result = await client
|
||||
.setHeaders({ Authorization: `Bearer ${socket.handshake.auth.token}` })
|
||||
.request(queries.QUERY_JOBS_FOR_CDK_EXPORT, { id: jobid });
|
||||
CdkBase.createLogEvent(
|
||||
socket,
|
||||
"TRACE",
|
||||
`Job data query result ${JSON.stringify(result)}`
|
||||
);
|
||||
return result;
|
||||
}
|
||||
86
server/cdk/cdk.js
Normal file
86
server/cdk/cdk.js
Normal file
@@ -0,0 +1,86 @@
|
||||
const path = require("path");
|
||||
const _ = require("lodash");
|
||||
require("dotenv").config({
|
||||
path: path.resolve(
|
||||
process.cwd(),
|
||||
`.env.${process.env.NODE_ENV || "development"}`
|
||||
),
|
||||
});
|
||||
|
||||
const { io } = require("../../server");
|
||||
const { admin } = require("../firebase/firebase-handler");
|
||||
const CdkJobExport = require("./cdk-job-export").default;
|
||||
|
||||
io.use(function (socket, next) {
|
||||
try {
|
||||
if (socket.handshake.auth.token) {
|
||||
admin
|
||||
.auth()
|
||||
.verifyIdToken(socket.handshake.auth.token)
|
||||
.then((user) => {
|
||||
socket.user = user;
|
||||
next();
|
||||
})
|
||||
.catch((error) => {
|
||||
next(new Error("Authentication error", JSON.stringify(error)));
|
||||
});
|
||||
} else {
|
||||
next(new Error("Authentication error - no authorization token."));
|
||||
}
|
||||
} catch (error) {
|
||||
console.log("Uncaught connection error:::", error);
|
||||
next(new Error(`Authentication error ${error}`));
|
||||
}
|
||||
});
|
||||
|
||||
io.on("connection", (socket) => {
|
||||
socket.log_level = "DEBUG";
|
||||
createLogEvent(socket, "DEBUG", `Connected and Authenticated.`);
|
||||
|
||||
socket.on("set-log-level", (level) => {
|
||||
socket.log_level = level;
|
||||
createLogEvent(socket, "DEBUG", `Updated log level to ${level}`);
|
||||
});
|
||||
|
||||
socket.on("export-job", (jobid) => {
|
||||
CdkJobExport(socket, jobid);
|
||||
});
|
||||
|
||||
socket.on("disconnect", () => {
|
||||
createLogEvent(socket, "DEBUG", `User disconnected.`);
|
||||
});
|
||||
});
|
||||
|
||||
function createLogEvent(socket, level, message) {
|
||||
if (LogLevelHierarchy(socket.log_level) >= LogLevelHierarchy(level)) {
|
||||
console.log(
|
||||
`[CDK LOG EVENT] ${level} - ${new Date()} - ${socket.user.email} - ${
|
||||
socket.id
|
||||
} - ${message}`
|
||||
);
|
||||
socket.emit("log-event", {
|
||||
timestamp: new Date(),
|
||||
level,
|
||||
message,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function LogLevelHierarchy(level) {
|
||||
switch (level) {
|
||||
case "TRACE":
|
||||
return 5;
|
||||
case "DEBUG":
|
||||
return 4;
|
||||
case "INFO":
|
||||
return 3;
|
||||
case "WARNING":
|
||||
return 2;
|
||||
case "ERROR":
|
||||
return 1;
|
||||
default:
|
||||
return 3;
|
||||
}
|
||||
}
|
||||
|
||||
exports.createLogEvent = createLogEvent;
|
||||
@@ -116,6 +116,80 @@ query QUERY_JOBS_FOR_RECEIVABLES_EXPORT($ids: [uuid!]!) {
|
||||
}
|
||||
`;
|
||||
|
||||
exports.QUERY_JOBS_FOR_CDK_EXPORT = `
|
||||
query QUERY_JOBS_FOR_CDK_EXPORT($id: uuid!) {
|
||||
jobs_by_pk(id: $id) {
|
||||
id
|
||||
job_totals
|
||||
date_invoiced
|
||||
ro_number
|
||||
clm_total
|
||||
clm_no
|
||||
invoice_allocation
|
||||
ownerid
|
||||
ownr_ln
|
||||
ownr_fn
|
||||
ownr_addr1
|
||||
ownr_addr2
|
||||
ownr_zip
|
||||
ownr_city
|
||||
ownr_st
|
||||
ins_co_nm
|
||||
job_totals
|
||||
rate_la1
|
||||
rate_la2
|
||||
rate_la3
|
||||
rate_la4
|
||||
rate_laa
|
||||
rate_lab
|
||||
rate_lad
|
||||
rate_lae
|
||||
rate_laf
|
||||
rate_lag
|
||||
rate_lam
|
||||
rate_lar
|
||||
rate_las
|
||||
rate_lau
|
||||
rate_ma2s
|
||||
rate_ma2t
|
||||
rate_ma3s
|
||||
rate_mabl
|
||||
rate_macs
|
||||
rate_mahw
|
||||
rate_mapa
|
||||
rate_mash
|
||||
rate_matd
|
||||
class
|
||||
ca_bc_pvrt
|
||||
ca_customer_gst
|
||||
bodyshop {
|
||||
id
|
||||
md_responsibility_centers
|
||||
accountingconfig
|
||||
cdk_dealerid
|
||||
}
|
||||
owner {
|
||||
accountingid
|
||||
}
|
||||
joblines(where:{removed: {_eq:false}}) {
|
||||
id
|
||||
line_desc
|
||||
part_type
|
||||
act_price
|
||||
mod_lb_hrs
|
||||
mod_lbr_ty
|
||||
part_qty
|
||||
op_code_desc
|
||||
profitcenter_labor
|
||||
profitcenter_part
|
||||
db_ref
|
||||
prt_dsmk_p
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
`;
|
||||
|
||||
exports.QUERY_BILLS_FOR_PAYABLES_EXPORT = `
|
||||
query QUERY_BILLS_FOR_PAYABLES_EXPORT($bills: [uuid!]!) {
|
||||
bills(where: {id: {_in: $bills}}) {
|
||||
|
||||
Reference in New Issue
Block a user