diff --git a/backend/lcfs/web/api/organizations/views.py b/backend/lcfs/web/api/organizations/views.py index 8ff755fc9..9f3e8f517 100644 --- a/backend/lcfs/web/api/organizations/views.py +++ b/backend/lcfs/web/api/organizations/views.py @@ -162,25 +162,23 @@ async def get_organization_types( return await service.get_organization_types() -# TODO review security of this endpoint around returning balances -# for all organizations @router.get( "/names/", response_model=List[OrganizationSummaryResponseSchema], status_code=status.HTTP_200_OK, ) -@cache(expire=1) # cache for 1 hour -@view_handler(["*"]) +@cache(expire=1) # Cache for 1 hour +@view_handler( + [RoleEnum.GOVERNMENT] +) # Ensure only government can access this endpoint because it returns balances async def get_organization_names( - request: Request, service: OrganizationsService = Depends() + request: Request, + only_registered: bool = Query(True), + service: OrganizationsService = Depends(), ): - """Fetch all organization names""" - - # Set the default sorting order + """Fetch all organization names.""" order_by = ("name", "asc") - - # Call the service with only_registered set to True to fetch only registered organizations - return await service.get_organization_names(True, order_by) + return await service.get_organization_names(only_registered, order_by) @router.get( diff --git a/frontend/src/hooks/useOrganizations.js b/frontend/src/hooks/useOrganizations.js index 7cc6f4bfc..e665e145b 100644 --- a/frontend/src/hooks/useOrganizations.js +++ b/frontend/src/hooks/useOrganizations.js @@ -11,13 +11,16 @@ export const useOrganizationStatuses = (options) => { }) } -export const useOrganizationNames = (options) => { +export const useOrganizationNames = (onlyRegistered = true, options) => { const client = useApiService() return useQuery({ - queryKey: ['organization-names'], - queryFn: async () => (await client.get('/organizations/names/')).data, - ...options + queryKey: ['organization-names', onlyRegistered], + queryFn: async () => { + const response = await client.get(`/organizations/names/?only_registered=${onlyRegistered}`) + return response.data + }, + ...options, }) } diff --git a/frontend/src/views/Transactions/__tests__/AddEditViewTransaction.test.jsx b/frontend/src/views/Transactions/__tests__/AddEditViewTransaction.test.jsx index 7a8be97a6..1b129f3e4 100644 --- a/frontend/src/views/Transactions/__tests__/AddEditViewTransaction.test.jsx +++ b/frontend/src/views/Transactions/__tests__/AddEditViewTransaction.test.jsx @@ -19,7 +19,7 @@ import { useCreateUpdateInitiativeAgreement, useInitiativeAgreement } from '@/hooks/useInitiativeAgreement' -import { useRegExtOrgs } from '@/hooks/useOrganizations' +import { useOrganizationNames } from '@/hooks/useOrganizations' import { useOrganizationBalance } from '@/hooks/useOrganization' import { useTransactionMutation } from '../transactionMutation' import { TRANSACTION_STATUSES } from '@/constants/statuses' @@ -110,7 +110,7 @@ vi.mock('@fortawesome/react-fontawesome', () => ({ // Mock the hooks vi.mock('@/hooks/useOrganizations', () => ({ - useRegExtOrgs: vi.fn().mockReturnValue({ + useOrganizationNames: vi.fn().mockReturnValue({ data: [ { organizationId: 1, @@ -281,7 +281,7 @@ describe('AddEditViewTransaction Component Tests', () => { state: null }) - useRegExtOrgs.mockReturnValue({ + useOrganizationNames.mockReturnValue({ data: [ { organizationId: 1, @@ -356,7 +356,7 @@ describe('AddEditViewTransaction Component Tests', () => { mutate: vi.fn(), isLoading: false }) - useRegExtOrgs.mockReturnValue({ + useOrganizationNames.mockReturnValue({ data: [], isLoading: false, isFetched: true, @@ -425,7 +425,7 @@ describe('AddEditViewTransaction Component Tests', () => { isLoadingError: false }) - useRegExtOrgs.mockReturnValue({ + useOrganizationNames.mockReturnValue({ data: [], isLoading: false, isFetched: true, @@ -500,7 +500,7 @@ describe('AddEditViewTransaction Component Tests', () => { isLoading: false }) - useRegExtOrgs.mockReturnValue({ + useOrganizationNames.mockReturnValue({ data: [], isLoading: false, isFetched: true, diff --git a/frontend/src/views/Transactions/components/TransactionDetails.jsx b/frontend/src/views/Transactions/components/TransactionDetails.jsx index 81fdd1b3b..1d674d651 100644 --- a/frontend/src/views/Transactions/components/TransactionDetails.jsx +++ b/frontend/src/views/Transactions/components/TransactionDetails.jsx @@ -16,7 +16,7 @@ import { } from '@mui/material' import { dateFormatter, numberFormatter } from '@/utils/formatters' import { useFormContext, Controller } from 'react-hook-form' -import { useRegExtOrgs } from '@/hooks/useOrganizations' +import { useOrganizationNames } from '@/hooks/useOrganizations' import { useOrganizationBalance } from '@/hooks/useOrganization' import Loading from '@/components/Loading' import { @@ -34,7 +34,7 @@ export const TransactionDetails = ({ transactionId, isEditable }) => { control } = useFormContext() - const { data: orgData } = useRegExtOrgs() + const { data: orgData } = useOrganizationNames(false) const organizations = orgData?.map((org) => ({ value: parseInt(org.organizationId), diff --git a/frontend/src/views/Transactions/components/__tests__/TransactionDetails.test.jsx b/frontend/src/views/Transactions/components/__tests__/TransactionDetails.test.jsx index 6782f7ca3..70a7eccce 100644 --- a/frontend/src/views/Transactions/components/__tests__/TransactionDetails.test.jsx +++ b/frontend/src/views/Transactions/components/__tests__/TransactionDetails.test.jsx @@ -3,7 +3,7 @@ import { describe, it, expect, beforeEach, vi, afterEach } from 'vitest' import { TransactionDetails } from '../TransactionDetails' import { QueryClient, QueryClientProvider } from '@tanstack/react-query' import { ThemeProvider } from '@mui/material' -import { useRegExtOrgs } from '@/hooks/useOrganizations' +import { useOrganizationNames } from '@/hooks/useOrganizations' import { useOrganizationBalance } from '@/hooks/useOrganization' import theme from '@/themes' import { FormProvider, useForm } from 'react-hook-form' @@ -65,7 +65,7 @@ describe('TransactionDetails Component', () => { beforeEach(() => { vi.clearAllMocks() - useRegExtOrgs.mockReturnValue({ + useOrganizationNames.mockReturnValue({ data: mockOrganizations, isLoading: false })