86 lines
2.3 KiB
TypeScript
86 lines
2.3 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";
|
|
import { logUserAction } from "../../lib/auditLog.ts";
|
|
|
|
/**
|
|
* POST /appointments-checkin
|
|
* Registrar check-in do paciente (chegou no consultório)
|
|
* Adiciona automaticamente à fila virtual
|
|
*
|
|
* Body:
|
|
* {
|
|
* appointment_id: uuid,
|
|
* check_in_method: 'manual' | 'qr_code' | 'nfc' | 'app' | 'kiosk'
|
|
* }
|
|
*/
|
|
|
|
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 body = await req.json();
|
|
const { appointment_id, check_in_method, external_patient_id } = body;
|
|
|
|
if (!external_patient_id) {
|
|
return errorResponse("external_patient_id is required", 400);
|
|
}
|
|
|
|
// Registrar check-in no banco (se houver tabela checkin_tracking)
|
|
// await mydb.from("checkin_tracking").insert({
|
|
// external_appointment_id: appointment_id,
|
|
// external_patient_id,
|
|
// check_in_method,
|
|
// });
|
|
|
|
// Buscar ou criar entrada na virtual_queue
|
|
const queueRes = await mydb
|
|
.from("virtual_queue")
|
|
.select("*")
|
|
.eq("external_appointment_id", appointment_id)
|
|
.single();
|
|
|
|
let position = 1;
|
|
let isNewQueue = false;
|
|
|
|
if (!queueRes.data) {
|
|
// Criar entrada na fila
|
|
const newQueueRes = await mydb
|
|
.from("virtual_queue")
|
|
.insert({
|
|
external_appointment_id: appointment_id,
|
|
external_patient_id,
|
|
position: 1,
|
|
status: "waiting",
|
|
estimated_wait_minutes: 15,
|
|
})
|
|
.select()
|
|
.single();
|
|
|
|
if (newQueueRes.data) {
|
|
position = newQueueRes.data.position;
|
|
isNewQueue = true;
|
|
}
|
|
} else {
|
|
position = queueRes.data.position;
|
|
}
|
|
|
|
return jsonResponse({
|
|
success: true,
|
|
appointment_id,
|
|
checked_in_at: new Date().toISOString(),
|
|
position_in_queue: position,
|
|
});
|
|
} catch (error: unknown) {
|
|
console.error("[checkin]", error);
|
|
const err = error as Error;
|
|
return errorResponse(err.message, 500);
|
|
}
|
|
});
|