Only employees can accept tasks from the general inbox:
incidents/[id].tsx
const handleAcceptTask = async () => { // Verify user is authenticated employee const { data: { user } } = await supabase.auth.getUser() if (!user) throw new Error("No hay usuario autenticado") // Check task is still available const { data: currentIncident } = await supabase .from("incidents") .select("status") .eq("id", id) .single() if (currentIncident.status !== "pendiente") { Alert.alert("Tarea no disponible", "Esta tarea ya fue aceptada por otro empleado del área.") return } // Accept the task const { error } = await supabase .from("incidents") .update({ status: "recibida", assigned_to: user.id, // Link to employee }) .eq("id", id)}
Supabase RLS policies enforce access control at the database level:
-- Guests can only read their own room's incidentsCREATE POLICY "Guests can view own room incidents"ON incidentsFOR SELECTUSING ( room_id IN ( SELECT room_id FROM guest_sessions WHERE access_code = current_setting('request.jwt.claims')::json->>'access_code' AND active = true ));-- Employees can view incidents in their areaCREATE POLICY "Employees can view area incidents"ON incidentsFOR SELECTUSING ( area_id IN ( SELECT a.id FROM areas a JOIN profiles p ON p.area = a.name WHERE p.id = auth.uid() ));-- Only assigned employees can update incidentsCREATE POLICY "Assigned employee can update incident"ON incidentsFOR UPDATEUSING (assigned_to = auth.uid());
RLS policies provide defense-in-depth security, ensuring access control even if client-side checks fail.
// Server-side validation (Supabase RLS)if (incident.assigned_to !== auth.uid()) { throw new Error('Unauthorized: Not assigned to this incident')}if (incident.area_id !== employeeArea.id) { throw new Error('Unauthorized: Incident not in your area')}
Employees are assigned to specific areas/departments:
CREATE TABLE areas ( id UUID PRIMARY KEY, name TEXT UNIQUE NOT NULL, description TEXT, created_at TIMESTAMP DEFAULT NOW());CREATE TABLE profiles ( id UUID PRIMARY KEY REFERENCES auth.users, role TEXT CHECK (role IN ('guest', 'empleado', 'admin')), area TEXT REFERENCES areas(name), created_at TIMESTAMP DEFAULT NOW());