225 lines
4.3 KiB
JavaScript
225 lines
4.3 KiB
JavaScript
const API_BASE = import.meta.env.VITE_API_BASE || '';
|
|
|
|
async function request(path, { method = 'GET', body, token } = {}) {
|
|
const headers = {};
|
|
const options = { method, headers };
|
|
|
|
if (body) {
|
|
headers['Content-Type'] = 'application/json';
|
|
options.body = JSON.stringify(body);
|
|
}
|
|
|
|
if (token) {
|
|
headers['x-session-token'] = token;
|
|
}
|
|
|
|
const response = await fetch(`${API_BASE}${path}`, options);
|
|
|
|
if (!response.ok) {
|
|
const errorBody = await response.json().catch(() => ({}));
|
|
const message = errorBody.message || 'Beklenmeyen bir hata olustu.';
|
|
throw new Error(message);
|
|
}
|
|
|
|
if (response.status === 204) {
|
|
return null;
|
|
}
|
|
|
|
return response.json();
|
|
}
|
|
|
|
export function login(credentials) {
|
|
return request('/api/auth/login', {
|
|
method: 'POST',
|
|
body: credentials
|
|
});
|
|
}
|
|
|
|
export function logout(token) {
|
|
return request('/api/auth/logout', {
|
|
method: 'POST',
|
|
token
|
|
});
|
|
}
|
|
|
|
export function fetchSession(token) {
|
|
return request('/api/session', {
|
|
method: 'GET',
|
|
token
|
|
});
|
|
}
|
|
|
|
export function listInventoryManagers(token) {
|
|
return request('/api/inventory-managers', {
|
|
method: 'GET',
|
|
token
|
|
});
|
|
}
|
|
|
|
export function createInventoryManager(token, payload) {
|
|
return request('/api/inventory-managers', {
|
|
method: 'POST',
|
|
body: payload,
|
|
token
|
|
});
|
|
}
|
|
|
|
export function updateInventoryManager(token, id, payload) {
|
|
return request(`/api/inventory-managers/${id}`, {
|
|
method: 'PUT',
|
|
body: payload,
|
|
token
|
|
});
|
|
}
|
|
|
|
export function deleteInventoryManager(token, id) {
|
|
return request(`/api/inventory-managers/${id}`, {
|
|
method: 'DELETE',
|
|
token
|
|
});
|
|
}
|
|
|
|
export function listVehicles(token) {
|
|
return request('/api/vehicles', {
|
|
method: 'GET',
|
|
token
|
|
});
|
|
}
|
|
|
|
export function createVehicle(token, payload) {
|
|
return request('/api/vehicles', {
|
|
method: 'POST',
|
|
body: payload,
|
|
token
|
|
});
|
|
}
|
|
|
|
export function updateVehicle(token, id, payload) {
|
|
return request(`/api/vehicles/${id}`, {
|
|
method: 'PUT',
|
|
body: payload,
|
|
token
|
|
});
|
|
}
|
|
|
|
export function deleteVehicle(token, id) {
|
|
return request(`/api/vehicles/${id}`, {
|
|
method: 'DELETE',
|
|
token
|
|
});
|
|
}
|
|
|
|
export function listUnits(token) {
|
|
return request('/api/units', {
|
|
method: 'GET',
|
|
token
|
|
});
|
|
}
|
|
|
|
export function createUnit(token, payload) {
|
|
return request('/api/units', {
|
|
method: 'POST',
|
|
body: payload,
|
|
token
|
|
});
|
|
}
|
|
|
|
export function updateUnit(token, id, payload) {
|
|
return request(`/api/units/${id}`, {
|
|
method: 'PUT',
|
|
body: payload,
|
|
token
|
|
});
|
|
}
|
|
|
|
export function deleteUnit(token, id) {
|
|
return request(`/api/units/${id}`, {
|
|
method: 'DELETE',
|
|
token
|
|
});
|
|
}
|
|
|
|
export function listFuelPersonnel(token) {
|
|
return request('/api/fuel-personnel', {
|
|
method: 'GET',
|
|
token
|
|
});
|
|
}
|
|
|
|
export function createFuelPersonnel(token, payload) {
|
|
return request('/api/fuel-personnel', {
|
|
method: 'POST',
|
|
body: payload,
|
|
token
|
|
});
|
|
}
|
|
|
|
export function updateFuelPersonnel(token, id, payload) {
|
|
return request(`/api/fuel-personnel/${id}`, {
|
|
method: 'PUT',
|
|
body: payload,
|
|
token
|
|
});
|
|
}
|
|
|
|
export function deleteFuelPersonnel(token, id) {
|
|
return request(`/api/fuel-personnel/${id}`, {
|
|
method: 'DELETE',
|
|
token
|
|
});
|
|
}
|
|
|
|
export function fetchFuelResources(token) {
|
|
return request('/api/fuel/resources', {
|
|
method: 'GET',
|
|
token
|
|
});
|
|
}
|
|
|
|
export function listFuelSlips(token) {
|
|
return request('/api/fuel-slips', {
|
|
method: 'GET',
|
|
token
|
|
});
|
|
}
|
|
|
|
export function createFuelSlip(token, payload) {
|
|
return request('/api/fuel-slips', {
|
|
method: 'POST',
|
|
body: payload,
|
|
token
|
|
});
|
|
}
|
|
|
|
export async function downloadFuelSlipPdf(token, id) {
|
|
const response = await fetch(`${API_BASE}/api/fuel-slips/${id}/pdf`, {
|
|
method: 'GET',
|
|
headers: {
|
|
'x-session-token': token
|
|
}
|
|
});
|
|
|
|
if (!response.ok) {
|
|
const errorBody = await response.json().catch(() => ({}));
|
|
const message = errorBody.message || 'PDF indirilemedi.';
|
|
throw new Error(message);
|
|
}
|
|
|
|
return response.blob();
|
|
}
|
|
|
|
export function fetchAssignedFuelSlips(token) {
|
|
return request('/api/fuel-slips/assigned', {
|
|
method: 'GET',
|
|
token
|
|
});
|
|
}
|
|
|
|
export function updateFuelSlipStatus(token, id, payload) {
|
|
return request(`/api/fuel-slips/${id}/status`, {
|
|
method: 'PATCH',
|
|
body: payload,
|
|
token
|
|
});
|
|
}
|