diff --git a/app/Helpers/NextCloudHelper.php b/app/Helpers/NextCloudHelper.php index 5c64be8..08f100b 100644 --- a/app/Helpers/NextCloudHelper.php +++ b/app/Helpers/NextCloudHelper.php @@ -29,7 +29,7 @@ class NextCloudHelper $currentPath .= '/' . $folder; // Add the current folder to the path // Build the URL to create the folder - $url = rtrim($baseUrl, '/') . "/mktia/remote.php/dav/files/{$username}" . $currentPath; + $url = rtrim($baseUrl, '/') . "/remote.php/dav/files/{$username}" . $currentPath; try { // Attempt to create the current folder @@ -78,7 +78,7 @@ class NextCloudHelper } $client = new Client(); - $url = rtrim($baseUrl, '/') . "/mktia/remote.php/dav/files/{$username}/" . ltrim($folderPath, '/') . '/' . $fileName; + $url = rtrim($baseUrl, '/') . "/remote.php/dav/files/{$username}/" . ltrim($folderPath, '/') . '/' . $fileName; try { // Check if the file is an image @@ -130,14 +130,9 @@ class NextCloudHelper public static function getFileUrl($filePath) { - $baseUrl = env('NEXT_CLOUD_URL'); - $username = env('NEXT_CLOUD_USERNAME'); - - // Build the user-specific WebDAV file URL - $relativePath = "/mktia/remote.php/dav/files/{$username}/" . ltrim($filePath, '/'); - - // Return the full accessible URL - return rtrim($baseUrl, '/') . '/' . $relativePath; + // Instead of returning the direct Nextcloud URL, + // return a URL to your own download route. + return route('admin.nextcloud.download', ['file' => base64_encode($filePath)]); } public static function getNextcloudFile($filePath) @@ -171,7 +166,7 @@ class NextCloudHelper public static function createUser($baseUrl, $adminUsername, $adminPassword, $userId, $password, $displayName, $email) { $client = new Client(); - $url = rtrim($baseUrl, '/') . "/mktia/ocs/v1.php/cloud/users"; + $url = rtrim($baseUrl, '/') . "/ocs/v1.php/cloud/users"; try { $response = $client->request('POST', $url, [ @@ -214,7 +209,7 @@ class NextCloudHelper public static function addUserToGroup($baseUrl, $adminUsername, $adminPassword, $userId, $groupName) { $client = new Client(); - $url = rtrim($baseUrl, '/') . "/mktia/ocs/v1.php/cloud/groups/{$groupName}"; + $url = rtrim($baseUrl, '/') . "/ocs/v1.php/cloud/groups/{$groupName}"; try { $response = $client->request('POST', $url, [ diff --git a/app/Helpers/WhatsappHelper.php b/app/Helpers/WhatsappHelper.php index cc6511f..3abab2a 100644 --- a/app/Helpers/WhatsappHelper.php +++ b/app/Helpers/WhatsappHelper.php @@ -37,19 +37,24 @@ class WhatsappHelper ]; } - // Send the request to the WABLAS API - $response = $client->post(env('WABLAS_HOST') . '/api/v2/send-message', [ - 'headers' => [ - 'Authorization' => env('WABLAS_TOKEN'), - 'Content-Type' => 'application/json' - ], - 'json' => [ - 'data' => $data - ], - 'verify' => false, - ]); + try { + // Send the request to the WABLAS API + $response = $client->post(env('WABLAS_HOST') . '/api/v2/send-message', [ + 'headers' => [ + 'Authorization' => env('WABLAS_TOKEN'), + 'Content-Type' => 'application/json' + ], + 'json' => [ + 'data' => $data + ], + 'verify' => false, + ]); - return $response->getBody()->getContents(); + return $response->getBody()->getContents(); + } catch (\Exception $e) { + AuditTrailHelper::AddAuditTrail('Error', 'Failed to send WhatsApp message at Form (' . $expense_number . ')'); + return null; + } } public static function approve2Expense($phones, $expense_number, $url = null, $tanggal, $total, $name) @@ -81,19 +86,24 @@ class WhatsappHelper ]; } - // Send the request to the WABLAS API - $response = $client->post(env('WABLAS_HOST') . '/api/v2/send-message', [ - 'headers' => [ - 'Authorization' => env('WABLAS_TOKEN'), - 'Content-Type' => 'application/json' - ], - 'json' => [ - 'data' => $data - ], - 'verify' => false, - ]); + try { + // Send the request to the WABLAS API + $response = $client->post(env('WABLAS_HOST') . '/api/v2/send-message', [ + 'headers' => [ + 'Authorization' => env('WABLAS_TOKEN'), + 'Content-Type' => 'application/json' + ], + 'json' => [ + 'data' => $data + ], + 'verify' => false, + ]); - return $response->getBody()->getContents(); + return $response->getBody()->getContents(); + } catch (\Exception $e) { + AuditTrailHelper::AddAuditTrail('Error', 'Failed to send WhatsApp message at Form (' . $expense_number . ')'); + return null; + } } public static function rejectExpense($phones, $expense_number, $url = null, $tanggal, $total, $name) @@ -125,19 +135,24 @@ class WhatsappHelper ]; } - // Send the request to the WABLAS API - $response = $client->post(env('WABLAS_HOST') . '/api/v2/send-message', [ - 'headers' => [ - 'Authorization' => env('WABLAS_TOKEN'), - 'Content-Type' => 'application/json' - ], - 'json' => [ - 'data' => $data - ], - 'verify' => false, - ]); + try { + // Send the request to the WABLAS API + $response = $client->post(env('WABLAS_HOST') . '/api/v2/send-message', [ + 'headers' => [ + 'Authorization' => env('WABLAS_TOKEN'), + 'Content-Type' => 'application/json' + ], + 'json' => [ + 'data' => $data + ], + 'verify' => false, + ]); - return $response->getBody()->getContents(); + return $response->getBody()->getContents(); + } catch (\Exception $e) { + AuditTrailHelper::AddAuditTrail('Error', 'Failed to send WhatsApp message at Form (' . $expense_number . ')'); + return null; + } } public static function finalApprove($phones, $expense_number, $url = null, $tanggal, $total, $name) @@ -169,19 +184,24 @@ class WhatsappHelper ]; } - // Send the request to the WABLAS API - $response = $client->post(env('WABLAS_HOST') . '/api/v2/send-message', [ - 'headers' => [ - 'Authorization' => env('WABLAS_TOKEN'), - 'Content-Type' => 'application/json' - ], - 'json' => [ - 'data' => $data - ], - 'verify' => false, - ]); + try { + // Send the request to the WABLAS API + $response = $client->post(env('WABLAS_HOST') . '/api/v2/send-message', [ + 'headers' => [ + 'Authorization' => env('WABLAS_TOKEN'), + 'Content-Type' => 'application/json' + ], + 'json' => [ + 'data' => $data + ], + 'verify' => false, + ]); - return $response->getBody()->getContents(); + return $response->getBody()->getContents(); + } catch (\Exception $e) { + AuditTrailHelper::AddAuditTrail('Error', 'Failed to send WhatsApp message at Form (' . $expense_number . ')'); + return null; + } } public static function newExpense($phones, $expense_number, $url = null, $tanggal, $total, $name) @@ -213,18 +233,28 @@ class WhatsappHelper ]; } - // Send the request to the WABLAS API - $response = $client->post(env('WABLAS_HOST') . '/api/v2/send-message', [ - 'headers' => [ - 'Authorization' => env('WABLAS_TOKEN'), - 'Content-Type' => 'application/json' - ], - 'json' => [ - 'data' => $data - ], - 'verify' => false, - ]); + dd(env('WABLAS_HOST'), env('WABLAS_TOKEN'), $data); - return $response->getBody()->getContents(); + try { + // Send the request to the WABLAS API + $response = $client->post(env('WABLAS_HOST') . '/api/v2/send-message', [ + 'headers' => [ + 'Authorization' => env('WABLAS_TOKEN'), + 'Content-Type' => 'application/json' + ], + 'json' => [ + 'data' => $data + ], + 'verify' => false, + ]); + + dd($response->getBody()->getContents()); + + return $response->getBody()->getContents(); + } catch (\Exception $e) { + dd($e->getMessage()); + AuditTrailHelper::AddAuditTrail('Error', 'Failed to send WhatsApp message at Form (' . $expense_number . ')'); + return null; + } } -} +} \ No newline at end of file diff --git a/app/Http/Controllers/Backend/NextCloudController.php b/app/Http/Controllers/Backend/NextCloudController.php new file mode 100644 index 0000000..9bd1220 --- /dev/null +++ b/app/Http/Controllers/Backend/NextCloudController.php @@ -0,0 +1,71 @@ +json([ + 'error' => "Error: Failed to fetch file (HTTP Code: {$httpCode}). " . + ($curlError ? " cURL Error: {$curlError}" : '') + ], $httpCode); + } + + // Determine the filename for download + $filename = basename($filePath); + + // Return the file content with download headers + return response($response, 200) + ->header('Content-Description', 'File Transfer') + ->header('Content-Type', 'application/octet-stream') + ->header('Content-Disposition', "attachment; filename=\"{$filename}\"") + ->header('Expires', '0') + ->header('Cache-Control', 'must-revalidate') + ->header('Pragma', 'public') + ->header('Content-Length', strlen($response)); + } +} diff --git a/app/Http/Controllers/Forms/FormOtherController.php b/app/Http/Controllers/Forms/FormOtherController.php index 98cb874..553ca1a 100644 --- a/app/Http/Controllers/Forms/FormOtherController.php +++ b/app/Http/Controllers/Forms/FormOtherController.php @@ -210,15 +210,8 @@ class FormOtherController extends Controller $phoneNumbers = array_unique($phoneNumbers); $receiver_ids = array_unique($receiver_ids); - try { - $url = route('forms.other.view', $form->id); - WhatsappHelper::newExpense($phoneNumbers, $expense_number, $url, $tanggal, $total, $name); - } catch (\Exception $e) { - // session()->flash('error', 'Failed to send whatsapp message.'); - AuditTrailHelper::AddAuditTrail('Error', 'Failed to send WhatsApp message at Form Other (' . $expense_number . ')'); - } finally { - Log::error("Failed to send WhatsApp message"); - } + $url = route('forms.other.view', $form->id); + WhatsappHelper::newExpense($phoneNumbers, $expense_number, $url, $tanggal, $total, $name); // Send bulk email foreach ($recipients as $recipient) { diff --git a/resources/views/backend/pages/audit_trail/index.blade.php b/resources/views/backend/pages/audit_trail/index.blade.php index e477919..eb2a654 100644 --- a/resources/views/backend/pages/audit_trail/index.blade.php +++ b/resources/views/backend/pages/audit_trail/index.blade.php @@ -61,7 +61,7 @@ $color = 'success'; } elseif ($item->action == 'Update') { $color = 'warning'; - } elseif ($item->action == 'Delete') { + } elseif ($item->action == 'Delete' || $item->action == 'Error') { $color = 'danger'; } @endphp diff --git a/routes/web.php b/routes/web.php index 1ea8a99..1d9f1bb 100644 --- a/routes/web.php +++ b/routes/web.php @@ -21,6 +21,7 @@ use App\Http\Controllers\Backend\BudgetControlController; use App\Http\Controllers\Backend\ReportController; use App\Http\Controllers\Master\RegionController; use App\Http\Controllers\Master\CabangController; +use App\Http\Controllers\Backend\NextCloudController; use Illuminate\Http\Request; Auth::routes(); @@ -44,6 +45,8 @@ Route::group(['prefix' => 'admin', 'as' => 'admin.', 'middleware' => ['auth:admi Route::resource('roles', RolesController::class); Route::get('/', [DashboardController::class, 'index'])->name('dashboard'); + Route::get('/nextcloud/download/{file}', [NextCloudController::class, 'download'])->name('nextcloud.download'); + // Rayon Route::get('/rayon', [RayonController::class, 'index'])->name('rayon.index'); Route::get('/rayon/create', [RayonController::class, 'create'])->name('rayon.create');