Rework the webhooks table, allow to update exists webhooks

This commit is contained in:
ErickSkrauch
2020-06-14 01:20:31 +03:00
parent 17f1794a4e
commit fb452901b8
10 changed files with 131 additions and 104 deletions

View File

@@ -4,7 +4,6 @@ declare(strict_types=1);
namespace common\models;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveQueryInterface;
use yii\db\ActiveRecord;
/**
@@ -12,18 +11,16 @@ use yii\db\ActiveRecord;
* @property int $id
* @property string $url
* @property string|null $secret
* @property string[] $events
* @property int $created_at
*
* Relations:
* @property WebHookEvent[] $events
*
* Behaviors:
* @mixin TimestampBehavior
*/
class WebHook extends ActiveRecord {
public static function tableName(): string {
return '{{%webhooks}}';
return 'webhooks';
}
public function behaviors(): array {
@@ -35,8 +32,4 @@ class WebHook extends ActiveRecord {
];
}
public function getEvents(): ActiveQueryInterface {
return $this->hasMany(WebHookEvent::class, ['webhook_id' => 'id']);
}
}

View File

@@ -1,27 +0,0 @@
<?php
declare(strict_types=1);
namespace common\models;
use yii\db\ActiveQueryInterface;
use yii\db\ActiveRecord;
/**
* Fields:
* @property int $webhook_id
* @property string $event_type
*
* Relations:
* @property WebHook $webhook
*/
class WebHookEvent extends ActiveRecord {
public static function tableName(): string {
return '{{%webhooks_events}}';
}
public function getWebhook(): ActiveQueryInterface {
return $this->hasOne(WebHook::class, ['id' => 'webhook_id']);
}
}

View File

@@ -6,6 +6,7 @@ namespace common\tasks;
use common\models\Account;
use common\models\WebHook;
use Yii;
use yii\db\Expression;
use yii\queue\RetryableJobInterface;
final class CreateWebHooksDeliveries implements RetryableJobInterface {
@@ -67,8 +68,9 @@ final class CreateWebHooksDeliveries implements RetryableJobInterface {
public function execute($queue): void {
/** @var WebHook[] $targets */
$targets = WebHook::find()
->joinWith('events e', false)
->andWhere(['e.event_type' => $this->type])
// It's very important to use exactly single quote to begin the string
// and double quote to specify the string value
->andWhere(new Expression("JSON_CONTAINS(`events`, '\"{$this->type}\"')"))
->all();
foreach ($targets as $target) {
$job = new DeliveryWebHook();

View File

@@ -1,19 +0,0 @@
<?php
declare(strict_types=1);
namespace common\tests\fixtures;
use common\models\WebHookEvent;
use yii\test\ActiveFixture;
class WebHooksEventsFixture extends ActiveFixture {
public $modelClass = WebHookEvent::class;
public $dataFile = '@root/common/tests/fixtures/data/webhooks-events.php';
public $depends = [
WebHooksFixture::class,
];
}

View File

@@ -1,11 +0,0 @@
<?php
return [
[
'webhook_id' => 1,
'event_type' => 'account.edit',
],
[
'webhook_id' => 2,
'event_type' => 'account.edit',
],
];

View File

@@ -4,18 +4,21 @@ return [
'id' => 1,
'url' => 'http://localhost:80/webhooks/ely',
'secret' => 'my-secret',
'events' => ['account.edit'],
'created_at' => 1531054333,
],
'webhook-without-secret' => [
'id' => 2,
'url' => 'http://localhost:81/webhooks/ely',
'secret' => null,
'events' => ['account.edit'],
'created_at' => 1531054837,
],
'webhook-without-events' => [
'id' => 3,
'url' => 'http://localhost:82/webhooks/ely',
'secret' => null,
'events' => [],
'created_at' => 1531054990,
],
];

View File

@@ -18,7 +18,6 @@ class CreateWebHooksDeliveriesTest extends TestCase {
public function _fixtures(): array {
return [
'webhooks' => fixtures\WebHooksFixture::class,
'webhooksEvents' => fixtures\WebHooksEventsFixture::class,
];
}