462 lines
15 KiB
JavaScript
462 lines
15 KiB
JavaScript
import { defineStore } from "pinia";
|
|
import { ref } from "vue";
|
|
import { getBudgets, getCompany, updateBudget, updateCompany, deleteBudget, createBudget, getUsers, updateUser, createUser, deleteUser, getLocations, createLocation, updateLocation, deleteLocation } from "../services/company";
|
|
import api from "../lib/axios";
|
|
import { saveProposal, updateProposal } from "../services/vehicles";
|
|
import { useI18n } from "vue-i18n";
|
|
|
|
export const useCompanyStore = defineStore('company', () => {
|
|
|
|
const { t } = useI18n();
|
|
const company = ref(null)
|
|
const users = ref([]);
|
|
const drivers = ref([]);
|
|
const usersTotal = ref(0);
|
|
const usersCurrentPage = ref(0);
|
|
const budgets = ref([]);
|
|
const budgetsTotal = ref(0);
|
|
const budgetsCurrentPage = ref(0);
|
|
const locations = ref([]);
|
|
const locationsLoad = ref([]);
|
|
const locationsDowload = ref([]);
|
|
const locationsTotal = ref(0);
|
|
const locationType = ref({value: '', label: t('labels.alls')})
|
|
const locationsCurrentPage = ref(0);
|
|
const proposals = ref([]);
|
|
const proposalsTotal = ref(0);
|
|
const proposalsCurrentPage = ref(0)
|
|
const loading = ref(false);
|
|
|
|
const getCompanyData = async() => {
|
|
const companyId = localStorage.getItem('id');
|
|
loading.value = true;
|
|
if(!company.value) {
|
|
loading.value = true;
|
|
const resp = await getCompany();
|
|
company.value = resp;
|
|
}
|
|
loading.value = false;
|
|
}
|
|
|
|
const getUsersCompany = async(limit = 1, page = 0, reload = false) => {
|
|
const companyId = localStorage.getItem('id');
|
|
if(users.value.length <= 0 || reload === true) {
|
|
// const filter = `company=${companyId}`;
|
|
const filter = `company=${companyId}&elements=${limit}&page=${page}`;
|
|
const resp = await getUsers(filter);
|
|
if(resp !== null && resp.total > 0) {
|
|
usersTotal.value = resp.total;
|
|
users.value = resp.data;
|
|
} else {
|
|
users.value = [];
|
|
}
|
|
}
|
|
}
|
|
|
|
const getDrivers = async() => {
|
|
const companyId = localStorage.getItem('id');
|
|
if(drivers.value.length <= 0) {
|
|
const filter = `company=${companyId}&elements=100&job_role=driver`;
|
|
const resp = await getUsers(filter);
|
|
if(resp !== null && resp.total > 0) {
|
|
drivers.value = resp.data;
|
|
} else {
|
|
drivers.value = [];
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
const createUserCompany = async(formData, localData) => {
|
|
const data = await createUser(formData);
|
|
if(data) {
|
|
users.value.unshift({
|
|
...data,
|
|
...localData
|
|
});
|
|
usersTotal.value++;
|
|
if(data.job_role === 'driver' && drivers.value.length > 0) {
|
|
drivers.value.unshift({
|
|
...data,
|
|
...localData
|
|
})
|
|
}
|
|
return 'success';
|
|
} else {
|
|
return t('errors.generic');
|
|
}
|
|
}
|
|
|
|
const updateUserCompany = async(id, formData, localData) => {
|
|
const data = await updateUser(id, formData);
|
|
if(data) {
|
|
const index = users.value.findIndex((user) => user._id === id); /// Actualizamos la lista de usuarios globales
|
|
if(index !== -1) {
|
|
users.value[index] = {
|
|
...users.value[index],
|
|
...data,
|
|
...localData
|
|
};
|
|
}
|
|
if(data.job_role === 'driver' && drivers.value.length > 0) { // Actualizamos en la lista drivers
|
|
const index = drivers.value.findIndex((user) => user._id === id);
|
|
if(index !== -1) {
|
|
drivers.value[index] = {
|
|
...drivers.value[index],
|
|
...data,
|
|
...localData
|
|
};
|
|
}
|
|
}
|
|
return 'success';
|
|
} else {
|
|
return t('errors.generic');
|
|
}
|
|
}
|
|
|
|
const deleteUserCompany = async(id) => {
|
|
const data = await deleteUser(id);
|
|
if(data) {
|
|
users.value = users.value.filter(user => user._id !== id);
|
|
if(data.job_role === 'driver' && drivers.value.length > 0) {
|
|
drivers.value = drivers.value.filter(user => user._id !== id);
|
|
}
|
|
return 'success';
|
|
} else {
|
|
return t('errors.generic');
|
|
}
|
|
}
|
|
|
|
const editCompany = async(formData) => {
|
|
const data = await updateCompany(formData);
|
|
if(data === null) {
|
|
return t('errors.generic');
|
|
} else {
|
|
company.value = {
|
|
...company.value,
|
|
...formData,
|
|
};
|
|
|
|
return 'success';
|
|
}
|
|
}
|
|
|
|
const clear = () => { //Cuando se cierra la sesion
|
|
company.value = null;
|
|
users.value = [];
|
|
drivers.value = [];
|
|
usersTotal.value = 0;
|
|
usersCurrentPage.value = 0;
|
|
budgets.value = [];
|
|
budgetsTotal.value = 0;
|
|
budgetsCurrentPage.value = 0;
|
|
locations.value = [];
|
|
locationsLoad.value = [];
|
|
locationsTotal.value = 0;
|
|
locationsCurrentPage.value = 0;
|
|
proposals.value = [];
|
|
proposalsTotal.value = 0;
|
|
proposalsCurrentPage.value = 0;
|
|
loading.value = false;
|
|
}
|
|
|
|
const $reset = () => {
|
|
company.value = null;
|
|
loading.value = false;
|
|
}
|
|
|
|
const getProposalsCompany = async(filter, reload = false) => {
|
|
const companyId = localStorage.getItem('id');
|
|
try {
|
|
if(proposals.value.length <= 0 || reload) {
|
|
const endpoint = `/v1/proposals/find?carrier=${companyId}&${filter}&$sort%5BcreatedAt%5D=-1`;
|
|
const {data} = await api.get(endpoint);
|
|
proposals.value = data.data;
|
|
proposalsTotal.value = data.total;
|
|
}
|
|
} catch (error) {
|
|
proposals.value = [];
|
|
proposalsTotal.value = 0;
|
|
}
|
|
}
|
|
|
|
const createPropsal = async(formData) => {
|
|
const data = await saveProposal(formData);
|
|
if(data) {
|
|
proposalsTotal.value++;
|
|
return 'success';
|
|
} else {
|
|
return t('errors.generic');
|
|
}
|
|
}
|
|
|
|
const updatePropsalLoad = async(id, formData, localData) => {
|
|
const data = await updateProposal(id, formData);
|
|
if(data) {
|
|
const index = proposals.value.findIndex((prop) => prop._id === id);
|
|
proposals.value[index] = {
|
|
...proposals.value[index],
|
|
...data,
|
|
...localData
|
|
};
|
|
return 'success';
|
|
} else {
|
|
return t('errors.generic');
|
|
}
|
|
}
|
|
|
|
const getBudgetsCompany = async(filterQuery, reload = false) => { //here
|
|
let filterArr = Object.values(filterQuery);
|
|
|
|
let cleanfilterArr = filterArr.filter(n=>n);
|
|
var filterStr = "";
|
|
if(cleanfilterArr.length >0){
|
|
filterStr ="?"+cleanfilterArr.join("&");
|
|
}
|
|
|
|
if(budgets.value.length <= 0 || reload === true) {
|
|
try {
|
|
const data = await getBudgets(filterStr);
|
|
if(data.total > 0) {
|
|
budgets.value = data.data;
|
|
budgetsTotal.value = data.total;
|
|
} else {
|
|
budgetsTotal.value = 0;
|
|
budgets.value = [];
|
|
}
|
|
} catch (error) {
|
|
budgets.value = [];
|
|
}
|
|
}
|
|
}
|
|
|
|
const updateBudgetCompany = async(id, formData, localData) => {
|
|
try {
|
|
const data = await updateBudget(id, formData);
|
|
if(data) {
|
|
const index = budgets.value.findIndex((budget) => budget._id === id);
|
|
budgets.value[index] = {
|
|
...budgets.value[index],
|
|
...data,
|
|
...localData
|
|
};
|
|
return 'success';
|
|
} else {
|
|
return 'No se pudo actualizar presupuesto, intente mas tarde';
|
|
}
|
|
} catch (error) {
|
|
return t('errors.generic');
|
|
}
|
|
}
|
|
|
|
const createBudgetCompany = async(formData, localData) => {
|
|
try {
|
|
const data = await createBudget(formData);
|
|
if(data) {
|
|
budgetsTotal.value++;
|
|
budgets.value.push({
|
|
...data,
|
|
...localData
|
|
});
|
|
return 'success';
|
|
} else {
|
|
return 'No se pudo agregar presupuesto, intente mas tarde';
|
|
}
|
|
} catch (error) {
|
|
return t('errors.generic');
|
|
}
|
|
}
|
|
|
|
const deleteBudgetCompany = async(id) => {
|
|
try {
|
|
const data = await deleteBudget(id);
|
|
if(data) {
|
|
budgetsTotal.value--;
|
|
budgets.value = budgets.value.filter(budget => budget._id !== id);
|
|
return data;
|
|
} else {
|
|
return null;
|
|
}
|
|
} catch (error) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
const getLocationsCompany = async(filterQuery, reload = false) => {
|
|
let filterArr = Object.values(filterQuery);
|
|
|
|
let cleanfilterArr = filterArr.filter(n=>n);
|
|
var filterStr = "";
|
|
if(cleanfilterArr.length > 0){
|
|
filterStr ="?"+cleanfilterArr.join("&");
|
|
}
|
|
if(locations.value.length <= 0 || reload === true) {
|
|
const resp = await getLocations(filterStr);
|
|
if(resp !== null && resp.total > 0) {
|
|
locations.value = resp.data;
|
|
locationsTotal.value = resp.total;
|
|
} else {
|
|
locations.value = [];
|
|
}
|
|
}
|
|
}
|
|
|
|
const getLocationsLoads = async(type) => {
|
|
// console.log(type);
|
|
let locationsType = (type === 'loading') ? locationsLoad.value : locationsDowload.value;
|
|
// console.log(locationsType);
|
|
if(locationsType.length <= 0) {
|
|
const filterStr = "?company="+ localStorage.getItem('id') + '&elements=100';
|
|
const respBoth = await getLocations(filterStr + '&type=' + 'both');
|
|
const resp = await getLocations(filterStr + '&type=' + type);
|
|
if(resp !== null && resp.total > 0) {
|
|
locationsType = [...respBoth.data, ...resp.data];
|
|
if(type === 'loading') {
|
|
locationsLoad.value = [
|
|
...respBoth.data,
|
|
...resp.data
|
|
];
|
|
} else {
|
|
locationsDowload.value = [
|
|
...respBoth.data,
|
|
...resp.data
|
|
];
|
|
}
|
|
} else {
|
|
if(type === 'loading') {
|
|
locationsLoad.value = [];
|
|
} else {
|
|
locationsDowload.value = [];
|
|
}
|
|
locationsType = [];
|
|
}
|
|
}
|
|
return locationsType;
|
|
}
|
|
|
|
const createLocationCompany = async(formData, localData) => {
|
|
const data = await createLocation(formData);
|
|
if(data?.data) {
|
|
locations.value.unshift({
|
|
...data?.data,
|
|
...localData
|
|
});
|
|
locationsTotal.value++;
|
|
locationsLoad.value.unshift({
|
|
...data?.data,
|
|
...localData
|
|
})
|
|
if(data?.data.type === 'both' || data?.data.type === 'loading' && locationsLoad.value.length > 0) {
|
|
locationsLoad.value.unshift({
|
|
...data?.data,
|
|
...localData
|
|
})
|
|
} else if (data?.data.type == 'both' || data?.data.type === 'unloading' && locationsDowload.value.length > 0) {
|
|
locationsDowload.value.unshift({
|
|
...data?.data,
|
|
...localData
|
|
})
|
|
}
|
|
return 'success';
|
|
} else {
|
|
return t('errors.generic');
|
|
}
|
|
}
|
|
|
|
const updateLocationCompany = async(id, formData, localData) => {
|
|
const data = await updateLocation(id, formData);
|
|
if(data) {
|
|
const index = locations.value.findIndex((loc) => loc._id === id);
|
|
if(index !== -1) {
|
|
locations.value[index] = {
|
|
...locations.value[index],
|
|
...data,
|
|
...localData
|
|
};
|
|
if(locationsLoad.value.length > 0) {
|
|
const indexl = locationsLoad.value.findIndex((loc) => loc._id === id);
|
|
if(indexl !== -1) {
|
|
locationsLoad.value[indexl] = {
|
|
...locationsLoad.value[index],
|
|
...data,
|
|
...localData
|
|
};
|
|
}
|
|
}
|
|
if(locationsDowload.value.length > 0) {
|
|
const indexl = locationsDowload.value.findIndex((loc) => loc._id === id);
|
|
if(indexl !== -1) {
|
|
locationsDowload.value[indexl] = {
|
|
...locationsDowload.value[index],
|
|
...data,
|
|
...localData
|
|
};
|
|
}
|
|
}
|
|
}
|
|
return 'success';
|
|
} else {
|
|
return t('errors.generic');
|
|
}
|
|
}
|
|
|
|
const deleteLocationCompany = async(id) => {
|
|
const data = await deleteLocation(id);
|
|
if(data) {
|
|
locations.value = locations.value.filter(loc => loc._id !== id);
|
|
if(locationsLoad.value.length > 0) {
|
|
locationsLoad.value = locationsLoad.value.filter(loc => loc._id !== id);
|
|
}
|
|
if(locationsDowload.value.length > 0) {
|
|
locationsDowload.value = locationsDowload.value.filter(loc => loc._id !== id);
|
|
}
|
|
|
|
return 'success';
|
|
} else {
|
|
return t('errors.generic');
|
|
}
|
|
}
|
|
|
|
|
|
return {
|
|
getCompanyData,
|
|
getProposalsCompany,
|
|
createPropsal,
|
|
updatePropsalLoad,
|
|
getBudgetsCompany,
|
|
getUsersCompany,
|
|
getDrivers,
|
|
createUserCompany,
|
|
updateUserCompany,
|
|
deleteUserCompany,
|
|
editCompany,
|
|
updateBudgetCompany,
|
|
createBudgetCompany,
|
|
deleteBudgetCompany,
|
|
getLocationsCompany,
|
|
getLocationsLoads,
|
|
createLocationCompany,
|
|
updateLocationCompany,
|
|
deleteLocationCompany,
|
|
budgets,
|
|
budgetsCurrentPage,
|
|
budgetsTotal,
|
|
users,
|
|
drivers,
|
|
usersTotal,
|
|
usersCurrentPage,
|
|
locations,
|
|
locationsLoad,
|
|
locationsDowload,
|
|
locationsTotal,
|
|
locationsCurrentPage,
|
|
locationType,
|
|
clear,
|
|
$reset,
|
|
loading,
|
|
proposals,
|
|
proposalsCurrentPage,
|
|
proposalsTotal,
|
|
company,
|
|
}
|
|
}); |