All files / src/api faceLiveness.js

95.65% Statements 44/46
90.47% Branches 19/21
100% Functions 6/6
95.65% Lines 44/46

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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147                                2x 4x 4x         2x 1x             1x       3x 2x   1x           2x 1x             2x 6x 6x         1x   5x   5x   1x 1x   1x 1x   1x 1x   1x 1x   1x   4x           2x 1x                         2x 6x   6x 4x   2x 1x     1x                         2x 8x 1x             7x   7x 6x   1x 1x                  
/**
 * @typedef {import('../types/faceLiveness').CreateFaceLivenessSessionResponse} CreateFaceLivenessSessionResponse
 */
 
import { getConfig } from "../config/index";
import { httpRequest } from "../utils/httpClient";
import { ApacuanaAPIError } from "../errors";
import ApacuanaSuccess from "../success";
import { INTEGRATION_TYPE } from "../utils/constant";
 
/**
 * Creates a new Face Liveness session.
 * @returns {Promise<CreateFaceLivenessSessionResponse>} Object with the session ID and a success indicator.
 * @throws {ApacuanaAPIError} If the API response is invalid or the integration type is not supported.
 * @throws {Error} If the request fails for another reason.
 */
const createFaceLivenessSessionOnBoarding = async () => {
  try {
    const response = await httpRequest(
      "services/api/faceliveness/create",
      {},
      "POST"
    );
    if (!response.sessionid) {
      throw new ApacuanaAPIError(
        "The API response does not contain the session ID.",
        response.status,
        "INVALID_API_RESPONSE"
      );
    }
 
    return new ApacuanaSuccess({
      sessionId: response.sessionid,
    });
  } catch (error) {
    if (error instanceof ApacuanaAPIError) {
      throw error;
    }
    throw new ApacuanaAPIError(
      `Failed to create Face Liveness session: ${error.message}`
    );
  }
};
 
const createFaceLivenessSessionOnPremise = async () => {
  throw new ApacuanaAPIError(
    "Creating a Face Liveness session is not supported for integration type: ONPREMISE",
    501,
    "NOT_IMPLEMENTED"
  );
};
 
const validateFaceLivenessOnBoarding = async (sessionId) => {
  try {
    const response = await httpRequest(
      "services/api/faceliveness/validate",
      { sessionid: sessionId },
      "POST"
    );
    return new ApacuanaSuccess({ status: "verified", ...response });
  } catch (error) {
    Eif (error instanceof ApacuanaAPIError) {
      let status;
      switch (error.statusCode) {
        case 402:
          status = "waitingForScan";
          break;
        case 403:
          status = "rejected";
          break;
        case 406:
          status = "processing";
          break;
        case 408:
          status = "expired";
          break;
        default:
          throw error;
      }
      return new ApacuanaSuccess({ status });
    }
    throw error;
  }
};
 
const validateFaceLivenessOnPremise = async () => {
  throw new ApacuanaAPIError(
    "Validating a Face Liveness session is not supported for integration type: ONPREMISE",
    501,
    "NOT_IMPLEMENTED"
  );
};
 
/**
 * Creates a new Face Liveness session.
 * @returns {Promise<CreateFaceLivenessSessionResponse>} Object with the session ID and a success indicator.
 * @throws {ApacuanaAPIError} If the API response is invalid or the integration type is not supported.
 * @throws {Error} If the request fails for another reason.
 */
export const createFaceLivenessSession = async () => {
  const { integrationType } = getConfig();
 
  if (integrationType === INTEGRATION_TYPE.ONBOARDING) {
    return createFaceLivenessSessionOnBoarding();
  }
  if (integrationType === INTEGRATION_TYPE.ONPREMISE) {
    return createFaceLivenessSessionOnPremise();
  }
 
  throw new ApacuanaAPIError(
    `Unsupported integration type: ${integrationType}`,
    400,
    "UNSUPPORTED_INTEGRATION_TYPE"
  );
};
 
/**
 * Validates a Face Liveness session and returns its status.
 * @param {{sessionId: string}} params - Object containing the session ID.
 * @returns {Promise<ApacuanaSuccess>} Object with the validation status.
 * @throws {ApacuanaAPIError} If the API response is invalid or the integration type is not supported.
 */
export const validateFaceLiveness = async ({ sessionId }) => {
  if (!sessionId) {
    throw new ApacuanaAPIError(
      "sessionId is a required parameter.",
      400,
      "INVALID_PARAMETER"
    );
  }
 
  const { integrationType } = getConfig();
 
  if (integrationType === INTEGRATION_TYPE.ONBOARDING) {
    return validateFaceLivenessOnBoarding(sessionId);
  }
  Eif (integrationType === INTEGRATION_TYPE.ONPREMISE) {
    return validateFaceLivenessOnPremise();
  }
 
  throw new ApacuanaAPIError(
    `Unsupported integration type: ${integrationType}`,
    400,
    "UNSUPPORTED_INTEGRATION_TYPE"
  );
};