ActionController.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace App\Http\Controllers\API;
  3. use App\Http\Controllers\Controller;
  4. use App\Http\Requests\API\Action\CommentRequest;
  5. use App\Models\Action;
  6. use App\Models\Enums\ActionObjectType;
  7. use App\Models\Enums\FileObjectType;
  8. use App\Models\Enums\ObjectAction;
  9. use App\Models\File;
  10. use App\Repositories\ActionRepository;
  11. use App\Repositories\ContainerFileRepository;
  12. use App\Services\File\FileAssociationService;
  13. use App\Services\File\ImageUrlService;
  14. use DOMDocument;
  15. use Illuminate\Http\Request;
  16. use Illuminate\Support\Facades\Auth;
  17. class ActionController extends Controller
  18. {
  19. public function history(string $objectType, string $objectId)
  20. {
  21. $actionObjectType = ActionObjectType::from($objectType);
  22. $model = $actionObjectType->modelBuilder()->findOrFail($objectId);
  23. $objectIds = [$objectId];
  24. if (ActionObjectType::CONTAINER_FILE->value == $objectType) {
  25. ContainerFileRepository::getSourceFileFiles($model)->each(function ($file) use (&$objectIds) {
  26. $objectIds[] = $file->id;
  27. $objectIds[] = $file->source_file_id;
  28. });
  29. }
  30. return $this->success([
  31. 'data' => ActionRepository::actionWithHistory($actionObjectType, array_unique($objectIds)),
  32. ]);
  33. }
  34. /**
  35. * action comment
  36. *
  37. * @param CommentRequest $request
  38. * @param string $objectType
  39. * @param string $objectId
  40. */
  41. public function comment(FileAssociationService $service, CommentRequest $request, string $objectType, string $objectId,ImageUrlService $imgService)
  42. {
  43. $actionObjectType = ActionObjectType::from($objectType);
  44. $comment=$request->get('comment');
  45. $newComment=$imgService->interceptImageUrl($comment);
  46. $object = $actionObjectType?->modelBuilderAllowed($objectId)
  47. ->where("company_id", Auth::user()->company_id)
  48. ->findOrFail($objectId);
  49. $projectId = match ($actionObjectType) {
  50. ActionObjectType::PROJECT => $objectId,
  51. ActionObjectType::TASK => $object->project_id,
  52. default => null
  53. };
  54. $service->check(
  55. $request->get("file_ids", []),
  56. FileObjectType::ACTION,
  57. );
  58. $action = ActionRepository::create(
  59. $objectId,
  60. $actionObjectType,
  61. ObjectAction::COMMENTED,
  62. projectId: $projectId,
  63. comment: $newComment,
  64. );
  65. $service->association($action->id);
  66. return $this->created();
  67. }
  68. /**
  69. * update comment
  70. *
  71. * @param CommentRequest $request
  72. * @param string $id
  73. * @return \Illuminate\Http\Response
  74. */
  75. public function updateComment(CommentRequest $request, string $id,ImageUrlService $imgService)
  76. {
  77. $action = Action::query()->findOrFail($id);
  78. $actionObjectType = ActionObjectType::from($action->object_type);
  79. $actionObjectType?->modelBuilderAllowed($action->object_id)
  80. ->where("company_id", Auth::user()->company_id)
  81. ->findOrFail($action->object_id);
  82. $comment=$request->get('comment');
  83. $newComment=$imgService->interceptImageUrl($comment);
  84. $action->comment = $newComment;
  85. $action->save();
  86. return $this->noContent();
  87. }
  88. public function lastDynamic(){
  89. return $this->success([
  90. 'data' => ActionRepository::allLatestDynamic()
  91. ]);
  92. }
  93. }