feat: автосменя статуса при сохранении отгрузки

- Все позиции отгружены → delivery_status = 'delivered'
- Частичная отгрузка (есть неотгруженные) → delivery_status = 'problem'
- Без сохранения отгрузки — статус не меняется
This commit is contained in:
root 2026-06-15 18:40:42 +00:00
parent b888eba09b
commit c288251cec
1 changed files with 20 additions and 6 deletions

View File

@ -394,19 +394,33 @@ export const saveShipmentData = async ({ orderGroupId, shipmentData }) => {
return safeSupabaseCall(async () => {
const client = requireSupabase();
const unshipped = shipmentData.filter((i) => !i.shipped);
const allShipped = unshipped.length === 0 && shipmentData.length > 0;
const hasProblem = unshipped.length > 0;
const problemNote = hasProblem
? unshipped.map((i) => `${i.name}${i.quantity ? ` (${i.quantity}${i.unit ? ` ${i.unit}` : ""})` : ""}${i.comment ? `${i.comment}` : ""}`).join("; ")
: null;
// Auto-update delivery status based on shipment
let newDeliveryStatus = undefined;
if (allShipped) {
newDeliveryStatus = "delivered";
} else if (hasProblem) {
newDeliveryStatus = "problem";
}
const updatePayload = {
driver_shipment_data: shipmentData,
has_delivery_problem: hasProblem,
delivery_problem_note: problemNote,
updated_at: new Date().toISOString(),
};
if (newDeliveryStatus) {
updatePayload.delivery_status = newDeliveryStatus;
}
const { error: updateError } = await client
.from("order_groups")
.update({
driver_shipment_data: shipmentData,
has_delivery_problem: hasProblem,
delivery_problem_note: problemNote,
updated_at: new Date().toISOString(),
})
.update(updatePayload)
.eq("id", orderGroupId);
if (updateError) throw updateError;