|
@@ -5,8 +5,10 @@ namespace App\Repositories;
|
|
|
use App\Models\Action;
|
|
|
use App\Models\Enums\ActionObjectType;
|
|
|
use App\Models\Enums\ObjectAction;
|
|
|
+use App\Models\History;
|
|
|
use App\Models\Project;
|
|
|
use App\Models\Requirement;
|
|
|
+use App\Services\History\ModelChangeDetector;
|
|
|
use Carbon\Carbon;
|
|
|
use Illuminate\Support\Collection;
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
@@ -19,10 +21,11 @@ class ActionRepository
|
|
|
ObjectAction $action,
|
|
|
int|null $projectId = null,
|
|
|
string $comment = null,
|
|
|
- array $extraFields = []
|
|
|
+ array $extraFields = [],
|
|
|
+ array $objectChanges = [],
|
|
|
): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Builder
|
|
|
{
|
|
|
- return Action::query()->create([
|
|
|
+ $action = Action::query()->create([
|
|
|
"object_id" => $objectId,
|
|
|
"object_type" => $objectType->value,
|
|
|
"action" => $action,
|
|
@@ -31,14 +34,21 @@ class ActionRepository
|
|
|
"extra_fields" => $extraFields ?: null,
|
|
|
"created_by" => Auth::id(),
|
|
|
]);
|
|
|
+
|
|
|
+ if ($objectChanges) {
|
|
|
+ History::query()->insert(array_map(fn($change) => [...$change, 'action_id' => $action->id], $objectChanges));
|
|
|
+ }
|
|
|
+
|
|
|
+ return $action;
|
|
|
}
|
|
|
|
|
|
public static function createByProject(
|
|
|
- Project $project,
|
|
|
- ObjectAction $action,
|
|
|
- string $comment = null,
|
|
|
- array $extraFields = []
|
|
|
-): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Builder
|
|
|
+ Project $project,
|
|
|
+ ObjectAction $action,
|
|
|
+ string $comment = null,
|
|
|
+ array $extraFields = [],
|
|
|
+ array $objectChanges = [],
|
|
|
+ ): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Builder
|
|
|
{
|
|
|
return self::create(
|
|
|
$project->id,
|
|
@@ -46,24 +56,27 @@ class ActionRepository
|
|
|
$action,
|
|
|
$project->id,
|
|
|
$comment,
|
|
|
- $extraFields
|
|
|
+ $extraFields,
|
|
|
+ $objectChanges,
|
|
|
);
|
|
|
}
|
|
|
|
|
|
public static function createRequirement(
|
|
|
- Requirement $requiremen,
|
|
|
- ObjectAction $action,
|
|
|
- string $comment = null,
|
|
|
- array $extraFields = []
|
|
|
-): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Builder
|
|
|
+ Requirement $requirement,
|
|
|
+ ObjectAction $action,
|
|
|
+ string $comment = null,
|
|
|
+ array $extraFields = [],
|
|
|
+ array $objectChanges = [],
|
|
|
+ ): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Builder
|
|
|
{
|
|
|
return self::create(
|
|
|
- $requiremen->id,
|
|
|
+ $requirement->id,
|
|
|
ActionObjectType::REQUIREMENT,
|
|
|
$action,
|
|
|
- $requiremen->id,
|
|
|
+ $requirement->id,
|
|
|
$comment,
|
|
|
- $extraFields
|
|
|
+ $extraFields,
|
|
|
+ $objectChanges,
|
|
|
);
|
|
|
}
|
|
|
|
|
@@ -105,6 +118,7 @@ class ActionRepository
|
|
|
'object_id' => $action['object_id'],
|
|
|
'object_type' => $action['object_type'],
|
|
|
'object_name' => data_get($objectNames, sprintf("%s.%d", $action['object_type'], $action['object_id'])),
|
|
|
+ 'comment' => $action['comment'],
|
|
|
];
|
|
|
}
|
|
|
|
|
@@ -163,4 +177,59 @@ class ActionRepository
|
|
|
|
|
|
return $objectNames;
|
|
|
}
|
|
|
+
|
|
|
+ public static function actionWithHistory(ActionObjectType $actionObjectType, string $objectId): array
|
|
|
+ {
|
|
|
+ $actions = Action::query()
|
|
|
+ ->with(['histories', 'createdBy'])
|
|
|
+ ->where("object_type", $actionObjectType->value)
|
|
|
+ ->where("object_id", $objectId)
|
|
|
+ ->orderBy("created_at")
|
|
|
+ ->get();
|
|
|
+
|
|
|
+ $objectNames = self::objectNamesGroupByType($actions);
|
|
|
+
|
|
|
+ $items = [];
|
|
|
+ foreach ($actions as $action) {
|
|
|
+ $item = self::actionFormat($action->toArray(), $objectNames);
|
|
|
+ $item['histories'] = self::formatHistories($actionObjectType, $action->histories);
|
|
|
+
|
|
|
+ $items[] = $item;
|
|
|
+ }
|
|
|
+
|
|
|
+ return $items;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static function formatHistories(ActionObjectType $actionObjectType, Collection $histories): array
|
|
|
+ {
|
|
|
+ $detector = $actionObjectType->detectorClassName();
|
|
|
+
|
|
|
+ $items = [];
|
|
|
+ foreach ($histories as $history) {
|
|
|
+ $labelKey = sprintf("fields.%s", $history->field);
|
|
|
+
|
|
|
+ $item = [
|
|
|
+ 'field' => $history->field,
|
|
|
+ 'field_label' => app('translator')->has($labelKey) ? __($labelKey) : $history->field,
|
|
|
+ 'new' => self::coverFieldValue($detector, $history->field, $history->new),
|
|
|
+ 'old' => self::coverFieldValue($detector, $history->field, $history->old),
|
|
|
+ 'diff' => (string)$history->diff,
|
|
|
+ ];
|
|
|
+
|
|
|
+ $items[] = $item;
|
|
|
+ }
|
|
|
+
|
|
|
+ return $items;
|
|
|
+ }
|
|
|
+
|
|
|
+ protected static function coverFieldValue(string $detector, string $field, string $value): mixed
|
|
|
+ {
|
|
|
+ if (! $detector) {
|
|
|
+ return $value;
|
|
|
+ }
|
|
|
+
|
|
|
+ $converter = call_user_func([$detector, "converter"], $field);
|
|
|
+
|
|
|
+ return $converter ? $converter->handle($value) : $value;
|
|
|
+ }
|
|
|
}
|