DELIMITER $$

CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`queueMember_updateQueue`(
	vBusinessFk INT
)
BEGIN
	/**
	* Elimina la entrada de la cola anterior y luego inserta la nueva para un trabajador.
	*
	* @param vBusinessFk ID del negocio
	*/
	DECLARE vNewQueue VARCHAR(10);
	DECLARE vExtension VARCHAR(10);
	DECLARE exit handler FOR SQLEXCEPTION

	SELECT d.pbxQueue, s.extension
		INTO vNewQueue, vExtension
		FROM business b
			JOIN department d ON d.id = b.departmentFk
			JOIN pbx.sip s ON s.user_id = b.workerFk
	WHERE b.id = vBusinessFk;

	DELETE FROM pbx.queueMember 
		WHERE extension = vExtension COLLATE utf8_general_ci;

	INSERT IGNORE INTO pbx.queueMember (queue, extension)
		VALUES (vNewQueue, vExtension);
END$$
DELIMITER ;