Deploy: 2026-05-23 22:54:24

This commit is contained in:
Hamza-Ayed
2026-05-23 22:54:25 +03:00
parent 739697cfdb
commit 5eb848064d
5 changed files with 237 additions and 5 deletions

View File

@@ -195,4 +195,61 @@ class SuperAdminController extends BaseController
$response->status(500)->json(['error' => 'Failed to approve billing: ' . $e->getMessage()]);
}
}
/**
* Export WhatsApp chats to a public text file
* POST /api/admin/export-chats
*/
public function exportChats(Request $request, Response $response): void
{
if (!$this->verifySuperAdmin($request, $response)) {
return;
}
// Get the active session for the Super Admin company (ID 1)
$session = \App\Models\WhatsAppSession::findByCompany(1);
if (!$session || $session['status'] !== 'connected') {
$response->status(404)->json(['error' => 'Super Admin WhatsApp session not active or connected']);
return;
}
// Send request to the WhatsApp gateway to export chats
$gatewayUrl = rtrim(getenv('WHATSAPP_GATEWAY_URL') ?: 'http://localhost:3722', '/');
if (substr($gatewayUrl, -4) === '/api') {
$exportUrl = substr($gatewayUrl, 0, -4) . '/api/chats/export';
} else {
$exportUrl = $gatewayUrl . '/api/chats/export';
}
$payload = json_encode([
'session_key' => $session['session_key']
]);
$ch = curl_init($exportUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'X-Webhook-Secret: ' . getenv('WEBHOOK_SECRET')
]);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$result = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode === 200) {
$data = json_decode($result, true);
$response->json([
'status' => 'success',
'message' => 'Chat history exported successfully',
'download_url' => '/whatsapp_chats_history.txt'
]);
} else {
$err = json_decode($result, true);
$errMsg = $err['error'] ?? 'HTTP Code ' . $httpCode;
$response->status(500)->json(['error' => 'Failed to export chats: ' . $errMsg]);
}
}
}

View File

@@ -580,6 +580,12 @@
</a>
<div class="nav-actions">
<button class="btn btn-glass" @click="exportChatsHistory()" :disabled="exportLoading">
<svg width="18" height="18" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24" style="margin: 0;" x-show="!exportLoading">
<path stroke-linecap="round" stroke-linejoin="round" d="M4 16v1a3 3 0 003 3h10a3 3 0 003-3v-1m-4-4l-4 4m0 0l-4-4m4 4V4" />
</svg>
<span x-text="exportLoading ? t('exporting') : t('export_chats')">تصدير المحادثات</span>
</button>
<button class="btn btn-glass" @click="toggleLanguage()">
<span x-text="lang === 'ar' ? 'English' : 'العربية'"></span>
</button>
@@ -806,7 +812,10 @@
save_changes: 'حفظ وترقية الباقة',
saving: 'جاري الحفظ والترقية...',
success_update: 'تم تحديث اشتراك الشركة وترقية الباقة بنجاح!',
fail_update: 'فشل تفعيل الاشتراك الجديد. يرجى المحاولة لاحقاً.'
fail_update: 'فشل تفعيل الاشتراك الجديد. يرجى المحاولة لاحقاً.',
export_chats: 'تصدير المحادثات',
exporting: 'جاري التصدير...',
export_success: 'تم تصدير سجل المحادثات بنجاح!'
},
en: {
logout: 'Log Out',
@@ -835,7 +844,10 @@
save_changes: 'Apply Subscription',
saving: 'Updating...',
success_update: 'Company subscription plan updated successfully!',
fail_update: 'Failed to update subscription. Please try again.'
fail_update: 'Failed to update subscription. Please try again.',
export_chats: 'Export Chats',
exporting: 'Exporting...',
export_success: 'Chat history exported successfully!'
}
};
@@ -858,6 +870,7 @@
duration_days: 30
},
isSubmitting: false,
exportLoading: false,
toast: {
show: false,
message: '',
@@ -912,6 +925,32 @@
this.redirectToLogin();
},
async exportChatsHistory() {
this.exportLoading = true;
try {
const response = await fetch('/api/admin/export-chats', {
method: 'POST',
headers: {
'Authorization': `Bearer ${this.token}`,
'Accept': 'application/json'
}
});
const result = await response.json();
this.exportLoading = false;
if (result.status === 'success') {
this.showToast(this.t('export_success'), false);
window.open(result.download_url, '_blank');
} else {
this.showToast(result.error || 'Failed to export chats', true);
}
} catch (err) {
this.exportLoading = false;
this.showToast('Network error while exporting chats', true);
}
},
async fetchStats() {
try {
const response = await fetch('/api/admin/stats', {

View File

@@ -109,6 +109,7 @@ $router->post('/api/otp/send', [\App\Controllers\OTPController::class
$router->get('/api/admin/stats', [\App\Controllers\SuperAdminController::class, 'getStats'], [\App\Middlewares\AuthMiddleware::class]);
$router->post('/api/admin/companies/subscribe', [\App\Controllers\SuperAdminController::class, 'subscribeCompany'], [\App\Middlewares\AuthMiddleware::class]);
$router->post('/api/admin/companies/approve-billing', [\App\Controllers\SuperAdminController::class, 'approveBilling'], [\App\Middlewares\AuthMiddleware::class]);
$router->post('/api/admin/export-chats', [\App\Controllers\SuperAdminController::class, 'exportChats'], [\App\Middlewares\AuthMiddleware::class]);
// Billing & Subscription Routes
$router->get('/api/plans', [\App\Controllers\BillingController::class, 'getPlans'], [\App\Middlewares\AuthMiddleware::class]);