tenantId(); $widgets = WidgetForm::where('restaurant_id', $tenantId) ->get() ->map(function($w){ return [ 'id' => intval($w->id), 'name' => $w->name, ]; }) ->toArray(); return $this->response([ 'success' => true, 'widgets' => $widgets, ]); } /** * Create a new bot API key for the restaurant. * POST /app/api-keys-bot/create */ public function create(Request $request) { $tenantId = $request->tenantId(); $widgetId = intval($request->widget_id); if (!$this->widgetBelongsToTenant($widgetId, $tenantId)) { return $this->response([ 'success' => false, 'error' => 'Invalid widget for this restaurant', ]); } $name = $request->name ?: ''; $platform = $request->platform ?: ''; $apiKey = new ApiKeyBot(); $apiKey->restaurant_id = $tenantId; $apiKey->widget_id = $widgetId; $apiKey->name = sanitize_text_field($name); $apiKey->platform = sanitize_text_field($platform); $apiKey->is_active = 1; $apiKey->save(); return $this->response([ 'success' => true, 'item' => $this->formatItem($apiKey), ]); } /** * Toggle active flag. * POST /app/api-keys-bot/:id/toggle */ public function toggle(Request $request) { $apiKey = $this->findKey($request); if (!$apiKey) { return $this->notFoundResponse(); } $apiKey->is_active = $apiKey->is_active ? 0 : 1; $apiKey->save(); return $this->response([ 'success' => true, 'is_active' => $apiKey->is_active ? true : false, ]); } /** * Update name, platform, widget_id. * POST /app/api-keys-bot/:id/update */ public function update(Request $request) { $apiKey = $this->findKey($request); if (!$apiKey) { return $this->notFoundResponse(); } $tenantId = $request->tenantId(); if ($request->name !== null) { $apiKey->name = sanitize_text_field($request->name); } if ($request->platform !== null) { $apiKey->platform = sanitize_text_field($request->platform); } if ($request->widget_id !== null) { $widgetId = intval($request->widget_id); if (!$this->widgetBelongsToTenant($widgetId, $tenantId)) { return $this->response([ 'success' => false, 'error' => 'Invalid widget for this restaurant', ]); } $apiKey->widget_id = $widgetId; } $apiKey->save(); return $this->response([ 'success' => true, 'item' => $this->formatItem($apiKey), ]); } /** * Delete an API key. * POST /app/api-keys-bot/:id/delete */ public function delete(Request $request) { $apiKey = $this->findKey($request); if (!$apiKey) { return $this->notFoundResponse(); } $apiKey->delete(); return $this->response(['success' => true]); } //------------------------------------------------------------------- protected function findKey(Request $request) { return ApiKeyBot::where('id', $request->id) ->where('restaurant_id', $request->tenantId()) ->first(); } protected function widgetBelongsToTenant($widgetId, $tenantId) { if (!$widgetId) return false; return WidgetForm::where('id', $widgetId) ->where('restaurant_id', $tenantId) ->exists(); } protected function notFoundResponse() { return $this->response([ 'success' => false, 'error' => 'API key not found', ]); } protected function formatItem(ApiKeyBot $apiKey) { $widgetName = null; if ($apiKey->widget_id) { $widget = WidgetForm::where('id', $apiKey->widget_id)->first(); $widgetName = $widget ? $widget->name : null; } return [ 'id' => intval($apiKey->id), 'uuid' => $apiKey->uuid, 'name' => $apiKey->name, 'platform' => $apiKey->platform, 'widget_id' => intval($apiKey->widget_id), 'widget_name' => $widgetName, 'api_key' => $apiKey->api_key, 'is_active' => $apiKey->is_active ? true : false, 'last_used_at' => $apiKey->last_used_at, 'date_created' => $apiKey->date_created, ]; } }