diff --git a/src/pages/api/update_clicks/[user]/[button].astro b/src/pages/api/update_clicks/[user]/[button].astro new file mode 100644 index 0000000..1323bf8 --- /dev/null +++ b/src/pages/api/update_clicks/[user]/[button].astro @@ -0,0 +1,48 @@ +--- +import { downloadImageUser, getUserId, supabase } from "@/supabase"; +const { user, button } = Astro.params; +console.log("user,button: ", user, button); + +const user_user = await getUserId(user || ""); +if (!user_user) { + return new Response(null, { + status: 404, + statusText: "Not Found" + }); +} else { + const link = user_user.links.find((e) => e.id == button); + + if (!link) { + return new Response(null, { + status: 404, + statusText: "Link Not Found" + }); + } + + if (!link.clicks) { + link.clicks = [new Date().toISOString()]; + } else { + link.clicks.push(new Date().toISOString()); + } + + const updatedLinks = user_user.links.map((e) => (e.id === button ? link : e)); + console.log("updatedLinks: ", updatedLinks); + + const { error } = await supabase.from("profiles").upsert({ + id: user_user.id as string, + links: updatedLinks + }); + console.error("error: ", error); + + const data = { + username: user_user.username, + displayname: user_user.displayname, + pic: downloadImageUser(user_user.pic) + }; + + return new Response(JSON.stringify(data), { + status: 200, + statusText: "OK" + }); +} +---