I am currently working on a project that involves Angular and Node. In this project, I have written a function with multiple if and else if statements containing various conditions. The code looks complex and lengthy, and I want to refactor it to make it more concise and clean. Although I tried using a switch statement, I haven't found a satisfactory solution yet.
if (
(
!orderRequest.paymentProvider
|| orderRequest.paymentProvider === OrderRequestV2.PaymentProviderEnum.ADYEN
)
&& orderRequest.paymentMethod === OrderRequestV2.PaymentMethodEnum.CREDITCARD
) {
const mainPaymentInstruments = await this.pickPaymentInstruments(basket, constants.PAYMENT_METHODS.CREDIT_CARD, true);
const giftCardPaymentInstruments = await this.pickPaymentInstruments(basket, constants.PAYMENT_METHODS.GIFT_CERTIFICATE);
const mainPayment = {
paymentInstrument: mainPaymentInstruments[0],
paymentMethod: this.AdyenCreditCardPaymentMethodService
};
const giftCardsPayments = this.getGiftCardsPayments(giftCardPaymentInstruments);
return [
mainPayment,
...giftCardsPayments
];
} else if (
orderRequest.paymentProvider === OrderRequestV2.PaymentProviderEnum.ADYEN
&& orderRequest.paymentMethod === OrderRequestV2.PaymentMethodEnum.PAYPAL
) {
const mainPaymentInstruments = await this.pickPaymentInstruments(basket, constants.PAYMENT_METHODS.ADYEN_PAYPAL, true);
const giftCardPaymentInstruments = await this.pickPaymentInstruments(basket, constants.PAYMENT_METHODS.GIFT_CERTIFICATE);
const mainPayment = {
paymentInstrument: mainPaymentInstruments[0],
paymentMethod: this.adyenPayPalPaymentMethodService
};
const giftCardsPayments = this.getGiftCardsPayments(giftCardPaymentInstruments);
return [
mainPayment,
...giftCardsPayments
];
} else if (
(
!orderRequest.paymentProvider
|| orderRequest.paymentProvider === OrderRequestV2.PaymentProviderEnum.ADYEN
)
&& orderRequest.paymentMethod === OrderRequestV2.PaymentMethodEnum.INVOICE
) {
const mainPaymentInstruments = await this.pickPaymentInstruments(basket, constants.PAYMENT_METHODS.INVOICE, true);
const giftCardPaymentInstruments = await this.pickPaymentInstruments(basket, constants.PAYMENT_METHODS.GIFT_CERTIFICATE);
const mainPayment = {
paymentInstrument: mainPaymentInstruments[0],
paymentMethod: this.AdyenInvoicePaymentMethodService
};
const giftCardsPayments = this.getGiftCardsPayments(giftCardPaymentInstruments);
return [
mainPayment,
...giftCardsPayments
];
} else if (
orderRequest.paymentProvider === OrderRequestV2.PaymentProviderEnum.GMO
&& orderRequest.paymentMethod === OrderRequestV2.PaymentMethodEnum.CREDITCARD
) {
const mainPaymentInstruments = await this.pickPaymentInstruments(basket, constants.PAYMENT_METHODS.CREDIT_CARD, true);
const mainPayment = {
paymentInstrument: mainPaymentInstruments[0],
paymentMethod: this.GMOCreditCardPaymentMethodService
};
return [mainPayment];
} else if (
orderRequest.paymentProvider === OrderRequestV2.PaymentProviderEnum.COD
&& orderRequest.paymentMethod === OrderRequestV2.PaymentMethodEnum.COD
) {
const mainPaymentInstruments = await this.pickPaymentInstruments(basket, constants.PAYMENT_METHODS.CASH_ON_DELIVERY, true);
const mainPayment = {
paymentInstrument: mainPaymentInstruments[0],
paymentMethod: this.CashOnDeliveryPaymentService
};
return [mainPayment];
} else if (
(
!orderRequest.paymentProvider
|| orderRequest.paymentProvider === OrderRequestV2.PaymentProviderEnum.ADYEN
)
&& orderRequest.paymentMethod === OrderRequestV2.PaymentMethodEnum.GIFTCERTIFICATE
) {
const giftCardPaymentInstruments = await this.pickPaymentInstruments(basket, constants.PAYMENT_METHODS.GIFT_CERTIFICATE, true);
return this.getGiftCardsPayments(giftCardPaymentInstruments);
} else if (
(
!orderRequest.paymentProvider
|| orderRequest.paymentProvider === OrderRequestV2.PaymentProviderEnum.ADYEN
)
&& orderRequest.paymentMethod === OrderRequestV2.PaymentMethodEnum.APPLEPAY
) {
const mainPaymentInstruments = await this.pickPaymentInstruments(basket, constants.PAYMENT_METHODS.APPLE_PAY, true);
const mainPayment = {
paymentInstrument: mainPaymentInstruments[0],
paymentMethod: this.AdyenApplePayPaymentService
};
return [mainPayment];
}
throw new BadRequestError(`${ERRORS.PLACE_ORDER_V2_PREFIX} Please verify your request`);
}