import { IFormData , Event , TransformedData , IFormField , IFormFieldQueryMapFunction } from '@client/forms' ; import { GQLRegWorkflow , GQLRegStatus } from '@opencrvs/gateway/src/graphql/schema' ; import { get , cloneDeep } from 'lodash' ; import { callingCountries } from 'country-data' ; export function transformStatusData ( transformedData , statusData , sectionId ) { const registrationStatus = statusData && statusData . find ( ( status ) => { return status . type && ( status . type as GQLRegStatus ) === 'REGISTERED' ; } ) ; transformedData [ sectionId ] = { ... transformedData [ sectionId ] , commentsOrNotes : ( statusData && statusData [ 0 ] && statusData [ 0 ] . comments && statusData [ 0 ] . comments [ 0 ] && statusData [ 0 ] . comments [ 0 ] . comment ) || '' } ; if ( ! registrationStatus ) { return transformedData ; } transformedData [ sectionId ] = { ... transformedData [ sectionId ] , regStatus : { type : registrationStatus . type || '' , statusDate : registrationStatus . timestamp , officeName : ( registrationStatus . office && registrationStatus . office . name ) || '' , officeAlias : ( registrationStatus . office && registrationStatus . office . alias && registrationStatus . office . alias . join ( ' ' ) ) || '' , officeAddressLevel3 : ( registrationStatus . office && registrationStatus . office . address && registrationStatus . office . address . district ) || '' , officeAddressLevel4 : ( registrationStatus . office && registrationStatus . office . address && registrationStatus . office . address . state ) || '' } } ; return transformedData ; } export function getBirthRegistrationSectionTransformer ( transformedData , queryData , sectionId ) { if ( queryData [ sectionId ] . trackingId ) { transformedData [ sectionId ] . trackingId = queryData [ sectionId ] . trackingId ; } if ( queryData [ sectionId ] . registrationNumber ) { transformedData [ sectionId ] . registrationNumber = queryData [ sectionId ] . registrationNumber ; } if ( queryData . presentAtBirthRegistration ) { transformedData [ sectionId ] . presentAtBirthRegistration = queryData . presentAtBirthRegistration ; } if ( queryData [ sectionId ] . type && queryData [ sectionId ] . type === 'BIRTH' ) { transformedData [ sectionId ] . type = Event . BIRTH ; } if ( queryData [ sectionId ] . status ) { transformStatusData ( transformedData , queryData [ sectionId ] . status as GQLRegWorkflow [ ] , sectionId ) ; } } const convertToLocal = ( mobileWithCountryCode , countryCode , codeReplacement ? ) => { countryCode = countryCode . toUpperCase ( ) ; return ( mobileWithCountryCode && mobileWithCountryCode . replace ( callingCountries [ countryCode ] . countryCallingCodes [ 0 ] , codeReplacement ? codeReplacement : '0' ) ) ; } ; export const localPhoneTransformer = ( transformedFieldName ? , codeReplacement ? ) => ( transformedData , queryData , sectionId , field ) => { const fieldName = transformedFieldName || field . name ; const msisdnPhone = get ( queryData , fieldName as string ) as unknown as string ; const localPhone = convertToLocal ( msisdnPhone , window . config . COUNTRY , codeReplacement ) ; transformedData [ sectionId ] [ field . name ] = localPhone ; return transformedData ; } ; export const changeHirerchyQueryTransformer = ( transformedFieldName ? , transformerMethod ? ) => ( transformedData , queryData , sectionId , field , nestedField ) => { if ( transformedFieldName ) { transformedData [ sectionId ] [ field . name ] [ 'nestedFields' ] [ nestedField . name ] = get ( queryData , transformedFieldName ) ; if ( transformerMethod ) { const clonedTransformedData = cloneDeep ( transformedData ) ; transformerMethod ( clonedTransformedData , queryData , sectionId , field ) ; transformedData [ sectionId ] [ field . name ] [ 'nestedFields' ] [ nestedField . name ] = clonedTransformedData [ sectionId ] [ field . name ] ; } } else { transformedData [ sectionId ] [ field . name ] [ 'nestedFields' ] [ nestedField . name ] = get ( queryData , `${ sectionId }.${ nestedField . name }` ) ; } return transformedData ; } ;