How to check User info and update it after file upload?

System Information
  • Strapi Version: 3.5.1
  • Operating System: Win 10

On my User model I have a media field avatar and a number field points. When the user uploads an avatar image, I want to check if he already has one. If he does - just upload the image and replace the existing one. Otherwise - upload the image and add X points to his tally.

Though I can easily do that in the client, I prefer to have all the points management on the server. However, I can’t figure out a way to do that. Any suggestions, please?

@Igal-Kleiner

Take a look at a custom update controller for clients API , which updates the client’s information and their avatar. The old avatar gets deleted if it exists.

const { parseMultipartData, sanitizeEntity } = require('strapi-utils');
const _ = require('lodash');

module.exports = {
  async update(ctx) {
    //id of the client which I want to update, it get's it from /clients/:id
    const { id } = ctx.params;
    let additionalPoints = 10; //additional points for the first time uploaded avatar.
    let entity;
    


    // get the existing object of the client,
    
    let client = await strapi.services.clients.findOne({ id });
    // if it doesnt exist then stop here as there is nothing to update.
    if (!client) {
      return ctx.badRequest(`Client was not found.`);
    }

    if (ctx.is('multipart')) {
      const { data, files } = parseMultipartData(ctx);

      //check if request constains any files
      if (!_.isEmpty(files)) {
        //if client has an existing avatar (otherwise it returns null)
        if (client.avatar) {
          //get the current avatar and delete it
          const file = await strapi.plugins['upload'].services.upload.fetch({ id: client.avatar.id });
          if (!file) {
            return ctx.notFound('file.notFound');
          }
          await strapi.plugins['upload'].services.upload.remove(file);
          additionalPoints = 0; //adding 0 points, since the client already had an avatar.
        }
        //update the client with a new amount of points and avatar
        client.points = client.points + additionalPoints; //the new amount of points
        entity = await strapi.services.clients.update({ id }, {...data, points: client.points} , {
          files,
        });
      } else {
        //update client without files
        entity = await strapi.services.clients.update({ id }, data);
      }
    } else {
      entity = await strapi.services.clients.update({ id }, ctx.request.body);
    }
    return sanitizeEntity(entity, { model: strapi.models.clients });
  },
};

In this case I am making a request to /clients/:ID

With the following appended fields

formData.append(‘files’, this.file); //avatar here, as file
formData.append(‘data’, '{"firstname":"Sunny Son"}'); //other Client's fields that need to be updated

I hope you got the logic. You Should delete all that is related to data updates if you don’t need it and keep only the avatar & points part.