tenantId(); $name = $request->name ?: ''; $platform = $request->platform ?: ''; $apiKey = new ApiKey(); $apiKey->restaurant_id = $tenantId; $apiKey->name = sanitize_text_field($name); $apiKey->platform = sanitize_text_field($platform); $apiKey->is_active = 1; $apiKey->save(); return $this->response([ 'success' => true, 'item' => [ 'id' => $apiKey->id, 'uuid' => $apiKey->uuid, 'name' => $apiKey->name, 'platform' => $apiKey->platform, 'api_key' => $apiKey->api_key, 'is_active' => true, 'last_used_at' => null, 'date_created' => $apiKey->date_created, ], ]); } /** * Toggle the active status of an API key */ public function toggle(Request $request) { $tenantId = $request->tenantId(); $id = $request->id; $apiKey = ApiKey::where('id', $id) ->where('restaurant_id', $tenantId) ->first(); if (!$apiKey) { return $this->response([ 'success' => false, 'error' => 'API key not found', ]); } $apiKey->is_active = $apiKey->is_active ? 0 : 1; $apiKey->save(); return $this->response([ 'success' => true, 'is_active' => $apiKey->is_active ? true : false, ]); } /** * Update name and platform of an API key */ public function update(Request $request) { $tenantId = $request->tenantId(); $id = $request->id; $apiKey = ApiKey::where('id', $id) ->where('restaurant_id', $tenantId) ->first(); if (!$apiKey) { return $this->response([ 'success' => false, 'error' => 'API key not found', ]); } if ($request->name !== null) { $apiKey->name = sanitize_text_field($request->name); } if ($request->platform !== null) { $apiKey->platform = sanitize_text_field($request->platform); } $apiKey->save(); return $this->response([ 'success' => true, 'item' => [ 'id' => $apiKey->id, 'uuid' => $apiKey->uuid, 'name' => $apiKey->name, 'platform' => $apiKey->platform, 'api_key' => $apiKey->api_key, 'is_active' => $apiKey->is_active ? true : false, 'last_used_at' => $apiKey->last_used_at, 'date_created' => $apiKey->date_created, ], ]); } /** * Delete an API key */ public function delete(Request $request) { $tenantId = $request->tenantId(); $id = $request->id; $apiKey = ApiKey::where('id', $id) ->where('restaurant_id', $tenantId) ->first(); if (!$apiKey) { return $this->response([ 'success' => false, 'error' => 'API key not found', ]); } $apiKey->delete(); return $this->response([ 'success' => true, ]); } }