estandarizado el manejo de errores
This commit is contained in:
@@ -25,7 +25,8 @@ export const useAsistenciasStore = defineStore('asistencias', {
|
|||||||
this.asistencias = response.data;
|
this.asistencias = response.data;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error fetching asistencias:', error);
|
console.error('Error fetching asistencias:', error);
|
||||||
// Handle error (e.g., show a notification to the user)
|
const errorMsg = error.response?.data?.message || error.message || 'Ocurrió un error.';
|
||||||
|
throw errorMsg; // Re-throw to allow handling in component
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -39,6 +40,8 @@ export const useAsistenciasStore = defineStore('asistencias', {
|
|||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(`Error fetching asistencia with id ${id}:`, error);
|
console.error(`Error fetching asistencia with id ${id}:`, error);
|
||||||
this.currentAsistencia = getDefaultCurrentAsistencia(); // Reset on error
|
this.currentAsistencia = getDefaultCurrentAsistencia(); // Reset on error
|
||||||
|
const errorMsg = error.response?.data?.message || error.message || 'Ocurrió un error.';
|
||||||
|
throw errorMsg; // Re-throw to allow handling in component
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -52,8 +55,9 @@ export const useAsistenciasStore = defineStore('asistencias', {
|
|||||||
await this.fetchAsistencias(); // Refresh the list
|
await this.fetchAsistencias(); // Refresh the list
|
||||||
return data;
|
return data;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error creating asistencia:', error.message);
|
console.error('Error creating asistencia:', error);
|
||||||
throw error; // Re-throw to allow form to handle it
|
const errorMsg = error.response?.data?.message || error.message || 'Ocurrió un error.';
|
||||||
|
throw errorMsg; // Re-throw to allow form to handle it
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -68,7 +72,8 @@ export const useAsistenciasStore = defineStore('asistencias', {
|
|||||||
return data;
|
return data;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(`Error updating asistencia with id ${id}:`, error);
|
console.error(`Error updating asistencia with id ${id}:`, error);
|
||||||
throw error; // Re-throw to allow form to handle it
|
const errorMsg = error.response?.data?.message || error.message || 'Ocurrió un error.';
|
||||||
|
throw errorMsg; // Re-throw to allow form to handle it
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -78,7 +83,8 @@ export const useAsistenciasStore = defineStore('asistencias', {
|
|||||||
await this.fetchAsistencias(); // Refresh the list
|
await this.fetchAsistencias(); // Refresh the list
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(`Error deleting asistencia with id ${id}:`, error);
|
console.error(`Error deleting asistencia with id ${id}:`, error);
|
||||||
throw error; // Re-throw to allow handling in component
|
const errorMsg = error.response?.data?.message || error.message || 'Ocurrió un error.';
|
||||||
|
throw errorMsg; // Re-throw to allow handling in component
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|||||||
@@ -1,21 +1,18 @@
|
|||||||
import { defineStore } from 'pinia';
|
import { defineStore } from 'pinia';
|
||||||
import apiClient from '../apiClient'; // ← asegurate que tenga baseURL y headers
|
import apiClient from '../apiClient';
|
||||||
|
|
||||||
// Valor por defecto para el empleado activo
|
|
||||||
const getDefaultEmpleado = () => ({
|
const getDefaultEmpleado = () => ({
|
||||||
id: null,
|
id: null,
|
||||||
created_at: null,
|
created_at: null,
|
||||||
updated_at: null,
|
updated_at: null,
|
||||||
name: '',
|
name: '',
|
||||||
cedula: null,
|
cedula: null,
|
||||||
ubicacion: '.', // default del schema
|
ubicacion: '.',
|
||||||
grupo_estudio: null,
|
grupo_estudio: null,
|
||||||
empleado: true, // marcamos que SÍ es empleado
|
empleado: true,
|
||||||
avatar_url: null,
|
avatar_url: null,
|
||||||
telefono: null,
|
telefono: null,
|
||||||
idciat: null,
|
idciat: null,
|
||||||
// Ojo: asistencias, tareasRealizadas y planillas se manejan en otras stores
|
|
||||||
// No sé la forma exacta de esos arrays; se cargan aparte.
|
|
||||||
});
|
});
|
||||||
|
|
||||||
export const useEmpleadosStore = defineStore('empleados', {
|
export const useEmpleadosStore = defineStore('empleados', {
|
||||||
@@ -25,61 +22,66 @@ export const useEmpleadosStore = defineStore('empleados', {
|
|||||||
}),
|
}),
|
||||||
|
|
||||||
actions: {
|
actions: {
|
||||||
// ────────── CRUD básico ──────────
|
async fetchEmpleados() {
|
||||||
async fetchEmpleados () {
|
|
||||||
try {
|
try {
|
||||||
const { data } = await apiClient.get('/api/empleados');
|
const { data } = await apiClient.get('/api/empleados');
|
||||||
this.empleados = data;
|
this.empleados = data;
|
||||||
} catch (err) {
|
} catch (error) {
|
||||||
console.error('Error al traer empleados:', err);
|
console.error('Error al traer empleados:', error);
|
||||||
|
const errorMsg = error.response?.data?.message || error.message || 'Ocurrió un error.';
|
||||||
|
throw errorMsg;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
async fetchEmpleadoById (id) {
|
async fetchEmpleadoById(id) {
|
||||||
try {
|
try {
|
||||||
const { data } = await apiClient.get(`/api/empleados/${id}`);
|
const { data } = await apiClient.get(`/api/empleados/${id}`);
|
||||||
this.currentEmpleado = data;
|
this.currentEmpleado = data;
|
||||||
} catch (err) {
|
} catch (error) {
|
||||||
console.error(`Error al traer empleado ${id}:`, err);
|
console.error(`Error al traer empleado ${id}:`, error);
|
||||||
this.currentEmpleado = getDefaultEmpleado();
|
this.currentEmpleado = getDefaultEmpleado();
|
||||||
|
const errorMsg = error.response?.data?.message || error.message || 'Ocurrió un error.';
|
||||||
|
throw errorMsg;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
async createEmpleado (empleadoData) {
|
async createEmpleado(empleadoData) {
|
||||||
try {
|
try {
|
||||||
const {data} = await apiClient.post('/api/empleados/', empleadoData);
|
const { data } = await apiClient.post('/api/empleados/', empleadoData);
|
||||||
await this.fetchEmpleados();
|
await this.fetchEmpleados();
|
||||||
return data
|
return data;
|
||||||
} catch (err) {
|
} catch (error) {
|
||||||
console.error('Error creando empleado:', err);
|
console.error('Error creando empleado:', error);
|
||||||
throw err; // para que el form muestre feedback
|
const errorMsg = error.response?.data?.message || error.message || 'Ocurrió un error.';
|
||||||
|
throw errorMsg;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
async updateEmpleado (id, empleadoData) {
|
async updateEmpleado(id, empleadoData) {
|
||||||
try {
|
try {
|
||||||
const {data} = await apiClient.put(`/api/empleados/${id}`, empleadoData);
|
const { data } = await apiClient.put(`/api/empleados/${id}`, empleadoData);
|
||||||
await this.fetchEmpleados();
|
await this.fetchEmpleados();
|
||||||
this.clearCurrentEmpleado();
|
this.clearCurrentEmpleado();
|
||||||
return data
|
return data;
|
||||||
} catch (err) {
|
} catch (error) {
|
||||||
console.error(`Error actualizando empleado ${id}:`, err);
|
console.error(`Error actualizando empleado ${id}:`, error);
|
||||||
throw err;
|
const errorMsg = error.response?.data?.message || error.message || 'Ocurrió un error.';
|
||||||
|
throw errorMsg;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
async deleteEmpleado (id) {
|
async deleteEmpleado(id) {
|
||||||
try {
|
try {
|
||||||
await apiClient.delete(`/api/empleados/${id}`);
|
await apiClient.delete(`/api/empleados/${id}`);
|
||||||
await this.fetchEmpleados();
|
await this.fetchEmpleados();
|
||||||
} catch (err) {
|
} catch (error) {
|
||||||
console.error(`Error eliminando empleado ${id}:`, err);
|
console.error(`Error eliminando empleado ${id}:`, error);
|
||||||
throw err;
|
const errorMsg = error.response?.data?.message || error.message || 'Ocurrió un error.';
|
||||||
|
throw errorMsg;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
// ────────── helpers ──────────
|
clearCurrentEmpleado() {
|
||||||
clearCurrentEmpleado () {
|
|
||||||
this.currentEmpleado = getDefaultEmpleado();
|
this.currentEmpleado = getDefaultEmpleado();
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -24,8 +24,10 @@ export const usePlanillasStore = defineStore('planillas', {
|
|||||||
try {
|
try {
|
||||||
const { data } = await apiClient.get('/api/planillas')
|
const { data } = await apiClient.get('/api/planillas')
|
||||||
this.planillas = data
|
this.planillas = data
|
||||||
} catch (e) {
|
} catch (error) {
|
||||||
console.error('Error fetching planillas:', e)
|
console.error('Error fetching planillas:', error)
|
||||||
|
const errorMsg = error.response?.data?.message || error.message || 'Ocurrió un error.'
|
||||||
|
throw errorMsg
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -33,9 +35,11 @@ export const usePlanillasStore = defineStore('planillas', {
|
|||||||
try {
|
try {
|
||||||
const { data } = await apiClient.get(`/api/planillas/${id}`)
|
const { data } = await apiClient.get(`/api/planillas/${id}`)
|
||||||
this.currentPlanilla = data
|
this.currentPlanilla = data
|
||||||
} catch (e) {
|
} catch (error) {
|
||||||
console.error(`Error fetching planilla ${id}:`, e)
|
console.error(`Error fetching planilla ${id}:`, error)
|
||||||
this.currentPlanilla = getDefaultCurrentPlanilla()
|
this.currentPlanilla = getDefaultCurrentPlanilla()
|
||||||
|
const errorMsg = error.response?.data?.message || error.message || 'Ocurrió un error.'
|
||||||
|
throw errorMsg
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -44,9 +48,10 @@ export const usePlanillasStore = defineStore('planillas', {
|
|||||||
const { data } = await apiClient.post('/api/planillas', planillaData)
|
const { data } = await apiClient.post('/api/planillas', planillaData)
|
||||||
await this.fetchPlanillas()
|
await this.fetchPlanillas()
|
||||||
return data
|
return data
|
||||||
} catch (e) {
|
} catch (error) {
|
||||||
console.error('Error creating planilla:', e)
|
console.error('Error creating planilla:', error)
|
||||||
throw e
|
const errorMsg = error.response?.data?.message || error.message || 'Ocurrió un error.'
|
||||||
|
throw errorMsg
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -56,9 +61,10 @@ export const usePlanillasStore = defineStore('planillas', {
|
|||||||
await this.fetchPlanillas()
|
await this.fetchPlanillas()
|
||||||
this.clearCurrentPlanilla()
|
this.clearCurrentPlanilla()
|
||||||
return data
|
return data
|
||||||
} catch (e) {
|
} catch (error) {
|
||||||
console.error(`Error updating planilla ${id}:`, e)
|
console.error(`Error updating planilla ${id}:`, error)
|
||||||
throw e
|
const errorMsg = error.response?.data?.message || error.message || 'Ocurrió un error.'
|
||||||
|
throw errorMsg
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -66,9 +72,10 @@ export const usePlanillasStore = defineStore('planillas', {
|
|||||||
try {
|
try {
|
||||||
await apiClient.delete(`/api/planillas/${id}`)
|
await apiClient.delete(`/api/planillas/${id}`)
|
||||||
await this.fetchPlanillas()
|
await this.fetchPlanillas()
|
||||||
} catch (e) {
|
} catch (error) {
|
||||||
console.error(`Error deleting planilla ${id}:`, e)
|
console.error(`Error deleting planilla ${id}:`, error)
|
||||||
throw e
|
const errorMsg = error.response?.data?.message || error.message || 'Ocurrió un error.'
|
||||||
|
throw errorMsg
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|||||||
@@ -1,18 +1,17 @@
|
|||||||
import { defineStore } from 'pinia';
|
import { defineStore } from 'pinia';
|
||||||
import apiClient from '../apiClient'; // Assuming apiClient is configured
|
import apiClient from '../apiClient';
|
||||||
|
|
||||||
// Helper function to get default values for currentTarea
|
// Helper function to get default values for currentTarea
|
||||||
const getDefaultCurrentTarea = () => ({
|
const getDefaultCurrentTarea = () => ({
|
||||||
id: null,
|
id: null,
|
||||||
empleado_id: null,
|
empleado_id: null,
|
||||||
planilla_id: null, // Optional
|
planilla_id: null,
|
||||||
titulo: '',
|
titulo: '',
|
||||||
precio: null, // Optional
|
precio: null,
|
||||||
estado: 'pendiente', // Default from schema
|
estado: 'pendiente',
|
||||||
observacion: '', // Optional
|
observacion: '',
|
||||||
fecha: null, // Should be a date
|
fecha: null,
|
||||||
tipo: '', // Default from schema
|
tipo: '',
|
||||||
// fecha_anulado is not typically part of creation/edit form directly
|
|
||||||
});
|
});
|
||||||
|
|
||||||
export const useTareasStore = defineStore('tareas', {
|
export const useTareasStore = defineStore('tareas', {
|
||||||
@@ -27,7 +26,8 @@ export const useTareasStore = defineStore('tareas', {
|
|||||||
this.tareas = response.data;
|
this.tareas = response.data;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error fetching tareas:', error);
|
console.error('Error fetching tareas:', error);
|
||||||
// Consider more sophisticated error handling (e.g., user notifications)
|
const errorMsg = error.response?.data?.message || error.message || 'Ocurrió un error.';
|
||||||
|
throw errorMsg;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -37,44 +37,48 @@ export const useTareasStore = defineStore('tareas', {
|
|||||||
this.currentTarea = response.data;
|
this.currentTarea = response.data;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(`Error fetching tarea with id ${id}:`, error);
|
console.error(`Error fetching tarea with id ${id}:`, error);
|
||||||
this.currentTarea = getDefaultCurrentTarea(); // Reset on error
|
this.currentTarea = getDefaultCurrentTarea();
|
||||||
|
const errorMsg = error.response?.data?.message || error.message || 'Ocurrió un error.';
|
||||||
|
throw errorMsg;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
async createTarea(tareaData) {
|
async createTarea(tareaData) {
|
||||||
try {
|
try {
|
||||||
const { data } = await apiClient.post('/api/tareas', tareaData);
|
const { data } = await apiClient.post('/api/tareas', tareaData);
|
||||||
await this.fetchTareas(); // Refresh the list
|
await this.fetchTareas();
|
||||||
return data; // Return created tarea data if needed
|
return data;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error creating tarea:', error);
|
console.error('Error creating tarea:', error);
|
||||||
throw error; // Re-throw to allow form to handle it
|
const errorMsg = error.response?.data?.message || error.message || 'Ocurrió un error.';
|
||||||
|
throw errorMsg;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
async updateTarea(id, tareaData) {
|
async updateTarea(id, tareaData) {
|
||||||
try {
|
try {
|
||||||
const { data } = await apiClient.put(`/api/tareas/${id}`, tareaData);
|
const { data } = await apiClient.put(`/api/tareas/${id}`, tareaData);
|
||||||
await this.fetchTareas(); // Refresh the list
|
await this.fetchTareas();
|
||||||
this.currentTarea = getDefaultCurrentTarea(); // Reset currentTarea
|
this.currentTarea = getDefaultCurrentTarea();
|
||||||
return data; // Return updated tarea data if needed
|
return data;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(`Error updating tarea with id ${id}:`, error);
|
console.error(`Error updating tarea with id ${id}:`, error);
|
||||||
throw error; // Re-throw to allow form to handle it
|
const errorMsg = error.response?.data?.message || error.message || 'Ocurrió un error.';
|
||||||
|
throw errorMsg;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
async deleteTarea(id) {
|
async deleteTarea(id) {
|
||||||
try {
|
try {
|
||||||
await apiClient.delete(`/api/tareas/${id}`);
|
await apiClient.delete(`/api/tareas/${id}`);
|
||||||
await this.fetchTareas(); // Refresh the list
|
await this.fetchTareas();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(`Error deleting tarea with id ${id}:`, error);
|
console.error(`Error deleting tarea with id ${id}:`, error);
|
||||||
throw error; // Re-throw to allow handling in component
|
const errorMsg = error.response?.data?.message || error.message || 'Ocurrió un error.';
|
||||||
|
throw errorMsg;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
// Action to clear currentTarea, useful when navigating away from a form
|
|
||||||
clearCurrentTarea() {
|
clearCurrentTarea() {
|
||||||
this.currentTarea = getDefaultCurrentTarea();
|
this.currentTarea = getDefaultCurrentTarea();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -191,8 +191,7 @@ const handleSubmit = async () => {
|
|||||||
router.push({ name: 'asistencias-index' });
|
router.push({ name: 'asistencias-index' });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error saving asistencia:', error);
|
console.error('Error saving asistencia:', error);
|
||||||
const errorMsg = error.response?.data?.message || error.message || 'Ocurrió un error.';
|
alert(`Error al guardar la asistencia: ${error}`);
|
||||||
alert(`Error al guardar la asistencia: ${errorMsg}`);
|
|
||||||
} finally {
|
} finally {
|
||||||
isSubmitting.value = false;
|
isSubmitting.value = false;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user