From 6fa2df7c0ad2dde4cbd2f69c003a5b8346348468 Mon Sep 17 00:00:00 2001 From: Heibert Date: Wed, 23 Oct 2024 17:14:56 -0500 Subject: [PATCH] Fix goals history --- INSIGHTSAPI/coexistence_committee/tests.py | 1 + INSIGHTSAPI/goals/views.py | 37 ++++++++- INSIGHTSAPI/vacation/tests.py | 96 ++++++++++++++-------- INSIGHTSAPI/vacation/views.py | 2 +- 4 files changed, 95 insertions(+), 41 deletions(-) diff --git a/INSIGHTSAPI/coexistence_committee/tests.py b/INSIGHTSAPI/coexistence_committee/tests.py index 490f8ad..44f01e5 100644 --- a/INSIGHTSAPI/coexistence_committee/tests.py +++ b/INSIGHTSAPI/coexistence_committee/tests.py @@ -1,5 +1,6 @@ from django.contrib.auth.models import Group from django.urls import reverse + from services.tests import BaseTestCase from users.models import User diff --git a/INSIGHTSAPI/goals/views.py b/INSIGHTSAPI/goals/views.py index dec372f..c9a87c2 100644 --- a/INSIGHTSAPI/goals/views.py +++ b/INSIGHTSAPI/goals/views.py @@ -310,14 +310,44 @@ def get_queryset(self): elif campaign is not None and date is not None and column is not None: if column == "delivery": column_name = "goal_date" + campaign_type = "campaign_goal" elif column == "execution": column_name = "execution_date" + campaign_type = "campaign_execution" else: return self.queryset.none() - return self.queryset.filter( - Q(campaign_goal=campaign.upper()) - & Q(**{f"{column_name}": date.upper()}) + filter_params = {f"{column_name}": date.upper()} + latest_history_dates = ( + HistoricalGoals.objects.filter(Q(**filter_params)) + .values("cedula") + .annotate(max_history_date=Max("history_date")) ) + # Filter records with the latest history_date + if column == "delivery": + history_goals = HistoricalGoals.objects.filter( + Q( + history_date__in=Subquery( + latest_history_dates.values("max_history_date") + ) + ) + & Q(campaign_goal=campaign.upper()) + & Q(cedula__in=latest_history_dates.values("cedula")) + ) + else: + history_goals = HistoricalGoals.objects.filter( + Q( + history_date__in=Subquery( + latest_history_dates.values("max_history_date") + ) + ) + & Q(campaign_execution=campaign.upper()) + & Q(cedula__in=latest_history_dates.values("cedula")) + ) + return history_goals + # return self.queryset.filter( + # Q(campaign_goal=campaign.upper()) + # & Q(**{f"{column_name}": date.upper()}) + # ) elif date is not None and column is not None: if column == "delivery": column_name = "goal_date" @@ -740,4 +770,3 @@ def format_cell_value(cell): {"message": "Excel no encontrado."}, status=framework_status.HTTP_400_BAD_REQUEST, ) - diff --git a/INSIGHTSAPI/vacation/tests.py b/INSIGHTSAPI/vacation/tests.py index eb69a58..e8aa5fc 100644 --- a/INSIGHTSAPI/vacation/tests.py +++ b/INSIGHTSAPI/vacation/tests.py @@ -133,28 +133,33 @@ def test_vacation_list_user(self): self.assertEqual(len(response.data), 1) self.assertEqual(response.data[0]["user"], self.user.get_full_name()) - def test_vacation_list_hr(self): - """Test listing all vacations endpoint for HR.""" - self.user.job_position.name = "GERENTE DE GESTION HUMANA" + def test_vacation_list_boss(self): + """Test listing all vacations endpoint for a boss.""" + self.user.job_position.rank = 2 self.user.job_position.save() + self.test_user.area = self.user.area + self.test_user.save() self.vacation_request["user"] = self.test_user self.vacation_request["uploaded_by"] = self.test_user VacationRequest.objects.create(**self.vacation_request) + self.vacation_request["user"] = self.user + self.vacation_request["uploaded_by"] = self.user + VacationRequest.objects.create(**self.vacation_request) response = self.client.get(reverse("vacation-list")) - vacation_requests = VacationRequest.objects.all() - serializer = VacationRequestSerializer(vacation_requests, many=True) - self.assertEqual(response.data, serializer.data) self.assertEqual(response.status_code, status.HTTP_200_OK) + self.assertEqual(len(response.data), 2) def test_vacation_list_manager(self): """Test listing all vacations endpoint for a manager.""" self.user.job_position.rank = 5 self.user.job_position.save() self.vacation_request["user"] = self.test_user - self.vacation_request["uploaded_by"] = self.user + self.vacation_request["uploaded_by"] = self.test_user + self.test_user.area.manager = self.user + self.test_user.area.save() VacationRequest.objects.create(**self.vacation_request) self.vacation_request["user"] = self.user - self.vacation_request["uploaded_by"] = self.test_user + self.vacation_request["uploaded_by"] = self.user VacationRequest.objects.create(**self.vacation_request) # Change the area of the test user to match the user's area demo_user_admin = self.create_demo_user_admin() @@ -163,18 +168,9 @@ def test_vacation_list_manager(self): demo_user_admin.job_position.rank = 1 demo_user_admin.job_position.save() self.vacation_request["user"] = demo_user_admin - self.vacation_request["uploaded_by"] = self.test_user + self.vacation_request["uploaded_by"] = demo_user_admin VacationRequest.objects.create(**self.vacation_request) response = self.client.get(reverse("vacation-list")) - vacation_requests = VacationRequest.objects.filter( - (Q(uploaded_by=self.user) | Q(user=self.user)) - | ( - Q(user__job_position__rank__lt=self.user.job_position.rank) - & Q(user__area=self.user.area) - ) - ).order_by("-created_at") - serializer = VacationRequestSerializer(vacation_requests, many=True) - self.assertEqual(response.data, serializer.data) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(len(response.data), 3, response.data) @@ -185,7 +181,7 @@ def test_vacation_list_manager_multiple_areas(self): # Check that the user has a different area than the manager self.assertNotEqual(self.test_user.area, self.user.area) self.vacation_request["user"] = self.test_user - self.vacation_request["uploaded_by"] = self.user + self.vacation_request["uploaded_by"] = self.test_user VacationRequest.objects.create(**self.vacation_request) demo_user = self.create_demo_user() Area.objects.create(name="Test Area", manager=self.user) @@ -195,10 +191,23 @@ def test_vacation_list_manager_multiple_areas(self): self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(len(response.data), 2) + def test_vacation_list_hr(self): + """Test listing all vacations endpoint for HR.""" + self.user.job_position.name = "GERENTE DE GESTION HUMANA" + self.user.job_position.save() + self.vacation_request["user"] = self.test_user + self.vacation_request["uploaded_by"] = self.test_user + VacationRequest.objects.create(**self.vacation_request) + response = self.client.get(reverse("vacation-list")) + vacation_requests = VacationRequest.objects.all() + serializer = VacationRequestSerializer(vacation_requests, many=True) + self.assertEqual(response.data, serializer.data) + self.assertEqual(response.status_code, status.HTTP_200_OK) + def test_vacation_retrieve(self): """Test retrieving a vacation endpoint.""" self.vacation_request["user"] = self.test_user - self.vacation_request["uploaded_by"] = self.user + self.vacation_request["uploaded_by"] = self.test_user vacation_object = VacationRequest.objects.create(**self.vacation_request) response = self.client.get( reverse("vacation-detail", kwargs={"pk": vacation_object.pk}) @@ -272,7 +281,7 @@ def test_vacation_create_same_user_rank_gte_4(self): def test_vacation_owner_cancel(self): """Test the owner cancelling a vacation.""" self.vacation_request["user"] = self.user - self.vacation_request["uploaded_by"] = self.test_user + self.vacation_request["uploaded_by"] = self.user vacation_object = VacationRequest.objects.create(**self.vacation_request) response = self.client.patch( reverse("vacation-detail", kwargs={"pk": vacation_object.pk}), @@ -284,8 +293,8 @@ def test_vacation_owner_cancel(self): def test_vacation_owner_cancel_approved(self): """Test the owner cancelling an approved vacation.""" self.vacation_request["user"] = self.user - self.vacation_request["uploaded_by"] = self.test_user - self.vacation_request["manager_is_approved"] = True + self.vacation_request["uploaded_by"] = self.user + self.vacation_request["boss_is_approved"] = True vacation_object = VacationRequest.objects.create(**self.vacation_request) response = self.client.patch( reverse("vacation-detail", kwargs={"pk": vacation_object.pk}), @@ -296,7 +305,7 @@ def test_vacation_owner_cancel_approved(self): ) self.assertEqual( str(response.data["non_field_errors"][0]), - "No puedes cancelar una solicitud que ya ha sido aprobada por tu jefe.", + "No puedes cancelar una solicitud que ya ha recibido aprobaciĆ³n.", ) def test_vacation_cancel_no_owner(self): @@ -315,7 +324,8 @@ def test_vacation_manager_approve(self): self.user.job_position.rank = 5 self.user.job_position.save() self.vacation_request["user"] = self.test_user - self.vacation_request["uploaded_by"] = self.user + self.vacation_request["uploaded_by"] = self.test_user + self.vacation_request["boss_is_approved"] = True vacation_object = VacationRequest.objects.create(**self.vacation_request) response = self.client.patch( reverse("vacation-detail", kwargs={"pk": vacation_object.pk}), @@ -331,7 +341,8 @@ def test_vacation_manager_reject(self): self.user.job_position.rank = 5 self.user.job_position.save() self.vacation_request["user"] = self.test_user - self.vacation_request["uploaded_by"] = self.user + self.vacation_request["uploaded_by"] = self.test_user + self.vacation_request["boss_is_approved"] = True vacation_object = VacationRequest.objects.create(**self.vacation_request) response = self.client.patch( reverse("vacation-detail", kwargs={"pk": vacation_object.pk}), @@ -343,10 +354,23 @@ def test_vacation_manager_reject(self): vacation_object.refresh_from_db() self.assertIsNotNone(vacation_object.manager_approved_at) + def test_vacation_manager_approve_before_boss(self): + """Test the manager approving a vacation before the boss.""" + self.user.job_position.rank = 5 + self.user.job_position.save() + self.vacation_request["user"] = self.test_user + self.vacation_request["uploaded_by"] = self.test_user + vacation_object = VacationRequest.objects.create(**self.vacation_request) + response = self.client.patch( + reverse("vacation-detail", kwargs={"pk": vacation_object.pk}), + {"manager_is_approved": True}, + ) + self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN, response.data) + def test_vacation_manager_approve_no_manager(self): """Test the manager approving a vacation without being a manager.""" self.vacation_request["user"] = self.test_user - self.vacation_request["uploaded_by"] = self.user + self.vacation_request["uploaded_by"] = self.test_user vacation_object = VacationRequest.objects.create(**self.vacation_request) response = self.client.patch( reverse("vacation-detail", kwargs={"pk": vacation_object.pk}), @@ -360,7 +384,7 @@ def test_vacation_hr_approve(self): self.user.job_position.save() self.vacation_request["user"] = self.test_user admin = self.create_demo_user_admin() - self.vacation_request["uploaded_by"] = admin + self.vacation_request["uploaded_by"] = self.test_user self.vacation_request["manager_is_approved"] = True vacation_object = VacationRequest.objects.create(**self.vacation_request) response = self.client.patch( @@ -377,7 +401,7 @@ def test_vacation_hr_reject(self): self.user.job_position.name = "GERENTE DE GESTION HUMANA" self.user.job_position.save() self.vacation_request["user"] = self.test_user - self.vacation_request["uploaded_by"] = self.user + self.vacation_request["uploaded_by"] = self.test_user self.vacation_request["manager_is_approved"] = True vacation_object = VacationRequest.objects.create(**self.vacation_request) response = self.client.patch( @@ -395,7 +419,7 @@ def test_vacation_hr_approve_before_manager(self): self.user.job_position.name = "GERENTE DE GESTION HUMANA" self.user.job_position.save() self.vacation_request["user"] = self.test_user - self.vacation_request["uploaded_by"] = self.user + self.vacation_request["uploaded_by"] = self.test_user vacation_object = VacationRequest.objects.create(**self.vacation_request) response = self.client.patch( reverse("vacation-detail", kwargs={"pk": vacation_object.pk}), @@ -406,7 +430,7 @@ def test_vacation_hr_approve_before_manager(self): def test_vacation_hr_approve_no_hr(self): """Test HR approving a vacation without being an HR.""" self.vacation_request["user"] = self.test_user - self.vacation_request["uploaded_by"] = self.user + self.vacation_request["uploaded_by"] = self.test_user vacation_object = VacationRequest.objects.create(**self.vacation_request) response = self.client.patch( reverse("vacation-detail", kwargs={"pk": vacation_object.pk}), @@ -418,7 +442,7 @@ def test_vacation_payroll_approve(self): """Test payroll approving a vacation.""" self.user.user_permissions.add(self.permission) self.vacation_request["user"] = self.test_user - self.vacation_request["uploaded_by"] = self.user + self.vacation_request["uploaded_by"] = self.test_user self.vacation_request["hr_is_approved"] = True vacation_object = VacationRequest.objects.create(**self.vacation_request) response = self.client.patch( @@ -434,7 +458,7 @@ def test_vacation_payroll_reject(self): """Test payroll rejecting a vacation.""" self.user.user_permissions.add(self.permission) self.vacation_request["user"] = self.test_user - self.vacation_request["uploaded_by"] = self.user + self.vacation_request["uploaded_by"] = self.test_user self.vacation_request["hr_is_approved"] = True vacation_object = VacationRequest.objects.create(**self.vacation_request) response = self.client.patch( @@ -451,7 +475,7 @@ def test_vacation_payroll_approve_before_hr(self): """Test payroll approving a vacation before HR.""" self.user.user_permissions.add(self.permission) self.vacation_request["user"] = self.test_user - self.vacation_request["uploaded_by"] = self.user + self.vacation_request["uploaded_by"] = self.test_user vacation_object = VacationRequest.objects.create(**self.vacation_request) response = self.client.patch( reverse("vacation-detail", kwargs={"pk": vacation_object.pk}), @@ -462,7 +486,7 @@ def test_vacation_payroll_approve_before_hr(self): def test_vacation_payroll_approve_no_payroll(self): """Test payroll approving a vacation without being in payroll.""" self.vacation_request["user"] = self.test_user - self.vacation_request["uploaded_by"] = self.user + self.vacation_request["uploaded_by"] = self.test_user vacation_object = VacationRequest.objects.create(**self.vacation_request) response = self.client.patch( reverse("vacation-detail", kwargs={"pk": vacation_object.pk}), @@ -595,7 +619,7 @@ def test_validate_vacation_request_more_than_15_days(self): def test_get_vacation_pdf(self): """Test getting the vacation request PDF.""" self.vacation_request["user"] = self.test_user - self.vacation_request["uploaded_by"] = self.user + self.vacation_request["uploaded_by"] = self.test_user vacation_object = VacationRequest.objects.create(**self.vacation_request) response = self.client.get( reverse("vacation-get-pdf", kwargs={"pk": vacation_object.pk}) diff --git a/INSIGHTSAPI/vacation/views.py b/INSIGHTSAPI/vacation/views.py index fe9901e..63e6708 100644 --- a/INSIGHTSAPI/vacation/views.py +++ b/INSIGHTSAPI/vacation/views.py @@ -376,7 +376,7 @@ def generate_pdf(self, request, pk=None): context, ) # PDF options - options = { + options = { "page-size": "Letter", "orientation": "portrait", "encoding": "UTF-8",