149 lines
4.2 KiB
TypeScript
149 lines
4.2 KiB
TypeScript
import { api } from './client';
|
|
import type { Delivery, PickupLocation, DeliveryStatus } from '../types';
|
|
|
|
// Types matching backend responses
|
|
interface BackendDelivery {
|
|
id: string;
|
|
date: string; // YYYY-MM-DD from pgtype.Date
|
|
pickup_location: PickupLocation;
|
|
product_name: string;
|
|
address: string;
|
|
phone: string;
|
|
additional_phone: string | null;
|
|
has_elevator: boolean;
|
|
comment: string;
|
|
status: DeliveryStatus;
|
|
created_at: string; // ISO timestamp
|
|
updated_at: string; // ISO timestamp
|
|
}
|
|
|
|
interface DeliveryCount {
|
|
date: string; // YYYY-MM-DD
|
|
count: number;
|
|
}
|
|
|
|
// API Response types
|
|
interface GetDeliveriesResponse {
|
|
deliveries: BackendDelivery[];
|
|
}
|
|
|
|
interface GetDeliveryResponse {
|
|
delivery: BackendDelivery;
|
|
}
|
|
|
|
interface GetDeliveryCountResponse {
|
|
counts: DeliveryCount[];
|
|
}
|
|
|
|
interface CreateDeliveryResponse {
|
|
message: string;
|
|
id: string;
|
|
}
|
|
|
|
interface UpdateDeliveryResponse {
|
|
message: string;
|
|
}
|
|
|
|
// Convert backend date format (YYYY-MM-DD) to frontend format (DD-MM-YYYY)
|
|
function backendDateToFrontend(dateStr: string): string {
|
|
const [year, month, day] = dateStr.split('-');
|
|
return `${day}-${month}-${year}`;
|
|
}
|
|
|
|
// Convert frontend date format (DD-MM-YYYY) to backend format (YYYY-MM-DD)
|
|
export function frontendDateToBackend(dateStr: string): string {
|
|
const [day, month, year] = dateStr.split('-');
|
|
return `${year}-${month}-${day}`;
|
|
}
|
|
|
|
// Map backend delivery to frontend delivery
|
|
function mapBackendToFrontend(backend: BackendDelivery): Delivery {
|
|
return {
|
|
id: backend.id,
|
|
date: backendDateToFrontend(backend.date),
|
|
pickupLocation: backend.pickup_location,
|
|
productName: backend.product_name,
|
|
address: backend.address,
|
|
phone: backend.phone,
|
|
additionalPhone: backend.additional_phone || undefined,
|
|
hasElevator: backend.has_elevator,
|
|
comment: backend.comment,
|
|
status: backend.status,
|
|
createdAt: new Date(backend.created_at).getTime(),
|
|
updatedAt: new Date(backend.updated_at).getTime(),
|
|
};
|
|
}
|
|
|
|
// Delivery API methods
|
|
export const deliveriesApi = {
|
|
// Get deliveries by date (DD-MM-YYYY)
|
|
getByDate: async (date: string): Promise<Delivery[]> => {
|
|
const response = await api.get<GetDeliveriesResponse>(
|
|
`/api/deliveries?date=${encodeURIComponent(date)}`
|
|
);
|
|
return response.deliveries.map(mapBackendToFrontend);
|
|
},
|
|
|
|
// Get single delivery by ID
|
|
getById: async (id: string): Promise<Delivery> => {
|
|
const response = await api.get<GetDeliveryResponse>(`/api/deliveries/${id}`);
|
|
return mapBackendToFrontend(response.delivery);
|
|
},
|
|
|
|
// Get delivery counts by date
|
|
getCounts: async (): Promise<Record<string, number>> => {
|
|
const response = await api.get<GetDeliveryCountResponse>('/api/deliveries/count');
|
|
const counts: Record<string, number> = {};
|
|
response.counts.forEach(({ date, count }) => {
|
|
counts[backendDateToFrontend(date)] = count;
|
|
});
|
|
return counts;
|
|
},
|
|
|
|
// Create delivery
|
|
create: async (
|
|
data: Omit<Delivery, 'id' | 'createdAt' | 'updatedAt'>
|
|
): Promise<string> => {
|
|
const payload = {
|
|
date: data.date,
|
|
pickup_location: data.pickupLocation,
|
|
product_name: data.productName,
|
|
address: data.address,
|
|
phone: data.phone,
|
|
additional_phone: data.additionalPhone || '',
|
|
has_elevator: data.hasElevator,
|
|
comment: data.comment,
|
|
};
|
|
const response = await api.post<CreateDeliveryResponse>('/api/deliveries', payload);
|
|
return response.id;
|
|
},
|
|
|
|
// Update delivery
|
|
update: async (
|
|
id: string,
|
|
data: Omit<Delivery, 'id' | 'createdAt' | 'updatedAt'>
|
|
): Promise<void> => {
|
|
const payload = {
|
|
date: data.date,
|
|
pickup_location: data.pickupLocation,
|
|
product_name: data.productName,
|
|
address: data.address,
|
|
phone: data.phone,
|
|
additional_phone: data.additionalPhone || '',
|
|
has_elevator: data.hasElevator,
|
|
comment: data.comment,
|
|
};
|
|
await api.patch<UpdateDeliveryResponse>(`/api/deliveries/${id}`, payload);
|
|
},
|
|
|
|
// Update delivery status
|
|
updateStatus: async (id: string, status: DeliveryStatus): Promise<void> => {
|
|
await api.patch(`/api/deliveries/${id}/status`, { status });
|
|
},
|
|
|
|
// Delete delivery
|
|
delete: async (id: string): Promise<void> => {
|
|
await api.delete(`/api/deliveries/${id}`);
|
|
},
|
|
};
|