Files
raven-client/ayanova/src/api/initialize.js
2019-03-11 22:48:38 +00:00

122 lines
3.8 KiB
JavaScript

/* xeslint-disable */
import store from "../store";
import roles from "./roles";
import locale from "./locale";
import api from "./apiutil";
function addNavItem(title, icon, route) {
store.commit("addNavItem", {
title,
icon,
route
});
}
/////////////////////////////////////
// Initialize the app
// on change of authentication status
export default function initialize() {
if (store.state.authenticated) {
//GET ALL DEFAULT LOCALIZED TEXT FOR SHELL
locale
.fetch(locale.coreKeys)
.then(function() {
//put nav items into store
//Everyone has a home
addNavItem(locale.get("Home"), "home", "/");
if (
roles.hasRole(roles.AuthorizationRoles.TechLimited) ||
roles.hasRole(roles.AuthorizationRoles.TechFull) ||
roles.hasRole(roles.AuthorizationRoles.SubContractorLimited) ||
roles.hasRole(roles.AuthorizationRoles.SubContractorFull)
) {
addNavItem(locale.get("Service"), "toolbox", "/service");
}
if (
roles.hasRole(roles.AuthorizationRoles.DispatchLimited) ||
roles.hasRole(roles.AuthorizationRoles.DispatchFull)
) {
addNavItem(locale.get("Dispatch"), "shipping-fast", "/dispatch");
}
if (
roles.hasRole(roles.AuthorizationRoles.InventoryLimited) ||
roles.hasRole(roles.AuthorizationRoles.InventoryFull)
) {
addNavItem(locale.get("Inventory"), "dolly", "/inventory");
}
if (roles.hasRole(roles.AuthorizationRoles.AccountingFull)) {
addNavItem(
locale.get("Accounting"),
"file-invoice-dollar",
"/accounting"
);
}
if (
roles.hasRole(roles.AuthorizationRoles.BizAdminLimited) ||
roles.hasRole(roles.AuthorizationRoles.BizAdminFull)
) {
addNavItem(locale.get("Administration"), "user-tie", "/admin");
}
if (
roles.hasRole(roles.AuthorizationRoles.OpsAdminFull) ||
roles.hasRole(roles.AuthorizationRoles.OpsAdminLimited)
) {
addNavItem(locale.get("Operations"), "cogs", "ops");
}
//Everyone can see about and logout
addNavItem(locale.get("HelpAboutAyaNova"), "info-circle", "/about");
addNavItem(locale.get("Logout"), "sign-out-alt", "/login");
})
.catch(function(error) {
store.commit("logItem", "Initialize::() ltfetch -> error" + error);
throw error;
});
//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
alert(api.apiErrorToHumanString(res.error));
} 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: timezone doesn't match, offer to fix it
alert(
"Time zone offset for this account is set to " +
res.data.timeZoneOffset +
" which doesn't match the local timezone offset of " +
localOffset +
"."
);
}
//Store offset in locale data
locale.timeZoneOffset = res.data.timeZoneOffset;
}
})
.catch(function(error) {
store.commit(
"logItem",
"Initialize::() fetch useroptions -> error" + error
);
throw error;
});
}
}