feat: update company management components and services

- Upgrade vue-tsc to version 3.2.6 in package.json.
- Refactor Companies.vue to replace address column with domains_count and update company service references.
- Modify CompaniesForm.vue to include new fields for email, primary domain, and certificate files, and adjust validation logic.
- Revamp companies.service.ts to implement new API endpoints for tenant management and improve error handling.
- Introduce companies.mapper.ts for payload transformation between form data and API requirements.
- Update companies.types.ts to reflect changes in data structure and types for better type safety.
This commit is contained in:
edgar.mendez 2026-03-21 18:04:08 -06:00
parent 6fe7c82c6d
commit 93a2527e60
9 changed files with 559 additions and 603 deletions

1
.gitignore vendored
View File

@ -30,3 +30,4 @@ dist-ssr
# Docker
docker-compose.override.yml
.agents

4
components.d.ts vendored
View File

@ -25,19 +25,19 @@ declare module 'vue' {
IconField: typeof import('primevue/iconfield')['default']
InputIcon: typeof import('primevue/inputicon')['default']
InputNumber: typeof import('primevue/inputnumber')['default']
InputSwitch: typeof import('primevue/inputswitch')['default']
InputText: typeof import('primevue/inputtext')['default']
KpiCard: typeof import('./src/components/shared/KpiCard.vue')['default']
Menu: typeof import('primevue/menu')['default']
Message: typeof import('primevue/message')['default']
Paginator: typeof import('primevue/paginator')['default']
Panel: typeof import('primevue/panel')['default']
Password: typeof import('primevue/password')['default']
ProgressSpinner: typeof import('primevue/progressspinner')['default']
RouterLink: typeof import('vue-router')['RouterLink']
RouterView: typeof import('vue-router')['RouterView']
Sidebar: typeof import('./src/components/layout/Sidebar.vue')['default']
Tag: typeof import('primevue/tag')['default']
Toast: typeof import('primevue/toast')['default']
Toolbar: typeof import('primevue/toolbar')['default']
TopBar: typeof import('./src/components/layout/TopBar.vue')['default']
}
export interface GlobalDirectives {

56
package-lock.json generated
View File

@ -27,7 +27,7 @@
"@vue/tsconfig": "^0.8.1",
"typescript": "~5.9.3",
"vite": "^7.1.7",
"vue-tsc": "^3.1.0"
"vue-tsc": "^3.2.6"
}
},
"node_modules/@babel/helper-string-parser": {
@ -1216,30 +1216,30 @@
}
},
"node_modules/@volar/language-core": {
"version": "2.4.23",
"resolved": "https://registry.npmjs.org/@volar/language-core/-/language-core-2.4.23.tgz",
"integrity": "sha512-hEEd5ET/oSmBC6pi1j6NaNYRWoAiDhINbT8rmwtINugR39loROSlufGdYMF9TaKGfz+ViGs1Idi3mAhnuPcoGQ==",
"version": "2.4.28",
"resolved": "https://registry.npmjs.org/@volar/language-core/-/language-core-2.4.28.tgz",
"integrity": "sha512-w4qhIJ8ZSitgLAkVay6AbcnC7gP3glYM3fYwKV3srj8m494E3xtrCv6E+bWviiK/8hs6e6t1ij1s2Endql7vzQ==",
"dev": true,
"license": "MIT",
"dependencies": {
"@volar/source-map": "2.4.23"
"@volar/source-map": "2.4.28"
}
},
"node_modules/@volar/source-map": {
"version": "2.4.23",
"resolved": "https://registry.npmjs.org/@volar/source-map/-/source-map-2.4.23.tgz",
"integrity": "sha512-Z1Uc8IB57Lm6k7q6KIDu/p+JWtf3xsXJqAX/5r18hYOTpJyBn0KXUR8oTJ4WFYOcDzWC9n3IflGgHowx6U6z9Q==",
"version": "2.4.28",
"resolved": "https://registry.npmjs.org/@volar/source-map/-/source-map-2.4.28.tgz",
"integrity": "sha512-yX2BDBqJkRXfKw8my8VarTyjv48QwxdJtvRgUpNE5erCsgEUdI2DsLbpa+rOQVAJYshY99szEcRDmyHbF10ggQ==",
"dev": true,
"license": "MIT"
},
"node_modules/@volar/typescript": {
"version": "2.4.23",
"resolved": "https://registry.npmjs.org/@volar/typescript/-/typescript-2.4.23.tgz",
"integrity": "sha512-lAB5zJghWxVPqfcStmAP1ZqQacMpe90UrP5RJ3arDyrhy4aCUQqmxPPLB2PWDKugvylmO41ljK7vZ+t6INMTag==",
"version": "2.4.28",
"resolved": "https://registry.npmjs.org/@volar/typescript/-/typescript-2.4.28.tgz",
"integrity": "sha512-Ja6yvWrbis2QtN4ClAKreeUZPVYMARDYZl9LMEv1iQ1QdepB6wn0jTRxA9MftYmYa4DQ4k/DaSZpFPUfxl8giw==",
"dev": true,
"license": "MIT",
"dependencies": {
"@volar/language-core": "2.4.23",
"@volar/language-core": "2.4.28",
"path-browserify": "^1.0.1",
"vscode-uri": "^3.0.8"
}
@ -1325,27 +1325,19 @@
}
},
"node_modules/@vue/language-core": {
"version": "3.1.3",
"resolved": "https://registry.npmjs.org/@vue/language-core/-/language-core-3.1.3.tgz",
"integrity": "sha512-KpR1F/eGAG9D1RZ0/T6zWJs6dh/pRLfY5WupecyYKJ1fjVmDMgTPw9wXmKv2rBjo4zCJiOSiyB8BDP1OUwpMEA==",
"version": "3.2.6",
"resolved": "https://registry.npmjs.org/@vue/language-core/-/language-core-3.2.6.tgz",
"integrity": "sha512-xYYYX3/aVup576tP/23sEUpgiEnujrENaoNRbaozC1/MA9I6EGFQRJb4xrt/MmUCAGlxTKL2RmT8JLTPqagCkg==",
"dev": true,
"license": "MIT",
"dependencies": {
"@volar/language-core": "2.4.23",
"@volar/language-core": "2.4.28",
"@vue/compiler-dom": "^3.5.0",
"@vue/shared": "^3.5.0",
"alien-signals": "^3.0.0",
"muggle-string": "^0.4.1",
"path-browserify": "^1.0.1",
"picomatch": "^4.0.2"
},
"peerDependencies": {
"typescript": "*"
},
"peerDependenciesMeta": {
"typescript": {
"optional": true
}
}
},
"node_modules/@vue/reactivity": {
@ -1468,9 +1460,9 @@
}
},
"node_modules/alien-signals": {
"version": "3.0.6",
"resolved": "https://registry.npmjs.org/alien-signals/-/alien-signals-3.0.6.tgz",
"integrity": "sha512-gCs0YqC1mkYGC6IRXsSrA62ShOSv1FlVN5tRp/Cs2vRWLK/BAeluWIdfsl253pFQPznKEvRmHhfep7crWfyfWQ==",
"version": "3.1.2",
"resolved": "https://registry.npmjs.org/alien-signals/-/alien-signals-3.1.2.tgz",
"integrity": "sha512-d9dYqZTS90WLiU0I5c6DHj/HcKkF8ZyGN3G5x8wSbslulz70KOxaqCT0hQCo9KOyhVqzqGojvNdJXoTumZOtcw==",
"dev": true,
"license": "MIT"
},
@ -2802,14 +2794,14 @@
}
},
"node_modules/vue-tsc": {
"version": "3.1.3",
"resolved": "https://registry.npmjs.org/vue-tsc/-/vue-tsc-3.1.3.tgz",
"integrity": "sha512-StMNfZHwPIXQgY3KxPKM0Jsoc8b46mDV3Fn2UlHCBIwRJApjqrSwqeMYgWf0zpN+g857y74pv7GWuBm+UqQe1w==",
"version": "3.2.6",
"resolved": "https://registry.npmjs.org/vue-tsc/-/vue-tsc-3.2.6.tgz",
"integrity": "sha512-gYW/kWI0XrwGzd0PKc7tVB/qpdeAkIZLNZb10/InizkQjHjnT8weZ/vBarZoj4kHKbUTZT/bAVgoOr8x4NsQ/Q==",
"dev": true,
"license": "MIT",
"dependencies": {
"@volar/typescript": "2.4.23",
"@vue/language-core": "3.1.3"
"@volar/typescript": "2.4.28",
"@vue/language-core": "3.2.6"
},
"bin": {
"vue-tsc": "bin/vue-tsc.js"

View File

@ -28,6 +28,6 @@
"@vue/tsconfig": "^0.8.1",
"typescript": "~5.9.3",
"vite": "^7.1.7",
"vue-tsc": "^3.1.0"
"vue-tsc": "^3.2.6"
}
}

