import { Timestamp , Nullable , Optional } from '@mds-core/mds-types' ; import { ModelMapper , IdentityColumn , RecordedColumn } from '@mds-core/mds-repository' ; import { uuid } from '@mds-core/mds-utils' ; import { JurisdictionDomainModel , CreateJurisdictionDomainModel } from '../../@types' ; import { JurisdictionEntityModel } from './entities/jurisdiction-entity' ; type MapJurisdictionEntityToDomainModelOptions = Partial < { effective : Timestamp ; } > ; export const JurisdictionEntityToDomain = ModelMapper < JurisdictionEntityModel , Nullable < JurisdictionDomainModel > , MapJurisdictionEntityToDomainModelOptions > ( ( entity , options ) => { const { effective = Date . now ( ) } = options ?? { } ; const { jurisdiction_id , agency_key , versions } = entity ; const version = versions . find ( properties => effective >= properties . timestamp ) ; if ( version ) { const { agency_name , geography_id , timestamp } = version ; if ( geography_id !== null ) { const domain = { jurisdiction_id , agency_key , agency_name , geography_id , timestamp } ; return domain ; } } return null ; } ) ; type JurisdictionDomainToEntityCreateOptions = Partial < { recorded : Timestamp ; } > ; type JurisdictionDomainToEntityCreateModel = Omit < Optional < JurisdictionEntityModel , keyof RecordedColumn > , keyof IdentityColumn > ; export const JurisdictionDomainToEntityCreate = ModelMapper < CreateJurisdictionDomainModel , JurisdictionDomainToEntityCreateModel , JurisdictionDomainToEntityCreateOptions > ( ( domain , options ) => { const { recorded } = options ?? { } ; const { jurisdiction_id = uuid ( ) , agency_key , agency_name , geography_id , timestamp = Date . now ( ) } = domain ; const entity = { jurisdiction_id , agency_key , versions : [ { timestamp , agency_name , geography_id } ] , recorded } ; return entity ; } ) ;