2023-04-20 04:20:00 -06:00
|
|
|
import { getAuth } from "@clerk/remix/ssr.server";
|
2023-12-13 14:52:56 -07:00
|
|
|
import { type ActionFunctionArgs, json } from "@remix-run/node";
|
2023-04-20 04:20:00 -06:00
|
|
|
import { createId } from "@paralleldrive/cuid2";
|
|
|
|
import { db } from "~/services/db.server";
|
|
|
|
import { emitter } from "~/services/emitter.server";
|
2023-12-11 22:58:49 -07:00
|
|
|
import { votes } from "~/services/schema.server";
|
2023-04-20 04:20:00 -06:00
|
|
|
|
|
|
|
export async function action({ request, params, context }: ActionFunctionArgs) {
|
|
|
|
const { userId } = await getAuth({ context, params, request });
|
|
|
|
|
|
|
|
if (!userId) {
|
|
|
|
return json("Not Signed In!", {
|
|
|
|
status: 403,
|
|
|
|
statusText: "UNAUTHORIZED!",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const data = await request.json();
|
|
|
|
const roomId = params.roomId;
|
|
|
|
|
|
|
|
const upsertResult = await db
|
|
|
|
.insert(votes)
|
|
|
|
.values({
|
|
|
|
id: `vote_${createId()}`,
|
|
|
|
created_at: Date.now().toString(),
|
|
|
|
value: data.value,
|
|
|
|
userId: userId || "",
|
|
|
|
roomId: roomId || "",
|
|
|
|
})
|
|
|
|
.onConflictDoUpdate({
|
|
|
|
target: [votes.userId, votes.roomId],
|
|
|
|
set: {
|
|
|
|
created_at: Date.now().toString(),
|
|
|
|
value: data.value,
|
|
|
|
userId: userId || "",
|
|
|
|
roomId: roomId,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2024-01-06 00:37:58 -07:00
|
|
|
const success = upsertResult.count > 0;
|
2023-04-20 04:20:00 -06:00
|
|
|
|
|
|
|
if (success) {
|
2023-12-01 14:57:31 -07:00
|
|
|
emitter.emit("nodes", "votes");
|
2023-04-20 04:20:00 -06:00
|
|
|
|
|
|
|
return json(upsertResult, {
|
|
|
|
status: 200,
|
|
|
|
statusText: "SUCCESS",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|