tweaks
This commit is contained in:
+49
-3
@@ -33,7 +33,13 @@ import type {
|
||||
RawMaterial,
|
||||
RawMaterialCreateInput,
|
||||
RawMaterialPriceCreateInput,
|
||||
Scenario
|
||||
Scenario,
|
||||
ThroughputEntry,
|
||||
ThroughputEntryCreateInput,
|
||||
ThroughputEntryListParams,
|
||||
ThroughputProduct,
|
||||
ThroughputProductCreateInput,
|
||||
ThroughputProductUpdateInput
|
||||
} from '$lib/types';
|
||||
import { getStoredAdminSession, getStoredClientSession } from '$lib/session';
|
||||
|
||||
@@ -248,12 +254,18 @@ async function request<T>(
|
||||
|
||||
async function requestBlob(
|
||||
path: string,
|
||||
options: RequestInit = {},
|
||||
auth: AuthMode = 'none',
|
||||
fetcher: ApiFetch = fetch
|
||||
): Promise<Blob> {
|
||||
try {
|
||||
const response = await fetcher(resolveRequestUrl(path, fetcher), {
|
||||
credentials: 'include'
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
...(options.headers ?? {})
|
||||
},
|
||||
credentials: 'include',
|
||||
...options
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
@@ -284,12 +296,17 @@ export const api = {
|
||||
mixCalculatorSession: (sessionId: number, fetcher?: ApiFetch) =>
|
||||
request<MixCalculatorSession>(`/api/mix-calculator/${sessionId}`, { method: 'GET' }, 'client', fetcher),
|
||||
mixCalculatorSessionPdf: (sessionId: number, fetcher?: ApiFetch) =>
|
||||
requestBlob(`/api/mix-calculator/${sessionId}/pdf`, 'client', fetcher),
|
||||
requestBlob(`/api/mix-calculator/${sessionId}/pdf`, {}, 'client', fetcher),
|
||||
previewMixCalculatorSession: (payload: MixCalculatorCreateInput) =>
|
||||
request<MixCalculatorPreview>('/api/mix-calculator/preview', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(payload)
|
||||
}, 'client'),
|
||||
previewMixCalculatorPdf: (payload: MixCalculatorCreateInput) =>
|
||||
requestBlob('/api/mix-calculator/preview/pdf', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(payload)
|
||||
}, 'client'),
|
||||
createMixCalculatorSession: (payload: MixCalculatorCreateInput) =>
|
||||
request<MixCalculatorSession>('/api/mix-calculator', {
|
||||
method: 'POST',
|
||||
@@ -304,6 +321,35 @@ export const api = {
|
||||
productCosts: (fetcher?: ApiFetch) =>
|
||||
cachedFetchJson<ProductCostBreakdown[]>('/api/powerbi/product-costs', mockCosts, 'client', fetcher),
|
||||
scenarios: (fetcher?: ApiFetch) => cachedFetchJson<Scenario[]>('/api/scenarios', mockScenarios, 'client', fetcher),
|
||||
throughputProducts: (fetcher?: ApiFetch) =>
|
||||
cachedFetchJson<ThroughputProduct[]>('/api/throughput/products', [], 'client', fetcher),
|
||||
throughputEntries: (params?: ThroughputEntryListParams, fetcher?: ApiFetch) => {
|
||||
const search = new URLSearchParams();
|
||||
if (params?.date_from) search.set('date_from', params.date_from);
|
||||
if (params?.date_to) search.set('date_to', params.date_to);
|
||||
if (params?.product_id != null) search.set('product_id', String(params.product_id));
|
||||
if (params?.staff_name) search.set('staff_name', params.staff_name);
|
||||
if (params?.quantity_type) search.set('quantity_type', params.quantity_type);
|
||||
if (params?.limit) search.set('limit', String(params.limit));
|
||||
const qs = search.toString();
|
||||
const path = qs ? `/api/throughput/entries?${qs}` : '/api/throughput/entries';
|
||||
return cachedFetchJson<ThroughputEntry[]>(path, [], 'client', fetcher);
|
||||
},
|
||||
createThroughputEntry: (payload: ThroughputEntryCreateInput) =>
|
||||
request<ThroughputEntry>('/api/throughput/entries', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(payload)
|
||||
}, 'client'),
|
||||
createThroughputProduct: (payload: ThroughputProductCreateInput) =>
|
||||
request<ThroughputProduct>('/api/throughput/products', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(payload)
|
||||
}, 'client'),
|
||||
updateThroughputProduct: (productId: number, payload: ThroughputProductUpdateInput) =>
|
||||
request<ThroughputProduct>(`/api/throughput/products/${productId}`, {
|
||||
method: 'PATCH',
|
||||
body: JSON.stringify(payload)
|
||||
}, 'client'),
|
||||
clientAccess: (fetcher?: ApiFetch) => cachedFetchJson<ClientAccessAccount[]>('/api/client-access', mockClientAccess, 'manager', fetcher),
|
||||
clientAccessExport: (fetcher?: ApiFetch) =>
|
||||
cachedFetchJson<ClientAccessPowerBiExport>('/api/powerbi/client-access', mockClientAccessExport, 'manager', fetcher),
|
||||
|
||||
Reference in New Issue
Block a user