206 lines
6.1 KiB
JavaScript
206 lines
6.1 KiB
JavaScript
import { defineStore } from 'pinia';
|
|
import api from '../services/api';
|
|
import router from '../routes/router';
|
|
import { useAuthStore } from './auth';
|
|
|
|
export const useShiftStore = defineStore('shifts', {
|
|
state: () => ({
|
|
shifts: [],
|
|
total: 0,
|
|
page: 1,
|
|
perPage: 10,
|
|
error: null,
|
|
loading: false,
|
|
}),
|
|
|
|
getters: {
|
|
currentPage(state) {
|
|
return state.page;
|
|
},
|
|
totalPages(state) {
|
|
return Math.ceil(state.total / state.perPage);
|
|
},
|
|
},
|
|
|
|
actions: {
|
|
/**
|
|
* Busca os turnos (shifts) registrados na API
|
|
*/
|
|
async fetchShifts() {
|
|
const authStore = useAuthStore();
|
|
const serviceInstanceId = authStore.service_instance_id;
|
|
|
|
if (!serviceInstanceId) {
|
|
this.error = 'Nenhuma instância de serviço selecionada';
|
|
return;
|
|
}
|
|
|
|
this.loading = true;
|
|
this.error = null;
|
|
|
|
try {
|
|
const response = await api.get('/shift/', {
|
|
params: {}
|
|
});
|
|
console.log('Requisição de turnos:', response.data);
|
|
console.log('ID da instância de serviço:', serviceInstanceId); // Exibe o ID da instância de serviço no console
|
|
// Filtra os shifts que pertencem à serviceInstanceId
|
|
const filteredShifts = response.data.filter(shift => {
|
|
return String(shift.service_instance_id) === String(serviceInstanceId);
|
|
});
|
|
console.log('Turnos filtrados:', filteredShifts); // Exibe os turnos filtrados no console
|
|
this.$patch({
|
|
shifts: filteredShifts,
|
|
//total: response.data,
|
|
});
|
|
|
|
console.log('Shifts filtrados:', filteredShifts);
|
|
return filteredShifts;
|
|
} catch (error) {
|
|
const status = error?.response?.status;
|
|
|
|
if (status === 401) {
|
|
console.warn('Não autorizado. Redirecionando para login...');
|
|
router.push('/login');
|
|
}
|
|
|
|
this.error = error?.response?.data?.message || error.message || 'Erro ao buscar turnos';
|
|
console.error('Erro ao buscar turnos', error);
|
|
throw error;
|
|
} finally {
|
|
this.loading = false;
|
|
}
|
|
},
|
|
|
|
async fetchShiftById(shift_id) {
|
|
const url = `/shift/${shift_id}`;
|
|
this.loading = true;
|
|
this.error = null;
|
|
|
|
try {
|
|
const response = await api.get(url);
|
|
console.log('requisicao: ',response.data);
|
|
return response.data;
|
|
} catch (error) {
|
|
this.error = error?.response?.data?.message || error.message || 'Erro ao buscar turno';
|
|
console.error('Erro ao buscar turno', error);
|
|
throw error;
|
|
} finally {
|
|
this.loading = false;
|
|
}
|
|
},
|
|
|
|
/**
|
|
* Cria um novo turno
|
|
*/
|
|
async createShift(shiftData) {
|
|
const authStore = useAuthStore();
|
|
const url = '/shift/';
|
|
this.loading = true;
|
|
this.error = null;
|
|
|
|
try {
|
|
// Validar service_instance_id
|
|
if (!authStore.service_instance_id) {
|
|
throw new Error('Nenhuma instância de serviço selecionada');
|
|
}
|
|
|
|
// Construir payload completo conforme esperado pela API
|
|
const payload = {
|
|
name: shiftData.name,
|
|
start_time: shiftData.start_time,
|
|
end_time: shiftData.end_time,
|
|
interval_start: shiftData.interval_start,
|
|
interval_end: shiftData.interval_end,
|
|
type_interval: shiftData.type_interval, // "manual" ou "automatic" dependendo da checkbox
|
|
description: shiftData.description || '',
|
|
tolerance: shiftData.tolerance || 0,
|
|
service_instance_id: authStore.service_instance_id,
|
|
time_schedule_ids: shiftData.time_schedule_ids || []
|
|
};
|
|
|
|
console.log('Enviando dados para a API:', payload);
|
|
|
|
// Requisição POST com payload completo
|
|
const response = await api.post(url, payload, {
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
}
|
|
});
|
|
|
|
// Atualizar estado local
|
|
const newShift = {
|
|
...response.data,
|
|
id: response.data.id,
|
|
service_instance_id: authStore.service_instance_id
|
|
};
|
|
|
|
this.shifts.push(newShift);
|
|
|
|
console.log('Turno criado com sucesso:', response.data);
|
|
return response.data;
|
|
} catch (error) {
|
|
this.error = error?.response?.data?.message ||
|
|
error.message ||
|
|
'Erro ao criar turno';
|
|
console.error('Erro ao criar turno:', error);
|
|
throw error;
|
|
} finally {
|
|
this.loading = false;
|
|
}
|
|
},
|
|
|
|
/**
|
|
* Atualiza os dados de um turno
|
|
*/
|
|
async updateShift(id, shift) {
|
|
const url = `/shift/${id}`;
|
|
this.loading = true;
|
|
this.error = null;
|
|
|
|
try {
|
|
const response = await api.put(url, {
|
|
name: shift.name,
|
|
days: shift.days,
|
|
break_time: shift.break_time,
|
|
break_type: shift.break_type,
|
|
status: shift.status,
|
|
});
|
|
|
|
const index = this.shifts.findIndex((s) => s.id === id);
|
|
if (index !== -1) {
|
|
this.shifts[index] = response.data;
|
|
}
|
|
|
|
return response.data;
|
|
} catch (error) {
|
|
this.error = error?.response?.data?.message || error.message || 'Erro ao atualizar turno';
|
|
console.error('Erro ao atualizar turno', error);
|
|
throw error;
|
|
} finally {
|
|
this.loading = false;
|
|
}
|
|
},
|
|
|
|
/**
|
|
* Exclui um turno
|
|
*/
|
|
async deleteShift(id) {
|
|
const url = `/shift/${id}`;
|
|
this.loading = true;
|
|
this.error = null;
|
|
|
|
try {
|
|
await api.delete(url);
|
|
this.shifts = this.shifts.filter((shift) => shift.id !== id);
|
|
} catch (error) {
|
|
this.error = error?.response?.data?.message || error.message || 'Erro ao excluir turno';
|
|
console.error('Erro ao excluir turno', error);
|
|
throw error;
|
|
} finally {
|
|
this.loading = false;
|
|
}
|
|
},
|
|
},
|
|
});
|