dynamicsmobile
Version:
Allows development of off-line mobile and web business apps over the Dynamics Mobile platform. More info on https://www.dynamicsmobile.com
191 lines (190 loc) • 9.62 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BackendConfigurationService = void 0;
const tslib_1 = require("tslib");
const injectable_1 = require("../ioc/injectable");
const configuration_service_1 = require("../lib-core/configuration-service");
const querystring_1 = require("querystring");
const dms_platform_bridge_factory_1 = require("../platform/dms-platform-bridge-factory");
const dms_api_wrapper_1 = require("../platform-web/dms-api-wrapper");
const application_context_service_1 = require("../lib-core/application-context-service");
const dms_root_container_1 = require("../ioc/dms-root-container");
const userinterface_service_1 = require("../lib-core/userinterface-service");
const dms_platform_web_device_1 = require("../platform-web/dms-platform-web-device");
const dms_platform_web_configuration_manager_1 = require("../platform-web/dms-platform-web-configuration-manager");
let BackendConfigurationService = exports.BackendConfigurationService = class BackendConfigurationService extends configuration_service_1.ConfigurationService {
getCfgStorageKey() {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const dms = dms_root_container_1.RootDIContainer.inject(application_context_service_1.DmsApplicationService);
const device = new dms_platform_web_device_1.WebPlatformDevice(dms);
const userName = yield device.getUsername();
const appArea = yield device.getApparea();
return dms_platform_web_configuration_manager_1.CFG_MANAGER_KEY + Buffer.from(appArea).toString('base64') + Buffer.from(userName).toString('base64');
});
}
synchronize() {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const dms = dms_root_container_1.RootDIContainer.inject(application_context_service_1.DmsApplicationService);
const ui = dms_root_container_1.RootDIContainer.inject(userinterface_service_1.UserInterfaceService);
const local = localStorage.getItem(yield this.getCfgStorageKey());
let localObj;
if (local) {
localObj = JSON.parse(local);
if (localObj.lastSync) {
if ((new Date()).getTime() - localObj.lastSync < 1000 * 60 * 5) {
//sync settings every 5 mins
return;
}
}
}
let remoteSettings;
var auth = yield (0, dms_platform_bridge_factory_1.PlatformBridgeFactory)(this.dms).execute("getSessionKey", null);
const appArea = yield (0, dms_platform_bridge_factory_1.PlatformBridgeFactory)(this.dms).execute("getApparea", null);
var url = dms_api_wrapper_1.DmsAPIWrapper._baseAPIUrl + "/mobile/settings?overrideAppCode=" + this.dms.appCode;
var response = yield dms_api_wrapper_1.DmsAPIWrapper.executeApiRequest(appArea, url, 'GET', auth);
if (response && response.data) {
if (typeof response.data === "string")
remoteSettings = JSON.parse(response.data);
else
remoteSettings = response.data;
}
this.settings = { lastSync: (new Date()).getTime(), remote: remoteSettings ? remoteSettings : {}, local: localObj && localObj.local ? localObj.local : {} };
this.saveSettings();
});
}
retreiveSettings() {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const _cfgObj = yield (0, dms_platform_bridge_factory_1.PlatformBridgeFactory)(this.dms).execute("retreiveSettings", this.settings);
//executed in backend web app only
var cfgAsString = localStorage.getItem(yield this.getCfgStorageKey());
if (cfgAsString && cfgAsString.length > 0) {
//clean tasks
//var _cfgObj = JSON.parse(cfgAsString);
//make all keys uppercase
var keys;
var newCfg = { remote: {}, local: {} };
if (_cfgObj.remote) {
keys = Object.keys(_cfgObj.remote);
keys.forEach(k => {
newCfg.remote[k.toUpperCase()] = _cfgObj.remote[k];
});
}
else {
newCfg.remote = {};
}
if (_cfgObj.local) {
keys = Object.keys(_cfgObj.local);
keys.forEach(k => {
newCfg.local[k.toUpperCase()] = _cfgObj.local[k];
});
}
else {
_cfgObj.local = {};
}
this.settings = newCfg;
}
else {
this.settings = { remote: {}, local: {} };
}
});
}
saveSettings() {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
yield (0, dms_platform_bridge_factory_1.PlatformBridgeFactory)(this.dms).execute("saveAllSettings", this.settings);
});
}
/**
* Returns the value of a setting by given name
* @param settingName - string , name of the setting
* @returns string or null
*
*/
getSetting(settingName) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
if (Object.getOwnPropertyNames(this.settings.remote).length == 0) {
yield this.retreiveSettings();
}
const remote = this.settings.remote[settingName.toUpperCase()];
if (remote == null || remote == undefined)
return this.settings.local[settingName.toUpperCase()];
else
return remote;
});
}
/**
* persists given value under given name
* the value can be retreived back by using method getSetting
* @param settingName - string , name of the setting
* @param value - string value to be persisted
* @returns string or null
*
*/
setSetting(settingName, value) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
if (settingName.indexOf('$TSK_') >= 0)
throw new Error('$TSK_ literal is not allowed in settingName');
var _value = (value ? (value.replace(/'/g, '').replace(/"/, '')) : undefined);
this.settings.local[settingName.toUpperCase()] = (_value ? _value.toString() : undefined);
yield (0, dms_platform_bridge_factory_1.PlatformBridgeFactory)(this.dms).execute("setSetting", { settingName: settingName, value: (_value ? _value.toString() : undefined) });
yield this.saveSettings();
});
}
getAllSettings() {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
var settingsNotAllowedInUserInterface = ["$DMSCURRENTNAVSTATE", "COMPANY", "DMSAPIKEY"];
var me = this;
var settingsAsArray = [];
var remoteKeys = Object.getOwnPropertyNames(this.settings.remote);
remoteKeys.forEach(key => {
if (key.indexOf('$') < 0 && key.indexOf('$TSK_') < 0 && settingsNotAllowedInUserInterface.indexOf(key) < 0)
settingsAsArray.push({ key: key, value: me.settings.remote[key] });
});
var localKeys = Object.getOwnPropertyNames(this.settings.local);
localKeys.forEach(key => {
if (key.indexOf('$') < 0 && key.indexOf('$TSK_') < 0 && settingsNotAllowedInUserInterface.indexOf(key) < 0)
settingsAsArray.push({ key: key, value: me.settings.local[key] });
});
return settingsAsArray;
});
}
getAllowedTasks() {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
var settingsAsArray = [];
var keys = Object.getOwnPropertyNames(this.settings.remote);
keys.forEach(key => {
if (key.indexOf('$TSK_') >= 0)
settingsAsArray.push({ id: key.substr(5), allowed: true });
});
return settingsAsArray;
});
}
getNavigationState() {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const urlTokens = window.parent.location.href.split('?');
const parsed = (0, querystring_1.parse)(urlTokens[1]);
let step = window.parent.location.hash ? window.parent.location.hash.substr(1) : null;
return { LastTaskId: parsed.task ? parsed.task : null, LastStepId: step };
});
}
setNavigationState(state) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
if (state.LastStepId) {
window.parent.location.hash = state.LastStepId;
if (window.parent.location.href.indexOf('?') > 0) {
//save the queryString portion in sessionstorage
//used by loginsuccess.html to redirect to proper task/step
//after login
const tokens = window.parent.location.href.split('?');
sessionStorage.setItem('$$$dmsredirect', tokens[1]);
}
}
else {
window.parent.location.hash = '';
}
});
}
};
exports.BackendConfigurationService = BackendConfigurationService = tslib_1.__decorate([
(0, injectable_1.Injectable)()
], BackendConfigurationService);
//# sourceMappingURL=backend-configuration-service.js.map