ActionRepository.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 dynamic(Project $project, array $filter = [])
  48. {
  49. $actions = Action::query()->filter($filter)
  50. ->where("project_id", $project->id)
  51. ->with(['createdBy'])
  52. ->selectRaw("*, date_format(created_at, '%Y-%m-%d') as created_date")
  53. ->orderBy("created_at")
  54. ->get();
  55. $objectNames = self::objectNamesGroupByType($actions);
  56. $items = $actions->groupBy("created_date")->toArray();
  57. krsort($items);
  58. $data = [];
  59. foreach ($items as $day => $actionItems) {
  60. $dayItems = [
  61. 'date' => $day,
  62. 'items' => []
  63. ];
  64. foreach ($actionItems as $actionItem) {
  65. $labelKey = sprintf("action-labels.label.%s", $actionItem['action']);
  66. $objectLabelKey = sprintf("action-labels.object_type.%s", $actionItem['object_type']);
  67. $dayItems['items'][] = [
  68. 'time' => Carbon::parse($actionItem['created_at'])->toTimeString(),
  69. 'created_by' => [
  70. 'id' => $actionItem['created_by']['id'],
  71. 'name' => $actionItem['created_by']['name'],
  72. 'username' => $actionItem['created_by']['username'],
  73. ],
  74. 'action_label' => app('translator')->has($labelKey) ? __($labelKey) : $actionItem['action'],
  75. 'object_label' => app('translator')->has($objectLabelKey) ? __($objectLabelKey) : $actionItem['object_type'],
  76. 'object_id' => $actionItem['object_id'],
  77. 'object_type' => $actionItem['object_type'],
  78. 'object_name' => data_get($objectNames, sprintf("%s.%d", $actionItem['object_type'], $actionItem['object_id'])),
  79. ];
  80. }
  81. $data[] = $dayItems;
  82. }
  83. return $data;
  84. }
  85. /**
  86. * @param Collection $actions
  87. * @return array
  88. */
  89. protected static function objectNamesGroupByType(Collection $actions): array
  90. {
  91. $groupActions = $actions->groupBy("object_type");
  92. $objectNames = [];
  93. foreach ($groupActions as $group => $items) {
  94. $objectType = ActionObjectType::tryFrom($group);
  95. $actionObjectBuilder= $objectType?->modelBuilder();
  96. if (! $actionObjectBuilder) {
  97. continue;
  98. }
  99. $objectNames[$group] = $actionObjectBuilder
  100. ->whereIn("id", $items->pluck("object_id")->unique()->toArray())
  101. ->pluck($objectType->nameField(), "id");
  102. }
  103. return $objectNames;
  104. }
  105. }