// routes/api/user/preferences.ts import { define } from "../../../utils.ts"; import { updateUser } from "../../../services/userService.ts"; export const handler = define.handlers({ async POST(ctx) { const { user_id } = ctx.state.data; if (!user_id) { return new Response(JSON.stringify({ error: "Unauthorized" }), { status: 401, headers: { "Content-Type": "application/json" }, }); } try { const body = await ctx.req.json(); const updates: { notification_preferences_json?: string; payment_preferences_json?: string; } = {}; // Handle notification preferences if (body.notification_preferences) { updates.notification_preferences_json = JSON.stringify( body.notification_preferences, ); } // Handle payment preferences if (body.payment_preferences) { updates.payment_preferences_json = JSON.stringify( body.payment_preferences, ); } if (Object.keys(updates).length === 0) { return new Response( JSON.stringify({ error: "No preferences provided for update" }), { status: 400, headers: { "Content-Type": "application/json" }, }, ); } const updatedUser = await updateUser(user_id, updates); if (!updatedUser) { return new Response( JSON.stringify({ error: "Failed to update user preferences" }), { status: 500, headers: { "Content-Type": "application/json" }, }, ); } return new Response( JSON.stringify({ message: "Preferences updated successfully" }), { status: 200, headers: { "Content-Type": "application/json" }, }, ); } catch (error) { console.error("API Error updating user preferences:", error); return new Response( JSON.stringify({ error: "Internal Server Error" }), { status: 500, headers: { "Content-Type": "application/json" }, }, ); } }, });