Joywork/cron/webhooks/send_webhook.php

67 lines
2.3 KiB
PHP
Raw Normal View History

2026-05-22 20:21:54 +02:00
<?php
set_time_limit(0);
require_once($_SERVER['DOCUMENT_ROOT']."/config.php");
ini_set('display_errors', 1);
error_reporting(E_ALL & ~E_DEPRECATED & ~E_STRICT & ~E_NOTICE);
header('Content-Type: application/json');
$pdo = new MysqlPdo(hst, ndb, user, pass);
/**
* Агентства, по которым отправляются вебхуки
*/
$agencys = array();
$sql = "SELECT id, company_id, enable_webhooks FROM users WHERE enable_webhooks = 1";
$q = $pdo->query($sql);
while($r = $pdo->fetch_assoc($q)){
if($r['company_id'] > 0){
if(!in_array($r['company_id'], $agencys)){
$agencys[] = (int)$r['company_id'];
}
} else {
if(!in_array($r['id'], $agencys)){
$agencys[] = (int)$r['id'];
}
}
}
/**
* Поиск записей для отправки
*/
$cron_ids = array();
if(!empty($agencys)){
$sql = "SELECT * FROM `webhook_cron` WHERE initiator_agency_id in (".implode(',', $agencys).") AND send = 0 LIMIT 100";
$q = $pdo->query($sql);
while($r = $pdo->fetch_assoc($q)){
$cron_ids[] = (int)$r['id'];
if($r['initiator_agency_id'] > 0 && (int)$r['entity_id'] > 0){
$dataWhook = array('action'=>$r['event_type']);
$dataWhook['user_id'] = $r['initiator_user_id'];
$dataWhook['agency_id'] = $r['initiator_agency_id'];
$dataWhook['section'] = $r['entity_type'];
if($r['entity_type'] == 'object'){
$dataWhook['object_id'] = (int)$r['entity_id'];
} else if($r['entity_type'] == 'requisition'){
$dataWhook['req_id'] = (int)$r['entity_id'];
} else if($r['entity_type'] == 'client'){
$dataWhook['client_id'] = (int)$r['entity_id'];
}
if(!empty($r['advert_platform'])){
$dataWhook['button'] = $r['advert_platform'];
}
if($r['event_type'] == 'object_update' && !empty($r['new_user_id'])){
$dataWhook['new_who_work'] = (int)$r['new_user_id'];
}
$w_in = new WebHookIn(null, $r['initiator_agency_id']);
$w_in->check_send($dataWhook);
}
}
//var_dump($dataWhook);
if(!empty($cron_ids)){
$sql_up = "UPDATE webhook_cron SET send = 1 WHERE id in (".implode(',', $cron_ids).")";
$q = $pdo->query( $sql_up);
}
}