import { JurisdictionServiceClient , JurisdictionDomainModel , JurisdictionIdType } from '@mds-core/mds-jurisdiction-service' ; import { AuthorizationError } from '@mds-core/mds-utils' ; import { isServiceError } from '@mds-core/mds-service-helpers' ; import { parseRequest } from '@mds-core/mds-api-helpers' ; import { ApiQuery } from '@mds-core/mds-api-server' ; import { JurisdictionApiResponse , JurisdictionApiRequest } from '../@types' ; import { HasJurisdictionClaim } from './utils' ; type GetJurisdictionRequest = JurisdictionApiRequest < { jurisdiction_id : JurisdictionIdType ; } > & ApiQuery < 'effective' > ; type GetJurisdictionResponse = JurisdictionApiResponse < { jurisdiction : JurisdictionDomainModel ; } > ; export const GetJurisdictionHandler = async ( req , res ) => { try { const { jurisdiction_id } = req . params ; const { effective } = parseRequest ( req , { parser : Number } ) . query ( 'effective' ) ; const jurisdiction = await JurisdictionServiceClient . getJurisdiction ( jurisdiction_id , { effective } ) ; const { version } = res . locals ; return HasJurisdictionClaim ( res ) ( jurisdiction ) ? res . status ( 200 ) . send ( { version , jurisdiction } ) : res . status ( 403 ) . send ( { error : new AuthorizationError ( 'Access Denied' , { jurisdiction_id } ) } ) ; } catch ( error ) { if ( isServiceError ( error ) ) { if ( error . type === 'NotFoundError' ) { return res . status ( 404 ) . send ( { error } ) ; } } return res . status ( 500 ) . send ( { error } ) ; } } ;