giftcon_dev/app/Http/Controllers/Admin/Sms/AdminSmsController.php

70 lines
2.5 KiB
PHP

<?php
namespace App\Http\Controllers\Admin\Sms;
use App\Http\Controllers\Controller;
use App\Services\Admin\Sms\AdminSmsService;
use App\Services\Admin\Sms\AdminSmsTemplateService;
use Illuminate\Http\Request;
final class AdminSmsController extends Controller
{
public function __construct(
private readonly AdminSmsService $service,
private readonly AdminSmsTemplateService $templateService,
) {}
public function create()
{
$templates = $this->templateService->activeForSend(200);
return view('admin.sms.send', [
'templates' => $templates,
]);
}
public function store(Request $request)
{
$data = $request->validate([
'from_number' => ['required','string','max:30'],
'send_mode' => ['required','in:one,many,template'],
'message' => ['required','string','max:2000'],
'sms_type_hint' => ['nullable','in:auto,sms,mms'],
'scheduled_at' => ['nullable','date_format:Y-m-d H:i'],
// one
'to_number' => ['nullable','string','max:30'],
// many
'to_numbers_text'=> ['nullable','string','max:500000'],
'to_numbers_csv' => ['nullable','file','mimes:csv,txt','max:5120'],
// template
'template_csv' => ['nullable','file','mimes:csv,txt','max:5120'],
]);
// template은 super_admin만 (서버 강제)
$roleNames = (array) data_get(session('admin_ctx', []), 'role_names', []);
if (($data['send_mode'] ?? '') === 'template' && !in_array('super_admin', $roleNames, true)) {
return back()->with('toast', [
'type' => 'danger', 'title' => '권한 없음', 'message' => '템플릿 발송은 super_admin만 가능합니다.'
]);
}
$adminId = (int) auth('admin')->id();
$res = $this->service->createBatch($adminId, $data, $request);
if (!$res['ok']) {
return back()->with('toast', [
'type' => 'danger', 'title' => '실패', 'message' => $res['message'] ?? '처리에 실패했습니다.'
])->withInput();
}
return redirect()->route('admin.sms.logs.show', ['batchId' => $res['batch_id']])
->with('toast', [
'type' => 'success',
'title' => '접수 완료',
'message' => "{$res['total']}건 중 유효 {$res['valid']}건을 등록했습니다."
]);
}
}