sagas/invalidateOdds.js

import { takeLatest, put, call } from 'redux-saga/effects';
import { requestGet, requestPost } from '../utils/request';
import { API_URLS, INVALIDATE_ODDS_GAME_ID, MODALS, SUCCESS_MODALS } from '../constants';
import { aGetInvalidatedOdds, aInvalidateOdds } from '../reducers/actions';
import { aSetIsFetched, aSetOpenModal } from '../reducers/common';
import { aGetInvalidateOddsSuccess } from '../reducers/invalidateOdds';

/**
 * Get Invalidate Odds
 *
 * @memberof sagas/invalidateOdds
 * @async
 */
function* getInvalidateOddsSaga() {
  let response;
  try {
    yield put(aSetIsFetched({ isFetched: false }));
    response = yield call(requestGet, API_URLS.INVALIDATE_ODDS_VIEW, true);

    if (response.status === -1) {
      const e = new Error(response.error.message);
      e.modal = MODALS.ERROR;
      throw e;
    } else {
      yield put(
        aGetInvalidateOddsSuccess({
          mobileLeagueSettings: response.data?.cashout,
          shopPremier: response.data?.shopPremier,
          shopLiga: response.data?.shopLiga,
          shopSeriea: response.data?.shopSeriea,
          shopSonic: response.data?.shopSonic,
        })
      );
    }
    yield put(aSetIsFetched({ isFetched: true }));
  } catch (error) {
    yield put(
      aSetOpenModal({
        modal: error.modal || MODALS.GENERAL_ERROR,
        errorMessage: error.modal ? error.message : MODALS.GENERAL_ERROR,
      })
    );
  }
}

/**
 * Invalidate Odds
 *
 * @param {object} action
 */
function* invalidateOddsSaga(action) {
  let response;
  try {
    const body = {
      competitionId: action.payload.competitionId,
      gameId: action.payload.gameId,
      regenerate: action.payload.regenerate,
    };

    yield put(aSetIsFetched({ isFetched: false }));

    const endPoint =
      action.payload.gameId === INVALIDATE_ODDS_GAME_ID.MOBILE
        ? API_URLS.INVALIDATE_ODDS_MOBILE
        : API_URLS.INVALIDATE_ODDS_SHOP;

    response = yield call(requestPost, endPoint, body, true);

    if (response.status === -1) {
      const e = new Error(response.error.message);
      e.modal = MODALS.ERROR;
      throw e;
    } else {
      yield put(
        aSetOpenModal({
          modal: MODALS.SUCCESS,
          modalData: {
            message: SUCCESS_MODALS.INVALIDATE_ODDS,
            additionalMessage: `${action.payload.competitionName} for Stadium ${
              action.payload.gameId === INVALIDATE_ODDS_GAME_ID.MOBILE ? 'Mobile' : 'Shop'
            }`,
            modal: SUCCESS_MODALS.INVALIDATE_ODDS,
          },
        })
      );
    }
    yield put(aSetIsFetched({ isFetched: true }));
  } catch (error) {
    yield put(
      aSetOpenModal({
        modal: error.modal || MODALS.GENERAL_ERROR,
        errorMessage: error.modal ? error.message : MODALS.GENERAL_ERROR,
      })
    );
  }
}

/**
 * @memberof sagas/invalidateOdds
 * @async
 * @returns {void}
 */
export default function* inValidateOddsSaga() {
  yield takeLatest(aGetInvalidatedOdds, getInvalidateOddsSaga);
  yield takeLatest(aInvalidateOdds, invalidateOddsSaga);
}