asset_browser/public/api/syncro.js

69 lines
2.9 KiB
JavaScript
Executable file

import { apiRequest } from './utils.js';
// ── Assets ───────────────────────────────────────────────────────────────────
export async function getAsset(id) {
const data = await apiRequest('GET', `/asset/${id}`, null, {}, { base: '/api' });
return data.asset ?? data;
}
export async function searchAssets(query) {
const data = await apiRequest('GET', '/asset/search', null, {
query,
per_page: 10,
}, { base: '/api' });
return data.assets ?? data.customer_assets ?? [];
}
export async function updateAsset(id, fields, customerId) {
// Routes through Node.js cache layer (/api/) which invalidates the server cache
// and forwards to Syncro. customer_id is used server-side for cache invalidation only.
const data = await apiRequest('PUT', `/customer_assets/${id}`, { asset: fields, customer_id: customerId }, {}, { base: '/api' });
return data.asset ?? data;
}
// ── Contacts ─────────────────────────────────────────────────────────────────
// Fetches via Node.js server cache — server handles pagination internally
export async function getContacts(customerId) {
const res = await fetch(`/api/contacts/${customerId}`, {
credentials: 'same-origin',
headers: { Accept: 'application/json' },
});
if (!res.ok) throw new Error(`HTTP ${res.status}`);
const data = await res.json();
return data.contacts ?? [];
}
// ── Customers ────────────────────────────────────────────────────────────────
// Fetches via Node.js server cache — server handles pagination internally
export async function getCustomers() {
const res = await fetch('/api/customers', {
credentials: 'same-origin',
headers: { Accept: 'application/json' },
});
if (!res.ok) throw new Error(`HTTP ${res.status}`);
const data = await res.json();
return data.customers ?? [];
}
export async function getCustomerAssets(customerId) {
const res = await fetch(`/api/customer_assets/${customerId}`, {
credentials: 'same-origin',
headers: { Accept: 'application/json' },
});
if (!res.ok) throw new Error(`HTTP ${res.status}`);
const data = await res.json();
return data.assets ?? [];
}
// ── Tickets ──────────────────────────────────────────────────────────────────
export async function getTickets(customerId) {
const data = await apiRequest('GET', '/tickets', null, {
customer_id: customerId,
per_page: 100, // fetch enough to filter client-side by contact
});
return data.tickets ?? [];
}