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

PIN-5855 - Allow certifier routes in ATT STAGE #1046

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
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
17 changes: 10 additions & 7 deletions src/components/layout/SideNav/hooks/useGetSideNavItems.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import type { RouteKey } from '@/router'
import { routes } from '@/router'
import { AuthHooks } from '@/api/auth'
import { TenantHooks } from '@/api/tenant'
import { isTenantCertifier } from '@/utils/tenant.utils'
import { isTenantCertifier, isTenantPA } from '@/utils/tenant.utils'
import { STAGE } from '@/config/env'

const views = [
{
Expand Down Expand Up @@ -42,8 +43,7 @@ export function useGetSideNavItems() {
const { data: tenant } = TenantHooks.useGetActiveUserParty()

const isCertifier = isTenantCertifier(tenant)

const isPA = jwt?.externalId?.origin === 'IPA'
const isPA = Boolean(jwt && isTenantPA(jwt))

return React.useMemo(() => {
/**
Expand All @@ -54,10 +54,13 @@ export function useGetSideNavItems() {
*/
const isAuthorizedToRoute = (routeKey: RouteKey) => {
if (!isSupport && !isOrganizationAllowedToProduce && routeKey === 'PROVIDE') return false

if (!isCertifier && routeKey === 'TENANT_CERTIFIER') return false

if (!isPA && routeKey === 'DELEGATIONS') return false
if (!isPA && routeKey === 'DELEGATIONS') {
// In ATT, the delegations routes are available to all organizations
if (STAGE !== 'ATT') {
return false
}
}

const authLevels = routes[routeKey].authLevels
return authLevels.some((authLevel) => currentRoles.includes(authLevel))
Expand All @@ -80,5 +83,5 @@ export function useGetSideNavItems() {

return [...acc, view]
}, [])
}, [currentRoles, isOrganizationAllowedToProduce, isSupport, isCertifier])
}, [currentRoles, isOrganizationAllowedToProduce, isSupport, isCertifier, isPA])
}
8 changes: 6 additions & 2 deletions src/router/components/RoutesWrapper/AuthGuard.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { AuthQueries } from '@/api/auth'
import { TenantHooks } from '@/api/tenant'
import { STAGE } from '@/config/env'
import type { RouteKey } from '@/router'
import { useAuthGuard, useCurrentRoute } from '@/router'
import type { JwtUser, UserProductRole } from '@/types/party.types'
import { ForbiddenError } from '@/utils/errors.utils'
import { isTenantCertifier } from '@/utils/tenant.utils'
import { isTenantCertifier, isTenantPA } from '@/utils/tenant.utils'
import { useQuery } from '@tanstack/react-query'
import React from 'react'

Expand Down Expand Up @@ -61,8 +62,11 @@ export const AuthGuard: React.FC<AuthGuardProps> = ({
}

function isUserAllowedToAccessDelegationsRoutes() {
// In ATT, the delegations routes are available to all organizations
if (STAGE === 'ATT') return true

// The IsUserAllowedToAccessDelegationsRoutes method checks if the organization is a PA. Only a PA can access the delegations routes
const isPA = jwt?.externalId?.origin === 'IPA'
const isPA = Boolean(jwt && isTenantPA(jwt))
const delegationsRoutes: Array<RouteKey> = [
'DELEGATIONS',
'DELEGATION_DETAILS',
Expand Down
7 changes: 6 additions & 1 deletion src/utils/tenant.utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Tenant, TenantFeature } from '@/api/api.generatedTypes'
import type { ExternalId, Tenant } from '@/api/api.generatedTypes'
import { type TenantFeature } from '@/api/api.generatedTypes'

export function isTenantCertifier(tenant: Tenant) {
return tenant.features.some((feature) => 'certifier' in feature && feature.certifier?.certifierId)
Expand All @@ -10,3 +11,7 @@ export function hasTenantGivenProducerDelegationAvailability(tenant: Tenant) {
Boolean('delegatedProducer' in feature && feature.delegatedProducer?.availabilityTimestamp)
)?.delegatedProducer?.availabilityTimestamp
}

export function isTenantPA(tenant: { externalId?: ExternalId }) {
return tenant.externalId?.origin === 'IPA'
}
Loading