142 lines
4.1 KiB
TypeScript
142 lines
4.1 KiB
TypeScript
import { serve } from "https://deno.land/std@0.168.0/http/server.ts";
|
|
import { mydb } from "../../lib/mySupabase.ts";
|
|
import { corsHeaders, jsonResponse, errorResponse } from "../../lib/utils.ts";
|
|
|
|
/**
|
|
* POST /appointments/confirm/:token ou /appointments/confirm
|
|
* Confirmar consulta via link com token temporário ou diretamente com appointment_id
|
|
*
|
|
* Params (via URL):
|
|
* token: string (UUID gerado e enviado por email/SMS) - opcional
|
|
*
|
|
* Body:
|
|
* {
|
|
* appointment_id?: uuid (requerido se não usar token),
|
|
* confirmed: boolean,
|
|
* confirmation_method?: string (email|sms|app|link)
|
|
* }
|
|
*
|
|
* Returns:
|
|
* {
|
|
* success: boolean,
|
|
* appointment_id: uuid,
|
|
* confirmed: boolean,
|
|
* message: string
|
|
* }
|
|
*/
|
|
|
|
serve(async (req) => {
|
|
if (req.method === "OPTIONS") {
|
|
return new Response("ok", { status: 200, headers: corsHeaders() });
|
|
}
|
|
|
|
try {
|
|
if (req.method !== "POST") {
|
|
return errorResponse("Method not allowed", 405);
|
|
}
|
|
|
|
const url = new URL(req.url);
|
|
const token = url.pathname.split("/").pop();
|
|
const body = await req.json();
|
|
const { appointment_id: bodyAppointmentId, confirmed, confirmation_method } = body;
|
|
|
|
let appointmentId = bodyAppointmentId;
|
|
|
|
// Se um token foi fornecido na URL, validar e extrair appointment_id
|
|
if (token && token !== "appointments-confirm" && !bodyAppointmentId) {
|
|
const tempTokenRes = await mydb
|
|
.from("temp_tokens")
|
|
.select("*")
|
|
.eq("token", token)
|
|
.eq("purpose", "appointment_confirmation")
|
|
.single();
|
|
|
|
if (tempTokenRes.error || !tempTokenRes.data) {
|
|
return errorResponse("Token inválido ou expirado", 400);
|
|
}
|
|
|
|
const tempToken = tempTokenRes.data;
|
|
|
|
// Verificar se token expirou
|
|
if (new Date(tempToken.expires_at) < new Date()) {
|
|
return errorResponse("Token expirado", 400);
|
|
}
|
|
|
|
appointmentId = tempToken.payload?.appointment_id;
|
|
|
|
// Marcar token como usado
|
|
await mydb
|
|
.from("temp_tokens")
|
|
.update({ used_at: new Date().toISOString() })
|
|
.eq("token", token);
|
|
}
|
|
|
|
if (!appointmentId) {
|
|
return errorResponse("appointment_id é obrigatório", 400);
|
|
}
|
|
|
|
// Atualizar status da consulta se confirmado
|
|
if (confirmed) {
|
|
const updateRes = await mydb
|
|
.from("appointments")
|
|
.update({
|
|
status: "confirmed",
|
|
confirmation_status: "confirmed",
|
|
confirmed_at: new Date().toISOString(),
|
|
updated_at: new Date().toISOString(),
|
|
})
|
|
.eq("id", appointmentId);
|
|
|
|
if (updateRes.error) {
|
|
console.error("[confirm] Erro ao atualizar appointment:", updateRes.error);
|
|
}
|
|
} else {
|
|
// Se não confirmado, cancelar a consulta
|
|
const updateRes = await mydb
|
|
.from("appointments")
|
|
.update({
|
|
status: "cancelled",
|
|
confirmation_status: "declined",
|
|
cancelled_at: new Date().toISOString(),
|
|
cancellation_reason: "Paciente não pode comparecer",
|
|
updated_at: new Date().toISOString(),
|
|
})
|
|
.eq("id", appointmentId);
|
|
|
|
if (updateRes.error) {
|
|
console.error("[confirm] Erro ao cancelar appointment:", updateRes.error);
|
|
}
|
|
}
|
|
|
|
// Registrar confirmação no banco
|
|
await mydb.from("appointment_history").insert({
|
|
appointment_id: appointmentId,
|
|
field_changed: "confirmation_status",
|
|
old_value: "pending_confirmation",
|
|
new_value: confirmed ? "confirmed" : "not_confirmed",
|
|
change_reason: `Confirmação via ${confirmation_method || "link"}`,
|
|
});
|
|
|
|
// Audit log
|
|
await mydb.from("audit_log").insert({
|
|
action: "confirm_appointment",
|
|
target_type: "appointment",
|
|
target_id: appointmentId,
|
|
payload: { confirmed, confirmation_method },
|
|
});
|
|
|
|
return jsonResponse({
|
|
success: true,
|
|
appointment_id: appointmentId,
|
|
confirmed,
|
|
message: confirmed
|
|
? "Consulta confirmada com sucesso!"
|
|
: "Consulta desmarcada com sucesso",
|
|
});
|
|
} catch (error: unknown) {
|
|
console.error("[confirm]", error);
|
|
const err = error as Error;
|
|
return errorResponse(err.message, 500);
|
|
}
|
|
});
|