我正在创建一个项目使用角和节点,在我的项目中,我已经写了一个函数,其中很多如果和如果语句与多个条件。我想重构这个代码与短和干净的代码。目前代码看起来非常复杂和冗长。我已经使用开关语句,但我找不到更好的解决方案。
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`);
}
1条答案
按热度按时间cbeh67ev1#
尝试打开支付提供程序,并为每个提供程序打开支付方法。每个提供程序都略有不同,因此我将避免创建一个可重用函数来传递服务。
如果您想更进一步,可以创建一些函数:
如果不需要嵌套的switch语句,请让每个支付提供程序调用一个函数: