ActionRepository.php 4.6 KB

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