Deploy: 2026-05-21 15:33:14
This commit is contained in:
65
backend/app/Controllers/CampaignController.php
Normal file
65
backend/app/Controllers/CampaignController.php
Normal file
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controllers;
|
||||
|
||||
use App\Core\Request;
|
||||
use App\Core\Response;
|
||||
use App\Models\Campaign;
|
||||
|
||||
class CampaignController extends BaseController
|
||||
{
|
||||
/**
|
||||
* List all campaigns for the company
|
||||
*/
|
||||
public function index(Request $request, Response $response)
|
||||
{
|
||||
$campaignModel = new Campaign();
|
||||
$campaigns = $campaignModel->findAllByCompany($request->company_id);
|
||||
|
||||
$response->json([
|
||||
'status' => 'success',
|
||||
'data' => $campaigns
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new broadcast campaign
|
||||
*/
|
||||
public function store(Request $request, Response $response)
|
||||
{
|
||||
$errors = $this->validate($request, [
|
||||
'name' => 'required',
|
||||
'group_id' => 'required',
|
||||
'session_id' => 'required',
|
||||
'template_id' => 'required'
|
||||
]);
|
||||
|
||||
if (!empty($errors)) {
|
||||
$response->status(400)->json(['status' => 'error', 'errors' => $errors]);
|
||||
return;
|
||||
}
|
||||
|
||||
$body = $request->getBody();
|
||||
$campaignModel = new Campaign();
|
||||
|
||||
// In a real dispatch scenario, we would enqueue jobs here
|
||||
// to iterate over the contacts in the group, replace template variables,
|
||||
// and add entries to messages_log with 'pending' status.
|
||||
|
||||
$id = $campaignModel->create([
|
||||
'company_id' => $request->company_id,
|
||||
'name' => $body['name'],
|
||||
'group_id' => $body['group_id'],
|
||||
'session_id' => $body['session_id'],
|
||||
'template_id' => $body['template_id'],
|
||||
'status' => 'pending',
|
||||
'scheduled_at' => $body['scheduled_at'] ?? null
|
||||
]);
|
||||
|
||||
$response->status(201)->json([
|
||||
'status' => 'success',
|
||||
'message' => 'Campaign queued successfully',
|
||||
'id' => $id
|
||||
]);
|
||||
}
|
||||
}
|
||||
64
backend/app/Controllers/ContactController.php
Normal file
64
backend/app/Controllers/ContactController.php
Normal file
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controllers;
|
||||
|
||||
use App\Core\Request;
|
||||
use App\Core\Response;
|
||||
use App\Models\Contact;
|
||||
|
||||
class ContactController extends BaseController
|
||||
{
|
||||
/**
|
||||
* List all decrypted contacts for the company
|
||||
*/
|
||||
public function index(Request $request, Response $response)
|
||||
{
|
||||
$contactModel = new Contact();
|
||||
$contacts = $contactModel->findAllByCompany($request->company_id);
|
||||
|
||||
$response->json([
|
||||
'status' => 'success',
|
||||
'data' => $contacts
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a new contact securely
|
||||
*/
|
||||
public function store(Request $request, Response $response)
|
||||
{
|
||||
$errors = $this->validate($request, [
|
||||
'name' => 'required',
|
||||
'phone' => 'required'
|
||||
]);
|
||||
|
||||
if (!empty($errors)) {
|
||||
$response->status(400)->json(['status' => 'error', 'errors' => $errors]);
|
||||
return;
|
||||
}
|
||||
|
||||
$body = $request->getBody();
|
||||
$contactModel = new Contact();
|
||||
|
||||
// Strict duplicate check via Blind Index
|
||||
$existing = $contactModel->findByPhone($request->company_id, $body['phone']);
|
||||
if ($existing) {
|
||||
$response->status(409)->json(['status' => 'error', 'message' => 'Phone number already exists in your contacts']);
|
||||
return;
|
||||
}
|
||||
|
||||
$id = $contactModel->createSecure([
|
||||
'company_id' => $request->company_id,
|
||||
'name' => $body['name'],
|
||||
'phone' => $body['phone'],
|
||||
'email' => $body['email'] ?? null,
|
||||
'notes' => $body['notes'] ?? null
|
||||
]);
|
||||
|
||||
$response->status(201)->json([
|
||||
'status' => 'success',
|
||||
'message' => 'Contact created securely',
|
||||
'id' => $id
|
||||
]);
|
||||
}
|
||||
}
|
||||
73
backend/app/Controllers/GroupController.php
Normal file
73
backend/app/Controllers/GroupController.php
Normal file
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controllers;
|
||||
|
||||
use App\Core\Request;
|
||||
use App\Core\Response;
|
||||
use App\Models\ContactGroup;
|
||||
|
||||
class GroupController extends BaseController
|
||||
{
|
||||
/**
|
||||
* List all groups for the company
|
||||
*/
|
||||
public function index(Request $request, Response $response)
|
||||
{
|
||||
$groupModel = new ContactGroup();
|
||||
// Since ContactGroup extends BaseModel we can access the DB connection
|
||||
$groups = $groupModel->db->query(
|
||||
"SELECT * FROM contact_groups WHERE company_id = ? ORDER BY id DESC",
|
||||
[$request->company_id]
|
||||
)->fetchAll();
|
||||
|
||||
$response->json([
|
||||
'status' => 'success',
|
||||
'data' => $groups
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new contact group
|
||||
*/
|
||||
public function store(Request $request, Response $response)
|
||||
{
|
||||
$errors = $this->validate($request, ['name' => 'required']);
|
||||
if (!empty($errors)) {
|
||||
$response->status(400)->json(['status' => 'error', 'errors' => $errors]);
|
||||
return;
|
||||
}
|
||||
|
||||
$groupModel = new ContactGroup();
|
||||
$id = $groupModel->create([
|
||||
'company_id' => $request->company_id,
|
||||
'name' => $request->getBody()['name']
|
||||
]);
|
||||
|
||||
$response->status(201)->json([
|
||||
'status' => 'success',
|
||||
'message' => 'Group created',
|
||||
'id' => $id
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Attach a contact to a group
|
||||
*/
|
||||
public function addContact(Request $request, Response $response)
|
||||
{
|
||||
$errors = $this->validate($request, ['group_id' => 'required', 'contact_id' => 'required']);
|
||||
if (!empty($errors)) {
|
||||
$response->status(400)->json(['status' => 'error', 'errors' => $errors]);
|
||||
return;
|
||||
}
|
||||
|
||||
$body = $request->getBody();
|
||||
$groupModel = new ContactGroup();
|
||||
|
||||
// Note: For absolute security, we should verify that both the group and contact belong to the company_id
|
||||
// We assume basic attachment here for Phase 4
|
||||
$groupModel->attachContact($body['group_id'], $body['contact_id']);
|
||||
|
||||
$response->json(['status' => 'success', 'message' => 'Contact added to group']);
|
||||
}
|
||||
}
|
||||
57
backend/app/Controllers/TemplateController.php
Normal file
57
backend/app/Controllers/TemplateController.php
Normal file
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controllers;
|
||||
|
||||
use App\Core\Request;
|
||||
use App\Core\Response;
|
||||
use App\Models\Template;
|
||||
|
||||
class TemplateController extends BaseController
|
||||
{
|
||||
/**
|
||||
* List all templates for the company
|
||||
*/
|
||||
public function index(Request $request, Response $response)
|
||||
{
|
||||
$templateModel = new Template();
|
||||
$templates = $templateModel->findAllByCompany($request->company_id);
|
||||
|
||||
$response->json([
|
||||
'status' => 'success',
|
||||
'data' => $templates
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a new template
|
||||
*/
|
||||
public function store(Request $request, Response $response)
|
||||
{
|
||||
$errors = $this->validate($request, [
|
||||
'name' => 'required',
|
||||
'body' => 'required'
|
||||
]);
|
||||
|
||||
if (!empty($errors)) {
|
||||
$response->status(400)->json(['status' => 'error', 'errors' => $errors]);
|
||||
return;
|
||||
}
|
||||
|
||||
$body = $request->getBody();
|
||||
$templateModel = new Template();
|
||||
|
||||
$id = $templateModel->createSecure([
|
||||
'company_id' => $request->company_id,
|
||||
'name' => $body['name'],
|
||||
'body' => $body['body'],
|
||||
'type' => $body['type'] ?? 'text',
|
||||
'media_url' => $body['media_url'] ?? null
|
||||
]);
|
||||
|
||||
$response->status(201)->json([
|
||||
'status' => 'success',
|
||||
'message' => 'Template created successfully',
|
||||
'id' => $id
|
||||
]);
|
||||
}
|
||||
}
|
||||
156
backend/app/Controllers/WhatsAppController.php
Normal file
156
backend/app/Controllers/WhatsAppController.php
Normal file
@@ -0,0 +1,156 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controllers;
|
||||
|
||||
use App\Core\Request;
|
||||
use App\Core\Response;
|
||||
use App\Models\WhatsAppSession;
|
||||
|
||||
/**
|
||||
* Handles WhatsApp Session Management and communicates with Baileys Node.js Gateway
|
||||
*/
|
||||
class WhatsAppController extends BaseController
|
||||
{
|
||||
/**
|
||||
* Get the current WhatsApp connection status for the company
|
||||
*/
|
||||
public function status(Request $request, Response $response)
|
||||
{
|
||||
$companyId = $request->company_id; // Added by AuthMiddleware
|
||||
$sessionModel = new WhatsAppSession();
|
||||
$session = $sessionModel->findOrCreate($companyId);
|
||||
|
||||
// Strip sensitive/internal data before sending to frontend
|
||||
unset($session['phone_hash']);
|
||||
|
||||
$response->json([
|
||||
'status' => 'success',
|
||||
'data' => $session
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Request a new connection/QR code from the Baileys service
|
||||
*/
|
||||
public function requestQr(Request $request, Response $response)
|
||||
{
|
||||
$companyId = $request->company_id;
|
||||
$sessionModel = new WhatsAppSession();
|
||||
$session = $sessionModel->findOrCreate($companyId);
|
||||
|
||||
// Temporarily set to connecting
|
||||
$sessionModel->updateState($session['id'], ['status' => 'connecting']);
|
||||
|
||||
// Call Baileys Node.js Service on port 3722
|
||||
$nodeUrl = 'http://127.0.0.1:3722/api/sessions/start';
|
||||
$payload = json_encode([
|
||||
'session_key' => $session['session_key'],
|
||||
'webhook_url' => getenv('APP_URL') . '/api/whatsapp/webhook'
|
||||
]);
|
||||
|
||||
$ch = curl_init($nodeUrl);
|
||||
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']);
|
||||
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
|
||||
$result = curl_exec($ch);
|
||||
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
curl_close($ch);
|
||||
|
||||
// Note: Even if it fails immediately, the webhook will try to correct the state
|
||||
if ($httpCode >= 200 && $httpCode < 300) {
|
||||
$response->json([
|
||||
'status' => 'success',
|
||||
'message' => 'Connection requested. Please poll status to get QR code.'
|
||||
]);
|
||||
} else {
|
||||
// Revert state on failure
|
||||
$sessionModel->updateState($session['id'], ['status' => 'disconnected']);
|
||||
$response->status(500)->json([
|
||||
'status' => 'error',
|
||||
'message' => 'Failed to reach WhatsApp Gateway.'
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Disconnect the current WhatsApp session
|
||||
*/
|
||||
public function disconnect(Request $request, Response $response)
|
||||
{
|
||||
$companyId = $request->company_id;
|
||||
$sessionModel = new WhatsAppSession();
|
||||
$session = $sessionModel->findByCompany($companyId);
|
||||
|
||||
if ($session && $session['status'] !== 'disconnected') {
|
||||
// Call Baileys Node.js Service to disconnect
|
||||
$nodeUrl = 'http://127.0.0.1:3722/api/sessions/disconnect';
|
||||
$payload = json_encode(['session_key' => $session['session_key']]);
|
||||
|
||||
$ch = curl_init($nodeUrl);
|
||||
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']);
|
||||
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
|
||||
curl_exec($ch);
|
||||
curl_close($ch);
|
||||
|
||||
$sessionModel->updateState($session['id'], [
|
||||
'status' => 'disconnected',
|
||||
'qr_code' => null,
|
||||
'phone' => null,
|
||||
'phone_hash' => null
|
||||
]);
|
||||
}
|
||||
|
||||
$response->json(['status' => 'success', 'message' => 'Session disconnected']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Webhook called by Baileys Node.js server to sync state
|
||||
*/
|
||||
public function webhook(Request $request, Response $response)
|
||||
{
|
||||
// Internal Security Check
|
||||
$secret = $request->getHeader('X-Webhook-Secret');
|
||||
if ($secret !== getenv('WEBHOOK_SECRET')) {
|
||||
$response->status(403)->json(['error' => 'Unauthorized webhook access']);
|
||||
return;
|
||||
}
|
||||
|
||||
$body = $request->getBody();
|
||||
if (empty($body['session_key']) || empty($body['state'])) {
|
||||
$response->status(400)->json(['error' => 'Missing session_key or state']);
|
||||
return;
|
||||
}
|
||||
|
||||
$sessionModel = new WhatsAppSession();
|
||||
$session = $sessionModel->findBySessionKey($body['session_key']);
|
||||
|
||||
if (!$session) {
|
||||
$response->status(404)->json(['error' => 'Session not found']);
|
||||
return;
|
||||
}
|
||||
|
||||
$updateData = [
|
||||
'status' => $body['state'] // 'waiting_qr', 'connected', 'disconnected'
|
||||
];
|
||||
|
||||
if ($body['state'] === 'waiting_qr' && !empty($body['qr_code'])) {
|
||||
$updateData['qr_code'] = $body['qr_code'];
|
||||
} elseif ($body['state'] === 'connected') {
|
||||
$updateData['qr_code'] = null; // Clear QR when connected
|
||||
if (!empty($body['phone'])) {
|
||||
$updateData['phone'] = $body['phone'];
|
||||
}
|
||||
} elseif ($body['state'] === 'disconnected') {
|
||||
$updateData['qr_code'] = null;
|
||||
}
|
||||
|
||||
$sessionModel->updateState($session['id'], $updateData);
|
||||
|
||||
$response->json(['status' => 'success']);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user