import { JurisdictionServiceClient , CreateJurisdictionDomainModel , JurisdictionDomainModel } from '@mds-core/mds-jurisdiction-service' ; import { isServiceError } from '@mds-core/mds-service-helpers' ; import { JurisdictionApiRequest , JurisdictionApiResponse } from '../@types' ; interface CreateJurisdictionRequest extends JurisdictionApiRequest { body : CreateJurisdictionDomainModel | CreateJurisdictionDomainModel [ ] ; } type CreateJurisdictionResponse = JurisdictionApiResponse < { jurisdiction : JurisdictionDomainModel ; } | { jurisdictions : JurisdictionDomainModel [ ] ; } > ; export const CreateJurisdictionHandler = async ( req , res ) => { try { const jurisdictions = await JurisdictionServiceClient . createJurisdictions ( Array . isArray ( req . body ) ? req . body : [ req . body ] ) ; const { version } = res . locals ; if ( ! Array . isArray ( req . body ) ) { const [ jurisdiction ] = jurisdictions ; return res . status ( 201 ) . send ( { version , jurisdiction } ) ; } return res . status ( 201 ) . send ( { version , jurisdictions } ) ; } catch ( error ) { if ( isServiceError ( error ) ) if ( error . type === 'ValidationError' ) { return res . status ( 400 ) . send ( { error } ) ; } if ( error . type === 'ConflictError' ) { return res . status ( 409 ) . send ( { error } ) ; } return res . status ( 500 ) . send ( { error } ) ; } } ;