update company & crud budgets

This commit is contained in:
Alexandro Uc Santos
2023-12-30 14:41:07 -06:00
parent 7afd77b218
commit 25528cae8f
12 changed files with 380 additions and 48 deletions

View File

@@ -17,7 +17,6 @@ export const useAuthStore = defineStore('auth', () => {
const user = ref(null)
onMounted( async() => {
console.log('Se ejecuta onMounted auth');
checkSession();
});

View File

@@ -1,6 +1,6 @@
import { defineStore } from "pinia";
import { ref } from "vue";
import { getBudgets, getCompany } from "../services/company";
import { getBudgets, getCompany, updateBudget, updateCompany, deleteBudget, createBudget } from "../services/company";
import api from "../lib/axios";
export const useCompanyStore = defineStore('company', () => {
@@ -22,9 +22,24 @@ export const useCompanyStore = defineStore('company', () => {
loading.value = false;
}
const editCompany = async(formData) => {
const data = await updateCompany(company.value._id, formData);
if(data === null) {
return 'Algo salio mal, intente más tarde';
} else {
company.value = {
...company.value,
...formData,
};
console.log(company.value);
return 'success';
}
}
const clear = () => {
company.value = null;
companyid.value = null;
companyid = null;
loading.value = false;
}
@@ -34,7 +49,6 @@ export const useCompanyStore = defineStore('company', () => {
const getProposalsCompany = async() => {
try {
const endpoint = `/proposals?carrier=${companyid}`;
console.log(endpoint);
const {data} = await api.get(endpoint);
proposals.value = data.data;
console.log(data);
@@ -54,8 +68,6 @@ export const useCompanyStore = defineStore('company', () => {
filterStr ="?"+cleanfilterArr.join("&");
}
console.log(filterStr);
if(budgets.value.length <= 0 || reload === true) {
try {
const data = await getBudgets(filterStr);
@@ -70,11 +82,65 @@ export const useCompanyStore = defineStore('company', () => {
}
}
const updateBudgetCompany = async(id, formData) => {
try {
const data = await updateBudget(id, formData);
console.log(data);
if(data) {
const index = budgets.value.findIndex((budget) => budget._id === id);
budgets.value[index] = {
...budgets.value[index],
...data,
material: udgets.value[index].material
};
return data;
} else {
return null;
}
} catch (error) {
return null;
}
}
const createBudgetCompany = async(formData) => {
try {
const data = await createBudget(formData);
console.log(data);
if(data) {
budgets.value.push(data);
return data;
} else {
return null;
}
} catch (error) {
return null;
}
}
const deleteBudgetCompany = async(id) => {
try {
const data = await deleteBudget(id);
console.log(data);
if(data) {
budgets.value = budgets.value.filter(budget => budget._id !== id);
return data;
} else {
return null;
}
} catch (error) {
return null;
}
}
return {
getCompanyData,
getProposalsCompany,
getBudgetsCompany,
editCompany,
updateBudgetCompany,
createBudgetCompany,
deleteBudgetCompany,
budgets,
clear,
loading,