From 5412e5b12c603a54f21677b418c9c24c88654ae1 Mon Sep 17 00:00:00 2001 From: Herculino Trotta Date: Sun, 20 Jul 2025 00:24:31 -0300 Subject: [PATCH] feat: improve ordering when searching existing transactions on DCA creation --- app/apps/transactions/views/transactions.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/app/apps/transactions/views/transactions.py b/app/apps/transactions/views/transactions.py index dbf1967..87e08ec 100644 --- a/app/apps/transactions/views/transactions.py +++ b/app/apps/transactions/views/transactions.py @@ -4,7 +4,7 @@ from copy import deepcopy from django.contrib import messages from django.contrib.auth.decorators import login_required from django.core.paginator import Paginator -from django.db.models import Q +from django.db.models import Q, When, Case, Value, IntegerField from django.http import HttpResponse, JsonResponse from django.shortcuts import render, get_object_or_404 from django.utils import timezone @@ -606,11 +606,26 @@ def get_recent_transactions(request, filter_type=None): # Get search term from query params search_term = request.GET.get("q", "").strip() + today = timezone.localdate(timezone.now()) + yesterday = today - timezone.timedelta(days=1) + tomorrow = today + timezone.timedelta(days=1) + # Base queryset with selected fields queryset = ( Transaction.objects.filter(deleted=False) + .annotate( + date_order=Case( + When(date=today, then=Value(0)), + When(date=tomorrow, then=Value(1)), + When(date=yesterday, then=Value(2)), + When(date__gt=tomorrow, then=Value(3)), + When(date__lt=yesterday, then=Value(4)), + default=Value(5), + output_field=IntegerField(), + ) + ) .select_related("account", "category") - .order_by("-created_at") + .order_by("date_order", "date", "id") ) if filter_type: