ActionRepository.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. namespace App\Repositories;
  3. use App\Models\Action;
  4. use App\Models\Enums\ActionObjectType;
  5. use App\Models\Enums\ObjectAction;
  6. use App\Models\History;
  7. use App\Models\Project;
  8. use App\Services\History\ModelChangeDetector;
  9. use Carbon\Carbon;
  10. use Illuminate\Support\Collection;
  11. use Illuminate\Support\Facades\Auth;
  12. class ActionRepository
  13. {
  14. public static function create(
  15. int $objectId,
  16. ActionObjectType $objectType,
  17. ObjectAction $action,
  18. int|null $projectId = null,
  19. string $comment = null,
  20. array $extraFields = [],
  21. array $objectChanges = [],
  22. ): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Builder
  23. {
  24. $action = Action::query()->create([
  25. "object_id" => $objectId,
  26. "object_type" => $objectType->value,
  27. "action" => $action,
  28. "project_id" => $projectId,
  29. "comment" => $comment,
  30. "extra_fields" => $extraFields ?: null,
  31. "created_by" => Auth::id(),
  32. ]);
  33. if ($objectChanges) {
  34. History::query()->insert(array_map(fn($change) => [...$change, 'action_id' => $action->id], $objectChanges));
  35. }
  36. return $action;
  37. }
  38. public static function createByProject(
  39. Project $project,
  40. ObjectAction $action,
  41. string $comment = null,
  42. array $extraFields = [],
  43. array $objectChanges = [],
  44. ): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Builder
  45. {
  46. return self::create(
  47. $project->id,
  48. ActionObjectType::PROJECT,
  49. $action,
  50. $project->id,
  51. $comment,
  52. $extraFields,
  53. $objectChanges,
  54. );
  55. }
  56. public static function latestDynamic(Project $project)
  57. {
  58. $actions = Action::query()
  59. ->where("project_id", $project->id)
  60. ->with(['createdBy'])
  61. ->orderByDesc("created_at")
  62. ->limit(20)
  63. ->get();
  64. $objectNames = self::objectNamesGroupByType($actions);
  65. $items = [];
  66. foreach ($actions as $action) {
  67. $items[] = self::actionFormat($action->toArray(), $objectNames);
  68. }
  69. return $items;
  70. }
  71. public static function actionFormat(array $action, array $objectNames): array
  72. {
  73. $labelKey = sprintf("action-labels.label.%s", $action['action']);
  74. $objectLabelKey = sprintf("action-labels.object_type.%s", $action['object_type']);
  75. $cratedAt = Carbon::parse($action['created_at']);
  76. return [
  77. 'time' => $cratedAt->toTimeString(),
  78. 'created_at' => $cratedAt->toDateTimeString(),
  79. 'created_by' => [
  80. 'id' => $action['created_by']['id'],
  81. 'name' => $action['created_by']['name'],
  82. 'username' => $action['created_by']['username'],
  83. ],
  84. 'action_label' => app('translator')->has($labelKey) ? __($labelKey) : $action['action'],
  85. 'object_label' => app('translator')->has($objectLabelKey) ? __($objectLabelKey) : $action['object_type'],
  86. 'object_id' => $action['object_id'],
  87. 'object_type' => $action['object_type'],
  88. 'object_name' => data_get($objectNames, sprintf("%s.%d", $action['object_type'], $action['object_id'])),
  89. ];
  90. }
  91. public static function dynamic(Project $project, array $filter = [])
  92. {
  93. $actions = Action::query()->filter($filter)
  94. ->where("project_id", $project->id)
  95. ->with(['createdBy'])
  96. ->selectRaw("*, date_format(created_at, '%Y-%m-%d') as created_date")
  97. ->orderBy("created_at")
  98. ->get();
  99. $objectNames = self::objectNamesGroupByType($actions);
  100. $items = $actions->groupBy("created_date")->toArray();
  101. krsort($items);
  102. $data = [];
  103. foreach ($items as $day => $actionItems) {
  104. $dayItems = [
  105. 'date' => $day,
  106. 'items' => []
  107. ];
  108. foreach ($actionItems as $actionItem) {
  109. $dayItems['items'][] = self::actionFormat($actionItem, $objectNames);
  110. }
  111. $data[] = $dayItems;
  112. }
  113. return $data;
  114. }
  115. /**
  116. * @param Collection $actions
  117. * @return array
  118. */
  119. protected static function objectNamesGroupByType(Collection $actions): array
  120. {
  121. $groupActions = $actions->groupBy("object_type");
  122. $objectNames = [];
  123. foreach ($groupActions as $group => $items) {
  124. $objectType = ActionObjectType::tryFrom($group);
  125. $actionObjectBuilder= $objectType?->modelBuilder();
  126. if (! $actionObjectBuilder) {
  127. continue;
  128. }
  129. $objectNames[$group] = $actionObjectBuilder
  130. ->whereIn("id", $items->pluck("object_id")->unique()->toArray())
  131. ->pluck($objectType->nameField(), "id");
  132. }
  133. return $objectNames;
  134. }
  135. }