All files / src/gmail/strategies gmailCrossBorderTransferStrategy.ts

100% Statements 14/14
100% Branches 4/4
100% Functions 2/2
100% Lines 13/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 248x   8x   8x   4x   4x 4x   13x   4x 4x 4x   4x 2x     2x    
import { AbstractPaymentDetailsStrategy } from "../../core/strategies/abstractPaymentDetailsStrategy";
import CrossBorderTransfer from "../../core/types/crossBorderTransfer";
import PaymentDetailsProcessingError from "../../core/errors/paymentDetailsProcessingError";
 
export default class GmailCrossBorderTransferStrategy extends AbstractPaymentDetailsStrategy<CrossBorderTransfer> {
    public tryCreate(paymentDetailsRaw: string[], additionalDetailsRaw: string[]): CrossBorderTransfer {
        const transactionDetailsRaw = paymentDetailsRaw.join('')
 
        const regex = /(?:AZV-)?(\w[^,]+)/g;
        const matches = [...transactionDetailsRaw.matchAll(regex)];
 
        const paymentDetails = matches.map(m => m[1].trim());
 
        const beneficiary = paymentDetails[0];
        const description = paymentDetails.slice(1, paymentDetails.length - 2).join(', '); // Skip BIC
        const iban = paymentDetails[paymentDetails.length - 1];
 
        if (beneficiary === undefined || description === undefined || iban === undefined) {
            throw new PaymentDetailsProcessingError(`Failed to execute regex on input '${transactionDetailsRaw}'`);
        }
 
        return this.paymentDetailsFactory.crossBorderTransfer(beneficiary, iban, description);
    }
}