JsonImporterService.php
1 <?php 2 3 namespace App\Http\Services; 4 5 use App\Helpers\JsonImporter\CAPImporter; 6 use App\Helpers\JsonImporter\EPImporter; 7 use App\Helpers\JsonImporter\ETRImporter; 8 use App\Helpers\JsonImporter\EvalInfoImporter; 9 use App\Helpers\JsonImporter\EvidenceImporter; 10 use App\Helpers\JsonImporter\JsonImporter; 11 use App\Helpers\JsonImporter\ORImporter; 12 use App\Helpers\JsonImporter\PTImporter; 13 use App\Helpers\JsonImporter\TPImporter; 14 use App\Helpers\JsonImporter\TSImporter; 15 use App\Helpers\JsonImporter\VAImporter; 16 use App\Helpers\JsonImporter\WBImporter; 17 use App\Models\Document; 18 use App\Models\DocumentType; 19 use App\Models\Dossier; 20 use App\Models\Folder; 21 use App\Models\Json; 22 use App\Models\JsonDraft; 23 use Exception; 24 use Illuminate\Support\Facades\Auth; 25 use Illuminate\Support\Facades\DB; 26 use Illuminate\Support\Facades\Storage; 27 28 class JsonImporterService 29 { 30 31 public static function search(string $searchString, string $sortField, string $sortDirection, array|null $filters) 32 { 33 $searchQuery = JsonDraft::query(); 34 35 $searchQuery->when($filters && $filters['withTrashed'], function ($query) { 36 $query->withTrashed(); 37 }); 38 39 $searchQuery 40 ->when($filters && $filters['created_at_since'], function ($query) use ($filters) { 41 $query->where('created_at', '>=', $filters['created_at_since']); 42 }) 43 ->when($filters && $filters['created_at_to'], function ($query) use ($filters) { 44 $query->where('created_at', '<=', $filters['created_at_to']); 45 }) 46 ->when($filters && $filters['updated_at_since'], function ($query) use ($filters) { 47 $query->where('updated_at', '>=', $filters['updated_at_since']); 48 }) 49 ->when($filters && $filters['updated_at_to'], function ($query) use ($filters) { 50 $query->where('updated_at', '<=', $filters['updated_at_to']); 51 }); 52 53 54 if ($sortField && $sortDirection) { 55 $searchQuery->orderBy($sortField, $sortDirection); 56 } 57 58 return $searchQuery; 59 } 60 61 62 /** 63 * @param $zipPath 64 * @return void 65 * @throws Exception 66 */ 67 public static function import($zipPath): void 68 { 69 $unzipPath = sys_get_temp_dir() . DIRECTORY_SEPARATOR . uniqid(); 70 $zip = new \ZipArchive(); 71 $zip->open($zipPath); 72 $zip->extractTo($unzipPath); 73 74 try { 75 DB::beginTransaction(); 76 foreach (new \DirectoryIterator($unzipPath) as $file) { 77 /** @var $file \SplFileInfo */ 78 if ($file->getExtension() == 'json') { 79 self::createJsonDraftFromPath($file->getRealPath()); 80 } 81 } 82 DB::commit(); 83 } catch (Exception $e) { 84 DB::rollBack(); 85 throw $e; 86 } 87 } 88 89 /** 90 * Sets the draft as completes and create/update the usable version of the json 91 * @param JsonDraft $draft 92 * @return Json 93 * @throws Exception 94 */ 95 public static function acceptDraft(JsonDraft $draft): Json 96 { 97 $draft->completed = true; 98 99 $json = self::getJsonFromDraft($draft); 100 101 $json->schema_name = $draft->schema_name; 102 $json->dossier_id = $draft->dossier_id; 103 $json->json = $draft->json; 104 $json->json_id = $draft->json_id; 105 106 $json->save(); 107 $draft->save(); 108 109 self::newDocument($json); 110 111 match ($json->schema_name) { 112 'EP' => TaskService::createTasksFromEPJson($json->json, $json->dossier_id), 113 'CAP' => NonConformityService::addCorrectiveActionFromCAPJson($json->json, $json->dossier_id), 114 default => '', 115 }; 116 117 118 return $json; 119 } 120 121 private static function getJsonFromDraft(JsonDraft $draft): Json 122 { 123 return Json::where('json_id', $draft->json_id) 124 ->where('schema_name', $draft->schema_name) 125 ->where('dossier_id', $draft->dossier_id) 126 ->firstOrNew(); 127 } 128 129 130 private static function getRootFolderId($dossierId) 131 { 132 $code = Dossier::find($dossierId)->code; 133 134 $folder = Folder::where('dossier_id', $dossierId)->where('name', $code) 135 ->first() 136 ->children() 137 ->where('name', __('documents.folders.internal')) 138 ->with('documents') 139 ->first(); 140 141 if (!$folder) 142 return null; 143 144 return $folder->id; 145 } 146 147 /** 148 * @param JsonDraft $draft 149 * @return Json|null 150 */ 151 public static function findJsonFromDraft(JsonDraft $draft): ?Json 152 { 153 return Json::where('json_id', $draft->json_id) 154 ->where('schema_name', $draft->schema_name) 155 ->where('dossier_id', $draft->dossier_id) 156 ->first(); 157 } 158 159 160 private static function newDocument(Json $json): Document 161 { 162 $serialized = json_encode($json->json); 163 164 165 $name = $json->getFilename(); 166 $data = [ 167 'name' => $name, 168 'original_name' => $name, 169 'revision_name' => $name, 170 'hash' => md5($serialized), 171 'size' => strlen($serialized), 172 'isInternal' => 1, 173 'dossier_id' => $json->dossier_id, 174 'type_id' => DocumentType::where('code', 'INT')->firstOrFail()->id, 175 'created_by' => Auth::id(), 176 'from' => 'external', 177 'path' => $json->id, 178 'folder_id' => self::getRootFolderId($json->dossier_id), 179 'active' => true, 180 'version' => '1.0', 181 'description' => '', 182 ]; 183 184 $doc = Document::whereHas('head', function ($q) use ($json) { 185 $q->where('path', $json->id); 186 })->first(); 187 188 if (!$doc) 189 $doc = Document::create($data); 190 else 191 unset($data['version']); 192 $doc->commit($data); 193 return $doc; 194 } 195 196 /** 197 * @param $path 198 * @return void 199 * @throws Exception 200 */ 201 public static function createJsonDraftFromPath($path): void 202 { 203 $jsonDecoded = (object)json_decode(file_get_contents($path)); 204 205 $typeSchema = $jsonDecoded->{'$schema'}; 206 $importer = self::matches($typeSchema, $jsonDecoded); 207 208 //Y COMPROBAR SI EXISTEN SUS RELACIONES U OTROS DATOS A TENER EN CUENTA SEGUN EL TIPO 209 //HAY QUE METER NOTIFICACIONES EN CASO DE QUE NO EXISTA EL DOSSIER 210 //Y REVISAR LAS PROPIEDADES DEL DOSSIERCODE SOBRE EL JSON QUE NOS LLEGA 211 $importer->validateData(); 212 213 214 $importer->execute(); 215 } 216 217 /** 218 * @param $type 219 * @param $jsonDecoded 220 * @return JsonImporter 221 * @throws Exception 222 */ 223 private static function matches($type, $jsonDecoded): JsonImporter 224 { 225 return match ($type) { 226 './SCHEMAS/EP.json' => new EPImporter($jsonDecoded), 227 './SCHEMAS/CAP.json' => new CAPImporter($jsonDecoded), 228 './SCHEMAS/ETR.json' => new ETRImporter($jsonDecoded), 229 './SCHEMAS/EVALINFO.json' => new EvalInfoImporter($jsonDecoded), 230 './SCHEMAS/EVIDENCES.json' => new EvidenceImporter($jsonDecoded), 231 './SCHEMAS/OR.json' => new ORImporter($jsonDecoded), 232 './SCHEMAS/PT.json' => new PTImporter($jsonDecoded), 233 './SCHEMAS/TP.json' => new TPImporter($jsonDecoded), 234 './SCHEMAS/TS.json' => new TSImporter($jsonDecoded), 235 './SCHEMAS/VA.json' => new VAImporter($jsonDecoded), 236 './SCHEMAS/WB.json' => new WBImporter($jsonDecoded), 237 }; 238 } 239 240 public static function diffJSON(Json $currentJson, string $newJson): bool 241 { 242 $isContentSame = false; 243 244 //COMPARAS UN STRING CON PRETTY EN LOS DOS JSONS 245 246 return $isContentSame; 247 } 248 }