calcuation fixes
This commit is contained in:
@@ -10,32 +10,41 @@ const DEFAULT_ICON = React.createElement(MonetizationOnIcon, {
|
||||
});
|
||||
|
||||
// ---------------- LATEST ----------------
|
||||
export async function fetchLatestExpenses(): Promise<LatestItem[]> {
|
||||
export async function fetchLatestTransactions(
|
||||
type: "expense" | "income"
|
||||
): Promise<LatestItem[]> {
|
||||
const res = await api.get('/expenses', {
|
||||
params: { limit: 10, sort: '-occurred_at' }
|
||||
params: { limit: 100, sort: '-occurred_at' }
|
||||
});
|
||||
|
||||
const items = res.data?.items || res.data || [];
|
||||
|
||||
return items.map((exp: any, index: number) => {
|
||||
const time = new Date(
|
||||
exp.occurred_at || exp.created_at || Date.now()
|
||||
).getTime();
|
||||
const isValid = (amt: number) =>
|
||||
type === "expense" ? amt < 0 : amt > 0;
|
||||
|
||||
const diffDays = Math.floor(
|
||||
Math.abs(Date.now() - time) / (1000 * 60 * 60 * 24)
|
||||
);
|
||||
return items
|
||||
.filter((item: any) => isValid(Number(item.amount) || 0))
|
||||
.slice(0, 10)
|
||||
.map((exp: any, index: number) => {
|
||||
const time = new Date(
|
||||
exp.occurred_at || exp.created_at || Date.now()
|
||||
).getTime();
|
||||
|
||||
return {
|
||||
id: exp.id || index,
|
||||
icon: DEFAULT_ICON,
|
||||
iconBgColor: "#e8f5e9",
|
||||
title: exp.payee?.name || exp.payee || "Unknown Payee",
|
||||
subtitle: exp.category?.name || exp.account?.name || "Transaction",
|
||||
amount: `Rs ${exp.amount || 0}`,
|
||||
timeAgo: diffDays === 0 ? "Today" : `${diffDays} days ago`
|
||||
};
|
||||
});
|
||||
const diffDays = Math.floor(
|
||||
Math.abs(Date.now() - time) / (1000 * 60 * 60 * 24)
|
||||
);
|
||||
|
||||
return {
|
||||
id: exp.id || index,
|
||||
icon: DEFAULT_ICON,
|
||||
iconBgColor:
|
||||
type === "expense" ? "#ffebee" : "#e8f5e9",
|
||||
title: exp.payee?.name || exp.payee || "Unknown Payee",
|
||||
subtitle: exp.category?.name || exp.account?.name || "Transaction",
|
||||
amount: `Rs ${Math.abs(exp.amount || 0)}`,
|
||||
timeAgo: diffDays === 0 ? "Today" : `${diffDays} days ago`
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
// ---------------- TYPES ----------------
|
||||
@@ -62,12 +71,6 @@ export async function fetchAggregatedData(
|
||||
|
||||
const normalize = (amt: number) => Math.abs(amt);
|
||||
|
||||
// ---------------- BUCKETS ----------------
|
||||
const todayBuckets: Record<string, number> = {
|
||||
"12am":0,"3am":0,"6am":0,"9am":0,
|
||||
"12pm":0,"3pm":0,"6pm":0,"9pm":0
|
||||
};
|
||||
|
||||
const weekBuckets: Record<string, number> = {
|
||||
"Mon":0,"Tue":0,"Wed":0,"Thu":0,
|
||||
"Fri":0,"Sat":0,"Sun":0
|
||||
@@ -134,25 +137,6 @@ export async function fetchAggregatedData(
|
||||
const payee = item.payee?.name || item.payee || "Unknown";
|
||||
payeeMap[payee] = (payeeMap[payee] || 0) + amt;
|
||||
|
||||
// ---- TODAY
|
||||
if (
|
||||
d.getDate() === now.getDate() &&
|
||||
d.getMonth() === now.getMonth() &&
|
||||
d.getFullYear() === now.getFullYear()
|
||||
) {
|
||||
const hr = d.getHours();
|
||||
let label = "12am";
|
||||
if (hr >= 3 && hr < 6) label = "3am";
|
||||
else if (hr >= 6 && hr < 9) label = "6am";
|
||||
else if (hr >= 9 && hr < 12) label = "9am";
|
||||
else if (hr >= 12 && hr < 15) label = "12pm";
|
||||
else if (hr >= 15 && hr < 18) label = "3pm";
|
||||
else if (hr >= 18 && hr < 21) label = "6pm";
|
||||
else if (hr >= 21) label = "9pm";
|
||||
|
||||
todayBuckets[label] += amt;
|
||||
}
|
||||
|
||||
// ---- WEEK
|
||||
if (d >= weekStart && d <= weekEnd) {
|
||||
const day = ["Sun","Mon","Tue","Wed","Thu","Fri","Sat"][d.getDay()];
|
||||
@@ -192,7 +176,6 @@ export async function fetchAggregatedData(
|
||||
}));
|
||||
|
||||
const chartData = {
|
||||
today: toPoints(todayBuckets),
|
||||
week: toPoints(weekBuckets),
|
||||
month: toPoints(monthBuckets),
|
||||
year: toPoints(yearBuckets)
|
||||
|
||||
Reference in New Issue
Block a user