feat: ajout du nombre de vote total
This commit is contained in:
@@ -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}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user