@lipagas/ember-core
Version:
Provides all the core services, decorators and utilities for building a Fleetbase extension for the Console.
271 lines (231 loc) • 6.37 kB
JavaScript
import Service from '@ember/service';
import Evented from '@ember/object/evented';
import { inject as service } from '@ember/service';
import { tracked } from '@glimmer/tracking';
import { dasherize } from '@ember/string';
import { computed, get, action } from '@ember/object';
import { isBlank } from '@ember/utils';
import { alias } from '@ember/object/computed';
import { storageFor } from 'ember-local-storage';
export default class CurrentUserService extends Service.extend(Evented) {
/**
* Inject the `session` service
*
* @var {Service}
*/
session;
/**
* Inject the `store` service
*
* @var {Service}
*/
store;
/**
* Inject the `fetch` service
*
* @var {Service}
*/
fetch;
/**
* Inject the `theme` service
*
* @var {Service}
*/
theme;
/**
* Inject the `notifications` service
*
* @var {Service}
*/
notifications;
/**
* Property to hold loaded user.
*
* @var {UserModel|Object}
* @memberof CurrentUserService
*/
user = {
id: 'anon',
};
/**
* User options in localStorage
*
* @var StorageObject
*/
options;
/**
* Alias for current user id
*
* @var {String}
*/
id;
/**
* Alias for current user name
*
* @var {String}
*/
name;
/**
* Alias for current user phone
*
* @var {String}
*/
phone;
/**
* Alias for current user phone
*
* @var {String}
*/
email;
/**
* Alias for current user's company id
*
* @var {String}
*/
companyId;
/**
* Loads the current authenticated user
*
* @void
*/
async load() {
if (this.session.isAuthenticated) {
let user = await this.store.findRecord('user', 'me');
this.set('user', user);
this.trigger('user.loaded', user);
}
}
/**
* Resolves a user model.
*
* @return {Promise}
*/
promiseUser(options = {}) {
const NoUserAuthenticatedError = new Error('Failed to authenticate user.');
return new Promise((resolve, reject) => {
if (this.session.isAuthenticated) {
return this.store
.queryRecord('user', { me: true })
.then((user) => {
// set the `current user`
this.set('user', user);
this.trigger('user.loaded', user);
// set environment from user option
this.theme.setEnvironment();
// @TODO Create an event dispatch for when an authenticated user is resolved from the server
if (typeof options?.onUserResolved === 'function') {
options.onUserResolved(user);
}
resolve(user);
})
.catch(() => {
reject(NoUserAuthenticatedError);
});
} else {
reject(NoUserAuthenticatedError);
}
});
}
/**
* Loads and resolved all current users installed order configurations.
*
* @return {Promise}
*/
getInstalledOrderConfigs(params = {}) {
return new Promise((resolve, reject) => {
this.fetch
.get('fleet-ops/order-configs/get-installed', params)
.then((configs) => {
const serialized = [];
for (let i = 0; i < configs.length; i++) {
const config = configs.objectAt(i);
const normalizedConfig = this.store.normalize('order-config', config);
const serializedConfig = this.store.push(normalizedConfig);
serialized.pushObject(serializedConfig);
}
resolve(serialized);
})
.catch(reject);
});
}
/**
* The prefix for this user options
*
* @var {String}
*/
get optionsPrefix() {
return `${this.id}:`;
}
get latitude() {
return this.whois('latitude');
}
get longitude() {
return this.whois('longitude');
}
get currency() {
return this.whois('currency.code');
}
get city() {
return this.whois('city');
}
get country() {
return this.whois('country_code');
}
whois(key) {
return this.getWhoisProperty(key);
}
/**
* Sets a user's option in local storage
*
* @param {String} key
* @param {Mixed} value
* @return {CurrentUserService}
*/
setOption(key, value) {
key = `${this.optionsPrefix}${dasherize(key)}`;
this.options.set(key, value);
return this;
}
/**
* Retrieves a user option from local storage
*
* @param {String} key
* @return {Mixed}
*/
getOption(key, defaultValue = null) {
key = `${this.optionsPrefix}${dasherize(key)}`;
const value = this.options.get(key);
return value !== undefined ? value : defaultValue;
}
/**
* Retrieves a user option from local storage
*
* @param {String} key
* @return {Mixed}
*/
getWhoisProperty(prop) {
const whois = this.getOption('whois');
if (!whois || typeof whois !== 'object') {
return null;
}
return get(whois, prop);
}
/**
* Checks if an option exists in users local storage
*
* @param {String} key
* @return {Boolean}
*/
hasOption(key) {
return this.getOption(key) !== undefined;
}
/**
* Checks if an option exists in users local storage
*
* @param {String} key
* @return {Boolean}
*/
filledOption(key) {
return !isBlank(this.getOption(key));
}
}