diff --git a/lstV2/server/services/notifications/controller/notifications/tiFullFlow/postToTI.ts b/lstV2/server/services/notifications/controller/notifications/tiFullFlow/postToTI.ts index 9db6160..8753c1d 100644 --- a/lstV2/server/services/notifications/controller/notifications/tiFullFlow/postToTI.ts +++ b/lstV2/server/services/notifications/controller/notifications/tiFullFlow/postToTI.ts @@ -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, diff --git a/lstV2/server/services/notifications/controller/notifications/tiFullFlow/tiImport.ts b/lstV2/server/services/notifications/controller/notifications/tiFullFlow/tiImport.ts index 4c36b50..c22cda5 100644 --- a/lstV2/server/services/notifications/controller/notifications/tiFullFlow/tiImport.ts +++ b/lstV2/server/services/notifications/controller/notifications/tiFullFlow/tiImport.ts @@ -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 { diff --git a/lstV2/server/services/notifications/utils/saveXml.ts b/lstV2/server/services/notifications/utils/saveXml.ts new file mode 100644 index 0000000..743a194 --- /dev/null +++ b/lstV2/server/services/notifications/utils/saveXml.ts @@ -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)}` + ); + } +};