import { format } from "date-fns-tz";
import { createLog } from "../../logger/logger.js";
import { query } from "../../sqlServer/prodSqlServer.js";
import { fakeEDIUpdate } from "../../sqlServer/querys/dataMart/fakeEDIUpdate.js";

export const getFakeEDI = async (address: string) => {
    let fakeEDI: any = [];

    let updatedQuery = fakeEDIUpdate;

    if (address) {
        createLog(
            "info",
            "datamart",
            "datamart",
            "The user requested a specific address."
        );
        updatedQuery = fakeEDIUpdate.replace(
            "--and IdAdresse = 14",
            `and IdAdresse = ${address}`
        );
    }

    try {
        fakeEDI = await query(updatedQuery, "Gets fakeEDI orders to be fixed");

        const correctedData = fakeEDI.data.map((n: any) => {
            return {
                ...n,
                DeliveryDate: format(n.DeliveryDate, "M/d/yyyy HH:mm"),
            };
        });

        return {
            success: true,
            message: "Current open orders",
            data: correctedData,
        };
    } catch (error) {
        console.log(error);
        return {
            success: false,
            message: "There was an error open orders",
            data: error,
        };
    }
};
