fix: assign driver preserves agreed+ statuses instead of downgrading to driver_assigned
This commit is contained in:
parent
a8d54699ff
commit
9ec7d8e8ae
|
|
@ -222,13 +222,27 @@ export const assignDriverToOrderGroup = async ({
|
|||
logger.debug("[assignDriver] orderGroupId:", orderGroupId, "driverId:", driverId);
|
||||
|
||||
// Direct UPDATE — RLS allows manager/logistician/admin
|
||||
const { data: currentGroup, error: fetchCurrentError } = await client
|
||||
.from("order_groups")
|
||||
.select("delivery_status")
|
||||
.eq("id", orderGroupId)
|
||||
.single();
|
||||
|
||||
if (fetchCurrentError) {
|
||||
throw fetchCurrentError;
|
||||
}
|
||||
|
||||
// If status is 'agreed' or beyond, keep it — don't downgrade to 'driver_assigned'
|
||||
const keepStatus = ["agreed", "loaded", "on_route", "delivered"].includes(currentGroup.delivery_status);
|
||||
const updates = {
|
||||
assigned_driver_id: driverId || null,
|
||||
delivery_status: driverId && !keepStatus ? "driver_assigned" : undefined,
|
||||
updated_at: new Date().toISOString(),
|
||||
};
|
||||
|
||||
const { error: updateError } = await client
|
||||
.from("order_groups")
|
||||
.update({
|
||||
assigned_driver_id: driverId || null,
|
||||
delivery_status: driverId ? "driver_assigned" : undefined,
|
||||
updated_at: new Date().toISOString(),
|
||||
})
|
||||
.update(updates)
|
||||
.eq("id", orderGroupId);
|
||||
|
||||
if (updateError) {
|
||||
|
|
|
|||
Loading…
Reference in New Issue