canManage($request->tenant)){ return $this->response(['success' => false, 'error' => __eva("You cannot access.")]); } $tenantId = $request->tenantId(); $flows = WhatsappFlow::where('restaurant_id', $tenantId) ->orderBy('ordering', 'asc') ->get() ->toArray(); return $this->response([ 'success' => true, 'flows' => $flows ]); } /** * Get a specific flow with structure and configs */ public function show(Request $request) { $user = Eva::make('user'); if (!$user->canManage($request->tenant)){ return $this->response(['success' => false, 'error' => __eva("You cannot access.")]); } $tenantId = $request->tenantId(); $flowId = $request->flowId; $flow = WhatsappFlow::where('restaurant_id', $tenantId)->find($flowId); if (!$flow) { return $this->response(['success' => false, 'error' => __eva("Flow not found.")]); } return $this->response([ 'success' => true, 'flow' => $flow->toArray() ]); } /** * Create a new flow */ public function create(Request $request) { $user = Eva::make('user'); if (!$user->canCreate('whatsapp_flows', $request->tenant)){ return $this->response(['success' => false, 'error' => __eva("You cannot create flows.")]); } $params = $request->params; $tenantId = $request->tenantId(); $flow = new WhatsappFlow(); $flow->restaurant_id = $tenantId; $flow->name = $params['name'] ?? ''; $flow->type = $params['type'] ?? 'whatsapp'; $flow->description = $params['description'] ?? ''; $flow->structure = $params['structure'] ?? WhatsappFlow::getDefaultStructure(); $flow->configs = $params['configs'] ?? []; $flow->active = isset($params['active']) ? (bool)$params['active'] : false; $flow->ordering = isset($params['ordering']) ? (int)$params['ordering'] : 1; $flow->save(); return $this->response([ 'success' => true, 'flow' => $flow->toArray() ]); } /** * Update an existing flow */ public function update(Request $request) { $user = Eva::make('user'); if (!$user->canEdit('whatsapp_flows', $request->tenant)){ return $this->response(['success' => false, 'error' => __eva("You cannot edit flows.")]); } $tenantId = $request->tenantId(); $flowId = $request->flowId; $params = $request->params; $flow = WhatsappFlow::where('restaurant_id', $tenantId)->find($flowId); if (!$flow) { return $this->response(['success' => false, 'error' => __eva("Flow not found.")]); } // Update flow data if (isset($params['name'])) $flow->name = $params['name']; if (isset($params['type'])) $flow->type = $params['type']; if (isset($params['description'])) $flow->description = $params['description']; if (isset($params['structure'])) $flow->structure = $params['structure']; if (isset($params['configs'])) $flow->configs = $params['configs']; if (isset($params['active'])) $flow->active = (bool)$params['active']; if (isset($params['ordering'])) $flow->ordering = (int)$params['ordering']; $flow->save(); return $this->response([ 'success' => true, 'flow' => $flow->toArray() ]); } /** * Delete a flow */ public function delete(Request $request) { $user = Eva::make('user'); if (!$user->canDelete('whatsapp_flows', $request->tenant)){ return $this->response(['success' => false, 'error' => __eva("You cannot delete flows.")]); } $tenantId = $request->tenantId(); $flowId = $request->flowId; $flow = WhatsappFlow::where('restaurant_id', $tenantId)->find($flowId); if (!$flow) { return $this->response(['success' => false, 'error' => __eva("Flow not found.")]); } $flow->delete(); return $this->response([ 'success' => true, 'message' => __eva('Flow deleted successfully.') ]); } /** * Duplicate a flow */ public function duplicate(Request $request) { try { $user = Eva::make('user'); if (!$user->canCreate('whatsapp_flows', $request->tenant)){ return $this->response(['success' => false, 'error' => __eva("You cannot create flows.")]); } $tenantId = $request->tenantId(); $flowId = $request->flowId; $originalFlow = WhatsappFlow::where('restaurant_id', $tenantId)->find($flowId); if (!$originalFlow) { return $this->response([ 'success' => false, 'error' => __eva('Flow not found') ]); } // Generate unique name for the copy $baseName = $originalFlow->name; $newName = $baseName . ' Copy'; // Check if a flow with that name already exists $counter = 1; while (WhatsappFlow::where('restaurant_id', $tenantId)->where('name', $newName)->first()) { $counter++; $newName = $baseName . ' Copy ' . $counter; } // Create the new flow $newFlow = new WhatsappFlow(); $newFlow->restaurant_id = $tenantId; $newFlow->name = $newName; $newFlow->type = $originalFlow->type; $newFlow->description = $originalFlow->description; $newFlow->structure = $originalFlow->structure; $newFlow->configs = $originalFlow->configs; $newFlow->active = false; // Duplicated flows start inactive $newFlow->ordering = $originalFlow->ordering + 1; $newFlow->save(); return $this->response([ 'success' => true, 'message' => __eva('Flow duplicated successfully'), 'flow' => $newFlow->toArray() ]); } catch (\Exception $e) { return $this->response([ 'success' => false, 'error' => __eva('Error duplicating flow: ' . $e->getMessage()) ]); } } /** * Toggle flow active status */ public function toggleActive(Request $request) { $user = Eva::make('user'); if (!$user->canEdit('whatsapp_flows', $request->tenant)){ return $this->response(['success' => false, 'error' => __eva("You cannot edit flows.")]); } $tenantId = $request->tenantId(); $flowId = $request->flowId; $flow = WhatsappFlow::where('restaurant_id', $tenantId)->find($flowId); if (!$flow) { return $this->response(['success' => false, 'error' => __eva("Flow not found.")]); } $flow->active = !$flow->active; $flow->save(); return $this->response([ 'success' => true, 'message' => $flow->active ? __eva('Flow activated successfully') : __eva('Flow deactivated successfully'), 'flow' => $flow->toArray() ]); } /** * Reorder flows */ public function reorder(Request $request) { $user = Eva::make('user'); if (!$user->canEdit('whatsapp_flows', $request->tenant)){ return $this->response(['success' => false, 'error' => __eva("You cannot edit flows.")]); } $tenantId = $request->tenantId(); $params = $request->params; // Expecting an array of flows with id and ordering $flowsData = $params['flows'] ?? []; if (empty($flowsData)) { return $this->response(['success' => false, 'error' => __eva("No flows data provided.")]); } try { foreach ($flowsData as $flowData) { $flow = WhatsappFlow::where('restaurant_id', $tenantId)->find($flowData['id']); if ($flow) { $flow->ordering = (int)$flowData['ordering']; $flow->save(); } } return $this->response([ 'success' => true, 'message' => __eva('Flows reordered successfully') ]); } catch (\Exception $e) { return $this->response([ 'success' => false, 'error' => __eva('Error reordering flows: ' . $e->getMessage()) ]); } } /** * Update only the structure of a flow */ public function updateStructure(Request $request) { $user = Eva::make('user'); if (!$user->canEdit('whatsapp_flows', $request->tenant)){ return $this->response(['success' => false, 'error' => __eva("You cannot edit flows.")]); } $tenantId = $request->tenantId(); $flowId = $request->flowId; $params = $request->params; $flow = WhatsappFlow::where('restaurant_id', $tenantId)->find($flowId); if (!$flow) { return $this->response(['success' => false, 'error' => __eva("Flow not found.")]); } if (isset($params['structure'])) { $flow->structure = $params['structure']; } $flow->save(); return $this->response([ 'success' => true, 'flow' => $flow->toArray() ]); } /** * Update only the configs of a flow */ public function updateConfigs(Request $request) { $user = Eva::make('user'); if (!$user->canEdit('whatsapp_flows', $request->tenant)){ return $this->response(['success' => false, 'error' => __eva("You cannot edit flows.")]); } $tenantId = $request->tenantId(); $flowId = $request->flowId; $params = $request->params; $flow = WhatsappFlow::where('restaurant_id', $tenantId)->find($flowId); if (!$flow) { return $this->response(['success' => false, 'error' => __eva("Flow not found.")]); } if (isset($params['configs'])) { $flow->configs = $params['configs']; } $flow->save(); return $this->response([ 'success' => true, 'flow' => $flow->toArray() ]); } /** * Update a specific node config */ public function updateNodeConfig(Request $request) { $user = Eva::make('user'); if (!$user->canEdit('whatsapp_flows', $request->tenant)){ return $this->response(['success' => false, 'error' => __eva("You cannot edit flows.")]); } $tenantId = $request->tenantId(); $flowId = $request->flowId; $nodeId = $request->nodeId; $params = $request->params; $flow = WhatsappFlow::where('restaurant_id', $tenantId)->find($flowId); if (!$flow) { return $this->response(['success' => false, 'error' => __eva("Flow not found.")]); } $config = $params['config'] ?? []; $flow->updateNodeConfig($nodeId, $config); $flow->save(); return $this->response([ 'success' => true, 'flow' => $flow->toArray() ]); } /** * Get flows by type */ public function byType(Request $request) { $user = Eva::make('user'); if (!$user->canManage($request->tenant)){ return $this->response(['success' => false, 'error' => __eva("You cannot access.")]); } $tenantId = $request->tenantId(); $type = $request->type; $flows = WhatsappFlow::where('restaurant_id', $tenantId) ->where('type', $type) ->where('active', true) ->orderBy('ordering', 'asc') ->get() ->toArray(); return $this->response([ 'success' => true, 'flows' => $flows ]); } /** * Get active flows for a restaurant */ public function active(Request $request) { $user = Eva::make('user'); if (!$user->canManage($request->tenant)){ return $this->response(['success' => false, 'error' => __eva("You cannot access.")]); } $tenantId = $request->tenantId(); $flows = WhatsappFlow::where('restaurant_id', $tenantId) ->where('active', true) ->orderBy('ordering', 'asc') ->get() ->toArray(); return $this->response([ 'success' => true, 'flows' => $flows ]); } }