From bce35792789e56667bd88ea68cc5bdc816abf076 Mon Sep 17 00:00:00 2001 From: Arturo Reyes Lopez Date: Tue, 17 Dec 2024 16:52:07 -0700 Subject: [PATCH] Supplier cannot add its own Organization as Legal Name Transaction Partner --- .../AddEditAllocationAgreements.jsx | 18 ++++++++++++++++-- .../src/views/AllocationAgreements/_schema.jsx | 9 ++++++--- .../AddEditNotionalTransfers.jsx | 18 ++++++++++++++++-- .../src/views/NotionalTransfers/_schema.jsx | 9 ++++++--- 4 files changed, 44 insertions(+), 10 deletions(-) diff --git a/frontend/src/views/AllocationAgreements/AddEditAllocationAgreements.jsx b/frontend/src/views/AllocationAgreements/AddEditAllocationAgreements.jsx index 6a79fd45e..779d430e5 100644 --- a/frontend/src/views/AllocationAgreements/AddEditAllocationAgreements.jsx +++ b/frontend/src/views/AllocationAgreements/AddEditAllocationAgreements.jsx @@ -15,6 +15,7 @@ import { useGetAllocationAgreements, useSaveAllocationAgreement } from '@/hooks/useAllocationAgreement' +import { useCurrentUser } from '@/hooks/useCurrentUser' import { v4 as uuid } from 'uuid' import * as ROUTES from '@/constants/routes/routes.js' import { DEFAULT_CI_FUEL } from '@/constants/common' @@ -31,6 +32,7 @@ export const AddEditAllocationAgreements = () => { const params = useParams() const { complianceReportId, compliancePeriod } = params const navigate = useNavigate() + const { data: currentUser } = useCurrentUser() const { data: optionsData, @@ -117,9 +119,9 @@ export const AddEditAllocationAgreements = () => { ) useEffect(() => { - const updatedColumnDefs = allocationAgreementColDefs(optionsData, errors) + const updatedColumnDefs = allocationAgreementColDefs(optionsData, errors, currentUser) setColumnDefs(updatedColumnDefs) - }, [errors, optionsData]) + }, [errors, optionsData, currentUser]) useEffect(() => { if ( @@ -173,6 +175,18 @@ export const AddEditAllocationAgreements = () => { async (params) => { if (params.oldValue === params.newValue) return + // User cannot select their own organization as the transaction partner + if (params.colDef.field === 'transactionPartner') { + if (params.newValue === currentUser.organization.name) { + alertRef.current?.triggerAlert({ + message: 'You cannot select your own organization as the transaction partner.', + severity: 'error' + }) + params.node.setDataValue('transactionPartner', '') + return + } + } + const isValid = validate( params, (value) => { diff --git a/frontend/src/views/AllocationAgreements/_schema.jsx b/frontend/src/views/AllocationAgreements/_schema.jsx index e9d6e29d6..26ef36700 100644 --- a/frontend/src/views/AllocationAgreements/_schema.jsx +++ b/frontend/src/views/AllocationAgreements/_schema.jsx @@ -21,7 +21,7 @@ import { export const PROVISION_APPROVED_FUEL_CODE = 'Fuel code - section 19 (b) (i)' -export const allocationAgreementColDefs = (optionsData, errors) => [ +export const allocationAgreementColDefs = (optionsData, errors, currentUser) => [ validation, actions({ enableDuplicate: false, @@ -89,8 +89,11 @@ export const allocationAgreementColDefs = (optionsData, errors) => [ let path = apiRoutes.organizationSearch path += 'org_name=' + queryKey[1] const response = await client.get(path) - params.node.data.apiDataCache = response.data - return response.data + const filteredData = response.data.filter( + (org) => org.name !== currentUser.organization.name + ) + params.node.data.apiDataCache = filteredData + return filteredData }, title: 'transactionPartner', api: params.api, diff --git a/frontend/src/views/NotionalTransfers/AddEditNotionalTransfers.jsx b/frontend/src/views/NotionalTransfers/AddEditNotionalTransfers.jsx index 5d8073495..812192fa9 100644 --- a/frontend/src/views/NotionalTransfers/AddEditNotionalTransfers.jsx +++ b/frontend/src/views/NotionalTransfers/AddEditNotionalTransfers.jsx @@ -12,6 +12,7 @@ import { useGetAllNotionalTransfers, useSaveNotionalTransfer } from '@/hooks/useNotionalTransfer' +import { useCurrentUser } from '@/hooks/useCurrentUser' import { v4 as uuid } from 'uuid' import { BCGridEditor } from '@/components/BCDataGrid/BCGridEditor' import { useApiService } from '@/services/useApiService' @@ -37,6 +38,7 @@ export const AddEditNotionalTransfers = () => { useGetAllNotionalTransfers(complianceReportId) const { mutateAsync: saveRow } = useSaveNotionalTransfer() const navigate = useNavigate() + const { data: currentUser } = useCurrentUser() useEffect(() => { if (location?.state?.message) { @@ -115,6 +117,18 @@ export const AddEditNotionalTransfers = () => { async (params) => { if (params.oldValue === params.newValue) return + // User cannot select their own organization as the transaction partner + if (params.colDef.field === 'legalName') { + if (params.newValue === currentUser.organization.name) { + alertRef.current?.triggerAlert({ + message: 'You cannot select your own organization as the transaction partner.', + severity: 'error' + }) + params.node.setDataValue('legalName', '') + return + } + } + const isValid = validate( params, (value) => { @@ -223,10 +237,10 @@ export const AddEditNotionalTransfers = () => { useEffect(() => { if (!optionsLoading) { - const updatedColumnDefs = notionalTransferColDefs(optionsData, errors) + const updatedColumnDefs = notionalTransferColDefs(optionsData, errors, currentUser) setColumnDefs(updatedColumnDefs) } - }, [errors, optionsData, optionsLoading]) + }, [errors, optionsData, optionsLoading, currentUser]) const handleNavigateBack = useCallback(() => { navigate( diff --git a/frontend/src/views/NotionalTransfers/_schema.jsx b/frontend/src/views/NotionalTransfers/_schema.jsx index 6064debe4..a7a92353f 100644 --- a/frontend/src/views/NotionalTransfers/_schema.jsx +++ b/frontend/src/views/NotionalTransfers/_schema.jsx @@ -12,7 +12,7 @@ import { formatNumberWithCommas as valueFormatter } from '@/utils/formatters' import { apiRoutes } from '@/constants/routes' import { StandardCellErrors } from '@/utils/grid/errorRenderers' -export const notionalTransferColDefs = (optionsData, errors) => [ +export const notionalTransferColDefs = (optionsData, errors, currentUser) => [ validation, actions({ enableDuplicate: false, @@ -45,8 +45,11 @@ export const notionalTransferColDefs = (optionsData, errors) => [ let path = apiRoutes.organizationSearch path += 'org_name=' + queryKey[1] const response = await client.get(path) - params.node.data.apiDataCache = response.data - return response.data + const filteredData = response.data.filter( + (org) => org.name !== currentUser.organization.name + ) + params.node.data.apiDataCache = filteredData + return filteredData }, title: 'legalName', api: params.api