Customized Strapi backend (content-manager) controller causes endless page reload

System Information
  • Strapi Version: 4.10.7
  • Operating System: macos
  • Database: postgresql
  • Node Version: 18 lts

I want to count rating automatically, but next code causes endless page reload. If I wouldnt use sanitize, the page would reload normally but would be visually empty untill next manual reload. How to handle that?

const oldUpdate = strapi.plugin("content-manager").controller('collection-types').update;
strapi.plugin("content-manager").controller('collection-types').update = async function (ctx) {
  if (ctx.params.model === 'api::book.book') {
    const { id } = ctx.params;
    const { data, files, } = parseMultipartData(ctx);
    const sumOfRatings = data.rating.otherScores.reduce((acc, item) => acc + item.stars, 0);
    const averageRating = sumOfRatings / data.rating.otherScores.length;
    data.rating.generalScore.stars = averageRating
    const entity = await strapi.entityService.update("api::book.book", id, {
      data
    }, { files })
    const sanitizedEntity = await sanitize.contentAPI.output(entity, strapi.getModel("api::book.book"));
    return { data: sanitizedEntity };
  } else { await oldUpdate(ctx); }
}