Limit number of users created for someone


#1

I am working on a workout app where a personal trainer can register and then create clients to train and keep track of their info. My question is, how do I limit the number of clients each trainer can create to lets say 50. I have a field on the users collection called client limit but im not too sure how to go about checking that value and making sure they are only allowed to create a certain number of accounts and preventing any further creations until they delete one of their clients. Any help would be appreciated.

Thanks


#2

Using collection hooks, you could use an after insert hook that increments a “clients number” field by one after every client insert. (Be careful to only do this on the server otherwise it will increment twice).

Then using Collection.deny, you could check that the user making the insert has a “clients number” below your desired limit, and if not return false and deny the insert.

Though there are several ways you could do it. :smile:


#3

Thanks. I figured out a different way but appreciate your help. I added a field to the users collection and just check against it in the method. Have to figure out how to return an error to display now. I’m returning to a different template after the method but need the error returned from the calling template.


#4

No problem! Assuming this is relating to some sort of form, are you using AutoForm? You could plug into onSubmit, onSuccess and onError hooks.

So something like:

AutoForm.hooks({
   clientForm: {
    onSubmit: function () {
          // run method
    },
    onSuccess: function () {
          // route to new template
    },
    onError: function () {
   // display error to user 
   }
});

#5

I’ll give that a try. I’m capturing the event in the templates events function though. Is there any disadvantage of using my own event capture?


#6

Almost forgot. Can I still get form values and pass them to the method I call?