CRM/backend/app/Services/AssignmentService.php

55 خطوط
1.6 KiB
PHP

<?php
namespace App\Services;
use App\Models\Lead;
use App\Models\User;
use App\Models\LeadAssignment;
use Illuminate\Support\Facades\DB;
class AssignmentService
{
public function __construct(private LeadService $leadService) {}
public function assignToAgent(int $leadId, int $agentId, int $assignedById): Lead
{
$lead = Lead::findOrFail($leadId);
return $this->leadService->assignLead($lead, $agentId, $assignedById);
}
public function bulkAssign(array $leadIds, int $agentId, int $assignedById): array
{
$results = [];
foreach ($leadIds as $leadId) {
$results[] = $this->assignToAgent($leadId, $agentId, $assignedById);
}
return $results;
}
public function roundRobin(array $leadIds, array $agentIds, int $assignedById): array
{
$leads = Lead::whereIn('id', $leadIds)->get();
return $this->leadService->roundRobinAssign($leads, $agentIds, $assignedById);
}
public function assignByCampaign(int $campaignId, array $agentIds, int $assignedById): array
{
$leads = Lead::where('campaign_id', $campaignId)->where('is_unassigned', true)->get();
return $this->leadService->roundRobinAssign($leads, $agentIds, $assignedById);
}
public function returnToPool(int $leadId): Lead
{
$lead = Lead::findOrFail($leadId);
$lead->update([
'assigned_to' => null,
'assigned_by' => null,
'is_unassigned' => true,
]);
ActivityLogger::log('lead_returned_to_pool', "Lead {$leadId} returned to unassigned pool", $lead);
return $lead->fresh();
}
}