const { getSearchRequest } = require("./db/searchRequest");
const { getRealEstateById } = require("./db/realEstate");
const { getKiviOriginalById } = require("./db/kiviOriginal");
const validator = require("validator");

const currentSearchRequest = async req => {
  const searchRequestId =
    req && req.params ? req.params["searchRequestId"] : null;
  if (!searchRequestId) return null;

  return await getSearchRequest(searchRequestId);
};

const currentRealEstate = async req => {
  const realEstateId = req && req.params ? req.params["realEstateId"] : null;
  if (!realEstateId) return null;

  return await getRealEstateById(parseInt(realEstateId));
};
const currentKiviRealEstate = async req => {
  const kiviRealEstateId =
    req && req.params ? req.params["kiviRealEstateId"] : null;
  if (!kiviRealEstateId || !validator.isUUID(kiviRealEstateId)) return null;

  return await getKiviOriginalById(kiviRealEstateId);
};

module.exports = {
  currentSearchRequest,
  currentRealEstate,
  currentKiviRealEstate
};
