123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- <?php
- namespace App\Repositories;
- 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
- {
- public static function create(
- int $objectId,
- ActionObjectType $objectType,
- ObjectAction $action,
- int|null $projectId = null,
- string $comment = null,
- array $extraFields = []
- ): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Builder
- {
- return Action::query()->create([
- "object_id" => $objectId,
- "object_type" => $objectType->value,
- "action" => $action,
- "project_id" => $projectId,
- "comment" => $comment,
- "extra_fields" => $extraFields ?: null,
- "created_by" => Auth::id(),
- ]);
- }
- public static function createByProject(
- Project $project,
- ObjectAction $action,
- string $comment = null,
- array $extraFields = []
- ): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Builder
- {
- return self::create(
- $project->id,
- ActionObjectType::PROJECT,
- $action,
- $project->id,
- $comment,
- $extraFields
- );
- }
- public static function latestDynamic(Project $project)
- {
- $actions = Action::query()
- ->where("project_id", $project->id)
- ->with(['createdBy'])
- ->orderByDesc("created_at")
- ->limit(20)
- ->get();
- $objectNames = self::objectNamesGroupByType($actions);
- $items = [];
- foreach ($actions as $action) {
- $items[] = self::actionFormat($action->toArray(), $objectNames);
- }
- return $items;
- }
- public static function actionFormat(array $action, array $objectNames): array
- {
- $labelKey = sprintf("action-labels.label.%s", $action['action']);
- $objectLabelKey = sprintf("action-labels.object_type.%s", $action['object_type']);
- $cratedAt = Carbon::parse($action['created_at']);
- return [
- 'time' => $cratedAt->toTimeString(),
- 'created_at' => $cratedAt->toDateTimeString(),
- 'created_by' => [
- 'id' => $action['created_by']['id'],
- 'name' => $action['created_by']['name'],
- 'username' => $action['created_by']['username'],
- ],
- 'action_label' => app('translator')->has($labelKey) ? __($labelKey) : $action['action'],
- 'object_label' => app('translator')->has($objectLabelKey) ? __($objectLabelKey) : $action['object_type'],
- 'object_id' => $action['object_id'],
- 'object_type' => $action['object_type'],
- 'object_name' => data_get($objectNames, sprintf("%s.%d", $action['object_type'], $action['object_id'])),
- ];
- }
- 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) {
- $dayItems['items'][] = self::actionFormat($actionItem, $objectNames);
- }
- $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;
- }
- }
|