Dynamic filtering based on current user

I have index called Customer and they have columns like name, email, password, user and user_id.
I also have a model ‘Users’ which has the column ‘admin’ (boolean value).

If the current user is an admin, I want to search ALL customers. However, if the current user is not an admin, I would like to filter my customer search results based on the user_id. However, this id changes depending on who is logged into my site, so the user_id is not a constant.

My code is as follows:

  var client = algoliasearch('x', 'x');
  var index = client.initIndex('Customer');
  let admin = document.getElementById("admin-field").innerText;
  let userId = document.getElementById("id-field").innerText;
  //initialize autocomplete on search input (ID selector must match)
  autocomplete('#aa-search-input',
    { hint: false }, {
      source: autocomplete.sources.hits(index, {
        hitsPerPage: 5
      }),
      //value to be displayed in input control after user's suggestion selection
      displayKey: 'name',
      //hash of templates used when rendering dataset
      templates: {
          //'suggestion' templating function used to render a single suggestion
          suggestion: function(suggestion) {
            if (admin == "true") {
              console.log(suggestion);
              console.log(suggestion.user.id);
              return `<a href="/customers/${suggestion.id}">` +
              suggestion._highlightResult.first_name.value + ' ' + suggestion._highlightResult.last_name.value + '</a>';
            } else if (userId == suggestion.user.id) {
              return `<a href="/customers/${suggestion.id}">` +
              suggestion._highlightResult.first_name.value + ' ' + suggestion._highlightResult.last_name.value + '</a>';
            }
          }
        }
      });

At the moment, I am managing to only display suggestions with first_name + last_name for the right customers, but I can’t get rid of the dropdown boxes for suggestions that don’t meet the user_id criteria. They are currently showing blank boxes: 22

Any help on how to remove these boxes or prevent them from appearing would be much appreciated!

Hi @bdr193,

Thanks for contacting Algolia!

Cross posting StackOverflow response

Thanks for your response. My ‘Customer’ index has a ‘user_id’ record which is the equivalent of your ‘viewable_by’ record - this is not an array but a single integer for each Customer:

31

I have also updated my JS as per your suggestions:

var client = algoliasearch('x', 'x');
var index = client.initIndex('Customer');
let admin = document.getElementById("admin-field").innerText; // returns "true" for admins
let userId = parseInt(document.getElementById("id-field").innerText); // returns the integer value of the user's id

var srcParams = { hitsPerPage: 5}

// conditionally set filter based on admin
if (admin != "true") {
  srcParams.filters = 'user_id: userId';
}

autocomplete('#aa-search-input', {
  hint: false
}, {
  // use the srcParams
  source: autocomplete.sources.hits(index, srcParams),
   displayKey: 'name',
  templates: {
    suggestion: function(suggestion) {
    console.log(srcParams);
    return  `<span>${suggestion.first_name.value}"</span>` ;
    }
  }
});

This is working fine for admin users but the search doesn’t run if the user is not an admin (i.e. when the srcParams include the filter). It seems as though the filter is being read as a string rather than pulling in the integer value of the userId:

50

Any ideas for how to fix this would be much appreciated.

All solved. Just needed to adjust the filter to:

srcParams.filters = `user_id=${userId}`;

thanks

1 Like