Strapi v4
const { errors } = require(‘@strapi/utils’);
const { ForbiddenError } = errors;
module.exports = {
async beforeCreate(event) {
const { data } = event.params;
console.log(“before create hook triggered”);
// Check if there are already 50 records created today
const todayStart = new Date();
todayStart.setHours(0, 0, 0, 0);const todayEnd = new Date(); todayEnd.setHours(23, 59, 59, 999); const count = strapi.db.query('api::xxx-form-entry.xxx-form-entry').count({ where: { created_at: { $gte: todayStart, $lte: todayEnd, }, }, }).then(function(count) { console.log(count); if (count >= 1) { // You can customize the error message throw new ForbiddenError("Cannot create more than 1 entries in a day"); } }); }
};
For above when i am hitting the Rest API, it does throw error but causes strapi process to restart and obviosly which causes API to fail, whats the best way to handle it