update api register

This commit is contained in:
Wian Drs
2026-08-01 11:42:42 +07:00
parent b0d08211ec
commit 75342dc7b7
55 changed files with 2254 additions and 35 deletions
@@ -9,7 +9,9 @@ use App\Http\Requests\Notification\StoreCustomerContactRequest;
use App\Http\Requests\Notification\StoreNotificationBroadcastRequest;
use App\Http\Requests\Notification\StoreNotificationChannelRequest;
use App\Http\Requests\Notification\StoreNotificationTemplateRequest;
use App\Http\Requests\Notification\StoreSystemNotificationRequest;
use App\Http\Resources\Notification\NotificationDeliveryResource;
use App\Http\Resources\Notification\SystemNotificationCampaignResource;
use App\Models\Customer;
use App\Models\CustomerContact;
use App\Models\NotificationBroadcast;
@@ -17,11 +19,12 @@ use App\Models\NotificationChannel;
use App\Models\NotificationEvent;
use App\Models\NotificationTemplate;
use App\Services\Notification\NotificationService;
use App\Services\Notification\WhapiIdService;
use Illuminate\Http\Request;
class NotificationController extends ApiController
{
public function __construct(protected NotificationService $service) {}
public function __construct(protected NotificationService $service, protected WhapiIdService $whapi) {}
public function events()
{
@@ -33,9 +36,42 @@ class NotificationController extends ApiController
return $this->success($this->service->channels($this->channel($request), $request->user(), $this->tenantId($request)), 'Konfigurasi channel berhasil diambil');
}
public function systemCampaigns(IndexNotificationRequest $request)
{
return $this->successPaginated(
$this->service->systemCampaigns($request->validated(), $request->user(), $this->tenantId($request)),
SystemNotificationCampaignResource::class,
'Riwayat notifikasi System berhasil diambil'
);
}
public function storeSystemCampaign(StoreSystemNotificationRequest $request)
{
return $this->created(
$this->service->createSystemCampaign($request->validated(), $request->user(), $this->tenantId($request)),
'Notifikasi System masuk antrean'
);
}
public function systemAudienceOptions(Request $request)
{
$data = $request->validate([
'tenant_id' => ['nullable', 'integer', 'exists:tenants,id'],
'search' => ['nullable', 'string', 'max:100'],
]);
return $this->success(
$this->service->systemAudienceOptions(
$request->user(), $this->tenantId($request), $data['tenant_id'] ?? null, $data['search'] ?? null
),
'Pilihan penerima notifikasi berhasil diambil'
);
}
public function storeChannel(StoreNotificationChannelRequest $request)
{
abort_unless($request->validated('channel') === $this->channel($request), 422);
$this->authorizeChannelMutation($request);
return $this->created($this->service->saveChannel($request->validated(), $request->user(), $this->tenantId($request)), 'Konfigurasi channel berhasil dibuat');
}
@@ -44,6 +80,7 @@ class NotificationController extends ApiController
{
$this->authorizeTenant($request, $notificationChannel->tenant_id);
abort_unless($notificationChannel->channel === $this->channel($request), 404);
$this->authorizeChannelMutation($request);
return $this->updated($this->service->saveChannel($request->validated(), $request->user(), $this->tenantId($request), $notificationChannel), 'Konfigurasi channel berhasil diperbarui');
}
@@ -52,10 +89,56 @@ class NotificationController extends ApiController
{
$this->authorizeTenant($request, $notificationChannel->tenant_id);
abort_unless($notificationChannel->channel === $this->channel($request), 404);
$this->authorizeChannelMutation($request);
return $this->success($this->service->testChannel($notificationChannel), 'Pengujian channel selesai');
}
public function destroyChannel(Request $request, NotificationChannel $notificationChannel)
{
$this->authorizeTenant($request, $notificationChannel->tenant_id);
abort_unless($notificationChannel->channel === $this->channel($request), 404);
$this->authorizeChannelMutation($request);
$this->service->deleteChannel($notificationChannel);
return $this->deleted('Konfigurasi channel berhasil dinonaktifkan atau dihapus');
}
public function whapiState(Request $request, NotificationChannel $notificationChannel)
{
$this->authorizeWhapiAccess($request, $notificationChannel);
return $this->success($this->whapi->state($notificationChannel), 'Status perangkat WHAPI berhasil diambil');
}
public function whapiStart(Request $request, NotificationChannel $notificationChannel)
{
$this->authorizeWhapiAccess($request, $notificationChannel);
return $this->success($this->whapi->start($notificationChannel), 'Layanan WHAPI berhasil dijalankan');
}
public function whapiQr(Request $request, NotificationChannel $notificationChannel)
{
$this->authorizeWhapiAccess($request, $notificationChannel);
return $this->success($this->whapi->qr($notificationChannel), 'QR WHAPI berhasil diambil');
}
public function whapiTestMessage(Request $request, NotificationChannel $notificationChannel)
{
$this->authorizeWhapiAccess($request, $notificationChannel);
$data = $request->validate([
'phone' => ['required', 'string', 'max:25', 'regex:/^\+?[0-9][0-9\s\-()]{7,24}$/'],
'message' => ['required', 'string', 'max:4000'],
]);
return $this->success(
$this->whapi->send($notificationChannel, $data['phone'], $data['message']),
'Pesan uji WHAPI berhasil dikirim'
);
}
public function templates(Request $request)
{
return $this->success($this->service->templates($this->channel($request), $request->user(), $this->tenantId($request)), 'Daftar template berhasil diambil');
@@ -185,8 +268,24 @@ class NotificationController extends ApiController
return $request->attributes->get('tenant_id');
}
private function authorizeTenant(Request $request, int $tenantId): void
private function authorizeTenant(Request $request, ?int $tenantId): void
{
abort_unless($request->user()->isMasterAdmin() || $tenantId === $this->tenantId($request), 403);
abort_unless($request->user()->isMasterAdmin()
|| ($tenantId !== null && $tenantId === $this->tenantId($request)), 403);
}
private function authorizeChannelMutation(Request $request): void
{
if ($this->channel($request) === 'whatsapp_unofficial') {
abort_unless($request->user()->isMasterAdmin(), 403, 'WhatsApp Unofficial hanya dapat dikonfigurasi oleh Master Admin.');
}
}
private function authorizeWhapiAccess(Request $request, NotificationChannel $channel): void
{
abort_unless($channel->channel === 'whatsapp_unofficial' && $channel->provider === 'whapi_id', 404);
abort_unless($request->user()->isMasterAdmin()
|| ((int) $channel->tenant_id === (int) $this->tenantId($request)
&& $request->user()->isTenantOwner($this->tenantId($request))), 403);
}
}