All files / src/api revocations.js

86.11% Statements 31/36
80% Branches 16/20
83.33% Functions 5/6
86.11% Lines 31/36

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110                  2x 3x 3x         1x   2x 1x   1x       2x                             2x 5x 2x         3x   3x 3x                       2x 4x 4x         2x 1x   1x   3x 2x   1x       2x 1x                       2x 5x   5x 4x   1x 1x                
import { httpRequest } from "../utils/httpClient";
import { getConfig } from "../config";
import { INTEGRATION_TYPE } from "../utils/constant";
import { ApacuanaAPIError } from "../errors";
import ApacuanaSuccess from "../success";
 
/** @typedef {import("../types/revocations").RequestRevocationResponse} RequestRevocationResponse */
/** @typedef {import("../types/revocations").GetRevocationReasonsResponse} GetRevocationReasonsResponse */
 
const requestRevocationOnBoarding = async (params) => {
  try {
    const response = await httpRequest(
      "services/api/onboardingclient/requestcert",
      { reason: params.reasonCode },
      "POST"
    );
    return new ApacuanaSuccess(response);
  } catch (error) {
    if (error instanceof ApacuanaAPIError) {
      throw error;
    }
    throw new Error(`Failed to request revocation: ${error.message}`);
  }
};
 
const requestRevocationOnPremise = async () => {
  throw new ApacuanaAPIError(
    "Requesting revocation is not supported for integration type: ONPREMISE",
    501,
    "NOT_IMPLEMENTED"
  );
};
 
/**
 * Solicita la revocación de un certificado.
 * @param {object} params - Parámetros para la solicitud.
 * @param {number} params.reasonCode - El código de la razón de la revocación.
 * @returns {Promise<RequestRevocationResponse>} Una promesa que resuelve con la confirmación de la solicitud.
 * @throws {Error} Si los parámetros son inválidos.
 */
export const requestRevocation = async (params) => {
  if (!params || typeof params.reasonCode !== "number") {
    throw new Error(
      'The "params" object with a numeric "reasonCode" property is required.'
    );
  }
 
  const { integrationType } = getConfig();
 
  Eif (integrationType === INTEGRATION_TYPE.ONBOARDING) {
    return requestRevocationOnBoarding(params);
  }
  if (integrationType === INTEGRATION_TYPE.ONPREMISE) {
    return requestRevocationOnPremise(params);
  }
  throw new ApacuanaAPIError(
    `Unsupported integration type: ${integrationType}`,
    400,
    "UNSUPPORTED_INTEGRATION_TYPE"
  );
};
 
const getRevocationReasonsOnBoarding = async () => {
  try {
    const response = await httpRequest(
      "config/api/revocation/reasonsonboarding",
      {},
      "GET"
    );
    if (!response || !response.records) {
      throw new ApacuanaAPIError("Failed to fetch revocation reasons.");
    }
    return new ApacuanaSuccess({ reasons: response.records });
  } catch (error) {
    if (error instanceof ApacuanaAPIError) {
      throw error;
    }
    throw new Error(`Failed to get revocation reasons: ${error.message}`);
  }
};
 
const getRevocationReasonsOnPremise = async () => {
  throw new ApacuanaAPIError(
    "Getting revocation reasons is not supported for integration type: ONPREMISE",
    501,
    "NOT_IMPLEMENTED"
  );
};
 
/**
 * Obtiene la lista de razones de revocación de certificados.
 * @returns {Promise<GetRevocationReasonsResponse>} Una promesa que resuelve con la lista de razones de revocación.
 * @throws {ApacuanaAPIError} Si la llamada a la API falla o el tipo de integración no es soportado.
 */
export const getRevocationReasons = async () => {
  const { integrationType } = getConfig();
 
  if (integrationType === INTEGRATION_TYPE.ONBOARDING) {
    return getRevocationReasonsOnBoarding();
  }
  Eif (integrationType === INTEGRATION_TYPE.ONPREMISE) {
    return getRevocationReasonsOnPremise();
  }
  throw new ApacuanaAPIError(
    `Unsupported integration type: ${integrationType}`,
    400,
    "UNSUPPORTED_INTEGRATION_TYPE"
  );
};