DROP PROCEDURE IF EXISTS GetDiscutMessages;
DELIMITER //
CREATE PROCEDURE GetDiscutMessages
(
  IN p_sid INT,
  IN p_password TEXT,
  IN p_did INT,
  OUT outputCode INT(11)
)
MODIFIES SQL DATA
BEGIN

CALL `CheckIdLogin`(p_sid,p_password, outputCode);
IF outputCode = 42 OR outputCode = 2005 THEN 
    SET outputCode :=( SELECT CASE WHEN (SELECT CheckInGroup(p_sid,authorizedGroupID)=42 OR p_sid=creatorID FROM discussions WHERE ID=p_did) THEN 42 ELSE 2019 END );
    IF outputCode = 42 THEN
        INSERT INTO smz SELECT m.ID AS ID,u.ID AS senderID,u.pseudo AS senderPseudo,m.texte AS texte,m.sendTime AS sendTime, p_sid = senderID AS rights FROM messages AS m INNER JOIN membres AS u ON u.ID = m.senderID WHERE m.discussionID = p_did;
    END IF;
END IF;

END//
DELIMITER ;
