fix(historical date): added so we can have all dates

This commit is contained in:
2025-11-10 15:14:19 -06:00
parent 247010d48f
commit 9d793d2205

View File

@@ -76,36 +76,65 @@ const missingHistoricalData = async (date: string) => {
} }
}; };
function parseFlexibleDate(date: string): Date {
const parts = date.replace(/[-.]/g, "/").split("/");
if (parts.length !== 3) throw new Error("Invalid date format");
let year: number, month: number, day: number;
// find which index looks like a year
if (parts[0].length === 4) {
// yyyy/mm/dd
year = +parts[0];
month = +parts[1];
day = +parts[2];
} else if (parts[2].length === 4) {
// mm/dd/yyyy
year = +parts[2];
month = +parts[0];
day = +parts[1];
} else {
throw new Error("Cannot determine year position");
}
return new Date(year, month - 1, day);
}
export const historicalInvByDate = async ( export const historicalInvByDate = async (
date: string, date: string,
includePlantToken: boolean = false, includePlantToken: boolean = false,
) => { ) => {
console.log(date); console.log(date);
// date format should always be yyyy-MM-dd or yyyy-MM-dd // date format should always be yyyy-MM-dd or yyyy-MM-dd
let splitDate: string[]; // let splitDate: string[];
if (date.includes("/")) { // if (date.includes("/")) {
splitDate = date.split("/"); // splitDate = date.split("/");
} else if (date.includes("-")) { // } else if (date.includes("-")) {
splitDate = date.split("-"); // splitDate = date.split("-");
} else { // } else {
return { // return {
success: false, // success: false,
message: "An invalid date was passed over", // message: "An invalid date was passed over",
data: [], // data: [],
}; // };
} // }
const year = parseInt(splitDate[0], 10); // const year = parseInt(splitDate[0], 10);
const month = parseInt(splitDate[1], 10) - 1; // zero-based months // const month = parseInt(splitDate[1], 10) - 1; // zero-based months
const day = parseInt(splitDate[2], 10); // const day = parseInt(splitDate[2], 10);
const histDate = new Date(year, month, day); //const histDate = new Date(year, month, day);
console.log(histDate); console.log(parseFlexibleDate(date));
console.log(format(histDate, "yyyy-MM-dd")); console.log(format(parseFlexibleDate(date), "yyyy-MM-dd"));
const { data, error } = (await tryCatch( const { data, error } = (await tryCatch(
db db
.select() .select()
.from(invHistoricalData) .from(invHistoricalData)
.where(eq(invHistoricalData.histDate, format(histDate, "yyyy-MM-dd"))), .where(
eq(
invHistoricalData.histDate,
format(parseFlexibleDate(date), "yyyy-MM-dd"),
),
),
)) as any; )) as any;
if (error) { if (error) {