import { define, State } from "../../../utils.ts"; import { Context } from "fresh"; import { deleteUserPin, getUserPin } from "../../../services/userService.ts"; export const handler = define.handlers({ async POST(ctx: Context) { try { // Resolve user ID from authenticated state/session (cookie-backed) const userId = ctx.state.auth?.userId ?? ctx.state.data.user_id?.trim(); if (!userId) { return new Response( JSON.stringify({ error: "User not authenticated" }), { status: 401, headers: { "Content-Type": "application/json" }, }, ); } // Check if user pin exists before deleting const existingPin = await getUserPin(userId); if (!existingPin) { return new Response( JSON.stringify({ error: "User pin not found" }), { status: 404, headers: { "Content-Type": "application/json" }, }, ); } // Delete user pin from database await deleteUserPin(userId); return new Response( JSON.stringify({ success: true, message: "User pin deleted successfully", user_id: userId, }), { status: 200, headers: { "Content-Type": "application/json" }, }, ); } catch (error) { console.error("Error deleting user pin:", error); return new Response( JSON.stringify({ error: "Internal server error" }), { status: 500, headers: { "Content-Type": "application/json" }, }, ); } }, });