UNPKG

nervatura-client

Version:
2 lines (1 loc) 7.33 kB
import{b as e,L as t}from"./main-DXT0hpEA.js";import"./module-FRmUNWHB.js";import"./module-CC7fmSsS.js";import"./module-C1fQGCtD.js";class r{constructor(e){this.host=e,this.loginData=this.loginData.bind(this),this.onLogin=this.onLogin.bind(this),this.onPageEvent=this.onPageEvent.bind(this),this.setCodeToken=this.setCodeToken.bind(this),this.setLogin=this.setLogin.bind(this),this.tokenError=this.tokenError.bind(this),this.tokenValidation=this.tokenValidation.bind(this),this.userLog=this.userLog.bind(this),e.addController(this)}async loginData(t){const{data:r}=this.host.app.store,{getSql:a,requestData:s}=this.host.app;let n={...r[e.LOGIN],token:t.token,engine:t.engine},o=[{key:"employee",text:a(t.engine,{select:["e.*","ug.groupvalue as usergroupName","dp.groupvalue as departmentName"],from:"employee e",inner_join:["groups ug","on",["e.usergroup","=","ug.id"]],left_join:["groups dp","on",["e.department","=","dp.id"]],where:["username","=","?"]}).sql,values:[n.username]},{key:"menuCmds",text:a(t.engine,{select:["m.*","st.groupvalue as methodName"],from:"ui_menu m",inner_join:["groups st","on",["m.method","=","st.id"]]}).sql,values:[]},{key:"menuFields",text:a(t.engine,{select:["mf.*","ft.groupvalue as fieldtypeName"],from:"ui_menufields mf",inner_join:["groups ft","on",["mf.fieldtype","=","ft.id"]],order_by:["menu_id","orderby"]}).sql,values:[]},{key:"userlogin",text:a(t.engine,{select:["value"],from:"fieldvalue",where:[["ref_id","is","null"],["and","fieldname","=","'log_login'"]]}).sql,values:[]},{key:"groups",text:a(t.engine,{select:["*"],from:"groups",where:["groupname","in",[[],"'usergroup'","'nervatype'","'transtype'","'inputfilter'","'transfilter'","'department'","'logstate'","'fieldtype'","'service'"]]}).sql,values:[]}],i={method:"POST",token:t.token,data:o},l=await s("/view",i);if(l.error)return l;if(n={...n,...l,employee:l.employee[0],userlogin:l.userlogin.length>0?l.userlogin[0].value:"false"},o=[{key:"audit",text:a(t.engine,{select:["au.nervatype","nt.groupvalue as nervatypeName","au.subtype","case when nt.groupvalue = 'trans' then st.groupvalue else m.menukey end as subtypeName","au.inputfilter","ip.groupvalue as inputfilterName","au.supervisor"],from:"ui_audit au",inner_join:[["groups nt","on",["au.nervatype","=","nt.id"]],["groups ip","on",["au.inputfilter","=","ip.id"]]],left_join:[["groups st","on",["au.subtype","=","st.id"]],["ui_menu m","on",["au.subtype","=","m.id"]]],where:["au.usergroup","=","?"]}).sql,values:[n.employee.usergroup]},{key:"transfilter",text:a(t.engine,{select:["ref_id_2 as transfilter","g.groupvalue as transfilterName"],from:"link",inner_join:["groups g","on",["link.ref_id_2","=","g.id"]],where:[["ref_id_1","=","?"],["and","link.deleted","=","0"],["and","nervatype_1","in",[{select:["id"],from:"groups",where:[["groupname","=","'nervatype'"],["and","groupvalue","=","'groups'"]]}]],["and","nervatype_2","in",[{select:["id"],from:"groups",where:[["groupname","=","'nervatype'"],["and","groupvalue","=","'groups'"]]}]]]}).sql,values:[n.employee.usergroup]}],i={method:"POST",token:t.token,data:o},l=await s("/view",i),l.error)return l;if(n={...n,transfilter:l.transfilter.length>0?l.transfilter[0].transfilter:null,audit:l.audit},null===n.transfilter){const e=n.groups.filter((e=>"transfilter"===e.groupname&&"all"===e.groupvalue))[0];n={...n,transfilter:e.id,transfilterName:"all"}}else n={...n,transfilterName:l.transfilter[0].transfilterName};n={...n,audit_filter:{trans:{},menu:{},report:{}},edit_new:[[],[],[],[]]};[["offer",0],["order",0],["worksheet",0],["rent",0],["invoice",0],["receipt",0],["bank",1],["cash",1],["delivery",2],["inventory",2],["waybill",2],["production",2],["formula",2]].forEach((e=>{const t=n.audit.filter((t=>"trans"===t.nervatypeName&&t.subtypeName===e[0]))[0];n={...n,audit_filter:{...n.audit_filter,trans:{...n.audit_filter.trans,[e[0]]:t?[t.inputfilterName,t.supervisor]:["all",1]}}},"disabled"!==n.audit_filter.trans[e[0]][0]&&(n={...n,edit_new:{...n.edit_new,[e[1]]:[...n.edit_new[e[1]],e[0]]}})}));return["customer","product","employee","tool","project","setting","audit"].forEach((e=>{const t=n.audit.filter((t=>t.nervatypeName===e&&null===t.subtypeName))[0];n={...n,audit_filter:{...n.audit_filter,[e]:t?[t.inputfilterName,t.supervisor]:["all",1]}},"disabled"!==n.audit_filter[e][0]&&"setting"!==e&&"audit"!==e&&(n={...n,edit_new:{...n.edit_new,3:[...n.edit_new[3],e]}})})),n}async onLogin(){const{data:t}=this.host.app.store,{requestData:r,resultError:a,msg:s}=this.host.app,n={method:"POST",data:{username:t[e.LOGIN].username,password:t[e.LOGIN].password,database:t[e.LOGIN].database}},o=await r("/auth/login",n);if(o.token&&o.engine){if(!t.session.engines.includes(o.engine))return a({error:{message:s("Invalid database type!",{id:"login_engine_err"})}});if(!t.session.service.includes(o.version))return a({error:{message:s("Invalid service version!",{id:"login_version_err"})}});const e=await this.loginData(o);return e.error?a(e):this.setLogin(e)}return a(o)}onPageEvent({key:r,data:a}){const{setData:s}=this.host.app.store;switch(r){case t.CHANGE:s(e.LOGIN,{[a.fieldname]:a.value});break;case t.THEME:case t.LANG:s("current",{[r]:a}),localStorage.setItem([r],a);break;case t.LOGIN:this.onLogin()}}async setCodeToken(e){const{setData:t}=this.host.app.store,{request:r}=this.host.app;if(!e.callback)return this.tokenError({id:"error_unauthorized",type:"error",message:"Unauthorized user"},e.error);{const a={headers:{"Content-Type":"application/json",Accept:"application/json"},method:"POST",body:JSON.stringify({code:e.code})};try{t("current",{request:!0});const s=await r(e.callback,a);if(s.access_token)return this.tokenValidation({access_token:s.access_token,callback:s.callback});if("string"==typeof s)try{const e=JSON.parse(s);if(e.access_token)return this.tokenValidation({access_token:e.access_token,callback:e.callback})}catch(t){return this.tokenError(t,e.error)}return this.tokenError(s.error,e.error)}catch(t){return this.tokenError(t,e.error)}}}async setLogin(t){const{data:r,setData:a}=this.host.app.store,{loadBookmark:s,resultError:n,currentModule:o}=this.host.app;if("t"===t.userlogin||"true"===t.userlogin){const e=await this.userLog(t);if(e.error)return void n(e)}a(e.LOGIN,{data:t}),localStorage.setItem("database",r[e.LOGIN].database),localStorage.setItem("username",r[e.LOGIN].username),localStorage.setItem("server",r[e.LOGIN].server),s({user_id:t.employee.id,token:t.token}),o({data:{module:e.SEARCH}})}tokenError(e,t){const{setData:r}=this.host.app.store,{resultError:a}=this.host.app;return t?window.location.replace(`${t}?error=${window.btoa(e.message)}`):(r("current",{request:!1}),a(e))}async tokenValidation(t){const{setData:r}=this.host.app.store,{requestData:a,resultError:s}=this.host.app,n={method:"GET",token:t.access_token},o=await a("/auth/validate",n);if(o.error)return this.tokenError(o.error,t.callback);r(e.LOGIN,{username:o.username,database:o.database,callback:t.callback});const i=await this.loginData({token:t.access_token,engine:o.engine,version:o.version});return i.error?s(i):(window.history.replaceState(null,null,window.location.pathname),this.setLogin(i))}async userLog(e){const{requestData:t}=this.host.app,r={method:"POST",token:e.token,data:[{keys:{employee_id:e.employee.empnumber,logstate:"login"}}]};return await t("/log",r)}}export{r as LoginController};