Return redirect if singleton entity value is set

System Information
  • Strapi Version: 4.24.0
  • Operating System: Ubuntu
  • Database: MySql
  • Node Version: 18.19.0
  • NPM Version: 10.2.3

I’ve got a strapi app which allows users to cast a vote. For this, the user gets a link which allows them to cast a vote, creating a vote instance in my backend.

I do want to limit the ability to vote to a certain timespan however. For this, I created a single type called votingPeriod which has a boolean called isVotingAllowed.

The bound route to cast a vote is /api/votes.

Now I’ve got a middleware set up which looks like this:

module.exports = (config, {strapi}) => {
  // Add your own logic here.
  return async (ctx, next) => {
    const votingPeriod = await strapi.entityService.findOne(
      'api::voting-period.voting-period',
      1
    );

    if(votingPeriod.isVotingAllowed) {
      // TODO: allow request, create vote instance, return 200 
      await next();

    } else {
      // TOOD: deny request, set appropriate error code, return response
      await next();
    }

  };
};

The system runs into that function, unfortunately I don’t have a clue as what to do in the else branch.

How would I return a response and the appropriate status code with some content like

{
  'message': 'voting currently not allowed'
}

if the instance of votingPeriod I received in the request has isVotingAllowed set to false?

Any help would be appreciated,

Greetz