feat: Enhance regimen filtering logic and add timeout to authenticatedFetch
This commit is contained in:
@@ -187,12 +187,31 @@
|
|||||||
// Combinar clientes y proveedores para shipped_to
|
// Combinar clientes y proveedores para shipped_to
|
||||||
const allClientsProviders = [...clients, ...providers];
|
const allClientsProviders = [...clients, ...providers];
|
||||||
|
|
||||||
// Filtrar regímenes por tipo de operación (1='1' exp, 2='2' imp)
|
// Filtrar regímenes por tipo de operación (1='E' exp, 2='I' imp) y obtener valores únicos
|
||||||
const filteredRegimens = $derived(
|
const filteredRegimens = $derived.by(() => {
|
||||||
codePedimentoRegimens.filter(r =>
|
const typeCode = formData.operation_type === 1 ? 'E' : formData.operation_type === 2 ? 'I' : null;
|
||||||
r.type_code === String(formData.operation_type)
|
const filtered = codePedimentoRegimens.filter(r => r.type_code === typeCode);
|
||||||
)
|
|
||||||
);
|
// Obtener solo regímenes únicos por regimen_code
|
||||||
|
const uniqueMap = new Map();
|
||||||
|
filtered.forEach(r => {
|
||||||
|
if (r.regimen_code && !uniqueMap.has(r.regimen_code)) {
|
||||||
|
uniqueMap.set(r.regimen_code, r);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return Array.from(uniqueMap.values());
|
||||||
|
});
|
||||||
|
|
||||||
|
// Efecto: Limpiar régimen si no existe en los regímenes filtrados al cambiar operation_type
|
||||||
|
$effect(() => {
|
||||||
|
if (formData.clave_regimen_aduanero && filteredRegimens.length > 0) {
|
||||||
|
const regimenExists = filteredRegimens.some(r => r.regimen_code === formData.clave_regimen_aduanero);
|
||||||
|
if (!regimenExists) {
|
||||||
|
formData.clave_regimen_aduanero = '';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<!-- Layout de 2 columnas compacto -->
|
<!-- Layout de 2 columnas compacto -->
|
||||||
@@ -692,7 +711,7 @@
|
|||||||
<Select.Trigger id="clave_regimen_aduanero" class="h-7 text-xs w-full">
|
<Select.Trigger id="clave_regimen_aduanero" class="h-7 text-xs w-full">
|
||||||
<span class="truncate">
|
<span class="truncate">
|
||||||
{#if formData.clave_regimen_aduanero}
|
{#if formData.clave_regimen_aduanero}
|
||||||
{formData.clave_regimen_aduanero}
|
{codePedimentoRegimens.find(r => r.regimen_code === formData.clave_regimen_aduanero)?.regimen_code || formData.clave_regimen_aduanero}
|
||||||
{:else if filteredRegimens.length > 0}
|
{:else if filteredRegimens.length > 0}
|
||||||
Selecciona régimen...
|
Selecciona régimen...
|
||||||
{:else if formData.operation_type}
|
{:else if formData.operation_type}
|
||||||
|
|||||||
@@ -133,13 +133,15 @@ export async function refreshAccessToken(
|
|||||||
* @param cookies - Objeto de cookies de SvelteKit
|
* @param cookies - Objeto de cookies de SvelteKit
|
||||||
* @param fetch - Función fetch de SvelteKit
|
* @param fetch - Función fetch de SvelteKit
|
||||||
* @param redirectUrl - URL a la que redirigir si falla la autenticación (opcional)
|
* @param redirectUrl - URL a la que redirigir si falla la autenticación (opcional)
|
||||||
|
* @param timeout - Timeout en milisegundos (default: 10000ms)
|
||||||
*/
|
*/
|
||||||
export async function authenticatedFetch(
|
export async function authenticatedFetch(
|
||||||
endpoint: string,
|
endpoint: string,
|
||||||
options: RequestInit = {},
|
options: RequestInit = {},
|
||||||
cookies: Cookies,
|
cookies: Cookies,
|
||||||
fetch: typeof globalThis.fetch,
|
fetch: typeof globalThis.fetch,
|
||||||
redirectUrl?: string
|
redirectUrl?: string,
|
||||||
|
timeout: number = 10000
|
||||||
): Promise<Response> {
|
): Promise<Response> {
|
||||||
try {
|
try {
|
||||||
const baseUrl = getServerApiUrl();
|
const baseUrl = getServerApiUrl();
|
||||||
@@ -156,24 +158,43 @@ export async function authenticatedFetch(
|
|||||||
// Construir URL completa
|
// Construir URL completa
|
||||||
const url = endpoint.startsWith('http') ? endpoint : `${baseUrl}${endpoint}`;
|
const url = endpoint.startsWith('http') ? endpoint : `${baseUrl}${endpoint}`;
|
||||||
|
|
||||||
|
// Crear AbortController para timeout
|
||||||
|
const controller = new AbortController();
|
||||||
|
const timeoutId = setTimeout(() => {
|
||||||
|
console.error(`⏱️ [API] Timeout después de ${timeout}ms:`, endpoint);
|
||||||
|
controller.abort();
|
||||||
|
}, timeout);
|
||||||
|
|
||||||
// Realizar la petición inicial
|
// Realizar la petición inicial
|
||||||
const headers = createAuthHeaders(accessToken, options.headers as Record<string, string>);
|
const headers = createAuthHeaders(accessToken, options.headers as Record<string, string>);
|
||||||
let response = await fetch(url, {
|
let response = await fetch(url, {
|
||||||
...options,
|
...options,
|
||||||
headers
|
headers,
|
||||||
|
signal: controller.signal
|
||||||
});
|
});
|
||||||
|
|
||||||
|
clearTimeout(timeoutId);
|
||||||
|
|
||||||
// Si es 401, intentar refrescar el token
|
// Si es 401, intentar refrescar el token
|
||||||
if (response.status === 401) {
|
if (response.status === 401) {
|
||||||
const newToken = await refreshAccessToken(cookies, fetch);
|
const newToken = await refreshAccessToken(cookies, fetch);
|
||||||
|
|
||||||
if (newToken) {
|
if (newToken) {
|
||||||
// Reintentar la petición con el nuevo token
|
// Reintentar la petición con el nuevo token
|
||||||
|
const newController = new AbortController();
|
||||||
|
const newTimeoutId = setTimeout(() => {
|
||||||
|
console.error(`⏱️ [API] Timeout en retry después de ${timeout}ms:`, endpoint);
|
||||||
|
newController.abort();
|
||||||
|
}, timeout);
|
||||||
|
|
||||||
const newHeaders = createAuthHeaders(newToken, options.headers as Record<string, string>);
|
const newHeaders = createAuthHeaders(newToken, options.headers as Record<string, string>);
|
||||||
response = await fetch(url, {
|
response = await fetch(url, {
|
||||||
...options,
|
...options,
|
||||||
headers: newHeaders
|
headers: newHeaders,
|
||||||
|
signal: newController.signal
|
||||||
});
|
});
|
||||||
|
|
||||||
|
clearTimeout(newTimeoutId);
|
||||||
} else {
|
} else {
|
||||||
// No se pudo refrescar, limpiar y redirigir
|
// No se pudo refrescar, limpiar y redirigir
|
||||||
clearAuthTokens(cookies);
|
clearAuthTokens(cookies);
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
import { browser } from '$app/environment';
|
import { browser } from '$app/environment';
|
||||||
import { createColumns } from '$lib/components/dashboard/ports/columns';
|
import { createColumns } from '$lib/components/dashboard/ports/columns';
|
||||||
import CreateEditDialog from '$lib/components/dashboard/ports/create-edit-dialog.svelte';
|
import CreateEditDialog from '$lib/components/dashboard/ports/create-edit-dialog.svelte';
|
||||||
import DataTable from '$lib/components/dashboard/units_of_measure/ace/data-table.svelte';
|
import DataTable from '$lib/components/dashboard/general_catalogs/units_of_measure/ace/data-table.svelte';
|
||||||
import { Button } from '$lib/components/ui/button';
|
import { Button } from '$lib/components/ui/button';
|
||||||
import { Input } from '$lib/components/ui/input';
|
import { Input } from '$lib/components/ui/input';
|
||||||
import { Plus } from 'lucide-svelte';
|
import { Plus } from 'lucide-svelte';
|
||||||
|
|||||||
Reference in New Issue
Block a user