Merge pull request 'fix(customs-brokers): update VU and personnel for multi-tenancy support' (#163) from hot-fix/customs-brokers-vu-multitenant into development

Reviewed-on: ADUANASOFT/anexo76#163
This commit is contained in:
2026-02-26 17:54:44 +00:00
6 changed files with 124 additions and 65 deletions

View File

@@ -1,4 +1,5 @@
import { api } from '$lib/api';
import { companyStore } from '$lib/stores/company.svelte'; // <--- NUEVO: Importamos el store para el fallback
import type { ApiResponse } from '$lib/api';
export interface CustomsBroker {
@@ -26,6 +27,8 @@ export interface CustomsBroker {
}
export interface CustomsBrokerVU {
tenant_id?: string | null;
company_id?: string | null;
certificate_path?: string | null;
key_path?: string | null;
access_key?: string | null;
@@ -95,7 +98,9 @@ export interface CustomsBrokerListResponse {
*/
export const customsBrokersApi = {
list: (companyId: string, page = 1, pageSize = 50) => {
return api.get<CustomsBrokerListResponse>(`/v1/a76/customs-brokers?company_id=${companyId}&page=${page}&page_size=${pageSize}`);
return api.get<CustomsBrokerListResponse>(
`/v1/a76/customs-brokers?company_id=${companyId}&page=${page}&page_size=${pageSize}`
);
},
get: (brokerKey: string, companyId: string) => {
@@ -111,20 +116,47 @@ export const customsBrokersApi = {
*/
update: (brokerKey: string, data: CreateCustomsBrokerData) => {
const companyId = data.company_id;
return api.put<CustomsBroker>(`/v1/a76/customs-brokers/${brokerKey}/?company_id=${companyId}`, data);
return api.put<CustomsBroker>(
`/v1/a76/customs-brokers/${brokerKey}/?company_id=${companyId}`,
data
);
},
/**
* Elimina un agente aduanal
*/
delete: (brokerKey: string, companyId: string) => {
return api.delete<CustomsBroker>(`/v1/a76/customs-brokers/${brokerKey}?company_id=${companyId}`);
return api.delete<CustomsBroker>(
`/v1/a76/customs-brokers/${brokerKey}?company_id=${companyId}`
);
},
/**
* Actualiza la información de Ventanilla Única (VU)
*/
updateVU: (brokerKey: string, data: CustomsBrokerVU, companyId: string) => {
return api.put<CustomsBrokerVU>(`/v1/a76/customs-broker-vu/${brokerKey}?company_id=${companyId}`, data);
// LOGICA DE RESCATE:
// Si companyId llega nulo/undefined, intentamos obtenerlo del store global
let finalCompanyId = companyId;
if (!finalCompanyId && companyStore.activeCompany?.id) {
finalCompanyId = companyStore.activeCompany.id.toString();
console.warn("WARN: companyId no fue provisto a updateVU, usando companyStore:", finalCompanyId);
}
// Aseguramos que el payload tenga los IDs
const payload = {
...data,
company_id: finalCompanyId,
tenant_id: finalCompanyId
};
console.log('[DEBUG] Enviando payload VU:', payload);
return api.put<CustomsBrokerVU>(
`/v1/a76/customs-broker-vu/${brokerKey}?company_id=${finalCompanyId}`,
payload
);
},
updatePersonnel: (brokerKey: string, line: number, data: CustomsBrokerPersonnel, companyId: string) => {