USE provisioning;

DROP TRIGGER voip_phost_urepl_trig;
DELIMITER ;;
/*!50003 CREATE*/ /*!50017 DEFINER=`sipwise`@`localhost`*/ /*!50003 TRIGGER voip_phost_urepl_trig AFTER UPDATE ON voip_peer_hosts
  FOR EACH ROW BEGIN

  UPDATE kamailio.lcr_gw
     SET gw_name = NEW.name, ip_addr = NEW.ip, hostname = NEW.host, port = NEW.port, transport = NEW.transport, flags = NEW.id
   WHERE lcr_id = 1 AND ip_addr <=> OLD.ip;

  UPDATE kamailio.lcr_rule_target rt, kamailio.lcr_gw gw
     SET rt.weight = NEW.weight
   WHERE gw.id <=> rt.gw_id
     AND gw.lcr_id = 1
     AND gw.group_id <=> NEW.group_id
     AND gw.ip_addr <=> NEW.ip;

  END */;;
DELIMITER ;

DROP TRIGGER provisioning.voip_prul_crepl_trig;
DELIMITER ;;
/*!50003 CREATE*/ /*!50017 DEFINER=`sipwise`@`localhost`*/ /*!50003 TRIGGER provisioning.voip_prul_crepl_trig AFTER INSERT ON voip_peer_rules
  FOR EACH ROW BEGIN

  INSERT INTO kamailio.lcr_rule (lcr_id, prefix, request_uri, from_uri, stopper, enabled, group_id)
                          VALUES(1, NEW.callee_prefix, NEW.callee_pattern, NEW.caller_pattern,
                                 0, 1, NEW.group_id);

  INSERT INTO kamailio.lcr_rule_target (lcr_id, rule_id, gw_id, priority, weight)
         SELECT gw.lcr_id, LAST_INSERT_ID(), gw.id, vpg.priority, vph.weight
           FROM kamailio.lcr_gw gw
           INNER JOIN provisioning.voip_peer_hosts vph ON vph.ip = gw.ip_addr
                                                      AND gw.lcr_id = 1
                                                      AND vph.group_id = gw.group_id
           INNER JOIN provisioning.voip_peer_groups vpg ON vpg.id = vph.group_id
          WHERE vph.group_id <=> NEW.group_id;

  END */;;
DELIMITER ;
