/* global ubs_filter */
const Session = require('@unitybase/ub').Session
/**
 * Set owner to current user before inserting data
 *
 * @private
 * @param {ubMethodParams} ctxt
 * @returns {boolean}
 */
ubs_filter.on('insert:before', function ubsFilterSetOwner (ctxt) {
  const execParams = ctxt.mParams.execParams
  execParams.owner = Session.userID
  return true
})

/**
 * Used in RLS to filter by owner == Session.userID
 *
 * @function byOwner
 * @memberOf ubs_filter_ns.prototype
 * @memberOfModule @unitybase/ubs
 * @protected
 * @returns {string}
 */
ubs_filter.byOwner = function () {
  return '( [owner] = :(' + Session.userID + '): )'
}