javascript 重构多个条件

7qhs6swi  于 2023-02-28  发布在  Java
关注(0)|答案(1)|浏览(109)

我正在创建一个项目使用角和节点,在我的项目中,我已经写了一个函数,其中很多如果和如果语句与多个条件。我想重构这个代码与短和干净的代码。目前代码看起来非常复杂和冗长。我已经使用开关语句,但我找不到更好的解决方案。

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`);
  }
cbeh67ev

cbeh67ev1#

尝试打开支付提供程序,并为每个提供程序打开支付方法。每个提供程序都略有不同,因此我将避免创建一个可重用函数来传递服务。

const {
  ADYEN,
  COD: COD_PROVIDER,
  GMO
} = OrderRequestV2.PaymentProviderEnum;

const {
  APPLEPAY,
  COD: COD_METHOD,
  CREDITCARD,
  GIFTCERTIFICATE,
  INVOICE,
  PAYPAL
} = OrderRequestV2.PaymentMethodEnum;

const getPaymentMethods = async (orderRequest) => {
  switch (orderRequest.paymentProvider) {
    case ADYEN:
      {
        switch (orderRequest.paymentMethod) {
          case 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];
            }
          case 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];
            }
          case 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];
            }
          case GIFTCERTIFICATE:
            {
              const giftCardPaymentInstruments = await this.pickPaymentInstruments(basket, constants.PAYMENT_METHODS.GIFT_CERTIFICATE, true);
              return this.getGiftCardsPayments(giftCardPaymentInstruments);
            }
          case APPLEPAY:
            {
              const mainPaymentInstruments = await this.pickPaymentInstruments(basket, constants.PAYMENT_METHODS.APPLE_PAY, true);
              const mainPayment = {
                paymentInstrument: mainPaymentInstruments[0],
                paymentMethod: this.AdyenApplePayPaymentService
              };
              return [mainPayment];
            }
        }
        break;
      }
    case COD_PROVIDER:
      {
        switch (orderRequest.paymentMethod) {
          case COD_METHOD:
            {
              const mainPaymentInstruments = await this.pickPaymentInstruments(basket, constants.PAYMENT_METHODS.CASH_ON_DELIVERY, true);
              const mainPayment = {
                paymentInstrument: mainPaymentInstruments[0],
                paymentMethod: this.CashOnDeliveryPaymentService
              };
              return [mainPayment];
            }
        }
        break;
      }
    case GMO:
      {
        switch (orderRequest.paymentMethod) {
          case CREDITCARD:
            {
              const mainPaymentInstruments = await this.pickPaymentInstruments(basket, constants.PAYMENT_METHODS.CREDIT_CARD, true);
              const mainPayment = {
                paymentInstrument: mainPaymentInstruments[0],
                paymentMethod: this.GMOCreditCardPaymentMethodService
              };
              return [mainPayment];
            }
        }
        break;
      }
  }
  throw new BadRequestError(`${ERRORS.PLACE_ORDER_V2_PREFIX} Please verify your request`);
};

如果您想更进一步,可以创建一些函数:

const {
  ADYEN,
  COD: COD_PROVIDER,
  GMO
} = OrderRequestV2.PaymentProviderEnum;

const {
  APPLEPAY,
  COD: COD_METHOD,
  CREDITCARD,
  GIFTCERTIFICATE,
  INVOICE,
  PAYPAL
} = OrderRequestV2.PaymentMethodEnum;

async function getPaymentMethods(orderRequest) {
  switch (orderRequest.paymentProvider) {
    case ADYEN:
      {
        switch (orderRequest.paymentMethod) {
          case CREDITCARD:
            return this.paymentWithGiftCards(this.AdyenCreditCardPaymentMethodService);
          case PAYPAL:
            return this.paymentWithGiftCards(this.adyenPayPalPaymentMethodService);
          case INVOICE:
            return this.paymentWithGiftCards(this.AdyenInvoicePaymentMethodService);
          case GIFTCERTIFICATE:
            return giftPaymentOnly();
          case APPLEPAY:
            return this.simplePayment(this.AdyenApplePayPaymentService);
        }
        break;
      }
    case COD_PROVIDER:
      {
        switch (orderRequest.paymentMethod) {
          case COD_METHOD:
            return this.simplePayment(this.CashOnDeliveryPaymentService);
        }
        break;
      }
    case GMO:
      {
        switch (orderRequest.paymentMethod) {
          case CREDITCARD:
            return this.simplePayment(this.GMOCreditCardPaymentMethodService);
        }
        break;
      }
  }
  throw new BadRequestError(`${ERRORS.PLACE_ORDER_V2_PREFIX} Please verify your request`);
};

async function paymentWithGiftCards(paymentMethod) {
  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
  };
  const giftCardsPayments = this.getGiftCardsPayments(giftCardPaymentInstruments);
  return [mainPayment, ...giftCardsPayments];
}

async function simplePayment(paymentMethod) {
  const mainPaymentInstruments = await this.pickPaymentInstruments(basket, constants.PAYMENT_METHODS.APPLE_PAY, true);
  const mainPayment = {
    paymentInstrument: mainPaymentInstruments[0],
    paymentMethod: paymentMethod;
  };
  return [mainPayment];
}

async function giftPaymentOnly(paymentMethod) {
  const giftCardPaymentInstruments = await this.pickPaymentInstruments(basket, constants.PAYMENT_METHODS.GIFT_CERTIFICATE, true);
  return this.getGiftCardsPayments(giftCardPaymentInstruments);
}

如果不需要嵌套的switch语句,请让每个支付提供程序调用一个函数:

const {
  ADYEN,
  COD: COD_PROVIDER,
  GMO
} = OrderRequestV2.PaymentProviderEnum;

const {
  APPLEPAY,
  COD: COD_METHOD,
  CREDITCARD,
  GIFTCERTIFICATE,
  INVOICE,
  PAYPAL
} = OrderRequestV2.PaymentMethodEnum;

async function getPaymentMethods(orderRequest) {
  switch (orderRequest.paymentProvider) {
    case ADYEN:
      return getAdyenPaymentMethods(orderRequest);
    case COD_PROVIDER:
      return getCodPaymentMethods(orderRequest);
    case GMO:
      return getGmoPaymentMethods(orderRequest);
  }
  throw new BadRequestError(`${ERRORS.PLACE_ORDER_V2_PREFIX} Please verify your request`);
};

async function getAdyenPaymentMethods(orderRequest) {
  switch (orderRequest.paymentMethod) {
    case CREDITCARD:
      return this.paymentWithGiftCards(this.AdyenCreditCardPaymentMethodService);
    case PAYPAL:
      return this.paymentWithGiftCards(this.adyenPayPalPaymentMethodService);
    case INVOICE:
      return this.paymentWithGiftCards(this.AdyenInvoicePaymentMethodService);
    case GIFTCERTIFICATE:
      return giftPaymentOnly();
    case APPLEPAY:
      return this.simplePayment(this.AdyenApplePayPaymentService);
  }
  throw new BadRequestError(`${ERRORS.PLACE_ORDER_V2_PREFIX} Please verify your request`);
}

async function getCodPaymentMethods(orderRequest) {
  switch (orderRequest.paymentMethod) {
    case COD_METHOD:
      return this.simplePayment(this.CashOnDeliveryPaymentService);
  }
  throw new BadRequestError(`${ERRORS.PLACE_ORDER_V2_PREFIX} Please verify your request`);
}

async function getGmoPaymentMethods(orderRequest) {
  switch (orderRequest.paymentMethod) {
    case CREDITCARD:
      return this.simplePayment(this.GMOCreditCardPaymentMethodService);
  }
  throw new BadRequestError(`${ERRORS.PLACE_ORDER_V2_PREFIX} Please verify your request`);
}

async function paymentWithGiftCards(paymentMethod) {
  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
  };
  const giftCardsPayments = this.getGiftCardsPayments(giftCardPaymentInstruments);
  return [mainPayment, ...giftCardsPayments];
}

async function simplePayment(paymentMethod) {
  const mainPaymentInstruments = await this.pickPaymentInstruments(basket, constants.PAYMENT_METHODS.APPLE_PAY, true);
  const mainPayment = {
    paymentInstrument: mainPaymentInstruments[0],
    paymentMethod: paymentMethod;
  };
  return [mainPayment];
}

async function giftPaymentOnly(paymentMethod) {
  const giftCardPaymentInstruments = await this.pickPaymentInstruments(basket, constants.PAYMENT_METHODS.GIFT_CERTIFICATE, true);
  return this.getGiftCardsPayments(giftCardPaymentInstruments);
}

相关问题