Skip to content

Commit 57fcd15

Browse files
authored
fix: how monthly total is calculated (#98)
1 parent 5acf71d commit 57fcd15

File tree

6 files changed

+16
-18
lines changed

6 files changed

+16
-18
lines changed

frontend/components/custom/CategoryAnalytics/CategoryAnalytics.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ export function CategoryAnalytics({ data }: CategoryAnalyticsProps) {
133133
<CardContent className="space-y-6">
134134
{/* Horizontal Progress Bar */}
135135
<div className="space-y-4">
136-
<div className="h-4 bg-gray-200 rounded-full overflow-hidden">
136+
<div className="h-4 rounded-full overflow-hidden">
137137
{categoriesWithPercentages.map((category) => (
138138
<div
139139
key={category.category_id}

frontend/components/custom/Dashboard/MonthlyAnalyticsCard.tsx

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@ import { useMonthlyAnalytics } from "@/components/hooks/useAnalytics";
44
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
55
import { Skeleton } from "@/components/ui/skeleton";
66
import { formatCurrency } from "@/lib/utils";
7-
import { CalendarIcon, TrendingDownIcon, TrendingUpIcon } from "lucide-react";
7+
import {
8+
ArrowRightLeftIcon,
9+
CalendarIcon,
10+
TrendingDownIcon,
11+
TrendingUpIcon,
12+
} from "lucide-react";
813
import { useRef, useState } from "react";
914

1015
interface MonthlyData {
@@ -215,9 +220,12 @@ export function MonthlyAnalyticsCard() {
215220

216221
{/* Net Amount */}
217222
<div className="flex items-center justify-between p-2 bg-blue-50 dark:bg-blue-950/20 rounded-lg">
218-
<span className="text-xs font-medium text-blue-700 dark:text-blue-400">
219-
Net Flow
220-
</span>
223+
<div className="flex items-center gap-2">
224+
<ArrowRightLeftIcon className="h-3 w-3 text-blue-600" />
225+
<span className="text-xs font-medium text-blue-700 dark:text-blue-400">
226+
Net Flow
227+
</span>
228+
</div>
221229
<span className="text-xs font-semibold text-blue-800 dark:text-blue-300">
222230
{formatCurrency(item.data.total_amount)}
223231
</span>

server/internal/api/controller/analytics_controller_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,10 +1074,9 @@ var _ = Describe("AnalyticsController", func() {
10741074
Expect(hasAmount).To(BeTrue())
10751075
Expect(totalAmount).To(BeAssignableToTypeOf(float64(0)))
10761076

1077-
// Verify values are non-negative (business logic check)
10781077
Expect(totalIncome.(float64)).To(BeNumerically(">=", 0))
10791078
Expect(totalExpenses.(float64)).To(BeNumerically(">=", 0))
1080-
Expect(totalAmount.(float64)).To(BeNumerically(">=", 0))
1079+
Expect(totalAmount.(float64)).To(BeNumerically("==", totalIncome.(float64)-totalExpenses.(float64)))
10811080
})
10821081

10831082
It("should return different results for different users", func() {

server/internal/repository/analytics_repository.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,7 @@ func (r *AnalyticsRepository) GetMonthlyAnalytics(ctx context.Context, userId in
209209
if err != nil {
210210
return nil, err
211211
}
212-
213-
// Calculate total amount (income + expenses, treating income as positive)
214-
totalAmount := totalIncome + totalExpenses
212+
totalAmount := totalIncome - totalExpenses
215213

216214
return &models.MonthlyAnalyticsResponse{
217215
TotalIncome: totalIncome,

server/internal/service/analytics_service.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,5 @@ func (s *AnalyticsService) GetMonthlyAnalytics(ctx context.Context, userId int64
155155
return nil, fmt.Errorf("end date must be after or equal to start date")
156156
}
157157

158-
// Call repository to get the analytics
159-
analytics, err := s.analyticsRepo.GetMonthlyAnalytics(ctx, userId, startDate, endDate)
160-
if err != nil {
161-
return nil, fmt.Errorf("failed to get monthly analytics: %w", err)
162-
}
163-
164-
return analytics, nil
158+
return s.analyticsRepo.GetMonthlyAnalytics(ctx, userId, startDate, endDate)
165159
}

server/internal/service/analytics_service_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -837,7 +837,6 @@ var _ = Describe("AnalyticsService", func() {
837837
It("should return error", func() {
838838
_, err := analyticsService.GetMonthlyAnalytics(ctx, userId, startDate, endDate)
839839
Expect(err).To(HaveOccurred())
840-
Expect(err.Error()).To(ContainSubstring("failed to get monthly analytics"))
841840
})
842841
})
843842

0 commit comments

Comments
 (0)