Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Transaction Unregistered Org List - 1525 #1570

Merged
merged 5 commits into from
Jan 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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'
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -281,7 +281,7 @@ describe('AddEditViewTransaction Component Tests', () => {
state: null
})

useRegExtOrgs.mockReturnValue({
useOrganizationNames.mockReturnValue({
data: [
{
organizationId: 1,
Expand Down Expand Up @@ -356,7 +356,7 @@ describe('AddEditViewTransaction Component Tests', () => {
mutate: vi.fn(),
isLoading: false
})
useRegExtOrgs.mockReturnValue({
useOrganizationNames.mockReturnValue({
data: [],
isLoading: false,
isFetched: true,
Expand Down Expand Up @@ -425,7 +425,7 @@ describe('AddEditViewTransaction Component Tests', () => {
isLoadingError: false
})

useRegExtOrgs.mockReturnValue({
useOrganizationNames.mockReturnValue({
data: [],
isLoading: false,
isFetched: true,
Expand Down Expand Up @@ -500,7 +500,7 @@ describe('AddEditViewTransaction Component Tests', () => {
isLoading: false
})

useRegExtOrgs.mockReturnValue({
useOrganizationNames.mockReturnValue({
data: [],
isLoading: false,
isFetched: true,
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
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -65,7 +65,7 @@ describe('TransactionDetails Component', () => {
beforeEach(() => {
vi.clearAllMocks()

useRegExtOrgs.mockReturnValue({
useOrganizationNames.mockReturnValue({
data: mockOrganizations,
isLoading: false
})
Expand Down