fix(historical date): added so we can have all dates
This commit is contained in:
@@ -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 (
|
||||
date: string,
|
||||
includePlantToken: boolean = false,
|
||||
) => {
|
||||
console.log(date);
|
||||
// date format should always be yyyy-MM-dd or yyyy-MM-dd
|
||||
let splitDate: string[];
|
||||
if (date.includes("/")) {
|
||||
splitDate = date.split("/");
|
||||
} else if (date.includes("-")) {
|
||||
splitDate = date.split("-");
|
||||
} else {
|
||||
return {
|
||||
success: false,
|
||||
message: "An invalid date was passed over",
|
||||
data: [],
|
||||
};
|
||||
}
|
||||
// let splitDate: string[];
|
||||
// if (date.includes("/")) {
|
||||
// splitDate = date.split("/");
|
||||
// } else if (date.includes("-")) {
|
||||
// splitDate = date.split("-");
|
||||
// } else {
|
||||
// return {
|
||||
// success: false,
|
||||
// message: "An invalid date was passed over",
|
||||
// data: [],
|
||||
// };
|
||||
// }
|
||||
|
||||
const year = parseInt(splitDate[0], 10);
|
||||
const month = parseInt(splitDate[1], 10) - 1; // zero-based months
|
||||
const day = parseInt(splitDate[2], 10);
|
||||
const histDate = new Date(year, month, day);
|
||||
console.log(histDate);
|
||||
console.log(format(histDate, "yyyy-MM-dd"));
|
||||
// const year = parseInt(splitDate[0], 10);
|
||||
// const month = parseInt(splitDate[1], 10) - 1; // zero-based months
|
||||
// const day = parseInt(splitDate[2], 10);
|
||||
//const histDate = new Date(year, month, day);
|
||||
console.log(parseFlexibleDate(date));
|
||||
console.log(format(parseFlexibleDate(date), "yyyy-MM-dd"));
|
||||
const { data, error } = (await tryCatch(
|
||||
db
|
||||
.select()
|
||||
.from(invHistoricalData)
|
||||
.where(eq(invHistoricalData.histDate, format(histDate, "yyyy-MM-dd"))),
|
||||
.where(
|
||||
eq(
|
||||
invHistoricalData.histDate,
|
||||
format(parseFlexibleDate(date), "yyyy-MM-dd"),
|
||||
),
|
||||
),
|
||||
)) as any;
|
||||
|
||||
if (error) {
|
||||
|
||||
Reference in New Issue
Block a user