updates on the patch updats on the multiple order
This commit is contained in:
@@ -434,9 +434,52 @@ export const createExpressBooking = async (data) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const createExpressBookingBulk = async (bookings) => {
|
export const createExpressBookingBulk = async (bookings) => {
|
||||||
// Max 200 per call; response carries per-row results.
|
const BATCH_SIZE = 200;
|
||||||
const response = await doormileAxios.post('/admin/expressbooking/bulk', { bookings });
|
const list = Array.isArray(bookings) ? bookings : [];
|
||||||
return response.data;
|
|
||||||
|
if (list.length <= BATCH_SIZE) {
|
||||||
|
const response = await doormileAxios.post('/admin/expressbooking/bulk', { bookings: list });
|
||||||
|
return response.data;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Automatic chunking for requests exceeding the backend's 200 per-call limit (e.g. 1,000 orders)
|
||||||
|
const chunks = [];
|
||||||
|
for (let i = 0; i < list.length; i += BATCH_SIZE) {
|
||||||
|
chunks.push(list.slice(i, i + BATCH_SIZE));
|
||||||
|
}
|
||||||
|
|
||||||
|
const results = [];
|
||||||
|
let totalCreated = 0;
|
||||||
|
let lastErrorMessage = '';
|
||||||
|
|
||||||
|
for (const chunk of chunks) {
|
||||||
|
try {
|
||||||
|
const response = await doormileAxios.post('/admin/expressbooking/bulk', { bookings: chunk });
|
||||||
|
const body = response.data || {};
|
||||||
|
results.push(body);
|
||||||
|
if (body.success !== false) {
|
||||||
|
totalCreated += chunk.length;
|
||||||
|
} else {
|
||||||
|
lastErrorMessage = body.message || 'One batch failed';
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
lastErrorMessage = err.response?.data?.message || err.message || 'Batch request failed';
|
||||||
|
results.push({ success: false, message: lastErrorMessage });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const anySuccess = totalCreated > 0;
|
||||||
|
const allSuccess = totalCreated === list.length;
|
||||||
|
|
||||||
|
return {
|
||||||
|
success: anySuccess,
|
||||||
|
message: allSuccess
|
||||||
|
? `${list.length} orders created successfully`
|
||||||
|
: `${totalCreated} of ${list.length} orders created${lastErrorMessage ? ` (${lastErrorMessage})` : ''}`,
|
||||||
|
data: results.flatMap((r) => r.data || []),
|
||||||
|
total: totalCreated,
|
||||||
|
batches: chunks.length
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getBooking = async (id) => {
|
export const getBooking = async (id) => {
|
||||||
|
|||||||
@@ -477,11 +477,14 @@ export default function MultipleOrders() {
|
|||||||
|
|
||||||
setIsSubmitting(true);
|
setIsSubmitting(true);
|
||||||
try {
|
try {
|
||||||
|
if (bookings.length > 200) {
|
||||||
|
OpenToast(`Creating ${bookings.length} orders in ${Math.ceil(bookings.length / 200)} batches...`, 'info', 3000);
|
||||||
|
}
|
||||||
const res = await createExpressBookingBulk(bookings);
|
const res = await createExpressBookingBulk(bookings);
|
||||||
if (res?.success) {
|
if (res?.success) {
|
||||||
queryClient.invalidateQueries({ queryKey: ['doormile', 'bookings'] });
|
queryClient.invalidateQueries({ queryKey: ['doormile', 'bookings'] });
|
||||||
queryClient.invalidateQueries({ queryKey: ['ordersBulk'] });
|
queryClient.invalidateQueries({ queryKey: ['ordersBulk'] });
|
||||||
OpenToast(`${bookings.length} Orders created successfully`, 'success', 2500);
|
OpenToast(`${res.total || bookings.length} orders created successfully`, 'success', 2500);
|
||||||
navigate('/doormile/orders');
|
navigate('/doormile/orders');
|
||||||
} else {
|
} else {
|
||||||
OpenToast(res?.message || 'Bulk order creation failed', 'error', 3500);
|
OpenToast(res?.message || 'Bulk order creation failed', 'error', 3500);
|
||||||
|
|||||||
Reference in New Issue
Block a user