Create limit of cases per user per month

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • amedellin
    Junior Member
    • Jan 2020
    • 6

    Create limit of cases per user per month

    How can a limit the creation of cases per user per month??

    For example.

    The user only can create up to 5 cases per month.

    Up to 5 cases on january
    up tu 5 cases on febrary

    Etc
  • Maximus
    Senior Member
    • Nov 2018
    • 2731

    #2
    Hello,
    there is no such embedded functionality. I assume that it is possible to achieve with the BPM. For example, such logic flowchart: if a user has the 5 linked case records that created by him in the current month, then unassign a role from him (or change a role to restrict the case creation). If in the current month a user has not the 5 linked case records that created by him in, then assign a role to him.

    Comment

    • telecastg
      Active Community Member
      • Jun 2018
      • 907

      #3
      This is not the Developer forum so I am not sure if my solution applies, but if you have not purchased the Advance Pack yet or are interested in developing/coding your own solution, here is what I am currently doing in our application to limit the number of "Service Requests" that a portal user can submit:

      1) Create a custom front-end controller to intercept the "Create" button action on the top right corner of the list view eg: client/custom/src/controllers/{{your-entity}}.js, in my case "service-ticket"

      Code:
      Espo.define('service-request-management:controllers/service-ticket', 'controllers/record', function (Dep) {
      
          return Dep.extend({
      
              // intercept the default "create" action
              create: function (options) {
                  options = options || {};            
                  // if the user is a portal user, filter for excessive tickets:
                  // more than one non-priority ticket submitted within the past 3 months
                  var okToSubmit = true;
                  var userObject = this.getUser();
                  var self = this;
                  self.createOptions = options;
                  if(userObject.attributes.isPortalUser) {
                      var contactId = userObject.attributes.contactId;
                      // use plain ajax to invoke an entry point action which returns the latest excessive ticket date if any
                      // the response text format will be: "MM/DD/YYYY" or an empty string
                      url = '?entryPoint=serviceTicketHandler&contactId='+contactId+'&action=portalPreCreateTicketFilter';    
                      var xmlhttp = new XMLHttpRequest();
                      xmlhttp.onreadystatechange = function() {
                          if (xmlhttp.readyState === XMLHttpRequest.DONE) {   // XMLHttpRequest.DONE == 4
                              if (xmlhttp.status === 200) {  
                                  var serverResponse = xmlhttp.responseText;
                                  var preCreateTicketData = JSON.parse(serverResponse);
                                  if(preCreateTicketData.latestExcessiveTicket) {
                                      var options = {};
                                      options.lastExcessive = preCreateTicketData.latestExcessiveTicket;
                                      options.nextTicket = preCreateTicketData.nextAllowedTicket;
                                      options.ticketReset = preCreateTicketData.ticketReset;
                                      self.viewFactory.create('service-request-management:views/service-ticket/modals/service-ticket-filter',options,function(view){
                                          // display the excessive ticket warning modal
                                          view.render();
                                      });                                
                                  } else {
                                      // continue with the create action
                                      self.actionProceedToCreate(self.createOptions);                                
                                  }                            
                              }
                              else if (xmlhttp.status === 400) {
                                  alert('There was an error 400');
                              }
                              else {
                                  alert('something else other than 200 was returned');
                              }                
                          }                        
                      };
                      xmlhttp.open("POST",url , true);
                      xmlhttp.send();                  
                  } else {  
                      // if the user is not a portal user, continue with the create action
                      self.actionProceedToCreate(self.createOptions);
                  }
              },
      
              // custom function to proceed with the create action after checking and warning for excessive tickets
              actionProceedToCreate: function (options) {          
                  var self = this;
                  // close the open warning modal if necessary
                  var $ticketWarning = $('a[data-name="cancelExcessiveTicketWarning"]');
                 if($ticketWarning.length !== 0) {
                      $ticketWarning.click();
                  }                
                  this.getModel().then(function (model) {
                      if (options.relate) {
                          model.setRelate(options.relate);
                      }
                      var o = {
                          scope: this.name,
                          model: model,
                          returnUrl: options.returnUrl,
                          returnDispatchParams: options.returnDispatchParams,
                          params: options
                      };
                      if (options.attributes) {
                          model.set(options.attributes);
                      }
                      // determine if the user is a Tenant (portal user) and update the model accordingly
                      var userObject = self.getUser();
                      if(userObject.attributes.isPortalUser) {
                          model.attributes.isTenant = true;                
                      } else {
                          model.attributes.isTenant = false;                                
                      }          
                      this.prepareModelCreate(model, options);
                      this.main(this.getViewName('edit'), o);
                  }.bind(this));            
              }
      
          });
      });
      2) Create the entry point that will receive the controller's ajax call, will execute the back-end query and return the response value

      Code:
      <?php
      
      namespace Espo\Modules\ServiceRequestManagement\EntryPoints;
      
      use \Espo\Core\Exceptions\NotFound;
      use \Espo\Core\Exceptions\Forbidden;
      use \Espo\Core\Exceptions\BadRequest;
      
      class ServiceTicketHandler extends \Espo\Core\EntryPoints\Base
      {
          public static $authRequired = true;
          public $excessDays = 122;
      
           // default action
          public function run()
          {
              // determine the threshold date for excessive ticket submission
              $dt = new \DateTime();
              $dt->modify('-' . $this->excessDays . ' days');
              $excessiveTicketThreshold = $dt->format('Y-m-d H:i:s');
              // find the most recent ticket within the excessive threshold  
              $sql = 'SELECT `created_at` FROM `service_ticket` WHERE `created_at` > "'.$excessiveTicketThreshold.'" AND `account_id` = "'.$accountId.'" AND `is_priority` <> "1" AND `deleted` <> "1" ORDER By `created_at` DESC LIMIT 1';
              $pdo =  $this->getEntityManager()->getPDO();   
              $ps = $pdo->query($sql);
              if ($ps) {
                  $dataArr = $ps->fetchAll();
              }
              if($dataArr[0]['created_at']) {
                  $latestDt = new \DateTime($dataArr[0]['created_at']);
                  $latestExcessiveTicketDate = $latestDt->format('m/d/Y');            
                  $nextAllowedTicketDate = $latestDt->modify('+' . $this->excessDays . ' days')->format('m/d/Y');  
                  $resetDt = new \DateTime();            
                  $ticketResetDate = $resetDt->modify('+' . $this->excessDays . ' days')->format('m/d/Y');
              } else {
                  $latestExcessiveTicketDate = '';
                  $nextAllowedTicketDate = '';
                  $ticketResetDate = '';
              }
              $excessiveTicketData = array('latestExcessiveTicket' => $latestExcessiveTicketDate, 'nextAllowedTicket' => $nextAllowedTicketDate,'ticketReset' => $ticketResetDate);                
              return json_encode($excessiveTicketData);        
          }
      }
      In our case, if the client has submitted an excessive number of tickets (per our criteria) a modal warning view is displayed, but in your custom front-end controller you could specify any other action, for example display an alert message and stop the case creation.

      3) Change the metadata settings of your entity's clientDefs to specify the custom front-end controller:
      Code:
          "controller": "service-request-management:controllers/service-ticket",

      Comment

      Working...