Skip to content

Commit

Permalink
feat: updated org names endpoint to accept only_registered param
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Zorkin committed Dec 31, 2024
1 parent 64b5b3d commit 3265b7c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 17 deletions.
20 changes: 9 additions & 11 deletions backend/lcfs/web/api/organizations/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
11 changes: 7 additions & 4 deletions frontend/src/hooks/useOrganizations.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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),
Expand Down

0 comments on commit 3265b7c

Please sign in to comment.