62 lines
1.9 KiB
TypeScript
62 lines
1.9 KiB
TypeScript
import type { CreateOrderDto, CustomerBalanceDto } from '@gold-bank/shared-types';
|
|||
|
|
|
||
|
|
const BASE = '/api';
|
||
|
|
|
||
|
|
async function request<T>(path: string, options?: RequestInit): Promise<T> {
|
||
|
|
const res = await fetch(`${BASE}${path}`, {
|
||
|
|
headers: { 'Content-Type': 'application/json' },
|
||
|
|
...options,
|
||
|
|
});
|
||
|
|
if (!res.ok) {
|
||
|
|
const body = await res.text();
|
||
|
|
throw new Error(`API ${path} failed (${res.status}): ${body}`);
|
||
|
|
}
|
||
|
|
return res.json();
|
||
|
|
}
|
||
|
|
|
||
|
|
export interface CustomerSummary {
|
||
|
|
id: string;
|
||
|
|
fullName: string;
|
||
|
|
mobile: string;
|
||
|
|
goldDebtGrams: string;
|
||
|
|
fiatDebtRial: string;
|
||
|
|
goldCreditLimitGrams: string;
|
||
|
|
}
|
||
|
|
|
||
|
|
export interface ProductVariantSummary {
|
||
|
|
id: string;
|
||
|
|
title: string;
|
||
|
|
rashWeightGrams: string;
|
||
|
|
defaultWagePercent: string;
|
||
|
|
}
|
||
|
|
|
||
|
|
export interface ProductSummary {
|
||
|
|
id: string;
|
||
|
|
title: string;
|
||
|
|
variants: ProductVariantSummary[];
|
||
|
|
}
|
||
|
|
|
||
|
|
export interface OrderDetail {
|
||
|
|
id: string;
|
||
|
|
orderNumber: string;
|
||
|
|
status: string;
|
||
|
|
totalGoldWeightGrams: string;
|
||
|
|
wageWeightGrams: string;
|
||
|
|
flatRialCharge: string;
|
||
|
|
isSuspendedOverCreditLimit: boolean;
|
||
|
|
deliveredAt: string | null;
|
||
|
|
items: Array<{ id: string; quantity: number; computedWeightGrams: string }>;
|
||
|
|
statusEvents: Array<{ toStatus: string; note: string | null; createdAt: string }>;
|
||
|
|
}
|
||
|
|
|
||
|
|
export const api = {
|
||
|
|
listCustomers: () => request<CustomerSummary[]>('/customers'),
|
||
|
|
getCustomerBalance: (id: string) => request<CustomerBalanceDto>(`/accounting/customers/${id}/balance`),
|
||
|
|
listProducts: () => request<ProductSummary[]>('/catalog/products'),
|
||
|
|
createOrder: (dto: CreateOrderDto) =>
|
||
|
|
request<OrderDetail>('/orders', { method: 'POST', body: JSON.stringify(dto) }),
|
||
|
|
getOrder: (id: string) => request<OrderDetail>(`/orders/${id}`),
|
||
|
|
updateOrderStatus: (id: string, status: string, note?: string) =>
|
||
|
|
request<OrderDetail>(`/orders/${id}/status`, { method: 'PATCH', body: JSON.stringify({ status, note }) }),
|
||
|
|
};
|