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);
|
logger.debug("[assignDriver] orderGroupId:", orderGroupId, "driverId:", driverId);
|
||||||
|
|
||||||
// Direct UPDATE — RLS allows manager/logistician/admin
|
// 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
|
const { error: updateError } = await client
|
||||||
.from("order_groups")
|
.from("order_groups")
|
||||||
.update({
|
.update(updates)
|
||||||
assigned_driver_id: driverId || null,
|
|
||||||
delivery_status: driverId ? "driver_assigned" : undefined,
|
|
||||||
updated_at: new Date().toISOString(),
|
|
||||||
})
|
|
||||||
.eq("id", orderGroupId);
|
.eq("id", orderGroupId);
|
||||||
|
|
||||||
if (updateError) {
|
if (updateError) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue