我正在使用 angular 和节点创建一个项目,在我的项目中,我编写了一个函数,其中有很多具有多个条件的 if 和 else if 语句。我遇到了认知复杂性问题。我浏览了一些链接,但我无法理解如何在不影响此方法用户的情况下更改我的代码。
const isAdyenProvider = orderRequest.paymentProvider === OrderRequestV2.PaymentProviderEnum.ADYEN;
if (
(
!orderRequest.paymentProvider
|| isAdyenProvider
)
&& 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 (
isAdyenProvider
&& 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
|| isAdyenProvider
)
&& 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.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
|| isAdyenProvider
)
&& 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
|| isAdyenProvider
)
&& 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];
} else if (
(
!orderRequest.paymentProvider
|| isAdyenProvider
)
&& orderRequest.paymentMethod === OrderRequestV2.PaymentMethodEnum.SHOPRUNNER
) {
const mainPaymentInstruments = await this.pickPaymentInstruments(basket, constants.PAYMENT_METHODS.CREDIT_CARD, true);
const mainPayment = {
paymentInstrument: mainPaymentInstruments[0],
paymentMethod: this.AdyenShoprunnerPaymentService
};
return [mainPayment];
}
throw new BadRequestError(`${ERRORS.PLACE_ORDER_V2_PREFIX} Please verify your request`);