DELIMITER $$
CREATE OR REPLACE DEFINER=`vn`@`localhost` TRIGGER `vn`.`department_afterUpdate`
	AFTER UPDATE ON `department`
	FOR EACH ROW
BEGIN
	IF !(OLD.parentFk <=> NEW.parentFk) THEN
		UPDATE vn.department_recalc SET isChanged = TRUE;
	END IF;

	IF !(OLD.twoFactor <=> NEW.twoFactor) THEN
		UPDATE account.user u
				JOIN vn.workerDepartment wd ON wd.workerFk = u.id
			SET u.twoFactor = NEW.twoFactor
		WHERE wd.departmentFk = NEW.id;
	END IF;
END$$
DELIMITER ;
