Untitled

 avatar
unknown
javascript
5 months ago
6.2 kB
2
Indexable
import axios from 'axios';

import TRouter from '../../../core/TRouter.js';
import DsdrHelpers from '../../../helpers/DsdrHelpers.js';
import { setTimeout } from 'timers/promises';

TRouter.post('/v2/verify_async', async function (req, res) {
  const initResponse = {
    success: true,
    error: '',
    code: '',
  };
  let initHttpStatusCode = 200;
  let data = {
    ResultText: '',
    ResultCode: '',
    Verify_ID_Number: '',
    Names: '',
    DOB: '',
    Gender: '',
    Phone_Number: '',
    ID_Verification: '',
    user_id: '',
    job_id: '',
    timestamp: '2022-09-21T17:57:30.410Z',
  };
  data.job_id = req.body.partner_params.job_id;
  data.user_id = req.body.partner_params.user_id;
  const first_name = req.body.first_name;

  switch (first_name) {
    // South Africa, Nigeria, Ghana, Kenya All Match for valid ids
    case 'Valid':
      res.status(200);
      data.ResultText = 'Exact Match';
      data.ResultCode = '1020';
      data.Verify_ID_Number = 'Verified';
      data.Names = 'Exact Match';
      data.DOB = 'Exact Match';
      data.Gender = 'Not Provided';
      data.Phone_Number = 'Exact Match';
      data.ID_Verification = 'Match';
      break;
    // South Africa, Nigeria, Ghana, Kenya Transposed Names and DOB
    case 'Transposed':
      res.status(200);
      data.ResultText = 'Partial Match';
      data.ResultCode = '1021';
      data.Verify_ID_Number = 'Verified';
      data.Names = 'Transposed';
      data.DOB = 'Transposed';
      data.Gender = 'Not Provided';
      data.Phone_Number = 'Not Provided';
      data.ID_Verification = 'Partial Match';
      break;
    // South Africa, Nigeria, Ghana, Kenya Partial Match
    case 'Partial':
      res.status(200);
      data.ResultText = 'Exact Match';
      data.ResultCode = '1021';
      data.Verify_ID_Number = 'Verified';
      data.Names = 'Partial Match';
      data.DOB = 'Exact Match';
      data.Gender = 'Not Provided';
      data.Phone_Number = 'Not Provided';
      data.ID_Verification = 'Exact Match';
      break;
    // South Africa, Nigeria, Ghana, Kenya No Match
    case 'NoMatch':
      res.status(200);
      data.ResultText = 'No Match';
      data.ResultCode = '1022';
      data.Verify_ID_Number = 'Not Verified';
      data.Names = 'No Match';
      data.DOB = 'No Match';
      data.Gender = 'Not Provided';
      data.Phone_Number = 'No Match';
      data.ID_Verification = 'No Match';
      break;
    case 'error':
      initHttpStatusCode = 400;
      initResponse.success = false;
      break;
    case 'timeout':
      await setTimeout(1000000);
      initHttpStatusCode = 408;
      initResponse.success = false;
      break;
    case '2412':
      initHttpStatusCode = 200;
      initResponse.code = '2412';
      initResponse.success = true;
      initResponse.error = 'Invalid job type.';
      data = JSON.parse(`{
        "SmileJobID": "0000000014",
        "job_id": "${req.body.partner_params.job_id}",
        "user_id": "${req.body.partner_params.user_id}",
        "job_type": 5,
        "ResultText": "Unable to verify ID - Unsupported ID number format",
        "ResultCode": "1014",
        "Return_Personal_Info": "Not Applicable",
        "Verify_ID_Number": "Not Done",
        "Source": "Async ID Verification",
        "timestamp": "2021-04-09T18:59:46.748Z",
        "sec_key": "ayR0ZHZ1fWFLprAFJFudmjfL725NJM9LPuJEmtfkghH5razC6qKEPwkedMq1A8DlYjNvEfyqi/L1H2cf4woJcoUM4UW9SApshM4/7eIeQgeBIVM+PYULm0hpybbLgLMuASKU2UjQbVvmQBgdnFsbYxMtgfyW1IRNAkcG33i2OiQ=|a05236ed87af0355ca315cec81f193bbabfdb96660f7a12181f210c490efd2ff"
      }`);
      break;
    case '2220':
      initHttpStatusCode = 200;
      initResponse.code = '2220';
      initResponse.success = true;
      initResponse.error = 'Invalid job type.';
      data = JSON.parse(`{
        "SmileJobID": "0000000015",
        "job_id": "${req.body.partner_params.job_id}",
        "user_id": "${req.body.partner_params.user_id}",
        "job_type": 5,
        "ResultText": "Unable to verify ID - Unsupported ID number format",
        "ResultCode": "1014",
        "Return_Personal_Info": "Not Applicable",
        "Verify_ID_Number": "Not Done",
        "Source": "Async ID Verification",
        "timestamp": "2021-04-09T18:59:46.748Z",
        "sec_key": "ayR0ZHZ1fWFLprAFJFudmjfL725NJM9LPuJEmtfkghH5razC6qKEPwkedMq1A8DlYjNvEfyqi/L1H2cf4woJcoUM4UW9SApshM4/7eIeQgeBIVM+PYULm0hpybbLgLMuASKU2UjQbVvmQBgdnFsbYxMtgfyW1IRNAkcG33i2OiQ=|a05236ed87af0355ca315cec81f193bbabfdb96660f7a12181f210c490efd2ff"
      }`);
      break;
    case '1016':
      initHttpStatusCode = 200;
      initResponse.code = '1016';
      initResponse.success = true;
      initResponse.error = 'Invalid job type.';
      data = JSON.parse(`{
        "SmileJobID": "0000000015",
        "job_id": "${req.body.partner_params.job_id}",
        "user_id": "${req.body.partner_params.user_id}",
        "job_type": 5,
        "ResultText": "Unable to verify ID - Unsupported ID number format",
        "ResultCode": "1016",
        "Return_Personal_Info": "Not Applicable",
        "Verify_ID_Number": "Not Done",
        "Source": "Async ID Verification",
        "timestamp": "2021-04-09T18:59:46.748Z",
        "sec_key": "ayR0ZHZ1fWFLprAFJFudmjfL725NJM9LPuJEmtfkghH5razC6qKEPwkedMq1A8DlYjNvEfyqi/L1H2cf4woJcoUM4UW9SApshM4/7eIeQgeBIVM+PYULm0hpybbLgLMuASKU2UjQbVvmQBgdnFsbYxMtgfyW1IRNAkcG33i2OiQ=|a05236ed87af0355ca315cec81f193bbabfdb96660f7a12181f210c490efd2ff"
      }`);
      break;
  }

  res.status(initHttpStatusCode);
  res.json(initResponse);

  // Render dynamic liquid file with data provided,
  // then send the json object to callback url provided in the request
  DsdrHelpers.renderLiquidFile('dynamicResponse', data).then((callbackResponse) => {
    const axiosRequestConfig = axios.create({
      url: req.body.callback_url,
      method: 'post',
      data: callbackResponse,
      headers: { 'Content-Type': 'application/json' },
    });
    DsdrHelpers.asyncCallback(axiosRequestConfig.defaults, 1000).catch((error) => {
      console.log(error);
    });
  });
});
Editor is loading...
Leave a Comment