|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Restify; |
| 4 | + |
| 5 | +use App\Http\Requests\AlertOperationRequest; |
| 6 | +use App\Models\AlertOperation; |
| 7 | +use App\Services\Alerting\AlertOperationSyncer; |
| 8 | +use Binaryk\LaravelRestify\Fields\HasMany; |
| 9 | +use Binaryk\LaravelRestify\Filters\MatchFilter; |
| 10 | +use Binaryk\LaravelRestify\Filters\SearchableFilter; |
| 11 | +use Binaryk\LaravelRestify\Filters\SortableFilter; |
| 12 | +use Binaryk\LaravelRestify\Http\Requests\RestifyRequest; |
| 13 | +use Illuminate\Support\Facades\Validator; |
| 14 | +use Illuminate\Validation\ValidationException; |
| 15 | + |
| 16 | +class AlertOperationRepository extends Repository |
| 17 | +{ |
| 18 | + public static string $model = AlertOperation::class; |
| 19 | + |
| 20 | + public static string $title = 'name'; |
| 21 | + |
| 22 | + public static function related(): array |
| 23 | + { |
| 24 | + return [ |
| 25 | + 'segments' => HasMany::make('segments', AlertOperationSegmentRepository::class), |
| 26 | + ]; |
| 27 | + } |
| 28 | + |
| 29 | + public static function searchables(): array |
| 30 | + { |
| 31 | + return [ |
| 32 | + 'name' => SearchableFilter::make()->setColumn('name'), |
| 33 | + ]; |
| 34 | + } |
| 35 | + |
| 36 | + public static function matches(): array |
| 37 | + { |
| 38 | + return [ |
| 39 | + 'name' => MatchFilter::make()->setType('text')->setColumn('name'), |
| 40 | + 'notificationsSuppressed' => MatchFilter::make()->setType('bool')->setColumn('notifications_suppressed'), |
| 41 | + 'defaultOperationStepDurationSeconds' => MatchFilter::make()->setType('integer')->setColumn('default_operation_step_duration_seconds'), |
| 42 | + ]; |
| 43 | + } |
| 44 | + |
| 45 | + public static function sorts(): array |
| 46 | + { |
| 47 | + return [ |
| 48 | + 'name' => SortableFilter::make()->setColumn('name'), |
| 49 | + 'notificationsSuppressed' => SortableFilter::make()->setColumn('notifications_suppressed'), |
| 50 | + 'defaultOperationStepDurationSeconds' => SortableFilter::make()->setColumn('default_operation_step_duration_seconds'), |
| 51 | + ]; |
| 52 | + } |
| 53 | + |
| 54 | + public function fields(RestifyRequest $request): array |
| 55 | + { |
| 56 | + return [ |
| 57 | + field('name')->rules('required', 'string', 'max:255'), |
| 58 | + field('defaultOperationStepDurationSeconds', fn ($value, $model) => $model->default_operation_step_duration_seconds) |
| 59 | + ->fillCallback(function ($request, $model, $attribute) { |
| 60 | + if ($request->exists($attribute)) { |
| 61 | + $model->default_operation_step_duration_seconds = $request->input($attribute); |
| 62 | + } |
| 63 | + }) |
| 64 | + ->rules('nullable', 'integer', 'min:0'), |
| 65 | + field('notificationsSuppressed', fn ($value, $model) => (bool) $model->notifications_suppressed) |
| 66 | + ->fillCallback(function ($request, $model, $attribute) { |
| 67 | + if ($request->exists($attribute)) { |
| 68 | + $model->notifications_suppressed = $request->boolean($attribute); |
| 69 | + } |
| 70 | + }) |
| 71 | + ->rules('boolean'), |
| 72 | + ]; |
| 73 | + } |
| 74 | + |
| 75 | + public function store(RestifyRequest $request) |
| 76 | + { |
| 77 | + $segments = $this->validatedSegments($request); |
| 78 | + |
| 79 | + $response = parent::store($request); |
| 80 | + |
| 81 | + try { |
| 82 | + /** @var AlertOperation $operation */ |
| 83 | + $operation = $this->resource; |
| 84 | + AlertOperationSyncer::sync($operation, $segments); |
| 85 | + } catch (\InvalidArgumentException $e) { |
| 86 | + throw ValidationException::withMessages(['segments' => $e->getMessage()]); |
| 87 | + } |
| 88 | + |
| 89 | + return $response; |
| 90 | + } |
| 91 | + |
| 92 | + public function update(RestifyRequest $request, $repositoryId) |
| 93 | + { |
| 94 | + $segments = $this->validatedSegments($request); |
| 95 | + |
| 96 | + $response = parent::update($request, $repositoryId); |
| 97 | + |
| 98 | + try { |
| 99 | + /** @var AlertOperation $operation */ |
| 100 | + $operation = $this->resource; |
| 101 | + AlertOperationSyncer::sync($operation, $segments); |
| 102 | + } catch (\InvalidArgumentException $e) { |
| 103 | + throw ValidationException::withMessages(['segments' => $e->getMessage()]); |
| 104 | + } |
| 105 | + |
| 106 | + return $response; |
| 107 | + } |
| 108 | + |
| 109 | + public function destroy(RestifyRequest $request, $repositoryId) |
| 110 | + { |
| 111 | + /** @var AlertOperation $operation */ |
| 112 | + $operation = $this->resource; |
| 113 | + if ($operation->alertRules()->exists()) { |
| 114 | + throw ValidationException::withMessages([ |
| 115 | + 'id' => 'This operation is assigned to one or more alert rules and cannot be deleted.', |
| 116 | + ]); |
| 117 | + } |
| 118 | + |
| 119 | + return parent::destroy($request, $repositoryId); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Run the segment portion of {@see AlertOperationRequest} rules and return the rows. |
| 124 | + * |
| 125 | + * @return array<int, array<string, mixed>> |
| 126 | + */ |
| 127 | + private function validatedSegments(RestifyRequest $request): array |
| 128 | + { |
| 129 | + $rules = (new AlertOperationRequest())->rules(); |
| 130 | + $segmentRules = array_filter( |
| 131 | + $rules, |
| 132 | + fn (string $key) => $key === 'segments' || str_starts_with($key, 'segments.'), |
| 133 | + ARRAY_FILTER_USE_KEY, |
| 134 | + ); |
| 135 | + |
| 136 | + $payload = ['segments' => $request->input('segments', [])]; |
| 137 | + if (is_array($payload['segments'])) { |
| 138 | + foreach ($payload['segments'] as $i => $seg) { |
| 139 | + if (is_array($seg) && ($seg['escalation_step_to'] ?? null) === '') { |
| 140 | + $payload['segments'][$i]['escalation_step_to'] = null; |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + Validator::make($payload, $segmentRules)->validate(); |
| 146 | + |
| 147 | + return $payload['segments']; |
| 148 | + } |
| 149 | +} |
0 commit comments