const { currentSearchRequest } = require("../helpers/url");

const getUnsubscribe = async (req, res) => {
  const title = "Uspješno ste se odjavili";
  const searchRequest = await currentSearchRequest(req);

  if (!searchRequest || !searchRequest.dataValues) {
    res.render("notFound", { title: " " });
    return;
  }

  searchRequest.subscribed = false;
  searchRequest.deletedEmail = searchRequest.email;
  searchRequest.email = "";
  await searchRequest.save();

  res.render("unsubscribe", { nextStep: "/vrstanekretnine", title });
};

module.exports = {
  getUnsubscribe
};
