feat(ti imports): saving the post to xml as a reference
This commit is contained in:
@@ -1,11 +1,14 @@
|
||||
import axios from "axios";
|
||||
import querystring from "querystring";
|
||||
import { createLog } from "../../../../logger/logger.js";
|
||||
import { saveXml } from "../../../utils/saveXml.js";
|
||||
//tiCreds
|
||||
const userid = process.env.USERID || "";
|
||||
const password = process.env.PASSWORD || "";
|
||||
|
||||
export const postToTi = async (data: string) => {
|
||||
export const postToTi = async (data: string, fileName: string) => {
|
||||
// 🔹 Save the XML before posting
|
||||
saveXml(data, fileName);
|
||||
const formBody = querystring.stringify({
|
||||
userid,
|
||||
password,
|
||||
|
||||
@@ -272,7 +272,14 @@ export const tiImport = async () => {
|
||||
|
||||
//console.log("payload", payload);
|
||||
|
||||
const { data: tiPost, error: tiError } = await tryCatch(postToTi(payload));
|
||||
const { data: tiPost, error: tiError } = await tryCatch(
|
||||
postToTi(
|
||||
payload,
|
||||
`${orderData[0].addressAlias.replaceAll(" ", "")}-${
|
||||
header[0].releaseNumber
|
||||
}`
|
||||
)
|
||||
);
|
||||
|
||||
if (tiError) {
|
||||
return {
|
||||
|
||||
47
lstV2/server/services/notifications/utils/saveXml.ts
Normal file
47
lstV2/server/services/notifications/utils/saveXml.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
import { createLog } from "../../logger/logger.js";
|
||||
|
||||
/**
|
||||
* Save XML string to /xml/ with timestamp in name
|
||||
* @param xmlContent The XML string
|
||||
* @param prefix File prefix (ex: request or response)
|
||||
*/
|
||||
export const saveXml = (xmlContent: string, prefix: string = "request") => {
|
||||
try {
|
||||
const now = new Date();
|
||||
|
||||
// Format YYYYMMDDHHmm
|
||||
const timestamp = `${now.getFullYear()}${String(
|
||||
now.getMonth() + 1
|
||||
).padStart(2, "0")}-${String(now.getDate()).padStart(2, "0")}-${String(
|
||||
now.getHours()
|
||||
).padStart(2, "0")}-${String(now.getMinutes()).padStart(2, "0")}`;
|
||||
|
||||
// Ensure xml folder path is always relative to project root
|
||||
const xmlDir = path.resolve(process.cwd(), "xml");
|
||||
const filePath = path.join(xmlDir, `${prefix}_${timestamp}.xml`);
|
||||
|
||||
// Make sure xml folder exists
|
||||
if (!fs.existsSync(xmlDir)) {
|
||||
fs.mkdirSync(xmlDir, { recursive: true });
|
||||
}
|
||||
|
||||
// Save file
|
||||
fs.writeFileSync(filePath, xmlContent, "utf-8");
|
||||
|
||||
createLog(
|
||||
"info",
|
||||
"notification",
|
||||
"notification",
|
||||
`XML saved to: ${filePath}`
|
||||
);
|
||||
} catch (error) {
|
||||
createLog(
|
||||
"error",
|
||||
"notification",
|
||||
"notification",
|
||||
`Error saving XML:", ${JSON.stringify(error)}`
|
||||
);
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user