Files
raven-client/ayanova/src/api/initialize.js
2019-05-02 19:46:34 +00:00

139 lines
4.9 KiB
JavaScript

/* xeslint-disable */
import store from "../store";
import roles from "./authorizationroles";
import locale from "./locale";
import api from "./gzapi";
import gzevent from "./eventbus";
function addNavItem(title, icon, route) {
store.commit("addNavItem", {
title,
icon,
route
});
}
/////////////////////////////////////
// Initialize the app
// on change of authentication status
export default function initialize() {
var promise = new Promise(function(resolve) {
if (store.state.authenticated) {
//Fetch the core localized text keys that will always be required by user
locale
.fetch(locale.coreKeys)
.then(function putFetchedNavItemsInStore() {
//put nav items into store
//Everyone has a home
addNavItem(locale.get("Home"), "home", "/");
if (
roles.hasRole(roles.AUTHORIZATION_ROLES.TechLimited) ||
roles.hasRole(roles.AUTHORIZATION_ROLES.TechFull) ||
roles.hasRole(roles.AUTHORIZATION_ROLES.SubContractorLimited) ||
roles.hasRole(roles.AUTHORIZATION_ROLES.SubContractorFull)
) {
addNavItem(locale.get("Service"), "toolbox", "/service");
}
if (
roles.hasRole(roles.AUTHORIZATION_ROLES.DispatchLimited) ||
roles.hasRole(roles.AUTHORIZATION_ROLES.DispatchFull)
) {
addNavItem(locale.get("Dispatch"), "shipping-fast", "/dispatch");
}
if (
roles.hasRole(roles.AUTHORIZATION_ROLES.InventoryLimited) ||
roles.hasRole(roles.AUTHORIZATION_ROLES.InventoryFull)
) {
addNavItem(locale.get("Inventory"), "dolly", "/inventory");
}
if (roles.hasRole(roles.AUTHORIZATION_ROLES.AccountingFull)) {
addNavItem(
locale.get("Accounting"),
"file-invoice-dollar",
"/accounting"
);
}
if (
roles.hasRole(roles.AUTHORIZATION_ROLES.BizAdminLimited) ||
roles.hasRole(roles.AUTHORIZATION_ROLES.BizAdminFull)
) {
addNavItem(locale.get("Administration"), "user-tie", "/admin");
}
if (
roles.hasRole(roles.AUTHORIZATION_ROLES.OpsAdminFull) ||
roles.hasRole(roles.AUTHORIZATION_ROLES.OpsAdminLimited)
) {
addNavItem(locale.get("Operations"), "cogs", "ops");
}
//MOVED TO MENU OUT OF NAV
//Everyone can see about and logout
// addNavItem(locale.get("HelpAboutAyaNova"), "info-circle", "/about");
// addNavItem(locale.get("Logout"), "sign-out-alt", "/login");
})
.then(() => {
//CACHE LOCALE SETTINGS
api
.get("UserOptions/" + store.state.userId)
.then(res => {
if (res.error) {
//In a form this would trigger a bunch of validation or error display code but for here and now:
//convert error to human readable string for display and popup a notification to user
var msg = api.apiErrorToHumanString(res.error);
store.commit(
"logItem",
"Initialize::() fetch useroptions -> error" + msg
);
gzevent.$emit("notify-error", msg);
} else {
//TODO: also need the other locale settings such as number and date formats etc
var localOffset = new Date().getTimezoneOffset();
if (localOffset != 0) {
localOffset = (localOffset / 60) * -1; //time is in minutes and reversed from what we want or expect
}
if (res.data.timeZoneOffset != localOffset) {
//TODO: localize message and also actually have a fix for it here
//so this should be a confirm prompt but for now will just show it
//for now just show the message
gzevent.$emit(
"notify-info",
"Time zone offset for this account is set to " +
res.data.timeZoneOffset +
" which doesn't match the local timezone offset of " +
localOffset +
". You might want to adjust that under user settings"
);
}
//Store offset in locale data
locale.timeZoneOffset = res.data.timeZoneOffset;
resolve();
}
})
.catch(function handleFetchUserOptionsError(error) {
store.commit(
"logItem",
"Initialize::() fetch useroptions -> error" + error
);
throw error;
});
})
.catch(function handleIntializeError(error) {
store.commit("logItem", "Initialize::() ltfetch -> error" + error);
throw error;
});
}
});
return promise;
}