diff --git a/Modules/Client/Http/Controllers/Api/ClaimReportController.php b/Modules/Client/Http/Controllers/Api/ClaimReportController.php index 40c02e82..21b8aaca 100755 --- a/Modules/Client/Http/Controllers/Api/ClaimReportController.php +++ b/Modules/Client/Http/Controllers/Api/ClaimReportController.php @@ -133,12 +133,46 @@ class ClaimReportController extends Controller ) ->get(); $results['request_files'] = $request_files; + + // Jika path kosong, kembalikan null + if (!$row->path) { + return null; + } + // $documents = DB::table('files') + // ->where('fileable_type', 'App\Models\ClaimRequest') + // ->where('fileable_id', $claimRequestId) + // ->select('original_name', \DB::raw("CONCAT('" . env('APP_URL') . "/storage/', path) as path"), 'type') + // ->orderBy('id', 'desc') + // ->get(); + $documents = DB::table('files') - ->where('fileable_type', 'App\Models\ClaimRequest') - ->where('fileable_id', $claimRequestId) - ->select('original_name', \DB::raw("CONCAT('" . env('APP_URL') . "/storage/', path) as path"), 'type') - ->orderBy('id', 'desc') - ->get(); + ->where('fileable_type', 'App\Models\ClaimRequest') + ->where('fileable_id', $claimRequestId) + ->select('id', 'original_name', 'path', 'source', 'type') + ->orderBy('id', 'desc') + ->get() + ->map(function ($row) { + // default null kalau tidak ada path + if (!$row->path) { + $row->path = null; // atau bisa $row->url = null + return $row; + } + + if ($row->source === 's3') { + try { + $row->path = Storage::disk('s3')->temporaryUrl( + $row->path, + now()->addMinutes(60) + ); + } catch (\Exception $e) { + $row->path = Storage::disk('s3')->url($row->path); + } + } else { + $row->path = Storage::disk('public')->url($row->path); + } + + return $row; + }); $results['documents'] = $documents; return Helper::responseJson($results); diff --git a/Modules/Client/Transformers/AlarmCenter/DataServiceMonitoring.php b/Modules/Client/Transformers/AlarmCenter/DataServiceMonitoring.php index 1028409f..b0af8cbf 100755 --- a/Modules/Client/Transformers/AlarmCenter/DataServiceMonitoring.php +++ b/Modules/Client/Transformers/AlarmCenter/DataServiceMonitoring.php @@ -130,8 +130,27 @@ class DataServiceMonitoring extends JsonResource ->get(); if ($document){ foreach($document as $d){ + // Jika path kosong, kembalikan null + if (!$row->path) { + return null; + } + + // Cek nilai 'source'. Jika 's3', gunakan disk S3. + // Selain itu (termasuk null atau 'local'), gunakan disk 'public'. + if ($row->source === 's3') { + try { + $path = Storage::disk('s3')->temporaryUrl( + $row->path, + now()->addMinutes(60) // expired 1 jam + ); + } catch (\Exception $e) { + $path = Storage::disk('s3')->url($row->path); // fallback kalau public + } + } else { + $path = Storage::disk('public')->url($row->path); + } $arr_document[]= [ - 'path' => env('APP_URL') . '/storage/lab_result/' . $d->name . '.' . $d->extension, + 'path' => $path, 'type' => $d->type, 'original_name' => $d->original_name, 'name' => $d->name, @@ -194,8 +213,28 @@ class DataServiceMonitoring extends JsonResource ->get(); if ($document){ foreach($document as $d){ + // Jika path kosong, kembalikan null + if (!$row->path) { + return null; + } + + // Cek nilai 'source'. Jika 's3', gunakan disk S3. + // Selain itu (termasuk null atau 'local'), gunakan disk 'public'. + if ($row->source === 's3') { + try { + $path = Storage::disk('s3')->temporaryUrl( + $row->path, + now()->addMinutes(60) // expired 1 jam + ); + } catch (\Exception $e) { + $path = Storage::disk('s3')->url($row->path); // fallback kalau public + } + } else { + $path = Storage::disk('public')->url($row->path); + } + $arr_document[]= [ - 'path' => env('APP_URL') . '/storage/lab_result/' . $d->name . '.' . $d->extension, + 'path' => $path, 'type' => $d->type, 'original_name' => $d->original_name, 'name' => $d->name, diff --git a/Modules/Internal/Http/Controllers/Api/ClaimController.php b/Modules/Internal/Http/Controllers/Api/ClaimController.php index 2a4bfe3f..d464b588 100755 --- a/Modules/Internal/Http/Controllers/Api/ClaimController.php +++ b/Modules/Internal/Http/Controllers/Api/ClaimController.php @@ -195,7 +195,10 @@ class ClaimController extends Controller $limit = $request->has('per_page') ? $request->input('per_page') : 50; $results = DB::table('request_logs') ->leftJoin('members', 'request_logs.member_id', '=', 'members.id') - ->join('files', 'request_logs.id', '=', 'files.fileable_id') + ->join('files', function ($join) { + $join->on('request_logs.id', '=', 'files.fileable_id') + ->whereNull('files.deleted_at'); + }) // ->leftJoin('member_plans', 'member_plans.member_id', '=', 'members.id') ->when($request->input('search'), function ($query, $search) { $query->where(function ($query) use ($search) { @@ -239,46 +242,82 @@ class ClaimController extends Controller (SELECT organizations.name FROM organizations WHERE organizations.id = request_logs.organization_id LIMIT 1) AS provider '), 'request_logs.status_final_log as status', - DB::raw("CONCAT('" . env('APP_URL') . "/storage/', path) as path") + 'files.path', + 'files.source' ) ->paginate($limit); + $results->getCollection()->transform(function ($row) { + if (!$row->path) { + $row->path = null; + return $row; + } + if ($row->source === 's3') { + try { + $row->path = Storage::disk('s3')->temporaryUrl( + $row->path, + now()->addMinutes(60) + ); + } catch (\Exception $e) { + $row->path = Storage::disk('s3')->url($row->path); + } + } else { + $row->path = Storage::disk('public')->url($row->path); + } + + return $row; + }); return response()->json(Helper::paginateResources($results)); } public function downloadZip(Request $request) { - $selectedRows = $request->selectedRows; // asumsi $selectedRows berisi array ID file yang dipilih - $files = []; + $selectedRows = $request->selectedRows; // array ID file + $files = []; - // Ambil path file dari database atau sumber lain sesuai dengan $selectedRows - $data = DB::table('files') - ->whereIn('id', $selectedRows) - ->select('path') - ->get(); + // Ambil path dan source + $data = DB::table('files') + ->whereIn('id', $selectedRows) + ->select('path', 'source', 'original_name') + ->get(); - foreach ($data as $value) { - $files[] = storage_path('app/public/' . $value->path); - } + $zipFileName = 'downloaded_files_' . time() . '.zip'; + $zipPath = storage_path('app/public/' . $zipFileName); - $zipFileName = 'downloaded_files.zip'; - $zip = new ZipArchive(); + $zip = new ZipArchive(); + if ($zip->open($zipPath, ZipArchive::CREATE | ZipArchive::OVERWRITE)) { + foreach ($data as $value) { + $localPath = null; - if ($zip->open(storage_path('app/public/' . $zipFileName), ZipArchive::CREATE | ZipArchive::OVERWRITE)) { - foreach ($files as $file) { - $zip->addFile($file, basename($file)); + if ($value->source === 's3') { + // download file dari S3 ke temporary file + $tmpFile = tempnam(sys_get_temp_dir(), 's3file_'); + $stream = Storage::disk('s3')->get($value->path); + file_put_contents($tmpFile, $stream); + $localPath = $tmpFile; + } else { + // ambil file dari local storage + $localPath = storage_path('app/public/' . $value->path); + } + + if ($localPath && file_exists($localPath)) { + $zip->addFile($localPath, $value->original_name ?? basename($value->path)); + } + } + + $zip->close(); + + // return URL ke file zip + return response()->json([ + 'file_url' => env('APP_URL') . Storage::url($zipFileName) + ], 200); } - $zip->close(); - - // Mengembalikan response berupa URL file zip - return response()->json(['file_url' => env('APP_URL').Storage::url($zipFileName)], 200); - } else { return response()->json(['message' => 'Gagal membuat file ZIP.'], 500); } - } + public function downloadTemplate() { @@ -1295,9 +1334,31 @@ class ClaimController extends Controller $documents = DB::table('files') ->where('fileable_type', 'App\Models\ClaimRequest') ->where('fileable_id', $claim_id) - ->select('original_name', \DB::raw("CONCAT('" . env('APP_URL') . "/storage/', path) as path"), 'type') + ->select('id', 'original_name', 'path', 'source', 'type') ->orderBy('id', 'desc') - ->get(); + ->get() + ->map(function ($row) { + if (!$row->path) { + $row->path = null; + return $row; + } + + if ($row->source === 's3') { + try { + $row->path = Storage::disk('s3')->temporaryUrl( + $row->path, + now()->addMinutes(60) + ); + } catch (\Exception $e) { + $row->path = Storage::disk('s3')->url($row->path); + } + } else { + $row->path = Storage::disk('public')->url($row->path); + } + + return $row; + }); + $results['documents'] = $documents; $request_documents = DB::table('claim_request_files') diff --git a/Modules/Internal/Http/Controllers/Api/InvoicePaymentController.php b/Modules/Internal/Http/Controllers/Api/InvoicePaymentController.php index f614f733..0bacd097 100644 --- a/Modules/Internal/Http/Controllers/Api/InvoicePaymentController.php +++ b/Modules/Internal/Http/Controllers/Api/InvoicePaymentController.php @@ -241,11 +241,34 @@ class InvoicePaymentController extends Controller 'files.id as file_id', 'invoice_payments.amount_paid', 'invoice_payments.payment_number', - DB::raw("CONCAT('" . env('APP_URL') . "/storage/', files.path) as path"), + 'files.path', + 'files.source', 'files.original_name' ) ->orderBy('invoice_payments.payment_number', 'asc') - ->get(); + ->get() + ->map(function ($row) { + // default null kalau tidak ada path + if (!$row->path) { + $row->path = null; // atau bisa $row->url = null + return $row; + } + + if ($row->source === 's3') { + try { + $row->path = Storage::disk('s3')->temporaryUrl( + $row->path, + now()->addMinutes(60) + ); + } catch (\Exception $e) { + $row->path = Storage::disk('s3')->url($row->path); + } + } else { + $row->path = Storage::disk('public')->url($row->path); + } + + return $row; + }); $invoice['files'] = $payments->groupBy('payment_number')->map(function ($group) { return [ diff --git a/Modules/Internal/Http/Controllers/Api/RequestLogController.php b/Modules/Internal/Http/Controllers/Api/RequestLogController.php index ada297ab..ba9ab91f 100755 --- a/Modules/Internal/Http/Controllers/Api/RequestLogController.php +++ b/Modules/Internal/Http/Controllers/Api/RequestLogController.php @@ -990,9 +990,31 @@ class RequestLogController extends Controller $documents = DB::table('files') ->where('fileable_type', 'App\Models\RequestLog') ->where('fileable_id', $claimRequestId) - ->select('original_name', \DB::raw("CONCAT('" . env('APP_URL') . "/storage/', path) as path"), 'type') + ->select('original_name', 'path', 'source','type') ->orderBy('id', 'desc') - ->get(); + ->get() + ->map(function ($row) { + // default null kalau tidak ada path + if (!$row->path) { + $row->path = null; // atau bisa $row->url = null + return $row; + } + + if ($row->source === 's3') { + try { + $row->path = Storage::disk('s3')->temporaryUrl( + $row->path, + now()->addMinutes(60) + ); + } catch (\Exception $e) { + $row->path = Storage::disk('s3')->url($row->path); + } + } else { + $row->path = Storage::disk('public')->url($row->path); + } + + return $row; + }); $results['documents'] = $documents; $dialog_submits = DB::table('claim_requests') ->leftJoin('members', 'claim_requests.member_id','=', 'members.id') diff --git a/app/Models/RequestDailyMonitoring.php b/app/Models/RequestDailyMonitoring.php index 0ffa1afc..0d0ec248 100755 --- a/app/Models/RequestDailyMonitoring.php +++ b/app/Models/RequestDailyMonitoring.php @@ -160,9 +160,29 @@ class RequestDailyMonitoring extends Model ])->get(); foreach ($document as $row) { + // Jika path kosong, kembalikan null + if (!$row->path) { + return null; + } + + // Cek nilai 'source'. Jika 's3', gunakan disk S3. + // Selain itu (termasuk null atau 'local'), gunakan disk 'public'. + if ($row->source === 's3') { + try { + $path = Storage::disk('s3')->temporaryUrl( + $row->path, + now()->addMinutes(60) // expired 1 jam + ); + } catch (\Exception $e) { + $path = Storage::disk('s3')->url($row->path); // fallback kalau public + } + } else { + $path = Storage::disk('public')->url($row->path); + } $arr_document[] = [ 'name' => $row->original_name, - 'path' => env('APP_URL') . '/storage/lab_result/' . $row->name .'.'. $row->extension, + // 'path' => env('APP_URL') . '/storage/lab_result/' . $row->name .'.'. $row->extension, + 'path' => $path, 'type' => $row->type, ]; } @@ -179,9 +199,29 @@ class RequestDailyMonitoring extends Model ])->get(); foreach ($document as $row) { + // Jika path kosong, kembalikan null + if (!$row->path) { + return null; + } + + // Cek nilai 'source'. Jika 's3', gunakan disk S3. + // Selain itu (termasuk null atau 'local'), gunakan disk 'public'. + if ($row->source === 's3') { + try { + $path = Storage::disk('s3')->temporaryUrl( + $row->path, + now()->addMinutes(60) // expired 1 jam + ); + } catch (\Exception $e) { + $path = Storage::disk('s3')->url($row->path); // fallback kalau public + } + } else { + $path = Storage::disk('public')->url($row->path); + } $arr_document[] = [ 'name' => $row->original_name, - 'path' => env('APP_URL') . '/storage/lab_result/' . $row->name .'.'. $row->extension, + // 'path' => env('APP_URL') . '/storage/lab_result/' . $row->name .'.'. $row->extension, + 'path' => $path, 'type' => $row->type, ]; } @@ -198,9 +238,30 @@ class RequestDailyMonitoring extends Model ])->get(); foreach ($document as $row) { + // Jika path kosong, kembalikan null + if (!$row->path) { + return null; + } + + // Cek nilai 'source'. Jika 's3', gunakan disk S3. + // Selain itu (termasuk null atau 'local'), gunakan disk 'public'. + if ($row->source === 's3') { + try { + $path = Storage::disk('s3')->temporaryUrl( + $row->path, + now()->addMinutes(60) // expired 1 jam + ); + } catch (\Exception $e) { + $path = Storage::disk('s3')->url($row->path); // fallback kalau public + } + } else { + $path = Storage::disk('public')->url($row->path); + } + $arr_document[] = [ 'name' => $row->original_name, - 'path' => env('APP_URL') . '/storage/lab_result/' . $row->name .'.'. $row->extension, + // 'path' => env('APP_URL') . '/storage/lab_result/' . $row->name .'.'. $row->extension, + 'path' => $path, 'type' => $row->type, ]; }