150 lines
4.5 KiB
TypeScript
150 lines
4.5 KiB
TypeScript
import {
|
|
getOrderUpdateForDeliveryInvitationAction,
|
|
} from "../_shared/delivery-invitations.ts";
|
|
import { createServiceClient } from "../_shared/chatbot.ts";
|
|
import { insertIntegrationEvent } from "../_shared/integration-events.ts";
|
|
|
|
const corsHeaders = {
|
|
"Access-Control-Allow-Origin": "*",
|
|
"Access-Control-Allow-Headers": "authorization, x-client-info, apikey, content-type",
|
|
};
|
|
|
|
Deno.serve(async (request) => {
|
|
if (request.method === "OPTIONS") {
|
|
return new Response("ok", { headers: corsHeaders });
|
|
}
|
|
|
|
if (request.method !== "POST") {
|
|
return new Response(JSON.stringify({ error: "Method not allowed" }), {
|
|
status: 405,
|
|
headers: {
|
|
...corsHeaders,
|
|
"Content-Type": "application/json",
|
|
},
|
|
});
|
|
}
|
|
|
|
try {
|
|
const body = (await request.json()) as {
|
|
orderId?: string;
|
|
result?: "delivered" | "problem";
|
|
note?: string;
|
|
payload?: Record<string, unknown>;
|
|
};
|
|
|
|
if (!body.orderId) {
|
|
return new Response(JSON.stringify({ error: "orderId is required" }), {
|
|
status: 400,
|
|
headers: {
|
|
...corsHeaders,
|
|
"Content-Type": "application/json",
|
|
},
|
|
});
|
|
}
|
|
|
|
const supabase = createServiceClient();
|
|
const { data: currentOrder, error: orderError } = await supabase
|
|
.from("orders")
|
|
.select("id, status, delivery_agreement_status")
|
|
.eq("id", body.orderId)
|
|
.single();
|
|
|
|
if (orderError) {
|
|
throw orderError;
|
|
}
|
|
|
|
const isDelivered = body.result === "delivered";
|
|
const action = isDelivered ? "mark_delivered" : "mark_paid_storage";
|
|
const orderUpdate = getOrderUpdateForDeliveryInvitationAction(action);
|
|
const nextStatus = isDelivered ? orderUpdate?.status || "Доставлен" : "Проблема доставки";
|
|
|
|
const { error: invitationError } = await supabase
|
|
.from("delivery_invitations")
|
|
.update({
|
|
state: isDelivered ? "delivered" : "paid_storage",
|
|
...(isDelivered ? { delivered_at: new Date().toISOString() } : { paid_storage_at: new Date().toISOString() }),
|
|
})
|
|
.eq("order_id", body.orderId);
|
|
|
|
if (invitationError) {
|
|
throw invitationError;
|
|
}
|
|
|
|
const { error: updateError } = await supabase
|
|
.from("orders")
|
|
.update({
|
|
status: nextStatus,
|
|
delivery_agreement_status: isDelivered
|
|
? "Подтверждено клиентом"
|
|
: body.note || currentOrder.delivery_agreement_status || "Ошибка отправки",
|
|
})
|
|
.eq("id", body.orderId);
|
|
|
|
if (updateError) {
|
|
throw updateError;
|
|
}
|
|
|
|
const { error: historyError } = await supabase.from("order_history").insert({
|
|
order_id: body.orderId,
|
|
action: isDelivered ? "Подтверждение доставки" : "Фиксация проблемы доставки",
|
|
old_status: currentOrder.status,
|
|
new_status: isDelivered ? "Доставлен" : "Проблема доставки",
|
|
metadata: {
|
|
old_delivery_agreement_status: currentOrder.delivery_agreement_status,
|
|
new_delivery_agreement_status: isDelivered
|
|
? "Подтверждено клиентом"
|
|
: body.note || currentOrder.delivery_agreement_status || "Ошибка отправки",
|
|
payload: body.payload || {},
|
|
},
|
|
});
|
|
|
|
if (historyError) {
|
|
throw historyError;
|
|
}
|
|
|
|
await insertIntegrationEvent(supabase, {
|
|
order_id: body.orderId,
|
|
event_type: isDelivered ? "delivery_result_delivered" : "delivery_result_problem",
|
|
direction: "internal",
|
|
status: "success",
|
|
payload: {
|
|
result: body.result || null,
|
|
note: body.note || null,
|
|
payload: body.payload || {},
|
|
},
|
|
});
|
|
|
|
return new Response(
|
|
JSON.stringify({
|
|
ok: true,
|
|
orderId: body.orderId,
|
|
status: nextStatus,
|
|
deliveryAgreementStatus: isDelivered
|
|
? "Подтверждено клиентом"
|
|
: body.note || currentOrder.delivery_agreement_status || "Ошибка отправки",
|
|
workflowStatus: nextStatus,
|
|
}),
|
|
{
|
|
headers: {
|
|
...corsHeaders,
|
|
"Content-Type": "application/json",
|
|
},
|
|
},
|
|
);
|
|
} catch (error) {
|
|
return new Response(
|
|
JSON.stringify({
|
|
ok: false,
|
|
error: error instanceof Error ? error.message : "Unexpected error",
|
|
}),
|
|
{
|
|
status: 500,
|
|
headers: {
|
|
...corsHeaders,
|
|
"Content-Type": "application/json",
|
|
},
|
|
},
|
|
);
|
|
}
|
|
});
|