mirror of
https://github.com/elyby/accounts.git
synced 2025-05-31 14:11:46 +05:30
Rework the webhooks table, allow to update exists webhooks
This commit is contained in:
@@ -4,9 +4,7 @@ declare(strict_types=1);
|
||||
namespace console\models;
|
||||
|
||||
use common\models\WebHook;
|
||||
use common\models\WebHookEvent;
|
||||
use Webmozart\Assert\Assert;
|
||||
use Yii;
|
||||
use yii\base\Model;
|
||||
|
||||
class WebHookForm extends Model {
|
||||
@@ -22,6 +20,9 @@ class WebHookForm extends Model {
|
||||
public function __construct(WebHook $webHook, array $config = []) {
|
||||
parent::__construct($config);
|
||||
$this->webHook = $webHook;
|
||||
$this->url = $webHook->url;
|
||||
$this->secret = $webHook->secret;
|
||||
$this->events = (array)$webHook->events;
|
||||
}
|
||||
|
||||
public function rules(): array {
|
||||
@@ -38,22 +39,12 @@ class WebHookForm extends Model {
|
||||
return false;
|
||||
}
|
||||
|
||||
$transaction = Yii::$app->db->beginTransaction();
|
||||
|
||||
$webHook = $this->webHook;
|
||||
$webHook->url = $this->url;
|
||||
$webHook->secret = $this->secret;
|
||||
$webHook->events = array_values($this->events); // Drop the keys order
|
||||
Assert::true($webHook->save(), 'Cannot save webhook.');
|
||||
|
||||
foreach ($this->events as $event) {
|
||||
$eventModel = new WebHookEvent();
|
||||
$eventModel->webhook_id = $webHook->id;
|
||||
$eventModel->event_type = $event;
|
||||
Assert::true($eventModel->save(), 'Cannot save webhook event.');
|
||||
}
|
||||
|
||||
$transaction->commit();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user