49 lines
1.4 KiB
TypeScript
49 lines
1.4 KiB
TypeScript
import { castDomainVote } from "../../../db/domains";
|
|
import { createNotification } from "../../../db/notifications";
|
|
import { getProfile } from "../../../db/profile";
|
|
import { usePrisma } from "../../../utils/prisma";
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
const user = await requireUser(event);
|
|
const id = getRouterParam(event, "id");
|
|
if (!id)
|
|
throw createError({ statusCode: 400, message: "Submission ID fehlt" });
|
|
|
|
const body = await readBody(event);
|
|
const vote = body?.vote as string;
|
|
if (vote !== "yes" && vote !== "no") {
|
|
throw createError({
|
|
statusCode: 400,
|
|
message: "vote muss 'yes' oder 'no' sein",
|
|
});
|
|
}
|
|
|
|
const db = usePrisma();
|
|
const [result, submission] = await Promise.all([
|
|
castDomainVote(user.id, id, vote),
|
|
db.domainSubmission.findUnique({
|
|
where: { id },
|
|
select: { userId: true, domain: true },
|
|
}),
|
|
]);
|
|
|
|
if (
|
|
submission?.userId &&
|
|
submission.userId !== user.id &&
|
|
(result as any).yesVotes !== undefined
|
|
) {
|
|
const pr = await getProfile(user.id).catch(() => null);
|
|
const actorName = pr?.nickname ?? pr?.username ?? "Jemand";
|
|
const actorAvatar = pr?.avatar ?? undefined;
|
|
createNotification({
|
|
recipientId: submission.userId,
|
|
type: "domain_vote",
|
|
actorName,
|
|
actorAvatar,
|
|
preview: submission.domain,
|
|
}).catch(() => {});
|
|
}
|
|
|
|
return result;
|
|
});
|