132 lines
3.6 KiB
JavaScript
132 lines
3.6 KiB
JavaScript
import { defineStore } from 'pinia';
|
|
import api from '../services/api';
|
|
import router from '../routes/router';
|
|
export const useServiceInstanceStore = defineStore('servic_instance', {
|
|
state: () => ({
|
|
service_instance: [],
|
|
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 fetchServiceInstance() {
|
|
this.loading = true;
|
|
this.error = null;
|
|
|
|
try {
|
|
const response = await api.get('/service_instance/', {
|
|
params: {}
|
|
})
|
|
|
|
return response.data;
|
|
} 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;
|
|
}
|
|
},
|
|
/**
|
|
* Busca o ID da instância de serviço pelo nome
|
|
*/
|
|
async fetchServiceInstanceIdByName(nome) {
|
|
this.loading = true;
|
|
this.error = null;
|
|
|
|
try {
|
|
const response = await api.get(`/service_instance/get-id-by-name/${nome}`, {
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
}
|
|
});
|
|
|
|
return response.data.id;
|
|
} 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 ID da instância de serviço';
|
|
console.error('Erro ao buscar ID da instância de serviço', error);
|
|
throw error;
|
|
} finally {
|
|
this.loading = false;
|
|
}
|
|
},
|
|
|
|
|
|
/**
|
|
* Busca um turno específico pelo ID
|
|
*/
|
|
async createServiceInstance(service_instance) {
|
|
const url = '/service_instance/';
|
|
this.loading = true;
|
|
this.error = null;
|
|
|
|
try {
|
|
const response = await api.post(url, service_instance, {
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
}
|
|
});
|
|
|
|
this.service_instance.push(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;
|
|
}
|
|
},
|
|
async deleteServiceInstance(service_instance_id) {
|
|
this.loading = true;
|
|
this.error = null;
|
|
|
|
try {
|
|
const response = await api.delete(`/service_instance/${service_instance_id}`);
|
|
return response.data;
|
|
} 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 deletar instância de serviço';
|
|
console.error('Erro ao deletar instância de serviço', error);
|
|
throw error;
|
|
} finally {
|
|
this.loading = false;
|
|
}
|
|
}
|
|
},
|
|
}); |