diff --git a/backend/common/views/dashboard_views.py b/backend/common/views/dashboard_views.py index 36fe6be4..2a710f32 100644 --- a/backend/common/views/dashboard_views.py +++ b/backend/common/views/dashboard_views.py @@ -223,7 +223,7 @@ class ApiHomeView(APIView): from opportunity.models import SalesGoal goal_filter = Q(assigned_to=profile) | Q(team__in=profile.user_teams.all()) - if profile.role == "ADMIN": + if is_admin: goal_filter |= Q(assigned_to__isnull=True, team__isnull=True) active_goals = ( diff --git a/backend/opportunity/tests/test_sales_goals.py b/backend/opportunity/tests/test_sales_goals.py index 4817afce..f6612161 100644 --- a/backend/opportunity/tests/test_sales_goals.py +++ b/backend/opportunity/tests/test_sales_goals.py @@ -257,7 +257,7 @@ class TestSalesGoalAPI: ), } response = admin_client.post(self.GOALS_URL, data, format="json") - assert response.status_code == 200 + assert response.status_code == 201 assert response.data["error"] is False assert SalesGoal.objects.filter(name="New Goal", org=org_a).exists() diff --git a/backend/opportunity/views/goal_views.py b/backend/opportunity/views/goal_views.py index f3047265..518dc908 100644 --- a/backend/opportunity/views/goal_views.py +++ b/backend/opportunity/views/goal_views.py @@ -44,7 +44,9 @@ class SalesGoalListView(APIView, LimitOffsetPagination): if params.get("search"): queryset = queryset.filter(name__icontains=params["search"]) - return queryset.distinct() + return queryset.select_related( + "assigned_to", "assigned_to__user", "team" + ).distinct() def get(self, request, *args, **kwargs): queryset = self.get_queryset(request) @@ -52,11 +54,8 @@ class SalesGoalListView(APIView, LimitOffsetPagination): serializer = SalesGoalSerializer(results, many=True) total_count = self.count - offset = None - if results: - offset = queryset.filter(id__gte=results[-1].id).count() - if offset == total_count: - offset = None + next_offset = self.offset + len(results) if results else None + offset = next_offset if (results and next_offset < total_count) else None return Response( { @@ -82,7 +81,7 @@ class SalesGoalListView(APIView, LimitOffsetPagination): ) return Response( {"error": False, "message": "Goal Created Successfully"}, - status=status.HTTP_200_OK, + status=status.HTTP_201_CREATED, ) return Response( {"error": True, "errors": serializer.errors}, diff --git a/frontend/src/lib/components/dashboard/GoalProgress.svelte b/frontend/src/lib/components/dashboard/GoalProgress.svelte index f9c2f5e8..eddd026c 100644 --- a/frontend/src/lib/components/dashboard/GoalProgress.svelte +++ b/frontend/src/lib/components/dashboard/GoalProgress.svelte @@ -4,39 +4,12 @@ import { formatCurrency } from '$lib/utils/formatting.js'; import { orgSettings } from '$lib/stores/org.js'; import { resolve } from '$app/paths'; + import { getStatusColor, getProgressColor } from '$lib/utils/goals.js'; /** @type {{ goals?: any[] }} */ let { goals = [] } = $props(); const orgCurrency = $derived($orgSettings.default_currency || 'USD'); - - /** - * @param {string} statusValue - */ - function getStatusColor(statusValue) { - switch (statusValue) { - case 'on_track': - return 'text-emerald-600 dark:text-emerald-400'; - case 'at_risk': - return 'text-amber-600 dark:text-amber-400'; - case 'behind': - return 'text-red-600 dark:text-red-400'; - case 'completed': - return 'text-blue-600 dark:text-blue-400'; - default: - return 'text-[var(--text-secondary)]'; - } - } - - /** - * @param {number} percent - */ - function getProgressColor(percent) { - if (percent >= 100) return '[&>[data-slot=progress-indicator]]:bg-blue-500'; - if (percent >= 75) return '[&>[data-slot=progress-indicator]]:bg-emerald-500'; - if (percent >= 50) return '[&>[data-slot=progress-indicator]]:bg-amber-500'; - return '[&>[data-slot=progress-indicator]]:bg-red-500'; - }
{:else}
- {#each goals as goal} + {#each goals as goal (goal.id)}
{goal.name} diff --git a/frontend/src/lib/utils/goals.js b/frontend/src/lib/utils/goals.js new file mode 100644 index 00000000..0f37d921 --- /dev/null +++ b/frontend/src/lib/utils/goals.js @@ -0,0 +1,29 @@ +/** + * Get Tailwind color classes for a goal status value. + * @param {string} statusValue + */ +export function getStatusColor(statusValue) { + switch (statusValue) { + case 'on_track': + return 'text-emerald-600 dark:text-emerald-400'; + case 'at_risk': + return 'text-amber-600 dark:text-amber-400'; + case 'behind': + return 'text-red-600 dark:text-red-400'; + case 'completed': + return 'text-blue-600 dark:text-blue-400'; + default: + return 'text-[var(--text-secondary)]'; + } +} + +/** + * Get Tailwind progress bar color class based on percent. + * @param {number} percent + */ +export function getProgressColor(percent) { + if (percent >= 100) return '[&>[data-slot=progress-indicator]]:bg-blue-500'; + if (percent >= 75) return '[&>[data-slot=progress-indicator]]:bg-emerald-500'; + if (percent >= 50) return '[&>[data-slot=progress-indicator]]:bg-amber-500'; + return '[&>[data-slot=progress-indicator]]:bg-red-500'; +} diff --git a/frontend/src/routes/(app)/goals/+page.svelte b/frontend/src/routes/(app)/goals/+page.svelte index 92155b11..c334652a 100644 --- a/frontend/src/routes/(app)/goals/+page.svelte +++ b/frontend/src/routes/(app)/goals/+page.svelte @@ -15,6 +15,7 @@ import { formatCurrency } from '$lib/utils/formatting.js'; import { orgSettings } from '$lib/stores/org.js'; import { getCurrentUser } from '$lib/api.js'; + import { getStatusColor, getProgressColor } from '$lib/utils/goals.js'; const goalTypeOptions = [ { @@ -149,23 +150,22 @@ const now = new Date(); const year = now.getFullYear(); const month = now.getMonth(); + const pad = (/** @type {number} */ n) => String(n).padStart(2, '0'); switch (periodType) { case 'MONTHLY': { - const start = new Date(year, month, 1); - const end = new Date(year, month + 1, 0); + const lastDay = new Date(year, month + 1, 0).getDate(); return { - start: start.toISOString().split('T')[0], - end: end.toISOString().split('T')[0] + start: `${year}-${pad(month + 1)}-01`, + end: `${year}-${pad(month + 1)}-${pad(lastDay)}` }; } case 'QUARTERLY': { const qStart = Math.floor(month / 3) * 3; - const start = new Date(year, qStart, 1); - const end = new Date(year, qStart + 3, 0); + const lastDay = new Date(year, qStart + 3, 0).getDate(); return { - start: start.toISOString().split('T')[0], - end: end.toISOString().split('T')[0] + start: `${year}-${pad(qStart + 1)}-01`, + end: `${year}-${pad(qStart + 3)}-${pad(lastDay)}` }; } case 'YEARLY': { @@ -226,6 +226,7 @@ /** @param {Event} e */ async function handleSubmit(e) { e.preventDefault(); + if (!isAdmin) return; const form = /** @type {HTMLFormElement} */ (e.target); const fd = new FormData(form); @@ -315,35 +316,6 @@ return goals; }); - /** - * Get color class for status - * @param {string} statusValue - */ - function getStatusColor(statusValue) { - switch (statusValue) { - case 'on_track': - return 'text-emerald-600 dark:text-emerald-400'; - case 'at_risk': - return 'text-amber-600 dark:text-amber-400'; - case 'behind': - return 'text-red-600 dark:text-red-400'; - case 'completed': - return 'text-blue-600 dark:text-blue-400'; - default: - return 'text-[var(--text-secondary)]'; - } - } - - /** - * Get progress bar color - * @param {number} percent - */ - function getProgressColor(percent) { - if (percent >= 100) return '[&>[data-slot=progress-indicator]]:bg-blue-500'; - if (percent >= 75) return '[&>[data-slot=progress-indicator]]:bg-emerald-500'; - if (percent >= 50) return '[&>[data-slot=progress-indicator]]:bg-amber-500'; - return '[&>[data-slot=progress-indicator]]:bg-red-500'; - }