|
@@ -6,6 +6,8 @@ use App\Models\Action;
|
|
|
use App\Models\Enums\ActionObjectType;
|
|
|
use App\Models\Enums\ObjectAction;
|
|
|
use App\Models\Project;
|
|
|
+use Carbon\Carbon;
|
|
|
+use Illuminate\Support\Collection;
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
|
|
class ActionRepository
|
|
@@ -46,4 +48,75 @@ class ActionRepository
|
|
|
$extraFields
|
|
|
);
|
|
|
}
|
|
|
+
|
|
|
+ public static function dynamic(Project $project, array $filter = [])
|
|
|
+ {
|
|
|
+ $actions = Action::query()->filter($filter)
|
|
|
+ ->where("project_id", $project->id)
|
|
|
+ ->with(['createdBy'])
|
|
|
+ ->selectRaw("*, date_format(created_at, '%Y-%m-%d') as created_date")
|
|
|
+ ->orderBy("created_at")
|
|
|
+ ->get();
|
|
|
+
|
|
|
+ $objectNames = self::objectNamesGroupByType($actions);
|
|
|
+
|
|
|
+ $items = $actions->groupBy("created_date")->toArray();
|
|
|
+ krsort($items);
|
|
|
+
|
|
|
+ $data = [];
|
|
|
+ foreach ($items as $day => $actionItems) {
|
|
|
+ $dayItems = [
|
|
|
+ 'date' => $day,
|
|
|
+ 'items' => []
|
|
|
+ ];
|
|
|
+
|
|
|
+ foreach ($actionItems as $actionItem) {
|
|
|
+ $labelKey = sprintf("action-labels.label.%s", $actionItem['action']);
|
|
|
+ $objectLabelKey = sprintf("action-labels.object_type.%s", $actionItem['object_type']);
|
|
|
+
|
|
|
+ $dayItems['items'][] = [
|
|
|
+ 'time' => Carbon::parse($actionItem['created_at'])->toTimeString(),
|
|
|
+ 'created_by' => [
|
|
|
+ 'id' => $actionItem['created_by']['id'],
|
|
|
+ 'name' => $actionItem['created_by']['name'],
|
|
|
+ 'username' => $actionItem['created_by']['username'],
|
|
|
+ ],
|
|
|
+ 'action_label' => app('translator')->has($labelKey) ? __($labelKey) : $actionItem['action'],
|
|
|
+ 'object_label' => app('translator')->has($objectLabelKey) ? __($objectLabelKey) : $actionItem['object_type'],
|
|
|
+ 'object_id' => $actionItem['object_id'],
|
|
|
+ 'object_type' => $actionItem['object_type'],
|
|
|
+ 'object_name' => data_get($objectNames, sprintf("%s.%d", $actionItem['object_type'], $actionItem['object_id'])),
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+ $data[] = $dayItems;
|
|
|
+ }
|
|
|
+
|
|
|
+ return $data;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param Collection $actions
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ protected static function objectNamesGroupByType(Collection $actions): array
|
|
|
+ {
|
|
|
+ $groupActions = $actions->groupBy("object_type");
|
|
|
+
|
|
|
+ $objectNames = [];
|
|
|
+ foreach ($groupActions as $group => $items) {
|
|
|
+ $objectType = ActionObjectType::tryFrom($group);
|
|
|
+
|
|
|
+ $actionObjectBuilder= $objectType?->modelBuilder();
|
|
|
+ if (! $actionObjectBuilder) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ $objectNames[$group] = $actionObjectBuilder
|
|
|
+ ->whereIn("id", $items->pluck("object_id")->unique()->toArray())
|
|
|
+ ->pluck($objectType->nameField(), "id");
|
|
|
+ }
|
|
|
+
|
|
|
+ return $objectNames;
|
|
|
+ }
|
|
|
}
|