/ app / Http / Livewire / Documents / Document / Upload.php
Upload.php
  1  <?php
  2  
  3  namespace App\Http\Livewire\Documents\Document;
  4  
  5  use App\Enums\InboxFileTypeEnum;
  6  use App\Helpers\InboxFileSaver;
  7  use App\Http\Services\AutoRequirementService;
  8  use App\Http\Services\DocumentService;
  9  use App\Http\Services\InboxFileService;
 10  use App\Http\Services\ValidationService;
 11  use App\Http\Services\ValidationTypeService;
 12  use App\Models\Document;
 13  use App\Models\DocumentType;
 14  use App\Models\Dossier;
 15  use App\Models\Folder;
 16  use App\Models\InboxFile;
 17  use App\Models\ValidationType;
 18  use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
 19  use Livewire\Component;
 20  use Livewire\WithFileUploads;
 21  use WireUi\Traits\Actions;
 22  
 23  class Upload extends Component
 24  {
 25      use AuthorizesRequests, WithFileUploads, Actions;
 26  
 27      public int $entityId;
 28      public string $description = "";
 29      public ?int $typeId = 0;
 30      public $file;
 31  
 32      public bool $btnIsDisabled = false;
 33  
 34      public bool $needsValidation = false;
 35      public ?int $inboxFileTypeId = null;
 36      public ?int $validationTypeId = null;
 37  
 38      protected $rules = [
 39          'file' => 'required|file',
 40          'description' => 'nullable|string|max:255',
 41          'typeId' => 'required',
 42          'inboxFileTypeId' => 'required_if:needsValidation,true',
 43          'validationTypeId' => 'nullable', //'required_if:needsValidation,true',
 44      ];
 45  
 46      public function mount()
 47      {
 48          $this->authorize('update', $this->folder);
 49      }
 50  
 51      public function updated($property)
 52      {
 53          $this->validateOnly($property);
 54  
 55          if ($property === 'needsValidation' and !$this->needsValidation) {
 56              $this->reset('inboxFileTypeId', 'validationTypeId');
 57          }
 58      }
 59  
 60      public function getTypesProperty()
 61      {
 62          return DocumentType::select('id', 'code as name')->get();
 63      }
 64  
 65      public function getInboxFileTypesProperty()
 66      {
 67          return InboxFileTypeEnum::valueLangArray();
 68      }
 69  
 70      public function getValidationTypesProperty()
 71      {
 72          return ValidationTypeService::getValidationTypesSelect($this->folder->dossier);
 73      }
 74  
 75      public function getFolderProperty(): Folder
 76      {
 77          return Folder::findOrFail($this->entityId);
 78      }
 79  
 80      public function alreadyExists()
 81      {
 82          $name = $this->file->getClientOriginalName();
 83          return Document::where('folder_id', $this->folder->id)
 84              ->whereHas('head', function ($query) use ($name) {
 85                  $query->whereName($name);
 86              })
 87              ->exists();
 88      }
 89  
 90      public function upload()
 91      {
 92          $this->authorize('update', $this->folder);
 93          $this->validate();
 94  
 95          try {
 96  
 97              if ($this->alreadyExists()) {
 98                  $this->notification()->error(
 99                      __('documents.notifications.create.conflict.title'),
100                      __('documents.notifications.create.conflict.message')
101                  );
102                  return;
103              }
104              $documentId = DocumentService::uploadToFolder($this->file, $this->folder, $this->description, $this->typeId);
105  
106              if ($this->needsValidation) {
107                  try {
108                      $this->createInboxFile($documentId);
109                  } catch (\Exception $e) {
110                      $this->notification()->error(__('documents.notifications.create.error.title'), $e->getMessage());
111                      return;
112                  }
113              }
114  
115              $this->emit('refresh');
116              $this->notification()->success(
117                  __('documents.notifications.create.success.title'),
118                  __('documents.notifications.create.success.message')
119              );
120              $this->emitUp('closePanel');
121              $this->reset('typeId', 'description', 'file', 'needsValidation', 'inboxFileTypeId', 'validationTypeId');
122              $this->dispatchBrowserEvent('reset-file');
123          } catch (\Exception $e) {
124              $this->notification()->error(
125                  __('documents.notifications.create.error.title'),
126                  __('documents.notifications.create.error.message')
127              );
128              log_exception($e);
129          }
130      }
131  
132      private function findInboxFile(Dossier $dossier): InboxFile | Null
133      {
134          return $dossier->inboxFiles()->where('type', $this->inboxFileTypeId)->latest()->first();
135      }
136  
137      public function createInboxFile(int $documentId)
138      {
139          $dossier = $this->folder->dossier;
140          $document = Document::findOrFail($documentId);
141  
142          $inboxFileSaver = new InboxFileSaver(
143              $document->head->path,
144              $document->head->name,
145              auth()->user()->id,
146              $dossier->id
147          );
148  
149          $inboxFile = $inboxFileSaver->save($dossier->id);
150  
151          // Create the tasks
152          match (InboxFileTypeEnum::from($inboxFile->type)) {
153              InboxFileTypeEnum::evaluation_request() => InboxFileService::evaluationRequestReceived($inboxFileSaver),
154              InboxFileTypeEnum::partial_report() => InboxFileService::partialReportReceived($inboxFileSaver, $inboxFile),
155              InboxFileTypeEnum::ETR() => InboxFileService::ETRReceived($inboxFileSaver, $inboxFile),
156              default => null,
157          };
158      }
159  
160      public function render()
161      {
162          return view('livewire.documents.document.upload');
163      }
164  }