|
|
|
|
import { defineStore } from 'pinia'
|
|
|
|
|
import api from '../axiosPostulante'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const useExamenStore = defineStore('examenStore', {
|
|
|
|
|
state: () => ({
|
|
|
|
|
procesos: [],
|
|
|
|
|
areas: [],
|
|
|
|
|
examenActual: null,
|
|
|
|
|
preguntas: [],
|
|
|
|
|
cargando: false,
|
|
|
|
|
error: null,
|
|
|
|
|
}),
|
|
|
|
|
actions: {
|
|
|
|
|
async fetchProcesos() {
|
|
|
|
|
try {
|
|
|
|
|
this.cargando = true
|
|
|
|
|
const { data } = await api.get('/examen/procesos')
|
|
|
|
|
|
|
|
|
|
// ✅ normaliza
|
|
|
|
|
this.procesos = (data || []).map(p => ({
|
|
|
|
|
...p,
|
|
|
|
|
requiere_pago: p.requiere_pago === 1 || p.requiere_pago === '1' || p.requiere_pago === true
|
|
|
|
|
}))
|
|
|
|
|
} catch (e) {
|
|
|
|
|
this.error = e.response?.data?.message || e.message
|
|
|
|
|
} finally {
|
|
|
|
|
this.cargando = false
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async fetchAreas(proceso_id) {
|
|
|
|
|
try {
|
|
|
|
|
this.cargando = true
|
|
|
|
|
const { data } = await api.get('/examen/areas', {
|
|
|
|
|
params: { proceso_id }
|
|
|
|
|
})
|
|
|
|
|
this.areas = data
|
|
|
|
|
} catch (e) {
|
|
|
|
|
this.error = e.response?.data?.message || e.message
|
|
|
|
|
} finally {
|
|
|
|
|
this.cargando = false
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
async crearExamen(area_proceso_id, pago = null) {
|
|
|
|
|
try {
|
|
|
|
|
this.cargando = true
|
|
|
|
|
const payload = { area_proceso_id, ...pago }
|
|
|
|
|
const { data } = await api.post('/examen/crear', payload)
|
|
|
|
|
this.examenActual = { id: data.examen_id }
|
|
|
|
|
return data
|
|
|
|
|
} catch (e) {
|
|
|
|
|
this.error = e.response?.data?.message || e.message
|
|
|
|
|
return { success: false, message: this.error }
|
|
|
|
|
} finally {
|
|
|
|
|
this.cargando = false
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async fetchExamenActual() {
|
|
|
|
|
try {
|
|
|
|
|
this.cargando = true
|
|
|
|
|
const { data } = await api.get('/examen/actual')
|
|
|
|
|
this.examenActual = data.examen
|
|
|
|
|
} catch (e) {
|
|
|
|
|
this.error = e.response?.data?.message || e.message
|
|
|
|
|
} finally {
|
|
|
|
|
this.cargando = false
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
async generarPreguntas(examenId) {
|
|
|
|
|
try {
|
|
|
|
|
this.cargando = true
|
|
|
|
|
const { data } = await api.post(`/examen/${examenId}/generar-preguntas`)
|
|
|
|
|
return data
|
|
|
|
|
} catch (e) {
|
|
|
|
|
this.error = e.response?.data?.message || e.message
|
|
|
|
|
return { success: false, message: this.error }
|
|
|
|
|
} finally {
|
|
|
|
|
this.cargando = false
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
async iniciarExamen(examenId) {
|
|
|
|
|
try {
|
|
|
|
|
this.cargando = true
|
|
|
|
|
const { data } = await api.post('/examen/iniciar', { examen_id: examenId })
|
|
|
|
|
this.examenActual = data.examen
|
|
|
|
|
this.preguntas = data.preguntas
|
|
|
|
|
return data
|
|
|
|
|
} catch (e) {
|
|
|
|
|
this.error = e.response?.data?.message || e.message
|
|
|
|
|
return { success: false, message: this.error }
|
|
|
|
|
} finally {
|
|
|
|
|
this.cargando = false
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async responderPregunta(preguntaId, respuesta) {
|
|
|
|
|
try {
|
|
|
|
|
const { data } = await api.post(
|
|
|
|
|
`/examen/pregunta/${preguntaId}/responder`,
|
|
|
|
|
{ respuesta }
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const index = this.preguntas.findIndex(p => p.id === preguntaId)
|
|
|
|
|
|
|
|
|
|
if (index !== -1 && data.success) {
|
|
|
|
|
this.preguntas[index].respuesta = respuesta
|
|
|
|
|
this.preguntas[index].es_correcta = data.correcta // 1, 0 o 2
|
|
|
|
|
this.preguntas[index].puntaje = data.puntaje
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return data
|
|
|
|
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
this.error = e.response?.data?.message || e.message
|
|
|
|
|
return { success: false, message: this.error }
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async finalizarExamen(examenId) {
|
|
|
|
|
try {
|
|
|
|
|
const { data } = await api.post(`/examen/${examenId}/finalizar`)
|
|
|
|
|
this.examenActual = null
|
|
|
|
|
this.preguntas = []
|
|
|
|
|
return data
|
|
|
|
|
} catch (e) {
|
|
|
|
|
this.error = e.response?.data?.message || e.message
|
|
|
|
|
return { success: false, message: this.error }
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
resetStore() {
|
|
|
|
|
this.procesos = []
|
|
|
|
|
this.areas = []
|
|
|
|
|
this.examenActual = null
|
|
|
|
|
this.preguntas = []
|
|
|
|
|
this.cargando = false
|
|
|
|
|
this.error = null
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|