import { IApplication , ITaskHistory } from '@client/applications' ; import { BirthSection , DeathSection , Event , IFormSectionData } from '@client/forms' ; import { GQLBirthEventSearchSet , GQLDeathEventSearchSet , GQLEventSearchSet } from '@opencrvs/gateway/src/graphql/schema' ; import { IUserDetails } from './userUtils' ; import { getEvent } from '@client/views/PrintCertificate/utils' ; import { BIRTH , DEATH } from './constants' ; const getApplicantFullName = ( sectionData , language = 'en' ) : string => { let fullName = '' ; if ( ! sectionData ) { return fullName ; } if ( language === 'en' ) { if ( sectionData . firstNamesEng ) { fullName = `${ sectionData . firstNamesEng as string } ${ sectionData . familyNameEng as string }` ; } else { fullName = sectionData . familyNameEng as string ; } } else { if ( sectionData . firstNames ) { fullName = `${ sectionData . firstNames as string } ${ sectionData . familyName as string }` ; } else { fullName = sectionData . familyName as string ; } } return fullName ; } ; export const getDraftApplicantFullName = ( draft , language ? ) => { switch ( draft . event ) { case Event . BIRTH : return getApplicantFullName ( draft . data . child , language ) ; case Event . DEATH : return getApplicantFullName ( draft . data . deceased , language ) ; } } ; const transformBirthSearchQueryDataToDraft = ( data , application ) => { application . data . child = { firstNamesEng : ( data . childName && data . childName . filter ( ( name ) => name && name . use === 'en' ) . map ( ( name ) => name && name . firstNames ) [ 0 ] ) || '' , familyNameEng : ( data . childName && data . childName . filter ( ( name ) => name && name . use === 'en' ) . map ( ( name ) => name && name . familyName ) [ 0 ] ) || '' , firstNames : ( data . childName && data . childName . filter ( ( name ) => name && name . use !== 'en' ) . map ( ( name ) => name && name . firstNames ) [ 0 ] ) || '' , familyName : ( data . childName && data . childName . filter ( ( name ) => name && name . use !== 'en' ) . map ( ( name ) => name && name . familyName ) [ 0 ] ) || '' } ; } ; const transformDeathSearchQueryDataToDraft = ( data , application ) => { application . data . deceased = { firstNamesEng : ( data . deceasedName && data . deceasedName . filter ( ( name ) => name && name . use === 'en' ) . map ( ( name ) => name && name . firstNames ) [ 0 ] ) || '' , familyNameEng : ( data . deceasedName && data . deceasedName . filter ( ( name ) => name && name . use === 'en' ) . map ( ( name ) => name && name . familyName ) [ 0 ] ) || '' , firstNames : ( data . deceasedName && data . deceasedName . filter ( ( name ) => name && name . use !== 'en' ) . map ( ( name ) => name && name . firstNames ) [ 0 ] ) || '' , familyName : ( data . deceasedName && data . deceasedName . filter ( ( name ) => name && name . use !== 'en' ) . map ( ( name ) => name && name . familyName ) [ 0 ] ) || '' } ; } ; export const transformSearchQueryDataToDraft = ( data ) : IApplication => { const eventType = getEvent ( data . type ) ; const application = { id : data . id , data : { registration : { contactPoint : { nestedFields : { } } } } , event : eventType } ; application . data . registration . contactPoint . nestedFields . registrationPhone = data . registration && data . registration . contactNumber ; application . trackingId = data . registration && data . registration . trackingId ; application . submissionStatus = data . registration && data . registration . status ; application . compositionId = data . id ; application . operationHistories = data . operationHistories as ITaskHistory [ ] ; switch ( eventType ) { case Event . BIRTH : default : transformBirthSearchQueryDataToDraft ( data , application ) ; break ; case Event . DEATH : transformDeathSearchQueryDataToDraft ( data , application ) ; break ; } return application ; } ; export const updateApplicationTaskHistory = ( application , userDetails ) : ITaskHistory => { return { operationType : application . submissionStatus , operatedOn : ( application . modifiedOn && new Date ( application . modifiedOn ) . toString ( ) ) || '' , operatorRole : ( userDetails && userDetails . role ) || '' , operatorName : ( userDetails && userDetails . name ) || [ ] , operatorOfficeName : ( userDetails && userDetails . primaryOffice && userDetails . primaryOffice . name ) || '' , operatorOfficeAlias : ( userDetails && userDetails . primaryOffice && userDetails . primaryOffice . alias ) || [ ] } ; } ; export const getAttachmentSectionKey = ( applicationEvent ) : string => { switch ( applicationEvent ) { case DEATH : return DeathSection . DeathDocuments ; case BIRTH : default : return BirthSection . Documents ; } } ;