Create interests endpoint for Vercel
cgen-d9a0d994326e4ad3a4efd0d486d4049f
This commit is contained in:
parent
90a058aea3
commit
0d4b5c3506
1 changed files with 27 additions and 0 deletions
27
api/interests.ts
Normal file
27
api/interests.ts
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
import type { VercelRequest, VercelResponse } from "@vercel/node";
|
||||
import { getAdminClient } from "./_supabase";
|
||||
|
||||
export default async function handler(req: VercelRequest, res: VercelResponse) {
|
||||
if (req.method !== "POST") return res.status(405).json({ error: "Method not allowed" });
|
||||
const { user_id, interests } = (req.body || {}) as { user_id?: string; interests?: string[] };
|
||||
if (!user_id || !Array.isArray(interests)) return res.status(400).json({ error: "invalid payload" });
|
||||
|
||||
try {
|
||||
const admin = getAdminClient();
|
||||
const { error: delErr } = await admin.from("user_interests").delete().eq("user_id", user_id);
|
||||
if (delErr) return res.status(500).json({ error: delErr.message });
|
||||
|
||||
if (interests.length) {
|
||||
const rows = interests.map((interest) => ({ user_id, interest }));
|
||||
const { error } = await admin.from("user_interests").insert(rows);
|
||||
if (error) return res.status(500).json({ error: error.message });
|
||||
}
|
||||
|
||||
return res.json({ ok: true });
|
||||
} catch (e: any) {
|
||||
if (/SUPABASE_/.test(String(e?.message || ""))) {
|
||||
return res.status(500).json({ error: `Server misconfigured: ${e.message}` });
|
||||
}
|
||||
return res.status(500).json({ error: e?.message || String(e) });
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue