Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix limit check #3821

Merged
merged 1 commit into from
Dec 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions lib/Controller/VoteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ public function list(int $pollId): JSONResponse {
// #[FrontpageRoute(verb: 'PUT', url: '/vote/{optionId}/set/{setTo}')]
public function set(int $optionId, string $setTo): JSONResponse {
$option = $this->optionService->get($optionId);

return $this->response(fn () => [
'vote' => $this->voteService->set($optionId, $setTo),
'poll' => $this->pollService->get($option->getPollId()),
'options' => $this->optionService->list($option->getPollId()),

]);
}

Expand All @@ -73,7 +73,12 @@ public function set(int $optionId, string $setTo): JSONResponse {
#[FrontpageRoute(verb: 'DELETE', url: '/poll/{pollId}/user/{userId}', postfix: 'named')]
#[FrontpageRoute(verb: 'DELETE', url: '/poll/{pollId}/user', postfix: 'self')]
public function delete(int $pollId, string $userId = ''): JSONResponse {
return $this->response(fn () => ['deleted' => $this->voteService->deleteUserFromPoll($pollId, $userId)]);
return $this->response(fn () => [
'deleted' => $this->voteService->deleteUserFromPoll($pollId, $userId),
'poll' => $this->pollService->get($pollId),
'options' => $this->optionService->list($pollId),
'votes' => $this->voteService->list($pollId)
]);
}

/**
Expand All @@ -85,6 +90,11 @@ public function delete(int $pollId, string $userId = ''): JSONResponse {
#[OpenAPI(OpenAPI::SCOPE_IGNORE)]
#[FrontpageRoute(verb: 'DELETE', url: '/poll/{pollId}/votes/orphaned')]
public function deleteOrphaned(int $pollId, string $userId = ''): JSONResponse {
return $this->response(fn () => ['deleted' => $this->voteService->deleteUserFromPoll($pollId, $userId, deleteOnlyOrphaned: true)]);
return $this->response(fn () => [
'deleted' => $this->voteService->deleteUserFromPoll($pollId, $userId, deleteOnlyOrphaned: true),
'poll' => $this->pollService->get($pollId),
'options' => $this->optionService->list($pollId),
'votes' => $this->voteService->list($pollId)
]);
}
}
18 changes: 18 additions & 0 deletions lib/Service/VoteService.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use OCA\Polls\Db\Vote;
use OCA\Polls\Db\VoteMapper;
use OCA\Polls\Event\VoteSetEvent;
use OCA\Polls\Exceptions\NotFoundException;
use OCA\Polls\Exceptions\VoteLimitExceededException;
use OCA\Polls\UserSession;
use OCP\AppFramework\Db\DoesNotExistException;
Expand Down Expand Up @@ -65,6 +66,18 @@ private function checkLimits(Option $option): void {
return;
}

private function checkVoteLimit(Option $option): void {
// check, if the optionlimit is reached or exceeded, if one is set
if ($option->getIsLockedByOptionLimit()) {
throw new VoteLimitExceededException();
}

if ($option->getIsLockedByVotesLimit()) {
throw new VoteLimitExceededException;
}
return;
}

/**
* Set vote
*/
Expand All @@ -73,6 +86,11 @@ public function set(int $optionId, string $setTo): ?Vote {
$poll = $this->pollMapper->find($option->getPollId());
$poll->request(Poll::PERMISSION_VOTE_EDIT);

if ($option->getIsLocked()) {
$this->checkVoteLimit($option);
throw new NotFoundException();
}

try {
$this->vote = $this->voteMapper->findSingleVote($poll->getId(), $option->getPollOptionText(), $this->userSession->getCurrentUserId());

Expand Down
20 changes: 14 additions & 6 deletions src/components/VoteTable/VoteItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,21 @@
/**
*
*/
function setVote() {
async function setVote() {
if (isVotable.value) {
votesStore.set({
option: props.option,
setTo: nextAnswer.value,
})
showSuccess(t('polls', 'Vote saved'), { timeout: 2000 })
try {
await votesStore.set({
option: props.option,
setTo: nextAnswer.value,
})
showSuccess(t('polls', 'Vote saved'), { timeout: 2000 })
} catch (error) {
if (error.response.status === 409 && error.response.data.message === 'Vote limit exceeded') {
showError(t('polls', 'Vote already booked out'))
} else {
showError(t('polls', 'Error saving vote'))
}
}
} else {
showError(t('polls', 'Error saving vote'))
}
Expand Down
20 changes: 9 additions & 11 deletions src/stores/votes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export const useVotesStore = defineStore('votes', {
this.$reset()
return
}

const votes: Vote[] = []
response.data.votes.forEach((vote: Vote) => {
if (vote.answer === Answer.Yes) {
Expand All @@ -104,7 +104,7 @@ export const useVotesStore = defineStore('votes', {
throw error
}
},

setItem(payload: { option: Option; vote: Vote }) {
const index = this.list.findIndex((vote: Vote) =>
vote.pollId === payload.option.pollId
Expand All @@ -116,7 +116,7 @@ export const useVotesStore = defineStore('votes', {
}
this.list.push(payload.vote)
},

async set(payload: { option: Option; setTo: Answer }) {
const sessionStore = useSessionStore()
const optionsStore = useOptionsStore()
Expand All @@ -135,17 +135,17 @@ export const useVotesStore = defineStore('votes', {
} catch (error) {
if (error?.code === 'ERR_CANCELED') return
if (error.response.status === 409) {
this.load()
optionsStore.load()
pollStore.load()
throw error
} else {
Logger.error('Error setting vote', { error, payload })
Logger.error('Error setting vote aa', { error, payload })
throw error
}
}
},

async resetVotes() {
Logger.debug('Resetting votes')
const sessionStore = useSessionStore()
try {
let response = null
Expand All @@ -162,7 +162,7 @@ export const useVotesStore = defineStore('votes', {
throw error
}
},

async deleteUser(payload) {
const sessionStore = useSessionStore()
try {
Expand All @@ -178,21 +178,19 @@ export const useVotesStore = defineStore('votes', {
async removeOrphanedVotes() {
const sessionStore = useSessionStore()
const pollStore = usePollStore()
const optionsStore = useOptionsStore()
try {
if (sessionStore.route.name === 'publicVote') {
await PublicAPI.removeOrphanedVotes(sessionStore.route.params.token)
} else {
await VotesAPI.removeOrphanedVotes(sessionStore.route.params.id)
}
pollStore.load()
optionsStore.load()
} catch (error) {
if (error?.code === 'ERR_CANCELED') return
Logger.error('Error deleting orphaned votes', { error })
throw error
}
},

},
})