Skip to content

Commit

Permalink
Fix goals history
Browse files Browse the repository at this point in the history
  • Loading branch information
Heibert committed Oct 23, 2024
1 parent 9d533ef commit 6fa2df7
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 41 deletions.
1 change: 1 addition & 0 deletions INSIGHTSAPI/coexistence_committee/tests.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down
37 changes: 33 additions & 4 deletions INSIGHTSAPI/goals/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -740,4 +770,3 @@ def format_cell_value(cell):
{"message": "Excel no encontrado."},
status=framework_status.HTTP_400_BAD_REQUEST,
)

96 changes: 60 additions & 36 deletions INSIGHTSAPI/vacation/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)

Expand All @@ -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)
Expand All @@ -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})
Expand Down Expand Up @@ -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}),
Expand All @@ -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}),
Expand All @@ -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):
Expand All @@ -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}),
Expand All @@ -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}),
Expand All @@ -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}),
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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}),
Expand All @@ -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}),
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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}),
Expand All @@ -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}),
Expand Down Expand Up @@ -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})
Expand Down
2 changes: 1 addition & 1 deletion INSIGHTSAPI/vacation/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ def generate_pdf(self, request, pk=None):
context,
)
# PDF options
options = {
options = {
"page-size": "Letter",
"orientation": "portrait",
"encoding": "UTF-8",
Expand Down

0 comments on commit 6fa2df7

Please sign in to comment.