DROP PROCEDURE IF EXISTS `update_user_to_seller`;
DELIMITER ;;
CREATE PROCEDURE `update_user_to_seller` (
    IN p_session_id CHAR(64),   
    IN in_location VARCHAR(200)
)
proc:BEGIN
    IF (SELECT COUNT(session_id) FROM sessions WHERE session_id = LOWER(p_session_id)) = 0 OR p_session_id IS NULL THEN
        SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'update_user_to_seller: No user logged in';
        LEAVE proc;
    END IF;
    IF (SELECT COUNT(session_id) FROM sessions WHERE user_type != 1 AND session_id = LOWER(p_session_id)) = 0 THEN
        SIGNAL SQLSTATE '45001' SET MESSAGE_TEXT = 'update_user_to_seller: User already a Seller.';
        LEAVE proc;
    END IF;
    UPDATE users AS user INNER JOIN sessions AS sess ON sess.user_id = user.id SET user.location = in_location, user.type = 1, sess.user_type = 1 WHERE sess.session_id = p_session_id;
END ;;
DELIMITER ;
