OutPutService.php
1 <?php 2 3 namespace App\Http\Services; 4 5 use App\Events\OutPutCreatedEvent; 6 use App\Models\Document; 7 use App\Models\Dossier; 8 use App\Models\Entity; 9 use App\Models\OutPut; 10 use App\Models\Revision; 11 use App\Models\User; 12 use Exception; 13 use Illuminate\Support\Collection; 14 use Illuminate\Support\Facades\DB; 15 16 class OutPutService 17 { 18 /** 19 * Create resources 20 * 21 * @return \Illuminate\Http\Response 22 */ 23 public static function search(string $searchString, string $sortField, string $sortDirection, array|null $filters, int $dossierId, ?Collection $revisionIds = null) 24 { 25 $outPutsQuery = $revisionIds 26 ? OutPut::whereHas('revisions', function ($query) use ($revisionIds) { 27 $query->whereIn('outputable_id', $revisionIds); 28 }) 29 : OutPut::where('dossier_id', $dossierId); 30 31 $outPutsQuery 32 ->when($filters['withTrashed'] ?? false, function ($query) { 33 $query->withTrashed(); 34 }) 35 ->when($searchString, function ($query) use ($searchString) { 36 $query->where('description', 'like', "%{$searchString}%"); 37 }) 38 ->when($filters['created_at_since'] ?? null, function ($query, $date) { 39 $query->where('created_at', '>=', $date); 40 }) 41 ->when($filters['created_at_to'] ?? null, function ($query, $date) { 42 $query->where('created_at', '<=', $date); 43 }) 44 ->when($filters['type'] ?? null, function ($query, $types) { 45 $query->whereIn('type_id', $types); 46 }) 47 ->when($filters['communication_type'] ?? null, function ($query, $types) { 48 $query->whereIn('communication_type_id', $types); 49 }) 50 ->when($sortField && $sortDirection, function ($query) use ($sortField, $sortDirection) { 51 $query->orderBy($sortField, $sortDirection); 52 }); 53 54 return $outPutsQuery; 55 } 56 57 /** 58 * Display a listing of the resource. 59 * 60 * @return \Illuminate\Http\Response 61 */ 62 public static function find(int $id) 63 { 64 return OutPut::where('id', $id)->first(); 65 } 66 67 /** 68 * Create a resource 69 * 70 * @return \Illuminate\Http\Response 71 */ 72 public static function create(array $data, int $dossierId, ?int $inboxFileId = null, array $users = [], array $revisions = []): OutPut|Exception|null 73 { 74 $output = OutPut::create([ 75 'description' => $data['description'], 76 'type_id' => $data['type_id'], 77 'communication_type_id' => $data['communication_type_id'], 78 'communication_date' => $data['communication_date'], 79 'task_id' => $data['task_id'] ?? null, 80 'dossier_id' => $dossierId, 81 'created_by' => $data['created_by'], 82 'is_passed' => $data['is_passed'] ?? null, 83 'needs_response' => $data['needs_response'] ?? false, 84 ]); 85 86 $output->users()->sync($users); 87 $output->revisions()->sync($revisions); 88 89 if ($output->wasRecentlyCreated) { 90 OutPutCreatedEvent::dispatch($output, $inboxFileId); 91 } 92 93 return $output; 94 } 95 96 /** 97 * Update a resource 98 * 99 * @return true 100 */ 101 public static function update(array $data, int $id, ?int $inboxFileId = null, array $users = [], array $revisions = []) 102 { 103 try { 104 DB::beginTransaction(); 105 106 $output = OutPut::findOrFail($id); 107 $output->description = $data['description']; 108 $output->type_id = $data['type_id']; 109 $output->communication_type_id = $data['communication_type_id']; 110 $output->communication_date = $data['communication_date']; 111 $output->task_id = $data['task_id']; 112 $output->needs_response = $data['needs_response']; 113 $output->save(); 114 115 $output->users()->sync($users); 116 $output->revisions()->sync($revisions); 117 118 DB::commit(); 119 120 return true; 121 } catch (Exception $e) { 122 DB::rollBack(); 123 124 logger()->error($e); 125 throw new Exception(__('out-puts.notifications.update.error.message')); 126 } 127 } 128 129 public static function getOutputables(Dossier $dossier) 130 { 131 $i = 0; 132 $users = $dossier 133 ->users() 134 ->get() 135 ->map(function ($user) use (&$i, $dossier) { 136 return [ 137 'id' => $user->id, 138 'name' => $user->fullname, 139 'description' => $user->getRoleInDossier($dossier->id), 140 'type' => User::class, 141 'pos' => $i++, 142 ]; 143 })->collect(); 144 145 $entities = collect([ 146 $dossier->applicant, 147 $dossier->manufacturer, 148 $dossier->sponsor, 149 $dossier->laboratory, 150 ]) 151 ->filter() 152 ->unique('id') 153 ->map(function ($entity) use (&$i, $dossier) { 154 return [ 155 'id' => $entity->id, 156 'name' => $entity->name, 157 'description' => $dossier->getEntityRole($entity->id), 158 'type' => Entity::class, 159 'pos' => $i++, 160 ]; 161 }); 162 163 return $users->merge(collect($entities)); 164 } 165 }