feature/IO-2776-cdk-fortellis - Cleanup + Auth Socket stuff
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
const GraphQLClient = require("graphql-request").GraphQLClient;
|
||||
|
||||
const queries = require("../../graphql-client/queries");
|
||||
const WsLogger = require("../../web-sockets/createLogEvent")
|
||||
const WsLogger = require("../../web-sockets/createLogEvent");
|
||||
const moment = require("moment");
|
||||
const Dinero = require("dinero.js");
|
||||
const AxiosLib = require("axios").default;
|
||||
@@ -18,8 +18,9 @@ axios.interceptors.request.use((x) => {
|
||||
...x.headers
|
||||
};
|
||||
|
||||
const printable = `${new Date()} | Request: ${x.method.toUpperCase()} | ${x.url
|
||||
} | ${JSON.stringify(x.data)} | ${JSON.stringify(headers)}`;
|
||||
const printable = `${new Date()} | Request: ${x.method.toUpperCase()} | ${
|
||||
x.url
|
||||
} | ${JSON.stringify(x.data)} | ${JSON.stringify(headers)}`;
|
||||
//console.log(printable);
|
||||
|
||||
WsLogger.createJsonEvent(socket, "SILLY", `Raw Request: ${printable}`, x.data);
|
||||
@@ -146,7 +147,9 @@ async function PbsCalculateAllocationsAp(socket, billids) {
|
||||
...billHash[key],
|
||||
Amount: billHash[key].Amount.toFormat("0.00")
|
||||
});
|
||||
APAmount = APAmount.add(billHash[key].Amount); //Calculate the total expense for the bill iteratively to create the corresponding credit to AP.
|
||||
//Calculate the total expense for the bill iteratively to
|
||||
// create the corresponding credit to AP.
|
||||
APAmount = APAmount.add(billHash[key].Amount);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -174,9 +177,13 @@ exports.PbsCalculateAllocationsAp = PbsCalculateAllocationsAp;
|
||||
async function QueryBillData(socket, billids) {
|
||||
WsLogger.createLogEvent(socket, "DEBUG", `Querying bill data for id(s) ${billids}`);
|
||||
const client = new GraphQLClient(process.env.GRAPHQL_ENDPOINT, {});
|
||||
const currentToken =
|
||||
(socket?.data && socket.data.authToken) || (socket?.handshake?.auth && socket.handshake.auth.token);
|
||||
|
||||
const result = await client
|
||||
.setHeaders({ Authorization: `Bearer ${socket.handshake.auth.token}` })
|
||||
.setHeaders({ Authorization: `Bearer ${currentToken}` })
|
||||
.request(queries.GET_PBS_AP_ALLOCATIONS, { billids: billids });
|
||||
|
||||
WsLogger.createLogEvent(socket, "SILLY", `Bill data query result ${JSON.stringify(result, null, 2)}`);
|
||||
|
||||
return result;
|
||||
@@ -191,7 +198,32 @@ function getCostAccount(billline, respcenters) {
|
||||
return respcenters.costs.find((c) => c.name === acctName);
|
||||
}
|
||||
|
||||
exports.PbsExportAp = async function (socket, { billids, txEnvelope }) {
|
||||
async function MarkApExported(socket, billids) {
|
||||
WsLogger.createLogEvent(socket, "DEBUG", `Marking bills as exported for id ${billids}`);
|
||||
const client = new GraphQLClient(process.env.GRAPHQL_ENDPOINT, {});
|
||||
const currentToken =
|
||||
(socket?.data && socket.data.authToken) || (socket?.handshake?.auth && socket.handshake.auth.token);
|
||||
|
||||
const result = await client
|
||||
.setHeaders({ Authorization: `Bearer ${currentToken}` })
|
||||
.request(queries.MARK_BILLS_EXPORTED, {
|
||||
billids,
|
||||
bill: {
|
||||
exported: true,
|
||||
exported_at: new Date()
|
||||
},
|
||||
logs: socket.bills.map((bill) => ({
|
||||
bodyshopid: socket.bodyshop.id,
|
||||
billid: bill.id,
|
||||
successful: true,
|
||||
useremail: socket.user.email
|
||||
}))
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
const defaultHandler = async (socket, { billids, txEnvelope }) => {
|
||||
WsLogger.createLogEvent(socket, "DEBUG", `Exporting selected AP.`);
|
||||
|
||||
//apAllocations has the same shap as the lines key for the accounting posting to PBS.
|
||||
@@ -222,24 +254,4 @@ exports.PbsExportAp = async function (socket, { billids, txEnvelope }) {
|
||||
socket.emit("ap-export-complete");
|
||||
};
|
||||
|
||||
async function MarkApExported(socket, billids) {
|
||||
WsLogger.createLogEvent(socket, "DEBUG", `Marking bills as exported for id ${billids}`);
|
||||
const client = new GraphQLClient(process.env.GRAPHQL_ENDPOINT, {});
|
||||
const result = await client
|
||||
.setHeaders({ Authorization: `Bearer ${socket.handshake.auth.token}` })
|
||||
.request(queries.MARK_BILLS_EXPORTED, {
|
||||
billids,
|
||||
bill: {
|
||||
exported: true,
|
||||
exported_at: new Date()
|
||||
},
|
||||
logs: socket.bills.map((bill) => ({
|
||||
bodyshopid: socket.bodyshop.id,
|
||||
billid: bill.id,
|
||||
successful: true,
|
||||
useremail: socket.user.email
|
||||
}))
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
||||
exports.PbsExportAp = defaultHandler;
|
||||
|
||||
Reference in New Issue
Block a user