import { IntlShape } from 'react-intl' ; import { IFunctionTransformer , TemplateTransformerData , TransformerPayload , ILanguagePayload , ILocationPayload } from '@client/pdfRenderer/transformer/types' ; import { userMessages } from '@client/i18n/messages' ; import { GQLHumanName } from '@opencrvs/gateway/src/graphql/schema' ; import { IUserDetails } from '@client/utils/userUtils' ; function getUserName ( userDetails ) { const nameObj = userDetails . name && ( userDetails . name . find ( ( storedName ) => { const name = storedName as GQLHumanName ; return name . use === 'en' ; } ) as GQLHumanName ) ; return nameObj ? `${ String ( nameObj . firstNames ) } ${ String ( nameObj . familyName ) }` : '' ; } export const userTransformers = { LocalRegistrarUserName : ( templateData , intl ) => { return getUserName ( templateData . userDetails . localRegistrar ) ; } , LoggedInUserName : ( templateData , intl ) => { return getUserName ( templateData . userDetails ) ; } , LoggedInUserOfficeName : ( templateData , intl ) => { return templateData . userDetails . primaryOffice ? templateData . userDetails . primaryOffice . name || '' : '' ; } , LocalRegistrarUserRole : ( templateData , intl ) => { return templateData . userDetails . localRegistrar . role ? intl . formatMessage ( userMessages [ templateData . userDetails . localRegistrar . role ] ) : '' ; } , LoggedInUserRole : ( templateData , intl ) => { return templateData . userDetails . role ? intl . formatMessage ( userMessages [ templateData . userDetails . role ] ) : '' ; } , LocalRegistrarUserSignature : ( templateData , intl ) => { return templateData . userDetails . localRegistrar . signature ? templateData . userDetails . localRegistrar . signature . data || '' : '' ; } , CRVSOfficeName : ( templateData , intl , payload ? ) => { const langKey = payload && ( payload as ILanguagePayload ) ; if ( ! langKey || langKey . language === 'en' ) { return ( ( templateData . userDetails . primaryOffice && templateData . userDetails . primaryOffice . name ) || '' ) ; } return ( ( templateData . userDetails . primaryOffice && templateData . userDetails . primaryOffice . alias && templateData . userDetails . primaryOffice . alias [ 0 ] ) || '' ) ; } , CRVSLocationName : ( templateData , intl , payload ? ) => { const key = payload && ( payload as ILocationPayload ) ; if ( ! key ) { throw new Error ( 'No payload found for this transformer' ) ; } if ( ! templateData . userDetails . catchmentArea ) { return '' ; } const crvsLocation = templateData . userDetails . catchmentArea && templateData . userDetails . catchmentArea . find ( ( cArea ) => { return ( ( cArea . identifier && cArea . identifier . find ( ( identifier ) => identifier . system === 'http://opencrvs.org/specs/id/jurisdiction-type' && identifier . value === key . jurisdictionType ) ) || false ) ; } ) ; if ( ! key . language || key . language === 'en' ) { return ( crvsLocation && crvsLocation . name ) || '' ; } return ( crvsLocation && crvsLocation . alias && crvsLocation . alias [ 0 ] ) || '' ; } } ;