fix(admin): resolve latest job selection by effective timestamp

This commit is contained in:
2026-04-14 11:01:52 +02:00
parent 148c4862d7
commit bb58e91b83
2 changed files with 20 additions and 5 deletions

View File

@@ -2,6 +2,8 @@ from apps.core.models import BackgroundJob
from apps.core.services import BackgroundJobService
from django.contrib.auth import authenticate, get_user_model
from django.contrib.auth.hashers import check_password
from django.db.models import F
from django.db.models.functions import Coalesce
from drf_yasg import openapi
from drf_yasg.utils import swagger_auto_schema
from rest_framework import generics, status
@@ -249,7 +251,14 @@ class AdminUsersManagementView(APIView):
latest_jobs: dict[int, BackgroundJob] = {}
jobs = BackgroundJobService.get_queryset().filter(
user_id__in=user_ids
).order_by("user_id", "-created_at")
).annotate(
_effective_job_ts=Coalesce(
F("completed_at"),
F("started_at"),
F("updated_at"),
F("created_at"),
)
).order_by("user_id", "-_effective_job_ts")
for job in jobs:
if job.user_id not in latest_jobs: