-- ----------------------------------------------------------------------------------------------------------
-- ----------------------------------------------------------------------------------------------------------
-- Triggers
-- ----------------------------------------------------------------------------------------------------------
-- ----------------------------------------------------------------------------------------------------------

DELIMITER $$

DROP TRIGGER IF EXISTS `clear_candidate_answer`;$$

CREATE TRIGGER `clear_candidate_answer`     
  BEFORE DELETE ON `ohrm_job_candidate`     
  FOR EACH ROW     
 BEGIN
  DELETE FROM  ohrm_candidate_answer where candidate_id = OLD.id;
 END;$$

DROP TRIGGER IF EXISTS `clear_answers`;$$

CREATE TRIGGER `clear_answers`     
  BEFORE DELETE ON `ohrm_candidate_answer`     
  FOR EACH ROW     
 BEGIN
  DELETE FROM ohrm_answer where id = OLD.answer_id;
 END;$$

DROP TRIGGER IF EXISTS `clear_vacancy_user_config`;$$

CREATE TRIGGER `clear_vacancy_user_config`     
  AFTER DELETE ON `ohrm_job_vacancy`     
  FOR EACH ROW     
 BEGIN
  DELETE FROM  `ohrm_user_config` where `key` = CONCAT('recruitment.candidate_filter_for_vacancy.',OLD.id);
 END;$$

DROP TRIGGER IF EXISTS `clear_past_candidate_vacancy_answer_attachments`;$$

CREATE TRIGGER `clear_past_candidate_vacancy_answer_attachments`
    AFTER DELETE ON `ohrm_past_candidate_vacancy_answer`
    FOR EACH ROW
BEGIN
    DELETE FROM  ohrm_past_candidate_vacancy_answer_attachment where id = OLD.attachment_answer;
END;$$

DELIMITER ;
