{"size": 760, "ext": "js", "max_stars_repo_path": "lib/mixins/get-current-user.js", "max_stars_repo_name": "pbalan/component-storage", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "lib/mixins/get-current-user.js", "max_issues_repo_name": "pbalan/component-storage", "max_issues_repo_issues_event_min_datetime": "2021-05-06T22:04:48.000Z", "max_issues_repo_issues_event_max_datetime": "2022-01-22T00:54:00.000Z", "max_forks_repo_path": "lib/mixins/get-current-user.js", "max_forks_repo_name": "pbalan/component-storage", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 2, "max_forks_count": null, "avg_line_length": 33.0434782609, "max_line_length": 84, "alphanum_fraction": 0.6828947368}

const debug = require('debug')('component:commerce:utils');
const LoopBackContext = require('loopback-context');

module.exports = function getCurrentUserMixin(Model) {
  debug('initializing GetCurrentUser Mixin for model %s', Model.modelName);

  Model.getCurrentUser = function getCurrentUser() {
    const ctx = LoopBackContext.getCurrentContext({bind: true});
    const currentUser = (ctx && ctx.get('currentUser')) || null;

    if (ctx) {
      debug(`${Model.definition.name}.getCurrentUser() - currentUser: %o`,
        currentUser);
    } else {
      // this means its a server-side logic call w/o any HTTP req/resp aspect to it.
      debug(`${Model.definition.name}.getCurrentUser() - no loopback context`);
    }

    return currentUser;
  };
};
