/* UPDATE EMAIL IN QUEUE AS SENT */
/* =============================================================================================================== */
DROP PROCEDURE IF EXISTS updateEmailAsSent;
COMMIT;

DELIMITER //

CREATE PROCEDURE `updateEmailAsSent`(`p_queueId` int)
BEGIN

DECLARE errno int;

DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
	GET CURRENT DIAGNOSTICS CONDITION 1 errno = MYSQL_ERRNO;

	INSERT INTO debug_log(activity, ts_when, log) 
  SELECT 'PROC ERR: emailSent', NOW(), errno; 

	RESIGNAL;
END;

UPDATE `EmailQueue`
SET `status` = 'sent',
  `attempts` = `attempts` + 1,
  `sentAt` = NOW(),
  `updatedAt` = NOW()
WHERE `_id` = p_queueId;

CALL getNextEmailInQueue();

END
//

COMMIT //

DELIMITER ;

