feat: ajout du nombre de vote total

This commit is contained in:
2026-01-24 22:14:49 +04:00
parent a184665ed1
commit c2f8a4fb19
2 changed files with 100 additions and 9 deletions
+40
View File
@@ -369,3 +369,43 @@ export async function getUserVote({
throw error
}
}
export async function getVoteCounts({
accessToken,
versionId
}) {
try {
const votes = await directusClient.request(
withToken(
accessToken,
readItems('votes', {
filter: {
content_version_id: {_eq: versionId}
},
fields: ['vote']
})
)
)
const counts = {
positive: 0,
negative: 0,
total: 0
}
for (const v of votes) {
if (v.vote === 1) {
counts.positive++
} else if (v.vote === -1) {
counts.negative++
}
}
counts.total = counts.positive - counts.negative
return counts
} catch (error) {
console.error('Error fetching vote counts:', error)
return {positive: 0, negative: 0, total: 0}
}
}