View File

@ -60,15 +60,11 @@
</template>
</Column>
<Column field="address" header="Ubicación" style="min-width: 200px">
<template #body="{ data }">
<div v-if="data.address">
<p class="text-sm">{{ data.address.city }}</p>
<p class="text-xs text-slate-400">{{ data.address.state }}</p>
</div>
<span v-else class="text-slate-400">Sin dirección</span>
</template>
</Column>
<Column field="domains_count" header="Dominios" style="min-width: 120px">
<template #body="{ data }">
<span class="font-mono text-sm">{{ data.domains_count || 0 }}</span>
</template>
</Column>
<Column header="Acciones" :exportable="false" style="min-width: 120px">
<template #body="{ data }">
@ -132,8 +128,8 @@
import { ref, onMounted } from 'vue';
import { useToast } from 'primevue/usetoast';
import CompaniesForm from './CompaniesForm.vue';
import { companyService } from './companies.service';
import type { Company, PaginatedResponse } from './companies.types';
import { companiesService } from './companies.service';
import type { Company, TenantFormData } from './companies.types';
// Toast
const toast = useToast();
@ -146,7 +142,7 @@ const companies = ref<Company[]>([]);
const loading = ref(false);
const dialogVisible = ref(false);
const deleteDialogVisible = ref(false);
const selectedCompany = ref<Company | null>(null);
const selectedCompany = ref<Company | undefined>(undefined);
const companyToDelete = ref<Company | null>(null);
// Pagination
@ -155,37 +151,17 @@ const totalRecords = ref(0);
const perPage = ref(10);
// Search filters
const searchFilters = ref({
rfc: '',
curp: '',
company_name: ''
});
// Methods
const loadCompanies = async () => {
const loadCompanies = async (page = currentPage.value) => {
loading.value = true;
try {
const params = {
paginate: true,
page: currentPage.value,
...searchFilters.value
};
const response = await companyService.getAll(params);
if ('current_page' in response) {
// Respuesta paginada
const paginatedData = response as PaginatedResponse<Company>;
companies.value = paginatedData.data;
currentPage.value = paginatedData.current_page;
totalRecords.value = paginatedData.total;
perPage.value = paginatedData.per_page;
} else {
// Respuesta sin paginación
companies.value = response as Company[];
totalRecords.value = companies.value.length;
}
const response = await companiesService.getAll(page);
companies.value = response.data;
currentPage.value = response.current_page;
totalRecords.value = response.total;
perPage.value = response.per_page;
} catch (error) {
console.error('Error loading companies:', error);
companies.value = [];
@ -196,25 +172,25 @@ const loadCompanies = async () => {
const onPageChange = (event: any) => {
currentPage.value = event.page + 1; // PrimeVue usa índice base 0
loadCompanies();
loadCompanies(currentPage.value);
};
const openCreateDialog = () => {
selectedCompany.value = null;
selectedCompany.value = undefined;
dialogVisible.value = true;
};
const editCompany = (company: any) => {
const editCompany = (company: Company) => {
selectedCompany.value = { ...company };
dialogVisible.value = true;
};
}; // No change needed assigns a valid Company
const viewCompany = (company: any) => {
// TODO: Implementar vista de detalles
const viewCompany = (company: Company) => {
// Aquí podrías abrir un modal de detalles o navegar a una vista
console.log('View company:', company);
};
const confirmDelete = (company: any) => {
const confirmDelete = (company: Company) => {
companyToDelete.value = company;
deleteDialogVisible.value = true;
};
@ -223,7 +199,7 @@ const deleteCompany = async () => {
if (!companyToDelete.value?.id) return;
try {
await companyService.delete(companyToDelete.value.id);
await companiesService.delete(companyToDelete.value.id);
toast.add({
severity: 'success',
@ -248,76 +224,54 @@ const deleteCompany = async () => {
}
};
const handleSave = async (companyData: any) => {
const handleSave = async (companyData: TenantFormData) => {
try {
if (selectedCompany.value?.id) {
// Actualizar empresa existente
await companyService.update(selectedCompany.value.id, companyData);
const isEditMode = Boolean(selectedCompany.value?.id);
if (isEditMode && selectedCompany.value?.id) {
await companiesService.update(selectedCompany.value.id, companyData);
toast.add({
severity: 'success',
summary: 'Empresa actualizada',
detail: `La empresa "${companyData.company_name}" ha sido actualizada exitosamente`,
life: 3000
detail: `La empresa "${companyData.company_name}" se actualizó correctamente`,
life: 3000,
});
} else {
// Crear nueva empresa
await companyService.create(companyData);
await companiesService.create(companyData);
toast.add({
severity: 'success',
summary: 'Empresa creada',
detail: `La empresa "${companyData.company_name}" ha sido registrada exitosamente`,
life: 3000
detail: `La empresa "${companyData.company_name}" se creó correctamente`,
life: 3000,
});
}
// Recargar la lista después de guardar
await loadCompanies();
dialogVisible.value = false;
selectedCompany.value = null;
selectedCompany.value = undefined;
companiesFormRef.value?.resetSubmitting();
return true;
} catch (error: any) {
console.error('Error saving company:', error);
// Verificar si es un error de validación (422)
if (error.response?.status === 422 && error.response?.data?.errors) {
const backendErrors = error.response.data.errors;
// Setear los errores en el formulario
if (companiesFormRef.value) {
companiesFormRef.value.setValidationErrors(backendErrors);
}
// Mostrar toast con mensaje general
toast.add({
severity: 'warn',
summary: 'Errores de validación',
detail: error.response.data.message || 'Por favor, corrija los errores en el formulario',
life: 5000
});
} else {
// Otros errores
const errorMessage = error.response?.data?.message ||
error.response?.data?.error ||
error.message ||
'No se pudo guardar la empresa';
if (error.response?.status === 422 && error.response?.data?.errors && companiesFormRef.value) {
companiesFormRef.value.setValidationErrors(error.response.data.errors);
toast.add({
severity: 'error',
summary: selectedCompany.value?.id ? 'Error al actualizar' : 'Error al crear',
detail: errorMessage,
life: 5000
summary: 'Error de validación',
detail: 'Revisa los campos marcados en rojo',
life: 4000,
});
// Resetear loading en el formulario para otros tipos de error
if (companiesFormRef.value) {
companiesFormRef.value.setValidationErrors({});
}
return false;
}
companiesFormRef.value?.resetSubmitting();
toast.add({
severity: 'error',
summary: 'Error al guardar',
detail: error.response?.data?.message || 'No se pudo guardar la empresa',
life: 5000,
});
return false;
}
};
@ -341,4 +295,4 @@ onMounted(() => {
.space-y-6 > * + * {
margin-top: 1.5rem;
}
</style>
</style>

View File

@ -1,107 +1,158 @@
<template>
<Dialog
v-model:visible="isVisible"
:modal="true"
<Dialog
v-model:visible="isVisible"
:modal="true"
:closable="true"
:style="{ width: '900px' }"
:style="{ width: '980px' }"
:header="formTitle"
@hide="handleClose"
>
<form @submit.prevent="handleSubmit" class="space-y-6">
<!-- Información Fiscal -->
<form class="space-y-6" @submit.prevent="handleSubmit">
<div class="border-b pb-4">
<h3 class="text-lg font-semibold mb-4">Información Fiscal</h3>
<h3 class="text-lg font-semibold mb-4">Información principal</h3>
<div class="grid grid-cols-1 md:grid-cols-2 gap-4">
<div class="flex flex-col gap-2 md:col-span-2">
<label for="company_name" class="font-medium text-sm">Razón social *</label>
<InputText
id="company_name"
v-model="form.company_name"
placeholder="Empresa Demo SA de CV"
:class="{ 'p-invalid': errors.company_name }"
/>
<small v-if="errors.company_name" class="p-error text-red-600">{{ errors.company_name }}</small>
</div>
<div class="flex flex-col gap-2">
<label for="rfc" class="font-medium text-sm">RFC *</label>
<InputText
id="rfc"
v-model="form.rfc"
placeholder="ABC123456789"
:class="{ 'p-invalid': errors.rfc }"
<InputText
id="rfc"
v-model="form.rfc"
placeholder="DEMO010101ABC"
maxlength="13"
class="font-mono"
:class="{ 'p-invalid': errors.rfc }"
@input="form.rfc = form.rfc.toUpperCase()"
/>
<small v-if="errors.rfc" class="p-error text-red-600">{{ errors.rfc }}</small>
</div>
<div class="flex flex-col gap-2">
<label for="curp" class="font-medium text-sm">CURP</label>
<InputText
id="curp"
v-model="form.curp"
placeholder="XYZ987654321"
:class="{ 'p-invalid': errors.curp }"
<InputText
id="curp"
v-model="form.curp"
placeholder="XEXX010101HNEXXXA1"
maxlength="18"
class="font-mono"
:class="{ 'p-invalid': errors.curp }"
@input="form.curp = form.curp.toUpperCase()"
/>
<small v-if="errors.curp" class="p-error text-red-600">{{ errors.curp }}</small>
</div>
<div class="flex flex-col gap-2">
<label for="email" class="font-medium text-sm">Email *</label>
<InputText
id="email"
v-model="form.email"
placeholder="admin@empresa-demo.com"
:class="{ 'p-invalid': errors.email }"
/>
<small v-if="errors.email" class="p-error text-red-600">{{ errors.email }}</small>
</div>
<div class="flex flex-col gap-2">
<label for="primary_domain" class="font-medium text-sm">
Dominio primario {{ isEditMode ? '(opcional en edición)' : '*' }}
</label>
<InputText
id="primary_domain"
v-model="form.primary_domain"
placeholder="empresa-demo.local"
:disabled="isEditMode"
:class="{ 'p-invalid': errors.primary_domain }"
/>
<small v-if="errors.primary_domain" class="p-error text-red-600">{{ errors.primary_domain }}</small>
</div>
<div class="flex flex-col gap-2">
<label for="phone" class="font-medium text-sm">Teléfono</label>
<InputText id="phone" v-model="form.phone" placeholder="5551234567" />
</div>
<div class="flex flex-col gap-2">
<label for="tax_regime" class="font-medium text-sm">Régimen fiscal</label>
<InputText id="tax_regime" v-model="form.tax_regime" placeholder="601" />
</div>
<div class="flex flex-col gap-2 md:col-span-2">
<label for="company_name" class="font-medium text-sm">Razón Social *</label>
<InputText
id="company_name"
v-model="form.company_name"
placeholder="Corporativo Ejemplo S.A. de C.V."
:class="{ 'p-invalid': errors.company_name }"
<label for="legal_representative" class="font-medium text-sm">Representante legal</label>
<InputText
id="legal_representative"
v-model="form.legal_representative"
placeholder="Juan Pérez"
/>
<small v-if="errors.company_name" class="p-error text-red-600">{{ errors.company_name }}</small>
</div>
<div class="flex items-center gap-3 md:col-span-2">
<InputSwitch v-model="form.is_active" inputId="is_active" />
<label for="is_active" class="font-medium text-sm">
Tenant activo
</label>
</div>
</div>
</div>
<!-- Tasas e Impuestos -->
<div class="border-b pb-4">
<h3 class="text-lg font-semibold mb-4">Tasas e Impuestos (%)</h3>
<h3 class="text-lg font-semibold mb-4">Tasas e impuestos (%)</h3>
<div class="grid grid-cols-1 md:grid-cols-2 gap-4">
<div class="flex flex-col gap-2">
<label for="vat_rate" class="font-medium text-sm">Tasa de IVA *</label>
<InputNumber
id="vat_rate"
v-model="form.vat_rate"
:min="0"
:max="100"
:minFractionDigits="2"
:maxFractionDigits="2"
suffix="%"
:class="{ 'p-invalid': errors.vat_rate }"
/>
<small v-if="errors.vat_rate" class="p-error text-red-600">{{ errors.vat_rate }}</small>
</div>
<div class="flex flex-col gap-2">
<label for="isr_withholding" class="font-medium text-sm">Retención ISR</label>
<InputNumber
id="isr_withholding"
v-model="form.isr_withholding"
:min="0"
:max="100"
:minFractionDigits="2"
<label for="vat_rate" class="font-medium text-sm">Tasa de IVA (opcional)</label>
<InputNumber
id="vat_rate"
v-model="form.vat_rate"
:min="0"
:max="100"
:minFractionDigits="1"
:maxFractionDigits="2"
suffix="%"
/>
</div>
<div class="flex flex-col gap-2">
<label for="vat_withholding" class="font-medium text-sm">Retención IVA</label>
<InputNumber
id="vat_withholding"
v-model="form.vat_withholding"
:min="0"
:max="100"
:minFractionDigits="2"
<label for="isr_withholding" class="font-medium text-sm">Retención ISR (opcional)</label>
<InputNumber
id="isr_withholding"
v-model="form.isr_withholding"
:min="0"
:max="100"
:minFractionDigits="1"
:maxFractionDigits="2"
suffix="%"
/>
</div>
<div class="flex flex-col gap-2">
<label for="additional_tax" class="font-medium text-sm">Impuesto Adicional</label>
<InputNumber
id="additional_tax"
v-model="form.additional_tax"
:min="0"
:max="100"
:minFractionDigits="2"
<label for="vat_withholding" class="font-medium text-sm">Retención IVA (opcional)</label>
<InputNumber
id="vat_withholding"
v-model="form.vat_withholding"
:min="0"
:max="100"
:minFractionDigits="1"
:maxFractionDigits="2"
suffix="%"
/>
</div>
<div class="flex flex-col gap-2">
<label for="additional_tax" class="font-medium text-sm">Impuesto adicional (opcional)</label>
<InputNumber
id="additional_tax"
v-model="form.additional_tax"
:min="0"
:max="100"
:minFractionDigits="1"
:maxFractionDigits="2"
suffix="%"
/>
@ -109,315 +160,204 @@
</div>
</div>
<!-- Dirección -->
<div>
<h3 class="text-lg font-semibold mb-4">Dirección Fiscal</h3>
<h3 class="text-lg font-semibold mb-4">Certificados SAT</h3>
<div class="grid grid-cols-1 md:grid-cols-2 gap-4">
<div class="flex flex-col gap-2">
<label for="country" class="font-medium text-sm">País *</label>
<InputText
id="country"
v-model="form.address.country"
placeholder="México"
:class="{ 'p-invalid': errors.country }"
<label for="certificate_password" class="font-medium text-sm">Password del certificado</label>
<Password
id="certificate_password"
v-model="form.certificate_password"
:feedback="false"
toggleMask
placeholder="Password del certificado"
:class="{ 'p-invalid': errors.certificate_password }"
/>
<small v-if="errors.country" class="p-error text-red-600">{{ errors.country }}</small>
<small v-if="errors.certificate_password" class="p-error text-red-600">{{ errors.certificate_password }}</small>
</div>
<div class="flex flex-col gap-2">
<label for="postal_code" class="font-medium text-sm">Código Postal *</label>
<InputText
id="postal_code"
v-model="form.address.postal_code"
placeholder="12345"
:class="{ 'p-invalid': errors.postal_code }"
maxlength="10"
<label for="certificate_cer_file" class="font-medium text-sm">Archivo .cer</label>
<input
id="certificate_cer_file"
type="file"
accept=".cer"
class="p-2 border rounded-md border-surface-300 dark:border-surface-700"
@change="onFileChange($event, 'certificate_cer_file')"
/>
<small v-if="errors.postal_code" class="p-error text-red-600">{{ errors.postal_code }}</small>
<small v-if="errors.certificate_cer_file" class="p-error text-red-600">{{ errors.certificate_cer_file }}</small>
</div>
<div class="flex flex-col gap-2">
<label for="state" class="font-medium text-sm">Estado *</label>
<InputText
id="state"
v-model="form.address.state"
placeholder="CDMX"
:class="{ 'p-invalid': errors.state }"
/>
<small v-if="errors.state" class="p-error text-red-600">{{ errors.state }}</small>
</div>
<div class="flex flex-col gap-2">
<label for="municipality" class="font-medium text-sm">Municipio / Alcaldía *</label>
<InputText
id="municipality"
v-model="form.address.municipality"
placeholder="Benito Juárez"
:class="{ 'p-invalid': errors.municipality }"
/>
<small v-if="errors.municipality" class="p-error text-red-600">{{ errors.municipality }}</small>
</div>
<div class="flex flex-col gap-2">
<label for="city" class="font-medium text-sm">Ciudad *</label>
<InputText
id="city"
v-model="form.address.city"
placeholder="Ciudad de México"
:class="{ 'p-invalid': errors.city }"
/>
<small v-if="errors.city" class="p-error text-red-600">{{ errors.city }}</small>
</div>
<div class="flex flex-col gap-2">
<label for="street" class="font-medium text-sm">Calle *</label>
<InputText
id="street"
v-model="form.address.street"
placeholder="Av. Ejemplo"
:class="{ 'p-invalid': errors.street }"
/>
<small v-if="errors.street" class="p-error text-red-600">{{ errors.street }}</small>
</div>
<div class="flex flex-col gap-2">
<label for="num_ext" class="font-medium text-sm">Número Exterior *</label>
<InputText
id="num_ext"
v-model="form.address.num_ext"
placeholder="100"
:class="{ 'p-invalid': errors.num_ext }"
/>
<small v-if="errors.num_ext" class="p-error text-red-600">{{ errors.num_ext }}</small>
</div>
<div class="flex flex-col gap-2">
<label for="num_int" class="font-medium text-sm">Número Interior</label>
<InputText
id="num_int"
v-model="form.address.num_int"
placeholder="10"
<label for="certificate_key_file" class="font-medium text-sm">Archivo .key</label>
<input
id="certificate_key_file"
type="file"
accept=".key"
class="p-2 border rounded-md border-surface-300 dark:border-surface-700"
@change="onFileChange($event, 'certificate_key_file')"
/>
<small v-if="errors.certificate_key_file" class="p-error text-red-600">{{ errors.certificate_key_file }}</small>
</div>
</div>
</div>
<div class="text-xs text-slate-500">
* Campos obligatorios
</div>
<div class="text-xs text-slate-500">* Campos obligatorios</div>
</form>
<template #footer>
<div class="flex justify-end gap-2">
<Button
label="Cancelar"
icon="pi pi-times"
@click="handleClose"
class="p-button-text"
/>
<Button
label="Guardar"
icon="pi pi-check"
@click="handleSubmit"
:loading="loading"
/>
<Button label="Cancelar" icon="pi pi-times" @click="handleClose" class="p-button-text" />
<Button label="Guardar" icon="pi pi-check" @click="handleSubmit" :loading="loading" />
</div>
</template>
</Dialog>
</template>
<script setup lang="ts">
import { ref, computed, watch } from 'vue';
import { computed, ref, watch } from 'vue';
import type { Company, TenantFormData } from './companies.types';
import { toTenantFormData } from './companies.mapper';
// Props
const props = defineProps<{
visible: boolean;
company?: any;
company?: Company;
}>();
// Emits
const emit = defineEmits<{
close: [];
save: [data: any];
save: [data: TenantFormData];
}>();
// Métodos
// Método para setear errores de validación del backend
const setValidationErrors = (backendErrors: Record<string, string[]>) => {
loading.value = false;
errors.value = {};
// Mapear los errores del backend al formato del formulario
Object.keys(backendErrors).forEach(key => {
if (backendErrors[key] && backendErrors[key].length > 0) {
// Tomar el primer mensaje de error de cada campo
const firstError = backendErrors[key][0];
if (firstError) {
errors.value[key] = firstError;
}
}
});
};
// Expose method to set errors from parent
defineExpose({
setValidationErrors
});
// State
const loading = ref(false);
const errors = ref<Record<string, string>>({});
const form = ref<TenantFormData>(toTenantFormData());
const isVisible = computed({
get: () => props.visible,
set: (value) => {
if (!value) emit('close');
}
},
});
const formTitle = computed(() => {
return props.company ? 'Editar Empresa' : 'Registrar Nueva Empresa';
});
const isEditMode = computed(() => Boolean(props.company?.id));
const formTitle = computed(() => (isEditMode.value ? 'Editar Empresa' : 'Registrar Nueva Empresa'));
// Form data
const defaultForm = () => ({
rfc: '',
curp: '',
company_name: '',
vat_rate: 16.00,
isr_withholding: 0.00,
vat_withholding: 0.00,
additional_tax: 0.00,
address: {
country: 'México',
postal_code: '',
state: '',
municipality: '',
city: '',
street: '',
num_ext: '',
num_int: ''
}
});
const form = ref(defaultForm());
const errors = ref<Record<string, string>>({});
// Watch for company changes
watch(() => props.company, (newCompany) => {
if (newCompany) {
form.value = {
...defaultForm(),
...newCompany,
address: {
...defaultForm().address,
...(newCompany.address || {})
}
};
} else {
form.value = defaultForm();
}
const setValidationErrors = (backendErrors: Record<string, string[]>) => {
loading.value = false;
errors.value = {};
loading.value = false; // Reset loading state
}, { immediate: true });
// Watch for dialog visibility to reset loading
watch(() => props.visible, (isVisible) => {
if (!isVisible) {
loading.value = false;
}
});
// Methods
const validateForm = (): boolean => {
errors.value = {};
let isValid = true;
// RFC validation
if (!form.value.rfc || form.value.rfc.trim() === '') {
errors.value.rfc = 'El RFC es obligatorio';
isValid = false;
} else if (form.value.rfc.length < 12 || form.value.rfc.length > 13) {
errors.value.rfc = 'El RFC debe tener 12 o 13 caracteres';
isValid = false;
}
// Company name validation
if (!form.value.company_name || form.value.company_name.trim() === '') {
errors.value.company_name = 'La razón social es obligatoria';
isValid = false;
}
// VAT rate validation
if (form.value.vat_rate === null || form.value.vat_rate === undefined) {
errors.value.vat_rate = 'La tasa de IVA es obligatoria';
isValid = false;
}
// Address validations
if (!form.value.address.country || form.value.address.country.trim() === '') {
errors.value.country = 'El país es obligatorio';
isValid = false;
}
if (!form.value.address.postal_code || form.value.address.postal_code.trim() === '') {
errors.value.postal_code = 'El código postal es obligatorio';
isValid = false;
}
if (!form.value.address.state || form.value.address.state.trim() === '') {
errors.value.state = 'El estado es obligatorio';
isValid = false;
}
if (!form.value.address.municipality || form.value.address.municipality.trim() === '') {
errors.value.municipality = 'El municipio es obligatorio';
isValid = false;
}
if (!form.value.address.city || form.value.address.city.trim() === '') {
errors.value.city = 'La ciudad es obligatoria';
isValid = false;
}
if (!form.value.address.street || form.value.address.street.trim() === '') {
errors.value.street = 'La calle es obligatoria';
isValid = false;
}
if (!form.value.address.num_ext || form.value.address.num_ext.trim() === '') {
errors.value.num_ext = 'El número exterior es obligatorio';
isValid = false;
}
return isValid;
Object.keys(backendErrors).forEach((key) => {
const firstError = backendErrors[key]?.[0];
if (firstError) {
errors.value[key] = firstError;
}
});
};
const handleSubmit = async () => {
const resetSubmitting = () => {
loading.value = false;
};
defineExpose({
setValidationErrors,
resetSubmitting,
});
watch(
() => props.company,
(newCompany) => {
const primaryDomain = newCompany?.primary_domain ?? newCompany?.domains?.[0]?.domain ?? '';
form.value = toTenantFormData({
id: newCompany?.id,
company_name: newCompany?.company_name,
rfc: newCompany?.rfc,
email: newCompany?.email,
primary_domain: primaryDomain,
curp: newCompany?.curp ?? '',
phone: newCompany?.phone ?? '',
legal_representative: newCompany?.legal_representative ?? '',
tax_regime: newCompany?.tax_regime ?? '',
is_active: newCompany?.is_active ?? true,
vat_rate: newCompany?.vat_rate ?? 0.0,
isr_withholding: newCompany?.isr_withholding ?? 0.0,
vat_withholding: newCompany?.vat_withholding ?? 0.0,
additional_tax: newCompany?.additional_tax ?? 0.0,
});
errors.value = {};
loading.value = false;
},
{ immediate: true }
);
watch(
() => props.visible,
(visible) => {
if (!visible) {
loading.value = false;
}
}
);
const isValidEmail = (email: string): boolean => /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email);
const validateForm = (): boolean => {
errors.value = {};
let valid = true;
if (!form.value.company_name.trim()) {
errors.value.company_name = 'La razón social es obligatoria';
valid = false;
}
if (!form.value.rfc.trim()) {
errors.value.rfc = 'El RFC es obligatorio';
valid = false;
} else if (form.value.rfc.trim().length > 13) {
errors.value.rfc = 'El RFC no puede exceder 13 caracteres';
valid = false;
}
if (!form.value.email.trim()) {
errors.value.email = 'El email es obligatorio';
valid = false;
} else if (!isValidEmail(form.value.email.trim())) {
errors.value.email = 'Ingresa un email válido';
valid = false;
}
if (!isEditMode.value && !form.value.primary_domain.trim()) {
errors.value.primary_domain = 'El dominio primario es obligatorio';
valid = false;
}
return valid;
};
const onFileChange = (event: Event, field: 'certificate_cer_file' | 'certificate_key_file') => {
const target = event.target as HTMLInputElement;
const file = target.files?.[0] ?? null;
form.value[field] = file;
};
const handleSubmit = () => {
if (!validateForm()) {
return;
}
loading.value = true;
try {
// Convert string values to uppercase for RFC and CURP
const formData = {
...form.value,
rfc: form.value.rfc.toUpperCase(),
curp: form.value.curp ? form.value.curp.toUpperCase() : null,
};
emit('save', formData);
// El loading se mantendrá hasta que el padre cierre el diálogo
// o maneje el error
} catch (error) {
console.error('Error submitting form:', error);
loading.value = false;
}
emit('save', {
...form.value,
rfc: form.value.rfc.trim().toUpperCase(),
curp: form.value.curp.trim().toUpperCase(),
});
};
const handleClose = () => {
form.value = defaultForm();
form.value = toTenantFormData();
errors.value = {};
loading.value = false;
emit('close');
};
</script>
@ -426,4 +366,4 @@ const handleClose = () => {
.space-y-6 > * + * {
margin-top: 1.5rem;
}
</style>
</style>

View File

@ -0,0 +1,114 @@
import type { CreateTenantPayload, TenantFormData, UpdateTenantPayload } from './companies.types';
const appendIfNotEmpty = (formData: FormData, key: string, value: string | number | null | undefined) => {
if (value === null || value === undefined) {
return;
}
const normalized = String(value).trim();
if (normalized.length > 0) {
formData.append(key, normalized);
}
};
const appendNumericIfDefined = (formData: FormData, key: string, value: number | null | undefined) => {
if (value === null || value === undefined || Number.isNaN(value)) {
return;
}
formData.append(key, String(value));
};
const appendFileIfPresent = (formData: FormData, key: string, value: File | null | undefined) => {
if (value instanceof File) {
formData.append(key, value);
}
};
export const buildCreateTenantPayload = (data: TenantFormData): CreateTenantPayload => ({
company_name: data.company_name.trim(),
rfc: data.rfc.trim().toUpperCase(),
email: data.email.trim(),
primary_domain: data.primary_domain.trim(),
curp: data.curp.trim().toUpperCase(),
phone: data.phone.trim(),
legal_representative: data.legal_representative.trim(),
tax_regime: data.tax_regime.trim(),
is_active: data.is_active,
vat_rate: data.vat_rate,
isr_withholding: data.isr_withholding,
vat_withholding: data.vat_withholding,
additional_tax: data.additional_tax,
certificate_password: data.certificate_password,
certificate_cer_file: data.certificate_cer_file,
certificate_key_file: data.certificate_key_file,
});
export const buildUpdateTenantPayload = (data: TenantFormData): UpdateTenantPayload => ({
company_name: data.company_name.trim(),
rfc: data.rfc.trim().toUpperCase(),
email: data.email.trim(),
primary_domain: data.primary_domain.trim() || undefined,
curp: data.curp.trim().toUpperCase(),
phone: data.phone.trim(),
legal_representative: data.legal_representative.trim(),
tax_regime: data.tax_regime.trim(),
is_active: data.is_active,
vat_rate: data.vat_rate,
isr_withholding: data.isr_withholding,
vat_withholding: data.vat_withholding,
additional_tax: data.additional_tax,
certificate_password: data.certificate_password,
certificate_cer_file: data.certificate_cer_file,
certificate_key_file: data.certificate_key_file,
});
export const toTenantFormData = (data?: Partial<TenantFormData>): TenantFormData => ({
id: data?.id,
company_name: data?.company_name ?? '',
rfc: data?.rfc ?? '',
email: data?.email ?? '',
primary_domain: data?.primary_domain ?? '',
curp: data?.curp ?? '',
phone: data?.phone ?? '',
legal_representative: data?.legal_representative ?? '',
tax_regime: data?.tax_regime ?? '',
is_active: data?.is_active ?? true,
vat_rate: data?.vat_rate ?? 0.0,
isr_withholding: data?.isr_withholding ?? 0.0,
vat_withholding: data?.vat_withholding ?? 0.0,
additional_tax: data?.additional_tax ?? 0.0,
certificate_password: data?.certificate_password ?? '',
certificate_cer_file: null,
certificate_key_file: null,
});
export const tenantPayloadToFormData = (
data: CreateTenantPayload | UpdateTenantPayload,
options?: { forcePut?: boolean }
): FormData => {
const formData = new FormData();
if (options?.forcePut) {
formData.append('_method', 'PUT');
}
appendIfNotEmpty(formData, 'company_name', data.company_name);
appendIfNotEmpty(formData, 'rfc', data.rfc);
appendIfNotEmpty(formData, 'email', data.email);
appendIfNotEmpty(formData, 'primary_domain', data.primary_domain);
appendIfNotEmpty(formData, 'curp', data.curp);
appendIfNotEmpty(formData, 'phone', data.phone);
appendIfNotEmpty(formData, 'legal_representative', data.legal_representative);
appendIfNotEmpty(formData, 'tax_regime', data.tax_regime);
appendNumericIfDefined(formData, 'vat_rate', data.vat_rate);
appendNumericIfDefined(formData, 'isr_withholding', data.isr_withholding);
appendNumericIfDefined(formData, 'vat_withholding', data.vat_withholding);
appendNumericIfDefined(formData, 'additional_tax', data.additional_tax);
appendIfNotEmpty(formData, 'certificate_password', data.certificate_password);
formData.append('is_active', data.is_active ? '1' : '0');
appendFileIfPresent(formData, 'certificate_cer_file', data.certificate_cer_file);
appendFileIfPresent(formData, 'certificate_key_file', data.certificate_key_file);
return formData;
};

View File

@ -1,123 +1,79 @@
import api from '@/services/api';
import type {
Company,
CompanyCreateResponse,
CompanyFormData,
CompanyQueryParams,
PaginatedResponse
} from './companies.types';
export class CompanyService {
import {
buildCreateTenantPayload,
buildUpdateTenantPayload,
tenantPayloadToFormData,
} from './companies.mapper';
import type { CompaniesPagination, TenantFormData } from './companies.types';
/**
* Servicio para obtener todas las empresas (tenants) con paginación.
*/
export class CompaniesService {
/**
* Obtener todas las empresas con paginación y filtros opcionales
* Obtiene todas las empresas del sistema.
* @returns Promesa con la respuesta paginada de empresas
*/
async getAll(params?: CompanyQueryParams): Promise<PaginatedResponse<Company> | Company[]> {
public async getAll(page = 1): Promise<CompaniesPagination> {
try {
const response = await api.get('api/catalogs/companies', { params });
// Si la respuesta tiene paginación, devolver el objeto completo
if (response.data.current_page !== undefined) {
return response.data as PaginatedResponse<Company>;
}
// Si no tiene paginación, devolver solo el array de data
return response.data.data as Company[];
} catch (error: any) {
const response = await api.get('/api/admin/admin/tenants', {
params: { page },
});
return response.data;
} catch (error) {
console.error('Error al obtener empresas:', error);
throw error;
}
}
/**
* Obtener una empresa por ID
* Crea una empresa (tenant) usando multipart/form-data.
*/
async getById(id: number): Promise<Company> {
public async create(data: TenantFormData): Promise<void> {
try {
const response = await api.get(`api/catalogs/companies/${id}`);
return response.data;
} catch (error: any) {
console.error(`Error al obtener empresa con ID ${id}:`, error);
throw error;
}
}
/**
* Crear una nueva empresa
*/
async create(data: CompanyFormData): Promise<CompanyCreateResponse> {
try {
const response = await api.post('api/catalogs/companies', data);
return response.data;
} catch (error: any) {
console.error('Error al crear empresa:', error);
// Mejorar el mensaje de error si viene del backend
if (error.response?.data) {
throw {
...error,
message: error.response.data.message || error.response.data.error || 'Error al crear la empresa'
};
}
throw error;
}
}
/**
* Actualizar una empresa existente
*/
async update(id: number, data: Partial<CompanyFormData>): Promise<Company> {
try {
const response = await api.put(`api/catalogs/companies/${id}`, data);
return response.data;
} catch (error: any) {
console.error(`Error al actualizar empresa con ID ${id}:`, error);
// Mejorar el mensaje de error si viene del backend
if (error.response?.data) {
throw {
...error,
message: error.response.data.message || error.response.data.error || 'Error al actualizar la empresa'
};
}
throw error;
}
}
/**
* Eliminar una empresa
*/
async delete(id: number): Promise<void> {
try {
await api.delete(`api/catalogs/companies/${id}`);
} catch (error: any) {
console.error(`Error al eliminar empresa con ID ${id}:`, error);
// Mejorar el mensaje de error si viene del backend
if (error.response?.data) {
throw {
...error,
message: error.response.data.message || error.response.data.error || 'Error al eliminar la empresa'
};
}
throw error;
}
}
/**
* Buscar empresas por múltiples criterios
*/
async search(params: CompanyQueryParams): Promise<Company[]> {
try {
const response = await api.get('api/catalogs/companies', {
params: { ...params, paginate: false }
const payload = buildCreateTenantPayload(data);
const formData = tenantPayloadToFormData(payload);
await api.post('/api/admin/admin/tenants', formData, {
headers: {
'Content-Type': 'multipart/form-data',
},
});
return response.data.data;
} catch (error: any) {
console.error('Error al buscar empresas:', error);
} catch (error) {
console.error('Error al crear empresa:', error);
throw error;
}
}
};
/**
* Actualiza una empresa (tenant) usando POST + _method=PUT para multipart.
*/
public async update(id: number, data: TenantFormData): Promise<void> {
try {
const payload = buildUpdateTenantPayload(data);
const formData = tenantPayloadToFormData(payload, { forcePut: true });
await api.post(`/api/admin/admin/tenants/${id}`, formData, {
headers: {
'Content-Type': 'multipart/form-data',
},
});
} catch (error) {
console.error('Error al actualizar empresa:', error);
throw error;
}
}
// Exportar una instancia única del servicio
export const companyService = new CompanyService();
/**
* Elimina una empresa por su ID.
* @param id ID de la empresa
*/
public async delete(id: number): Promise<void> {
try {
await api.delete(`/api/admin/admin/tenants/${id}`);
} catch (error) {
console.error('Error al eliminar empresa:', error);
throw error;
}
}
}
export const companiesService = new CompaniesService();

View File

@ -1,72 +1,71 @@
export interface CompanyAddress {
country: string;
postal_code: string;
state: string;
municipality: string;
city: string;
street: string;
num_ext: string;
num_int?: string;
export interface Company {
id: number;
rfc: string;
curp?: string | null;
vat_rate?: number | null;
isr_withholding?: number | null;
vat_withholding?: number | null;
additional_tax?: number | null;
company_name: string;
email?: string;
primary_domain?: string;
phone?: string | null;
legal_representative?: string | null;
tax_regime?: string | null;
created_at: string;
updated_at: string;
deleted_at?: string | null;
data?: Record<string, unknown> | null;
is_active: boolean;
trial_ends_at?: string | null;
subscribed_until?: string | null;
domains_count: number;
domains: CompanyDomain[];
}
export interface Company {
export interface CompanyDomain {
id?: number;
domain: string;
}
export interface TenantFormData {
id?: number;
rfc: string;
curp?: string;
company_name: string;
rfc: string;
email: string;
primary_domain: string;
curp: string;
phone: string;
legal_representative: string;
tax_regime: string;
is_active: boolean;
vat_rate: number;
isr_withholding: number;
vat_withholding: number;
additional_tax: number;
address: CompanyAddress;
created_at?: string;
updated_at?: string;
certificate_password: string;
certificate_cer_file: File | null;
certificate_key_file: File | null;
}
export interface CompanyCreateResponse{
data: Company;
}
export type CreateTenantPayload = Omit<TenantFormData, 'id'>;
export interface CompanyFormData extends Omit<Company, 'id' | 'created_at' | 'updated_at'> {}
export type UpdateTenantPayload = Omit<TenantFormData, 'id' | 'primary_domain'> & {
primary_domain?: string;
};
export interface CompanyStats {
total: number;
active: number;
pending: number;
monthlyIncrease: number;
activePercentage: number;
}
export interface PaginationLink {
url: string | null;
label: string;
active: boolean;
}
export interface PaginatedResponse<T> {
export interface CompaniesPagination {
current_page: number;
data: T[];
data: Company[];
first_page_url: string;
from: number;
last_page: number;
last_page_url: string;
links: PaginationLink[];
next_page_url: string | null;
links: Array<{ url: string | null; label: string; active: boolean }>;
next_page_url?: string | null;
path: string;
per_page: number;
prev_page_url: string | null;
prev_page_url?: string | null;
to: number;
total: number;
}
export interface CompanyListResponse {
data: Company[];
}
export interface CompanyQueryParams {
paginate?: boolean;
rfc?: string;
curp?: string;
company_name?: string;
page?: number;
}