11import { AgreementMutations } from '@/api/agreement'
2- import type { CatalogEServiceDescriptor , DelegationTenant } from '@/api/api.generatedTypes'
2+ import type {
3+ AgreementState ,
4+ CatalogEServiceDescriptor ,
5+ DelegationTenant ,
6+ } from '@/api/api.generatedTypes'
37import { useNavigate } from '@/router'
48import type { ActionItemButton } from '@/types/common.types'
59import { useTranslation } from 'react-i18next'
@@ -22,7 +26,7 @@ function useGetEServiceConsumerActions(
2226 isDelegator ?: boolean
2327) {
2428 const { t } = useTranslation ( 'eservice' )
25- const { isAdmin } = AuthHooks . useJwt ( )
29+ const { jwt , isAdmin } = AuthHooks . useJwt ( )
2630
2731 const navigate = useNavigate ( )
2832
@@ -43,13 +47,18 @@ function useGetEServiceConsumerActions(
4347
4448 if ( ! eservice || ! descriptor || ! isAdmin ) return { actions : [ ] satisfies Array < ActionItemButton > }
4549
50+ const inspectableAgreementsStates : AgreementState [ ] = [ 'ACTIVE' , 'SUSPENDED' , 'PENDING' ]
51+ const inspectableAgreements = eservice . agreements . filter ( ( agreement ) =>
52+ inspectableAgreementsStates . includes ( agreement . state )
53+ )
54+
4655 const handleInspectAgreementAction = ( ) => {
47- match ( eservice . agreements . length )
56+ match ( inspectableAgreements . length )
4857 . with ( 0 , ( ) => { } )
4958 . with ( 1 , ( ) => {
5059 navigate ( 'SUBSCRIBE_AGREEMENT_READ' , {
5160 params : {
52- agreementId : eservice . agreements [ 0 ] . id ,
61+ agreementId : inspectableAgreements [ 0 ] . id ,
5362 } ,
5463 } )
5564 } )
@@ -63,13 +72,15 @@ function useGetEServiceConsumerActions(
6372 } )
6473 }
6574
75+ const editableAgreements = eservice . agreements . filter ( ( agreement ) => agreement . state === 'DRAFT' )
76+
6677 const handleEditAgreementAction = ( ) => {
67- match ( eservice . agreements . length )
78+ match ( editableAgreements . length )
6879 . with ( 0 , ( ) => { } )
6980 . with ( 1 , ( ) => {
7081 navigate ( 'SUBSCRIBE_AGREEMENT_EDIT' , {
7182 params : {
72- agreementId : eservice . agreements [ 0 ] . id ,
83+ agreementId : editableAgreements [ 0 ] . id ,
7384 } ,
7485 } )
7586 } )
@@ -167,11 +178,26 @@ function useGetEServiceConsumerActions(
167178 ( canCreateAgreementDraft && ! isDelegator && ( delegators ?. length === 0 || ! delegators ) ) ||
168179 ( delegators && delegators ?. length > 0 )
169180 ) {
181+ const existingAgreements = eservice . agreements . filter (
182+ ( agreement ) => agreement . state !== 'ARCHIVED' && agreement . state !== 'REJECTED'
183+ )
184+
185+ const tenants : DelegationTenant [ ] = jwt
186+ ? [ { id : jwt . organizationId as string , name : jwt . organization . name } , ...( delegators ?? [ ] ) ]
187+ : delegators ?? [ ]
188+
189+ const tenantsWithoutAgreement = tenants . filter (
190+ ( tenant ) => ! existingAgreements . some ( ( agreement ) => agreement . consumerId === tenant . id )
191+ )
192+
193+ tenantsWithoutAgreement . length === 1 && tenantsWithoutAgreement [ 0 ] . id === jwt ?. organizationId
194+
170195 actions . push ( {
171196 action :
172- delegators && delegators ?. length > 0
173- ? handleOpenCreateAgreementDraftDialog
174- : handleCreateAgreementDraftAction ,
197+ tenantsWithoutAgreement . length === 1 &&
198+ tenantsWithoutAgreement [ 0 ] . id === jwt ?. organizationId
199+ ? handleCreateAgreementDraftAction
200+ : handleOpenCreateAgreementDraftDialog ,
175201 label : t ( 'tableEServiceCatalog.subscribe' ) ,
176202 icon : SendIcon ,
177203 disabled : isSuspended ,
0 commit comments