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

Limit number of votes on backend #237

Open
diegoalzate opened this issue Mar 1, 2024 · 4 comments
Open

Limit number of votes on backend #237

diegoalzate opened this issue Mar 1, 2024 · 4 comments
Labels
improvement improving an existing feature

Comments

@diegoalzate
Copy link
Contributor

overview

user can use api to vote as many times as he wants

@diegoalzate diegoalzate added the bug Something isn't working label Mar 1, 2024
@MartinBenediktBusch
Copy link
Collaborator

@diegoalzate the may amount of votes a user can do on a given forum_question should be the max amount of available hearts. For example, currently that number is set to 20. How is it currently possible that a user can vote more than that?

@diegoalzate
Copy link
Contributor Author

@MartinBenediktBusch this is true, but only enforced by the frontend. If the user finds the api to vote, then he user can in theory vote infinite times.

@diegoalzate diegoalzate changed the title There is no max amount votes Limit number of votes on backend Mar 5, 2024
@MartinBenediktBusch
Copy link
Collaborator

MartinBenediktBusch commented Mar 20, 2024

Overview

@diegoalzate Here a suggested solution. The solution adds two maxima to the saveVotes function:

  • The first one is a maximum maxAllowedVotesPerRequest validate the number of votes a user attempts to submit in each request to ensure to not exceed the allowed limit with a single vote. This check is especially relevant for the first vote.
  • The second maxAllowedVotes checks whether a user has already exceeded the maximum number of votes

Proposed solution

export async function saveVotes(dbPool: PostgresJsDatabase<typeof db>) {
  return async function (req: Request, res: Response) {
    const userId = req.session.userId;
    const out: db.Vote[] = [];
    const errors = [];

    const reqBody = z
      .array(
        z.object({
          optionId: z.string(),
          numOfVotes: z.number().min(0),
        }),
      )
      .safeParse(req.body);

    if (!reqBody.success) {
      return res.status(400).json({ errors: reqBody.error.errors });
    }

    const maxAllowedVotesPerRequest = 'some number';
    const totalRequestedVotes = req.body.reduce((total, vote) => total + vote.numOfVotes, 0);
    if (totalRequestedVotes > maxAllowedVotesPerRequest) {
      return res.status(400).json({ errors: [{ message: 'Exceeded maximum allowed votes per request.' }] });
    }

    // Check if the user has exceeded the maximum allowed votes
    const userVoteCount = await getUserVoteCount(dbPool, userId);
    const maxAllowedVotes = 'some other number'
    const remainingVotes = maxAllowedVotes - userVoteCount;
    if (totalRequestedVotes > remainingVotes) {
      return res.status(400).json({ errors: [{ message: 'Exceeded maximum allowed votes.' }] });
    }

    // Rest of the code to save votes...
  };
}

Limitations

I dont like the fact that the two maxima are hard coded. Is there a way to load it through and environemnt variable or set them by the admin?

@diegoalzate
Copy link
Contributor Author

we could somehow set them in the admin but this would require a new field somewhere. a place to store info

@MartinBenediktBusch MartinBenediktBusch self-assigned this Mar 22, 2024
@MartinBenediktBusch MartinBenediktBusch removed their assignment May 14, 2024
@MartinBenediktBusch MartinBenediktBusch added improvement improving an existing feature and removed bug Something isn't working labels May 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
improvement improving an existing feature
Projects
None yet
Development

No branches or pull requests

2 participants