HotTRDealsBackend/adapters/responses/vote.adapter.js
2026-01-23 17:28:21 +00:00

33 lines
814 B
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const { z } = require("zod");
const VoteBodySchema = z.object({
dealId: z.coerce.number().int().positive(),
voteType: z.coerce
.number()
.int()
.refine((v) => v === 1 || v === 0 || v === -1, {
message: "voteType 1, 0 veya -1 olmalı",
}),
});
function mapVoteRequestToVoteInput(req) {
const parsed = VoteBodySchema.safeParse(req.body);
if (!parsed.success) {
const err = new Error(parsed.error.issues?.[0]?.message || "Geçersiz istek");
err.statusCode = 400;
err.details = parsed.error.flatten();
throw err;
}
const userIdRaw = req.user?.userId;
const userId = Number(userIdRaw);
return {
userId,
dealId: parsed.data.dealId,
voteType: parsed.data.voteType, // 1 | 0 | -1
};
}
module.exports = { mapVoteRequestToVoteInput, VoteBodySchema };