Created
March 2, 2022 19:57
-
-
Save kabootit/b36edf58fc51003244e558f5c5370e15 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as R from 'ramda'; | |
import { Request, Response } from 'express'; | |
import { StatusCodes } from 'http-status-codes'; | |
import { ExternalAccountNature, ExternalAccountOwnerType } from '../../../lib/cft/externalAccount/types'; | |
import { CFTUrl } from '../../../lib/cft/apiUrls'; | |
import * as CFT from '../../../lib/cft'; | |
import mapErrorMessageToFailureReason from '../../helpers/mapErrorMessageToFailureReason'; | |
import { isLeadOwnedBankAccount, validateBankAccount } from '../../../lib/cft/enrollClient/helpers/enrollmentOperations/createAndValidateBankAccount'; | |
const createCFTBankAccount = async ({ | |
contact, bankAccount, | |
}) => { | |
await CFT.syncExternalAccount({ | |
externalAccountRequest: { | |
holderName: `${contact.firstName} ${contact.lastName}`, | |
accountNumber: bankAccount.accountNumber, | |
routingNumber: bankAccount.routingNumber, | |
type: bankAccount.type, | |
}, | |
ownerType: ExternalAccountOwnerType.CLIENT, | |
nature: ExternalAccountNature.CONSUMER, | |
leadOrClientId: contact.externalProcessorId, | |
useExistingClientAsLead: true, | |
bankAccountUuid: bankAccount.bankAccountUuid, | |
}); | |
const cftBankAccount = await CFT.getCFTByExternalId( | |
CFTUrl.EXTERNAL_ACCOUNT, | |
bankAccount.bankAccountUuid, | |
); | |
return { cftBankAccount }; | |
}; | |
const checkBankAccount = async ({ | |
contact, cftExternalAccount, bankAccount, | |
}) => { | |
const isSameAccountNumber = cftExternalAccount | |
&& R.equals(cftExternalAccount.accountNumber, bankAccount.accountNumber); | |
const isSameRoutingNumber = cftExternalAccount | |
&& R.equals(cftExternalAccount.routingNumber, bankAccount.routingNumber); | |
const isLeadOwned = await isLeadOwnedBankAccount({ cftExternalAccount, bankAccount }); | |
if ( | |
!cftExternalAccount | |
|| isLeadOwned | |
|| !isSameAccountNumber | |
|| !isSameRoutingNumber | |
) { | |
const { cftBankAccount } = await createCFTBankAccount({ | |
contact, | |
bankAccount, | |
}); | |
return { cftExternalAccount: cftBankAccount }; | |
} | |
return { cftExternalAccount }; | |
}; | |
export const createAndValidateBankAccount = async ( | |
req: Request, | |
res: Response, | |
) => { | |
const { | |
contact, bankAccount, | |
} = req.body; | |
try { | |
const existingBankAccount = await CFT.getCFTByExternalId( | |
CFTUrl.EXTERNAL_ACCOUNT, | |
bankAccount.bankAccountUuid, | |
).catch(() => null); | |
const { cftExternalAccount } = await checkBankAccount({ | |
cftExternalAccount: existingBankAccount, | |
bankAccount, | |
contact, | |
}); | |
const { bankSupported } = await validateBankAccount({ | |
bankAccountId: cftExternalAccount.id, | |
bankAccountExternalId: cftExternalAccount.externalId, | |
}); | |
return res.status(StatusCodes.OK).json({ results: { cftExternalAccount, bankSupported } }); | |
} catch (error) { | |
// This will provide a more useful failReason | |
const failReason = mapErrorMessageToFailureReason(error); | |
return res.status(StatusCodes.OK).json({ failReason, errors: error.reasons }); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment