2026-01-20 09:02:26 -06:00

59 lines
1.5 KiB
TypeScript

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<State>) {
try {
// Get user ID from session data
const userId = ctx.state.data.user_id;
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" },
},
);
}
},
});