52 lines
1.1 KiB
SQL
52 lines
1.1 KiB
SQL
DELIMITER $$
|
|
CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`vehicle_notifyEvents`()
|
|
proc:BEGIN
|
|
/**
|
|
* Query the vehicleEvent table to see if there are any events that need to be notified.
|
|
*
|
|
*/
|
|
DECLARE vEventEarlyDays INT;
|
|
DECLARE vHasEvents INT;
|
|
DECLARE EXIT HANDLER FOR SQLEXCEPTION
|
|
BEGIN
|
|
ROLLBACK;
|
|
RESIGNAL;
|
|
END;
|
|
|
|
SELECT eventEarlyDays INTO vEventEarlyDays
|
|
FROM vehicleConfig;
|
|
|
|
DROP TEMPORARY TABLE IF EXISTS tEvents;
|
|
CREATE TEMPORARY TABLE tEvents
|
|
SELECT e.id
|
|
FROM vehicleEvent e
|
|
JOIN vehicleState s ON e.vehicleStateFk = s.id
|
|
WHERE e.finished BETWEEN util.VN_CURDATE() AND TIMESTAMPADD(DAY, vEventEarlyDays, util.VN_CURDATE())
|
|
AND e.notified IS NULL
|
|
AND s.hasToNotify;
|
|
|
|
SELECT COUNT(id) INTO vHasEvents FROM tEvents;
|
|
|
|
IF NOT vHasEvents THEN
|
|
LEAVE proc;
|
|
END IF;
|
|
|
|
START TRANSACTION;
|
|
|
|
SELECT util.notification_send('vehicle-event-expired',
|
|
JSON_OBJECT('eventIds', JSON_ARRAYAGG(id)),
|
|
account.myUser_getId()
|
|
)
|
|
FROM tEvents;
|
|
|
|
UPDATE vehicleEvent e
|
|
JOIN tEvents s ON e.id = s.id
|
|
SET e.notified = util.VN_NOW()
|
|
WHERE e.id = s.id;
|
|
COMMIT;
|
|
|
|
DROP TEMPORARY TABLE IF EXISTS tEvents;
|
|
|
|
END$$
|
|
DELIMITER ;
|