129 خطوط
4.8 KiB
PHP
129 خطوط
4.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\SalesScript;
|
|
use App\Services\ActivityLogger;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
|
|
class ScriptController extends Controller
|
|
{
|
|
public function index(): JsonResponse
|
|
{
|
|
return response()->json(
|
|
SalesScript::with('sections', 'campaign:id,name')
|
|
->orderBy('created_at', 'desc')
|
|
->paginate(15)
|
|
);
|
|
}
|
|
|
|
public function store(Request $request): JsonResponse
|
|
{
|
|
$validated = $request->validate([
|
|
'title' => 'required|string|max:255',
|
|
'description' => 'nullable|string',
|
|
'campaign_id' => 'nullable|exists:campaigns,id',
|
|
'product_id' => 'nullable|exists:products,id',
|
|
'version' => 'nullable|string|max:20',
|
|
'checklist' => 'nullable|array',
|
|
'objection_handling' => 'nullable|array',
|
|
'sections' => 'nullable|array',
|
|
'sections.*.title' => 'required|string|max:255',
|
|
'sections.*.content' => 'required|string',
|
|
'sections.*.sort_order' => 'nullable|integer',
|
|
]);
|
|
|
|
$script = SalesScript::create([
|
|
'title' => $validated['title'],
|
|
'description' => $validated['description'] ?? null,
|
|
'campaign_id' => $validated['campaign_id'] ?? null,
|
|
'product_id' => $validated['product_id'] ?? null,
|
|
'version' => $validated['version'] ?? '1.0',
|
|
'is_active' => true,
|
|
'checklist' => $validated['checklist'] ?? null,
|
|
'objection_handling' => $validated['objection_handling'] ?? null,
|
|
]);
|
|
|
|
if ($request->sections) {
|
|
foreach ($validated['sections'] as $i => $section) {
|
|
$script->sections()->create([
|
|
'title' => $section['title'],
|
|
'content' => $section['content'],
|
|
'sort_order' => $section['sort_order'] ?? $i,
|
|
]);
|
|
}
|
|
}
|
|
|
|
ActivityLogger::log('script_created', "Script {$script->title} created", $script);
|
|
|
|
return response()->json($script->load('sections'), 201);
|
|
}
|
|
|
|
public function show(SalesScript $script): JsonResponse
|
|
{
|
|
return response()->json($script->load('sections', 'campaign'));
|
|
}
|
|
|
|
public function update(Request $request, SalesScript $script): JsonResponse
|
|
{
|
|
$validated = $request->validate([
|
|
'title' => 'sometimes|string|max:255',
|
|
'description' => 'nullable|string',
|
|
'campaign_id' => 'nullable|exists:campaigns,id',
|
|
'product_id' => 'nullable|exists:products,id',
|
|
'version' => 'nullable|string|max:20',
|
|
'is_active' => 'nullable|boolean',
|
|
'checklist' => 'nullable|array',
|
|
'objection_handling' => 'nullable|array',
|
|
'sections' => 'nullable|array',
|
|
'sections.*.id' => 'nullable|exists:script_sections,id',
|
|
'sections.*.title' => 'required|string|max:255',
|
|
'sections.*.content' => 'required|string',
|
|
'sections.*.sort_order' => 'nullable|integer',
|
|
]);
|
|
|
|
$script->update($validated);
|
|
|
|
if ($request->has('sections')) {
|
|
$existingIds = $script->sections->pluck('id')->toArray();
|
|
$updatedIds = [];
|
|
|
|
foreach ($validated['sections'] as $i => $section) {
|
|
if (isset($section['id']) && in_array($section['id'], $existingIds)) {
|
|
$script->sections()->where('id', $section['id'])->update([
|
|
'title' => $section['title'],
|
|
'content' => $section['content'],
|
|
'sort_order' => $section['sort_order'] ?? $i,
|
|
]);
|
|
$updatedIds[] = $section['id'];
|
|
} else {
|
|
$newSection = $script->sections()->create([
|
|
'title' => $section['title'],
|
|
'content' => $section['content'],
|
|
'sort_order' => $section['sort_order'] ?? $i,
|
|
]);
|
|
$updatedIds[] = $newSection->id;
|
|
}
|
|
}
|
|
|
|
$toDelete = array_diff($existingIds, $updatedIds);
|
|
if (!empty($toDelete)) {
|
|
$script->sections()->whereIn('id', $toDelete)->delete();
|
|
}
|
|
}
|
|
|
|
ActivityLogger::log('script_updated', "Script {$script->title} updated", $script);
|
|
|
|
return response()->json($script->load('sections'));
|
|
}
|
|
|
|
public function destroy(SalesScript $script): JsonResponse
|
|
{
|
|
$script->delete();
|
|
ActivityLogger::log('script_deleted', "Script {$script->title} deleted");
|
|
return response()->json(['message' => 'اسکریپت حذف شد']);
|
|
}
|
|
}
|