DELIMITER $$
CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`worker_checkMultipleDevice`(
	vSelf INT
)
BEGIN
/**
 * Verify if a worker has multiple assigned devices,
 * except for freelancers.
 *
 * @param vUserFk worker id.
 */
	DECLARE vHasPda BOOLEAN;
	DECLARE vIsFreelance BOOLEAN;
	DECLARE vMaxDevicesPerUser INT;
	
	SELECT COUNT(*) INTO vHasPda FROM deviceProductionUser WHERE userFk = vSelf; 
	SELECT IFNULL(isFreelance, FALSE) INTO vIsFreelance FROM worker WHERE id = vSelf;
	SELECT IFNULL(maxDevicesPerUser, FALSE)  INTO vMaxDevicesPerUser FROM deviceProductionConfig LIMIT 1;

	IF NOT vIsFreelance AND vHasPda > vMaxDevicesPerUser THEN
		CALL util.throw('You can only have one PDA');
	END IF;
END$$
DELIMITER ;
