66 lines
2.0 KiB
TypeScript
66 lines
2.0 KiB
TypeScript
import { createClient } from "https://esm.sh/@supabase/supabase-js@2.49.8";
|
|
import { getOrderUpdateForInboundAction } from "./workflow.ts";
|
|
|
|
export type ProviderName = "telegram" | "vk" | "messenger_max";
|
|
|
|
export type NormalizedChatEvent = {
|
|
provider: ProviderName;
|
|
orderId: string;
|
|
externalMessageId: string | null;
|
|
senderType: "client" | "bot" | "system";
|
|
text: string;
|
|
payload: Record<string, unknown>;
|
|
action: "confirm_delivery" | "reschedule" | "cancel_delivery" | "unknown";
|
|
};
|
|
|
|
export const createServiceClient = () => {
|
|
const supabaseUrl = Deno.env.get("SUPABASE_URL") || "";
|
|
const serviceRoleKey = Deno.env.get("SUPABASE_SERVICE_ROLE_KEY") || "";
|
|
return createClient(supabaseUrl, serviceRoleKey);
|
|
};
|
|
|
|
export const json = (body: unknown, status = 200) =>
|
|
new Response(JSON.stringify(body), {
|
|
status,
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
},
|
|
});
|
|
|
|
export const normalizeIncomingEvent = (
|
|
provider: ProviderName,
|
|
body: Record<string, unknown>,
|
|
): NormalizedChatEvent => {
|
|
const payload = (body.payload as Record<string, unknown>) || {};
|
|
|
|
return {
|
|
provider,
|
|
orderId: String(body.order_id || payload.order_id || ""),
|
|
externalMessageId: body.external_message_id ? String(body.external_message_id) : null,
|
|
senderType: "client",
|
|
text: String(body.text || payload.text || ""),
|
|
payload,
|
|
action: resolveAction(body.action || payload.action),
|
|
};
|
|
};
|
|
|
|
export const resolveAction = (action: unknown): NormalizedChatEvent["action"] => {
|
|
switch (String(action || "").toLowerCase()) {
|
|
case "confirm":
|
|
case "confirm_delivery":
|
|
return "confirm_delivery";
|
|
case "reschedule":
|
|
return "reschedule";
|
|
case "cancel":
|
|
case "cancel_delivery":
|
|
return "cancel_delivery";
|
|
default:
|
|
return "unknown";
|
|
}
|
|
};
|
|
|
|
export const orderUpdateByAction = (action: NormalizedChatEvent["action"]) =>
|
|
getOrderUpdateForInboundAction(action);
|
|
|
|
export const channelFromProvider = (provider: ProviderName) => provider;
|