Implement creating of meta data and tests
This commit is contained in:
@@ -96,6 +96,13 @@ final class CreateIncidentCommand
|
||||
*/
|
||||
public $template_vars;
|
||||
|
||||
/**
|
||||
* Meta key/value pairs.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $meta = [];
|
||||
|
||||
/**
|
||||
* The validation rules.
|
||||
*
|
||||
@@ -112,6 +119,7 @@ final class CreateIncidentCommand
|
||||
'stickied' => 'required|bool',
|
||||
'occurred_at' => 'nullable|string',
|
||||
'template' => 'nullable|string',
|
||||
'meta' => 'required|array',
|
||||
];
|
||||
|
||||
/**
|
||||
@@ -128,10 +136,11 @@ final class CreateIncidentCommand
|
||||
* @param string|null $occurred_at
|
||||
* @param string|null $template
|
||||
* @param array $template_vars
|
||||
* @param array $meta
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($name, $status, $message, $visible, $component_id, $component_status, $notify, $stickied, $occurred_at, $template, array $template_vars = [])
|
||||
public function __construct($name, $status, $message, $visible, $component_id, $component_status, $notify, $stickied, $occurred_at, $template, array $template_vars = [], $meta = [])
|
||||
{
|
||||
$this->name = $name;
|
||||
$this->status = $status;
|
||||
@@ -144,5 +153,6 @@ final class CreateIncidentCommand
|
||||
$this->occurred_at = $occurred_at;
|
||||
$this->template = $template;
|
||||
$this->template_vars = $template_vars;
|
||||
$this->meta = $meta;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ use CachetHQ\Cachet\Bus\Exceptions\Incident\InvalidIncidentTimestampException;
|
||||
use CachetHQ\Cachet\Models\Component;
|
||||
use CachetHQ\Cachet\Models\Incident;
|
||||
use CachetHQ\Cachet\Models\IncidentTemplate;
|
||||
use CachetHQ\Cachet\Models\Meta;
|
||||
use CachetHQ\Cachet\Services\Dates\DateFactory;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Contracts\Auth\Guard;
|
||||
@@ -100,6 +101,18 @@ class CreateIncidentCommandHandler
|
||||
// Create the incident
|
||||
$incident = Incident::create($data);
|
||||
|
||||
// Store any meta?
|
||||
if ($meta = $command->meta) {
|
||||
foreach ($meta as $key => $value) {
|
||||
Meta::create([
|
||||
'key' => $key,
|
||||
'value' => $value,
|
||||
'meta_type' => 'incidents',
|
||||
'meta_id' => $incident->id,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
// Update the component.
|
||||
if ($component = Component::find($command->component_id)) {
|
||||
dispatch(new UpdateComponentCommand(
|
||||
|
||||
@@ -78,7 +78,8 @@ class IncidentController extends AbstractApiController
|
||||
Binput::get('stickied', false),
|
||||
Binput::get('occurred_at'),
|
||||
Binput::get('template'),
|
||||
Binput::get('vars', [])
|
||||
Binput::get('vars', []),
|
||||
Binput::get('meta', [])
|
||||
));
|
||||
} catch (QueryException $e) {
|
||||
throw new BadRequestHttpException();
|
||||
|
||||
@@ -154,4 +154,24 @@ class IncidentTest extends AbstractApiTestCase
|
||||
$this->delete('/api/v1/incidents/1');
|
||||
$this->assertResponseStatus(204);
|
||||
}
|
||||
|
||||
public function testCreateIncidentWithMeta()
|
||||
{
|
||||
$this->beUser();
|
||||
|
||||
$this->post('/api/v1/incidents', [
|
||||
'name' => 'Foo',
|
||||
'message' => 'Lorem ipsum dolor sit amet',
|
||||
'status' => 1,
|
||||
'meta' => [
|
||||
'id' => 123456789,
|
||||
],
|
||||
]);
|
||||
$this->seeJson([
|
||||
'meta' => [
|
||||
'id' => 123456789,
|
||||
],
|
||||
]);
|
||||
$this->assertResponseOk();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,6 +40,7 @@ class CreateIncidentCommandTest extends AbstractTestCase
|
||||
'occurred_at' => null,
|
||||
'template' => null,
|
||||
'template_vars' => [],
|
||||
'meta' => [],
|
||||
];
|
||||
|
||||
$object = new CreateIncidentCommand(
|
||||
@@ -53,7 +54,8 @@ class CreateIncidentCommandTest extends AbstractTestCase
|
||||
$params['stickied'],
|
||||
$params['occurred_at'],
|
||||
$params['template'],
|
||||
$params['template_vars']
|
||||
$params['template_vars'],
|
||||
$params['meta']
|
||||
);
|
||||
|
||||
return compact('params', 'object');
|
||||
|
||||
Reference in New Issue
Block a user