|
@@ -3,9 +3,11 @@
|
|
|
namespace App\Http\Resources\API;
|
|
|
|
|
|
use App\Models\Enums\ApprovalObjectType;
|
|
|
+use App\Models\File;
|
|
|
use App\Models\User;
|
|
|
use Illuminate\Http\Request;
|
|
|
use Illuminate\Http\Resources\Json\ResourceCollection;
|
|
|
+use Illuminate\Support\Collection;
|
|
|
|
|
|
class ApprovalCollection extends ResourceCollection
|
|
|
{
|
|
@@ -21,14 +23,15 @@ class ApprovalCollection extends ResourceCollection
|
|
|
*/
|
|
|
public function toArray(Request $request): array
|
|
|
{
|
|
|
- $usersId=make_array_list($this->users??'');
|
|
|
- $approvalUsers=User::query()->whereIn('id',$usersId)->get();
|
|
|
$groupObjects = $this->getGroupObjects();
|
|
|
|
|
|
+ $containerFiles = $this->getContainerFiles();
|
|
|
+
|
|
|
$items = [];
|
|
|
foreach ($this->resource as $approval) {
|
|
|
$usersId=make_array_list($approval->users??'');
|
|
|
- $approvalUsers=User::query()->whereIn('id',$usersId)->get();
|
|
|
+ $approvalUsers=User::query()->whereIn('id', $usersId)->get();
|
|
|
+
|
|
|
$items[] = [
|
|
|
'id' => $approval->id,
|
|
|
'status' => $approval->status,
|
|
@@ -37,17 +40,53 @@ class ApprovalCollection extends ResourceCollection
|
|
|
'node_level' => $approval->node_level,
|
|
|
'created_by' => new UserProfileResource($approval->createdBy),
|
|
|
'display_id' => $approval->display_id,
|
|
|
- 'approval_user'=>UserProfileResource::collection($approvalUsers),
|
|
|
+ 'approval_user' => UserProfileResource::collection($approvalUsers),
|
|
|
'object' => [
|
|
|
'id' => $approval->object_id,
|
|
|
'name' => data_get($groupObjects, sprintf("%s.%s", $approval->object_type, $approval->object_id)),
|
|
|
- ]
|
|
|
+ ],
|
|
|
+ 'sub_objects' => $this->getSubContainerFiles($containerFiles, $approval->sub_object_ids),
|
|
|
];
|
|
|
}
|
|
|
|
|
|
return $items;
|
|
|
}
|
|
|
|
|
|
+ protected function getSubContainerFiles(Collection $files, ?array $fileIds = null)
|
|
|
+ {
|
|
|
+ if (! $fileIds) {
|
|
|
+ return [];
|
|
|
+ }
|
|
|
+
|
|
|
+ $items = [];
|
|
|
+ foreach ($fileIds as $fileId) {
|
|
|
+ if ($files->has($fileId)) {
|
|
|
+ $items[] = $files->get($fileId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return FileSimpleResource::collection($items);
|
|
|
+ }
|
|
|
+
|
|
|
+ protected function getContainerFiles(): \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection|array
|
|
|
+ {
|
|
|
+ $result = $this->resource->where("object_type", ApprovalObjectType::CONTAINER_FILE->value)->pluck("sub_object_ids");
|
|
|
+
|
|
|
+ $files = collect([]);
|
|
|
+ $fileIds = [];
|
|
|
+ foreach ($result as $item) {
|
|
|
+ if (is_array($item)) {
|
|
|
+ $fileIds = [...$fileIds, ...$item];
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (! $fileIds) {
|
|
|
+ return $files;
|
|
|
+ }
|
|
|
+
|
|
|
+ return File::query()->whereIn("id", $fileIds)->get()->keyBy("id");
|
|
|
+ }
|
|
|
+
|
|
|
protected function getGroupObjects(): array
|
|
|
{
|
|
|
$groupObjects = [];
|