From ab94607e81c12b3c1555c92ae2f0928731a2ae49 Mon Sep 17 00:00:00 2001 From: Dell Date: Thu, 4 Aug 2022 08:39:11 +0700 Subject: [PATCH] Jobs --- app/Jobs/ProcessImport.php | 143 ++++++++++++++++++ app/Models/ImportLog.php | 26 ++-- ..._07_04_074656_create_import_logs_table.php | 2 +- .../2022_08_03_114155_create_jobs_table.php | 36 +++++ .../src/pages/Corporates/Division/List.tsx | 6 +- 5 files changed, 198 insertions(+), 15 deletions(-) create mode 100644 app/Jobs/ProcessImport.php create mode 100644 database/migrations/2022_08_03_114155_create_jobs_table.php diff --git a/app/Jobs/ProcessImport.php b/app/Jobs/ProcessImport.php new file mode 100644 index 00000000..ed51482f --- /dev/null +++ b/app/Jobs/ProcessImport.php @@ -0,0 +1,143 @@ +import_log = $importLog; + } + + /** + * Execute the job. + * + * @return void + */ + public function handle() + { + $file = $this->import_log->files()->where('type', 'import')->first(); + + $this->import_log->fill(['status' => 'progress'])->save(); + $corporate = $this->import_log->importable; + + $import = new ImportService(); + $import->read(Storage::path($file->full_path)); + $import->write(Storage::disk('public')->path('result/'.$this->import_log->file_path), 'xsls'); + + foreach ($import->sheetsIterator() as $sheetIndex => $sheet) { + $doc_headers_indexes = []; + foreach ($sheet->getRowIterator() as $index => $row) { + if ($index == 1) { // First Row Must be Header + foreach ($row->getCells() as $index => $cell) { + $title = $cell->getValue(); + $title = preg_replace( "/\r|\n/", " ", $title ); + $title = preg_replace('/\xc2\xa0/', " ", $title ); + $title = rtrim($title); + $title = ltrim($title); + $doc_headers_indexes[$index] = $title; + } + + // Write Header to File + $result_headers = array_merge($doc_headers_indexes, ['Ingest Code', 'Ingest Note']); + $import->addArrayToRow($result_headers); + + // TODO Validate if First Row not Header + } else { // Next Row Should be Data + $row_data = []; + $row_map = [ + 0 => 'code', + 1 => 'description', + 2 => 'ip_exclusion', + 3 => 'op_exclusion', + 4 => 'de_exclusion', + 5 => 'ma_exclusion', + 6 => 'sp_exclusion', + 7 => 'pre_exist_exclusion', + 8 => 'op_de_exclusion', + 9 => 'keterangan', + 10 => 'maternity_waiting' + ]; + + foreach ($row->getCells() as $header_index => $cell) { + if (isset($row_map[$header_index])) { + $value = $cell->getValue(); + $value = preg_replace( "/\r|\n/", " ", $value ); + $value = preg_replace('/\xc2\xa0/', " ", $value ); + $value = rtrim($value); + $value = ltrim($value); + $row_data[$row_map[$header_index]] = $cell->getValue(); + } + } + + try { // Process the Row Data + if ( + // empty($row_data['code']) && + // empty($row_data['description']) && + empty($row_data['ip_exclusion']) && + empty($row_data['op_exclusion']) && + empty($row_data['de_exclusion']) && + empty($row_data['ma_exclusion']) && + empty($row_data['sp_exclusion']) && + empty($row_data['pre_exis_exclusion']) && + empty($row_data['op_de_exclusion']) && + empty($row_data['maternity_waiting'])) { + continue; + } + + // Save the Row + $exclusionService = new ExclusionService(); + $exclusionService->handleDiagnosisExclusionRow($corporate, $row_data); + + // Write Success Result to File + $import->addArrayToRow(array_merge($row_data, [ + 'Ingest Code' => 200, + 'Ingest Note' => 'Success', + ]), $sheet->getName()); + + } catch (ImportRowException $e) { + // Write Data Validation Error to File + $import->addArrayToRow(array_merge($row_data, [ + 'Ingest Code' => $e->getCode(), + 'Ingest Note' => $e->getMessage(), + ]), $sheet->getName()); + } catch (\Exception $e) { + throw new \Exception($e); + // Write Server Error to File + $import->addArrayToRow(array_merge($row_data, [ + 'Ingest Code' => 500, + 'Ingest Note' => env('APP_DEBUG') ? $e->getMessage() : 'Server Error', + ]), $sheet->getName()); + } + } + } + + break; // Only Read First Row + } + $import->reader->close(); + Storage::delete('temp/'.$file_name); + $import->writer->close(); + + } +} diff --git a/app/Models/ImportLog.php b/app/Models/ImportLog.php index b1a02df0..ffb3d13c 100644 --- a/app/Models/ImportLog.php +++ b/app/Models/ImportLog.php @@ -12,18 +12,9 @@ class ImportLog extends Model { use HasFactory, SoftDeletes, Blameable; - protected static function boot() - { - parent::boot(); + // protected $keyType = 'string'; - static::creating(function ($model) { - try { - $model->uuid = (string) Str::orderedUuid(); // generate uuid - } catch (\Exception $e) { - abort(500, $e->getMessage()); - } - }); - } + // public $incrementing = false; protected $fillable = [ 'importable_type', @@ -34,6 +25,19 @@ class ImportLog extends Model 'progress', ]; + // protected static function boot() + // { + // parent::boot(); + + // static::creating(function ($model) { + // try { + // $model->id = (string) Str::orderedUuid(); // generate uuid + // } catch (\Exception $e) { + // abort(500, $e->getMessage()); + // } + // }); + // } + public function files() { return $this->morphMany(File::class, 'fileable'); diff --git a/database/migrations/2022_07_04_074656_create_import_logs_table.php b/database/migrations/2022_07_04_074656_create_import_logs_table.php index c19a176a..4d8ae1a4 100644 --- a/database/migrations/2022_07_04_074656_create_import_logs_table.php +++ b/database/migrations/2022_07_04_074656_create_import_logs_table.php @@ -14,7 +14,7 @@ return new class extends Migration public function up() { Schema::create('import_logs', function (Blueprint $table) { - $table->uuid()->primary(); + $table->id(); $table->morphs('importable'); $table->string('type')->nullable(); $table->string('file_path')->nullable(); diff --git a/database/migrations/2022_08_03_114155_create_jobs_table.php b/database/migrations/2022_08_03_114155_create_jobs_table.php new file mode 100644 index 00000000..a786a891 --- /dev/null +++ b/database/migrations/2022_08_03_114155_create_jobs_table.php @@ -0,0 +1,36 @@ +bigIncrements('id'); + $table->string('queue')->index(); + $table->longText('payload'); + $table->unsignedTinyInteger('attempts'); + $table->unsignedInteger('reserved_at')->nullable(); + $table->unsignedInteger('available_at'); + $table->unsignedInteger('created_at'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('jobs'); + } +}; diff --git a/frontend/dashboard/src/pages/Corporates/Division/List.tsx b/frontend/dashboard/src/pages/Corporates/Division/List.tsx index d2d63971..fb019971 100644 --- a/frontend/dashboard/src/pages/Corporates/Division/List.tsx +++ b/frontend/dashboard/src/pages/Corporates/Division/List.tsx @@ -6,7 +6,7 @@ import AddIcon from '@mui/icons-material/Add'; import UploadIcon from '@mui/icons-material/Upload'; import CancelIcon from '@mui/icons-material/Cancel'; // hooks -import React, { Component, useEffect, useRef, useState } from 'react'; +import React, { ChangeEvent, Component, useEffect, useRef, useState } from 'react'; import useSettings from '../../../hooks/useSettings'; import { Link, useNavigate, useParams, useSearchParams } from 'react-router-dom'; // components @@ -143,7 +143,7 @@ export default function PlanList() { total: 0 }); - const loadDataTableData = async (appliedFilter = null) => { + const loadDataTableData = async (appliedFilter : any | null = null) => { setDataTableLoading(true); const filter = appliedFilter ? appliedFilter : Object.fromEntries([...searchParams.entries()]); const response = await axios.get('/corporates/'+corporate_id+'/divisions', { params: filter }); @@ -157,7 +157,7 @@ export default function PlanList() { fontWeight: 'bold', }; - const applyFilter = async (searchFilter) => { + const applyFilter = async (searchFilter: any) => { await loadDataTableData({ "search" : searchFilter }); setSearchParams({ "search" : searchFilter }); }