/* SYNC MEDICINES TO CALCULATE */
/* =============================================================================================================== */
DROP PROCEDURE IF EXISTS getMedicineRiskToCalculate;
COMMIT;

DELIMITER //

CREATE PROCEDURE `getMedicineRiskToCalculate`()

BEGIN

DECLARE errno INT;

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

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

  RESIGNAL;
END;

UPDATE `MedicineRiskToCalculate` 
SET `status` = 'empty',
  `updatedAt` = NOW()
WHERE `status` = 'ready'
AND (
  `medicines` IS NULL
  OR `medicines` = ''
);

SELECT `_id`, `userId`, REPLACE(`medicines`, ',', '|') AS `medicines`
FROM `MedicineRiskToCalculate`
WHERE `status` = 'ready';

END
//

COMMIT //

DELIMITER ;
