import { IntlShape } from 'react-intl' ; import { getValueFromApplicationDataByKey , getEventMessageDescription , getExecutorFieldValue , getMatchedCondition } from '@client/pdfRenderer/transformer/utils' ; import { IIntLabelPayload , IConditionExecutorPayload , IApplicantNamePayload , IFeildValuePayload , INumberFeildConversionPayload , IDateFeildValuePayload , IFunctionTransformer , TemplateTransformerData , TransformerPayload , IFormattedFeildValuePayload , IPersonIdentifierValuePayload , IApplicantNameCondition , IArithmeticOperationPayload } from '@client/pdfRenderer/transformer/types' ; import moment from 'moment' ; import { IFormSectionData } from '@client/forms' ; export const fieldTransformers = { IntlLabel : ( templateData , intl , payload ? ) => { let messageValues = { } ; const message = payload && ( payload as IIntLabelPayload ) ; if ( ! message ) { throw new Error ( 'No payload found for this transformer' ) ; } if ( message . messageValues ) { Object . keys ( message . messageValues ) . forEach ( ( valueKey ) => { const messageValue = valueKey === 'event' ? intl . formatMessage ( getEventMessageDescription ( templateData . application . event ) ) : getValueFromApplicationDataByKey ( templateData . application . data , ( message . messageValues && message . messageValues [ valueKey ] ) || '' ) || '' ; messageValues = { ... messageValues , ... { [ valueKey ] : messageValue } } ; } ) ; } return intl . formatMessage ( message . messageDescriptor , messageValues ) ; } , ApplicantName : ( templateData , intl , payload ? ) => { const formatPayload = payload && ( payload as IApplicantNamePayload ) ; if ( ! formatPayload ) { throw new Error ( 'No payload found for this transformer' ) ; } const matchedCondition = getMatchedCondition ( formatPayload . conditions , templateData . application . data ) as IApplicantNameCondition ; if ( ! matchedCondition ) { throw new Error ( 'No condition has matched for ApplicantName transformer' ) ; } if ( ! matchedCondition . key [ templateData . application . event ] ) { throw new Error ( `No data key defined on payload for event: ${ templateData . application . event }` ) ; } const applicantObj = getValueFromApplicationDataByKey ( templateData . application . data , matchedCondition . key [ templateData . application . event ] ) ; let applicantName = '' ; matchedCondition . format [ formatPayload . language ? formatPayload . language : intl . locale ] . forEach ( ( field ) => { applicantName = applicantName . concat ( `${ ( applicantObj && applicantObj [ field ] ) || '' } ` ) ; } ) ; const fullName = applicantName . substr ( 0 , applicantName . length - 1 ) ; return formatPayload . allCapital ? fullName . toUpperCase ( ) : fullName ; } , FieldValue : ( templateData , intl , payload ? ) => { const key = payload && ( payload as IFeildValuePayload ) ; if ( ! key ) { throw new Error ( 'No payload found for this transformer' ) ; } const draftData = templateData . application . data ; if ( key . condition && ! eval ( key . condition ) ) { return '' ; } const fieldValue = getValueFromApplicationDataByKey ( templateData . application . data , key . valueKey ) ; if ( key . messageDescriptors ) { const matchedOption = key . messageDescriptors . find ( ( option ) => option . matchValue === fieldValue ) ; return ( ( matchedOption && intl . formatMessage ( matchedOption . messageDescriptor ) ) || fieldValue ) ; } return fieldValue ; } , DateFieldValue : ( templateData , intl , payload ? ) => { const formatPayload = payload && ( payload as IDateFeildValuePayload ) ; if ( ! formatPayload ) { throw new Error ( 'No payload found for this transformer' ) ; } const dateValue = formatPayload . key ? getValueFromApplicationDataByKey ( templateData . application . data , formatPayload . key [ templateData . application . event ] ) : Date . now ( ) ; const locale = formatPayload . language ? formatPayload . language : intl . locale ; if ( formatPayload . momentLocale && formatPayload . momentLocale [ locale ] ) { require ( `moment/${ formatPayload . momentLocale [ locale ] }` ) ; } moment . locale ( locale ) ; return ( dateValue && moment ( dateValue ) . format ( formatPayload . format ) ) || '' ; } , FormattedFieldValue : ( templateData , intl , payload ? ) => { const keyParam = payload && ( payload as IFormattedFeildValuePayload ) ; if ( ! keyParam ) { throw new Error ( 'No payload found for this transformer' ) ; } const keys = keyParam . formattedKeys . match ( /\{.*?\}/g ) ; const keyValues = { } ; keys && keys . forEach ( ( key ) => { keyValues [ key ] = getValueFromApplicationDataByKey ( templateData . application . data , key . substr ( 1 , key . length - 2 ) ) || '' ; } ) ; let value = keyParam . formattedKeys ; Object . keys ( keyValues ) . forEach ( ( key ) => { value = value . replace ( new RegExp ( `${ key }` , 'g' ) , keyValues [ key ] || '' ) ; } ) ; return value ; } , ArithmeticOperation : ( templateData , intl , payload ? ) => { const params = payload && ( payload as IArithmeticOperationPayload ) ; if ( ! params ) { throw new Error ( 'No payload found for this transformer' ) ; } const leftValue = getValueFromApplicationDataByKey ( templateData . application . data , params . leftValueKey ) || 0 ; const rightValue = getValueFromApplicationDataByKey ( templateData . application . data , params . rightValueKey ) || 0 ; let value = null ; switch ( params . operationType ) { case 'ADDITION' : value = leftValue + rightValue ; break ; case 'SUBTRACTION' : value = leftValue - rightValue ; break ; case 'DIVISION' : value = leftValue / rightValue ; break ; case 'MULTIPLICATION' : value = leftValue * rightValue ; break ; } return ( value && value . toString ( ) ) || '' ; } , ConditionExecutor : ( templateData , intl , payload ? ) => { const params = payload && ( payload as IConditionExecutorPayload ) ; if ( ! params ) { throw new Error ( 'No payload found for this transformer' ) ; } const fromValue = getExecutorFieldValue ( params . fromKey , templateData . application ) ; const toValue = getExecutorFieldValue ( params . toKey , templateData . application ) ; let value = null ; params . conditions . forEach ( ( condition ) => { if ( condition . type === 'COMPARE_DATE_IN_DAYS' && toValue !== null && fromValue !== null ) { const diffInDays = moment ( toValue ) . diff ( moment ( fromValue ) , 'days' ) ; if ( diffInDays >= condition . minDiff && diffInDays <= condition . maxDiff ) { value = fieldTransformers . IntlLabel ( templateData , intl , condition . output ) ; } } } ) ; return value ; } , NumberConversion : ( templateData , intl , payload ? ) => { const params = payload && ( payload as INumberFeildConversionPayload ) ; if ( ! params ) { throw new Error ( 'No payload found for this transformer' ) ; } let value = ( getValueFromApplicationDataByKey ( templateData . application . data , params . valueKey ) || '' ) . toString ( ) ; Object . keys ( params . conversionMap ) . forEach ( ( numberKey ) => { value = value . replace ( new RegExp ( numberKey , 'g' ) , params . conversionMap [ numberKey ] ) ; } ) ; return value ; } , IdentifierValue : ( templateData , intl , payload ? ) => { const key = payload && ( payload as IPersonIdentifierValuePayload ) ; if ( ! key ) { throw new Error ( 'No payload found for this transformer' ) ; } const idType = getValueFromApplicationDataByKey ( templateData . application . data , key . idTypeKey ) || '' ; if ( idType !== key . idTypeValue ) { return ' ' ; } return ( getValueFromApplicationDataByKey ( templateData . application . data , key . idValueKey ) || ' ' ) ; } } ;