Files
lst/lstV2/server/services/eom/controller/getHistoricalInvByDate.ts

56 lines
1.5 KiB
TypeScript

import { eq } from "drizzle-orm";
import { db } from "../../../../database/dbclient.js";
import { invHistoricalData } from "../../../../database/schema/historicalINV.js";
import { tryCatch } from "../../../globalUtils/tryCatch.js";
import { format } from "date-fns";
import { settings } from "../../../../database/schema/settings.js";
export const historicalInvByDate = async (
date: string,
includePlantToken: boolean = false
) => {
const histDate = new Date(date);
const { data, error } = (await tryCatch(
db
.select()
.from(invHistoricalData)
.where(
eq(invHistoricalData.histDate, format(histDate, "yyyy-MM-dd"))
)
)) as any;
if (error) {
return {
success: false,
message: "There was an error with getting the inventory",
data: error,
};
}
if (includePlantToken) {
const { data: s, error: se } = (await tryCatch(
db.select().from(settings).where(eq(settings.name, "plantToken"))
)) as any;
if (se) {
console.log("Error getting articles");
return data;
}
return {
success: true,
message: `Historical inventory for ${date}`,
data: data.map((n: any) => {
return { plantToken: s[0].value, ...n };
}),
};
} else {
return {
success: true,
message: `Historical inventory for ${date}`,
data: data,
};
}
};