6238b8b580
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
64 lines
2.1 KiB
PHP
64 lines
2.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Services\Project;
|
|
|
|
use App\Jobs\SyncSupplierProjectJob;
|
|
use App\Models\Project;
|
|
use App\Models\Tenant;
|
|
use Illuminate\Http\Exceptions\HttpResponseException;
|
|
|
|
class ProjectService
|
|
{
|
|
public function update(Project $project, array $data): Project
|
|
{
|
|
// Immutable fields — silently drop (don't 422)
|
|
unset(
|
|
$data['tenant_id'], $data['signal_type'], $data['signal_identifier'],
|
|
$data['delivered_today'], $data['delivered_in_month'],
|
|
$data['supplier_b1_project_id'], $data['supplier_b2_project_id'], $data['supplier_b3_project_id'],
|
|
$data['archived_at'],
|
|
);
|
|
|
|
if (isset($data['daily_limit_target']) && $data['daily_limit_target'] < $project->delivered_today) {
|
|
throw new HttpResponseException(response()->json([
|
|
'errors' => [
|
|
'daily_limit_target' => [
|
|
"Лимит не может быть меньше уже доставленных лидов сегодня ({$project->delivered_today}).",
|
|
],
|
|
],
|
|
], 422));
|
|
}
|
|
|
|
$needsResync = array_key_exists('sms_senders', $data) || array_key_exists('sms_keyword', $data);
|
|
|
|
$project->update($data);
|
|
|
|
if ($needsResync) {
|
|
SyncSupplierProjectJob::dispatch($project->id);
|
|
}
|
|
|
|
return $project->fresh();
|
|
}
|
|
|
|
public function create(Tenant $tenant, array $data): Project
|
|
{
|
|
$limit = (int) ($tenant->limits['max_projects'] ?? 10);
|
|
$current = Project::where('tenant_id', $tenant->id)->active()->count();
|
|
if ($current >= $limit) {
|
|
throw new HttpResponseException(response()->json([
|
|
'message' => "Достигнут лимит проектов ({$limit}). Смените тариф.",
|
|
], 403));
|
|
}
|
|
|
|
$data['tenant_id'] = $tenant->id;
|
|
$data['is_active'] = true;
|
|
$project = Project::create($data);
|
|
|
|
SyncSupplierProjectJob::dispatch($project->id);
|
|
|
|
return $project->fresh();
|
|
}
|
|
}
|