feature/continuacion-vista-tablas-location
This commit is contained in:
@@ -1,64 +1,84 @@
|
||||
import type { PaginatedResponse } from '$lib/types';
|
||||
import { api } from '$lib/api';
|
||||
|
||||
/** Contexto de uso: Fixed Asset (activo fijo) o inventario */
|
||||
export type LocationSystem = 'fixed_asset' | 'inventory';
|
||||
|
||||
export interface Location {
|
||||
id: number;
|
||||
location_code: string;
|
||||
location_description: string | null;
|
||||
company_id: number;
|
||||
tenant_id: number;
|
||||
clave_localizacion: string;
|
||||
localizacion: string | null;
|
||||
system: LocationSystem;
|
||||
}
|
||||
|
||||
export interface LocationCreate {
|
||||
location_code: string;
|
||||
location_description?: string | null;
|
||||
clave_localizacion: string;
|
||||
localizacion?: string | null;
|
||||
system: LocationSystem;
|
||||
/** Used when system === 'fixed_asset' */
|
||||
department?: string | null;
|
||||
responsible?: string | null;
|
||||
observations?: string | null;
|
||||
}
|
||||
|
||||
export interface LocationUpdate {
|
||||
location_description?: string | null;
|
||||
clave_localizacion?: string;
|
||||
localizacion?: string | null;
|
||||
system?: LocationSystem;
|
||||
}
|
||||
|
||||
export interface LocationListResponse extends PaginatedResponse {
|
||||
export interface LocationListResponse {
|
||||
items: Location[];
|
||||
total: number;
|
||||
page: number;
|
||||
page_size: number;
|
||||
}
|
||||
|
||||
export interface LocationFilters {
|
||||
location_code?: string;
|
||||
location_description?: string;
|
||||
clave_localizacion?: string;
|
||||
localizacion?: string;
|
||||
system?: LocationSystem;
|
||||
page?: number;
|
||||
page_size?: number;
|
||||
}
|
||||
|
||||
import { portsApi, PortType } from './ports';
|
||||
const BASE_URL = '/v1/a76/locations';
|
||||
|
||||
function buildQuery(companyId: number, params?: LocationFilters): string {
|
||||
const search = new URLSearchParams();
|
||||
search.set('company_id', String(companyId));
|
||||
if (params?.page != null) search.set('page', String(params.page));
|
||||
if (params?.page_size != null) search.set('page_size', String(params.page_size));
|
||||
if (params?.clave_localizacion) search.set('clave_localizacion', params.clave_localizacion);
|
||||
if (params?.localizacion) search.set('localizacion', params.localizacion);
|
||||
if (params?.system) search.set('system', params.system);
|
||||
return search.toString();
|
||||
}
|
||||
|
||||
export async function getLocations(
|
||||
companyId: number,
|
||||
filters?: LocationFilters
|
||||
): Promise<LocationListResponse> {
|
||||
const res = await portsApi.list(companyId, filters || {});
|
||||
return (res.data || res) as unknown as LocationListResponse;
|
||||
const q = buildQuery(companyId, filters);
|
||||
const res = await api.get<LocationListResponse>(`${BASE_URL}/?${q}`);
|
||||
return (res.data ?? res) as LocationListResponse;
|
||||
}
|
||||
|
||||
export async function getLocation(
|
||||
locationId: number,
|
||||
companyId: number
|
||||
): Promise<Location> {
|
||||
const res = await portsApi.get(locationId, companyId);
|
||||
return (res.data || res) as unknown as Location;
|
||||
const q = new URLSearchParams({ company_id: String(companyId) });
|
||||
const res = await api.get<Location>(`${BASE_URL}/${locationId}?${q}`);
|
||||
return (res.data ?? res) as Location;
|
||||
}
|
||||
|
||||
export async function createLocation(
|
||||
data: LocationCreate,
|
||||
companyId: number
|
||||
): Promise<Location> {
|
||||
const res = await portsApi.create({
|
||||
port_code: data.location_code,
|
||||
location_code: data.location_code,
|
||||
description: null,
|
||||
location_description: data.location_description || null,
|
||||
port_type: PortType.ENTRY
|
||||
}, companyId);
|
||||
return (res.data || res) as unknown as Location;
|
||||
const q = new URLSearchParams({ company_id: String(companyId) });
|
||||
const res = await api.post<Location>(`${BASE_URL}/?${q}`, data);
|
||||
return (res.data ?? res) as Location;
|
||||
}
|
||||
|
||||
export async function updateLocation(
|
||||
@@ -66,15 +86,15 @@ export async function updateLocation(
|
||||
data: LocationUpdate,
|
||||
companyId: number
|
||||
): Promise<Location> {
|
||||
const res = await portsApi.update(locationId, {
|
||||
location_description: data.location_description
|
||||
}, companyId);
|
||||
return (res.data || res) as unknown as Location;
|
||||
const q = new URLSearchParams({ company_id: String(companyId) });
|
||||
const res = await api.put<Location>(`${BASE_URL}/${locationId}/?${q}`, data);
|
||||
return (res.data ?? res) as Location;
|
||||
}
|
||||
|
||||
export async function deleteLocation(
|
||||
locationId: number,
|
||||
companyId: number
|
||||
): Promise<void> {
|
||||
await portsApi.delete(locationId, companyId);
|
||||
}
|
||||
const q = new URLSearchParams({ company_id: String(companyId) });
|
||||
await api.delete(`${BASE_URL}/${locationId}?${q}`);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user