Skip to main content

Overview

The useEncuestasStore manages surveys (encuestas) with support for multiple question types, user responses, and statistical analysis. It handles survey creation, submission, and results visualization.

State Properties

encuestas
Encuesta[]
Array of surveys
encuestaActual
Encuesta | null
Currently selected/viewed survey
respuestas
RespuestaEncuesta[]
Array of survey responses
loading
boolean
Loading state for async operations
error
string | null
Error message from last failed operation

Actions

fetchEncuestas

Fetches surveys with optional active filter.
const result = await encuestasStore.fetchEncuestas(soloActivas)
soloActivas
boolean
default:"true"
If true, returns only active surveys
return
Promise<{ success: boolean; data?: Encuesta[]; error?: string }>
Result object with surveys array

fetchEncuesta

Fetches a single survey by ID.
const result = await encuestasStore.fetchEncuesta(id)
id
string
required
Survey ID
return
Promise<{ success: boolean; data?: Encuesta; error?: string }>
Result object with survey data

crearEncuesta

Creates a new survey (admin only).
const result = await encuestasStore.crearEncuesta(encuesta)
encuesta
InsertEncuesta
required
Survey data
return
Promise<{ success: boolean; data?: Encuesta; error?: string }>
Result object with created survey data
Behavior:
  • Verifies session validity before creation
  • Prepends new survey to local encuestas array

actualizarEncuesta

Updates an existing survey.
const result = await encuestasStore.actualizarEncuesta(id, updates)
id
string
required
Survey ID to update
updates
Partial<InsertEncuesta>
required
Fields to update
return
Promise<{ success: boolean; data?: Encuesta; error?: string }>
Result object with updated survey data
Behavior:
  • Automatically updates updated_at timestamp
  • Updates survey in local encuestas array if present

eliminarEncuesta

Deletes a survey.
const result = await encuestasStore.eliminarEncuesta(id)
id
string
required
Survey ID to delete
return
Promise<{ success: boolean; error?: string }>
Result object indicating success or failure

responderEncuesta

Submits a user’s response to a survey.
const result = await encuestasStore.responderEncuesta(encuestaId, respuesta)
encuestaId
string
required
Survey ID
respuesta
Record<string, any>
required
Response data (format depends on survey type)
return
Promise<{ success: boolean; data?: RespuestaEncuesta; error?: string }>
Result object with submission data
Behavior:
  • Requires authenticated user
  • Checks if user has already responded (prevents duplicates)
  • Throws error if user already submitted response
  • Automatically adds usuario_id from auth store

fetchRespuestasEncuesta

Fetches all responses for a specific survey.
const result = await encuestasStore.fetchRespuestasEncuesta(encuestaId)
encuestaId
string
required
Survey ID
return
Promise<{ success: boolean; data?: RespuestaEncuesta[]; error?: string }>
Result object with responses array

verificarYaRespondio

Checks if current user has already responded to a survey.
const yaRespondio = await encuestasStore.verificarYaRespondio(encuestaId)
encuestaId
string
required
Survey ID
return
Promise<boolean>
True if user has already responded, false otherwise

obtenerRespuestaUsuario

Retrieves the current user’s response for a survey.
const respuesta = await encuestasStore.obtenerRespuestaUsuario(encuestaId)
encuestaId
string
required
Survey ID
return
Promise<Record<string, any> | null>
User’s response data or null if not found

obtenerEstadisticas

Calculates and returns statistics for survey responses.
const result = await encuestasStore.obtenerEstadisticas(encuestaId)
encuestaId
string
required
Survey ID
return
Promise<object>
Statistics object with format depending on survey type
Features:
  • Automatically detects survey type from database
  • Processes responses differently based on question type
  • Supports complex multi-question surveys
  • Returns aggregated counts for multiple choice
  • Returns full text responses for open-ended questions

Usage Examples

<script setup>
import { useEncuestasStore } from '@/stores/encuestas.store'
import { onMounted } from 'vue'

const encuestasStore = useEncuestasStore()

onMounted(async () => {
  // Fetch only active surveys
  await encuestasStore.fetchEncuestas(true)
  
  console.log('Encuestas activas:', encuestasStore.encuestas.length)
})
</script>

Survey Types

Supported Survey Types:
  1. abierta (Open-ended): Users provide free-text responses
  2. calificacion (Rating): Users select a numeric rating (e.g., 1-5 stars)
  3. opcion_multiple (Multiple choice): Users select from predefined options
    • Can have single or multiple questions
    • Supports complex multi-question format with pregunta property
Users can only respond to each survey once. The store automatically prevents duplicate submissions by checking existing responses before allowing submission.

Notes

Use verificarYaRespondio() before displaying survey forms to show appropriate UI (e.g., “You’ve already completed this survey”).
The obtenerEstadisticas() action intelligently formats results based on survey type, making it easy to render appropriate visualizations (bar charts for multiple choice, lists for open-ended, etc.).

Build docs developers (and LLMs) love