import {
  DATA_URL,
  PROCEDURES,
} from 'config'
import { callApi } from 'helpers'

const proc = PROCEDURES.save_user_subscription

type Response = {
  _p_error: string | null,
  _p_status: 1 | 2,
}

const responseStatus = {
  FAILURE: 2,
  SUCCESS: 1,
}

export const saveUserSubscription = async (subscriptionId : number | null) => {
  const config = {
    body: {
      params: {
        _p_subscription_id: subscriptionId,
      },
      proc,
    },
  }

  const response: Response = await callApi({
    config,
    url: DATA_URL,
  })

  if (response._p_status === responseStatus.SUCCESS) {
    return Promise.resolve()
  }
  return Promise.reject(response._p_error)
}
