43 lines
945 B
JavaScript
43 lines
945 B
JavaScript
const voteDb = require("../db/vote.db");
|
||
|
||
async function voteDeal({ dealId, userId, voteType }) {
|
||
if (!dealId || !userId || voteType === undefined) {
|
||
const err = new Error("Eksik veri");
|
||
err.statusCode = 400;
|
||
throw err;
|
||
}
|
||
|
||
if (![ -1, 0, 1 ].includes(voteType)) {
|
||
const err = new Error("voteType -1, 0 veya 1 olmalı");
|
||
err.statusCode = 400;
|
||
throw err;
|
||
}
|
||
|
||
return voteDb.voteDealTx({ dealId, userId, voteType });
|
||
}
|
||
|
||
async function getVotes(dealId) {
|
||
const votes = await voteDb.findVotes(
|
||
{ dealId },
|
||
{
|
||
select: {
|
||
id: true,
|
||
dealId: true,
|
||
userId: true,
|
||
voteType: true,
|
||
createdAt: true,
|
||
lastVotedAt: true,
|
||
},
|
||
orderBy: { createdAt: "desc" },
|
||
}
|
||
)
|
||
|
||
return votes.map((vote) => ({
|
||
...vote,
|
||
createdAt: vote.createdAt.toISOString(),
|
||
lastVotedAt: vote.lastVotedAt.toISOString(),
|
||
}))
|
||
}
|
||
|
||
module.exports = { voteDeal, getVotes };
|