content: /*
   * Copyright 2018-2024 adorsys GmbH & Co KG
   *
   * This program is free software: you can redistribute it and/or modify it
   * under the terms of the GNU Affero General Public License as published
   * by the Free Software Foundation, either version 3 of the License, or (at
   * your option) any later version. This program is distributed in the hope that
   * it will be useful, but WITHOUT ANY WARRANTY; without even the implied
   * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
   * See the GNU Affero General Public License for more details.
   *
   * You should have received a copy of the GNU Affero General Public License
   * along with this program. If not, see https://www.gnu.org/licenses/.
   *
   * This project is also available under a separate commercial license. You can
   * contact us at sales@adorsys.com.
   */
  
  package de.adorsys.psd2.xs2a.service.mapper.cms_xs2a_mappers;
  
  import de.adorsys.psd2.consent.api.ActionStatus;
  import de.adorsys.psd2.consent.api.TypeAccess;
  import de.adorsys.psd2.xs2a.core.error.MessageErrorCode;
  import org.springframework.stereotype.Component;
  
  @Component
  public class ErrorToActionStatusMapper {
  
      public ActionStatus mapActionStatusError(MessageErrorCode error, boolean withBalance, TypeAccess access) {
          ActionStatus actionStatus = ActionStatus.FAILURE_ACCOUNT;
          if (error == MessageErrorCode.ACCESS_EXCEEDED) {
              actionStatus = ActionStatus.CONSENT_LIMIT_EXCEEDED;
          } else if (error == MessageErrorCode.CONSENT_EXPIRED) {
              actionStatus = ActionStatus.CONSENT_INVALID_STATUS;
          } else if (error.getName().equals(MessageErrorCode.CONSENT_UNKNOWN_400.getName()) && error.getCode() == MessageErrorCode.CONSENT_UNKNOWN_400.getCode()) {
              actionStatus = ActionStatus.CONSENT_NOT_FOUND;
          } else if (error.getName().equals(MessageErrorCode.CONSENT_INVALID.getName())) {
              if (access == TypeAccess.TRANSACTION) {
                  actionStatus = ActionStatus.FAILURE_TRANSACTION;
              } else if (access == TypeAccess.BALANCE || withBalance) {
                  actionStatus = ActionStatus.FAILURE_BALANCE;
              }
          }
          return actionStatus;
      }
  }
  