99 lines
3.7 KiB
TypeScript
99 lines
3.7 KiB
TypeScript
import log from "electron-log/main";
|
|
import path from "path";
|
|
import errorTypeCheck from "../../util/errorTypeCheck";
|
|
import DecodeAD1 from "./decode-ad1";
|
|
import { DecodedAd1 } from "./decode-ad1.interface";
|
|
import DecodeAD2 from "./decode-ad2";
|
|
import { DecodedAD2 } from "./decode-ad2.interface";
|
|
import DecodeLin from "./decode-lin";
|
|
import { DecodedLin } from "./decode-lin.interface";
|
|
import DecodePfh from "./decode-pfh";
|
|
import { DecodedPfh } from "./decode-pfh.interface";
|
|
import DecodePfl from "./decode-pfl";
|
|
import { DecodedPfl } from "./decode-pfl.interface";
|
|
import DecodePfm from "./decode-pfm";
|
|
import { DecodedPfm } from "./decode-pfm.interface";
|
|
import DecodePfo from "./decode-pfo";
|
|
import { DecodedPfo } from "./decode-pfo.interface";
|
|
import DecodePfp from "./decode-pfp";
|
|
import { DecodedPfp } from "./decode-pfp.interface";
|
|
import DecodePft from "./decode-pft";
|
|
import { DecodedPft } from "./decode-pft.interface";
|
|
import DecodeStl from "./decode-stl";
|
|
import { DecodedStl } from "./decode-stl.interface";
|
|
import DecodeTtl from "./decode-ttl";
|
|
import { DecodedTtl } from "./decode-ttl.interface";
|
|
import DecodeVeh from "./decode-veh";
|
|
import { DecodedVeh } from "./decode-veh.interface";
|
|
import { DecodedEnv } from "./decode-env.interface";
|
|
import DecodeEnv from "./decode-env";
|
|
import fs from "fs";
|
|
|
|
async function ImportJob(filepath: string): Promise<void> {
|
|
const parsedFilePath = path.parse(filepath);
|
|
const extensionlessFilePath = path.join(
|
|
parsedFilePath.dir,
|
|
parsedFilePath.name
|
|
);
|
|
log.debug("Importing Job", extensionlessFilePath);
|
|
|
|
try {
|
|
//The below all end up returning parts of the job object.
|
|
//Some of them return additional info - e.g. owner or vehicle record data at both the job and corresponding table level.
|
|
const env: DecodedEnv = await DecodeEnv(extensionlessFilePath);
|
|
const ad1: DecodedAd1 = await DecodeAD1(extensionlessFilePath);
|
|
const ad2: DecodedAD2 = await DecodeAD2(extensionlessFilePath);
|
|
const veh: DecodedVeh = await DecodeVeh(extensionlessFilePath);
|
|
const lin: DecodedLin = await DecodeLin(extensionlessFilePath);
|
|
const pfh: DecodedPfh = await DecodePfh(extensionlessFilePath);
|
|
const pfl: DecodedPfl = await DecodePfl(extensionlessFilePath);
|
|
const pft: DecodedPft = await DecodePft(extensionlessFilePath);
|
|
const pfm: DecodedPfm = await DecodePfm(extensionlessFilePath);
|
|
const pfo: DecodedPfo = await DecodePfo(extensionlessFilePath); // TODO: This will be the `cieca_pfo` object
|
|
const stl: DecodedStl = await DecodeStl(extensionlessFilePath); // TODO: This will be the `cieca_stl` object
|
|
const ttl: DecodedTtl = await DecodeTtl(extensionlessFilePath);
|
|
const pfp: DecodedPfp = await DecodePfp(extensionlessFilePath);
|
|
|
|
const jobObject = {
|
|
...env,
|
|
...ad1,
|
|
...ad2,
|
|
...veh,
|
|
...lin,
|
|
...pfh,
|
|
...pfl,
|
|
...pft,
|
|
...pfm,
|
|
...pfo,
|
|
...stl,
|
|
...ttl,
|
|
...pfp,
|
|
};
|
|
|
|
// Save jobObject to a timestamped JSON file
|
|
const timestamp = new Date()
|
|
.toISOString()
|
|
.replace(/:/g, "-")
|
|
.replace(/\..+/, "");
|
|
const fileName = `job_${timestamp}_${parsedFilePath.name}.json`;
|
|
const logsDir = path.join(process.cwd(), "logs");
|
|
|
|
// Create logs directory if it doesn't exist
|
|
if (!fs.existsSync(logsDir)) {
|
|
fs.mkdirSync(logsDir, { recursive: true });
|
|
}
|
|
|
|
const filePath = path.join(logsDir, fileName);
|
|
fs.writeFileSync(filePath, JSON.stringify(jobObject, null, 2), "utf8");
|
|
log.info(`Job data saved to: ${filePath}`);
|
|
|
|
log.debug("Job Object", {
|
|
jobObject,
|
|
});
|
|
} catch (error) {
|
|
log.error("Error encountered while decoding job. ", errorTypeCheck(error));
|
|
}
|
|
}
|
|
|
|
export default ImportJob;
|