zet-lib 3.3.2 → 3.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/zAppRouter.js CHANGED
@@ -93,6 +93,7 @@ router.get("/profile", csrfProtection, access, async (req, res) => {
93
93
  let verify_signed = result.verify_signed
94
94
  ? "/uploads/zuser/" + result.verify_signed
95
95
  : "/img/user.png";
96
+ const layout = res.locals.layout || "finsrecipe";
96
97
  res.render("layouts/" + layout, {
97
98
  menu: { menu: "profile" },
98
99
  data: result,
@@ -574,8 +575,9 @@ router.get(
574
575
 
575
576
  router.get("/no-access", async (req, res) => {
576
577
  let myview = "fronts";
577
- if (req.session.user) {
578
- myview = "main";
578
+ if (req.session && req.session.user) {
579
+ // backend layout mengikuti preferensi user (res.locals.layout)
580
+ myview = res.locals.layout || "main";
579
581
  }
580
582
  res.render("layouts/" + myview, {
581
583
  data: { table: "error" },
@@ -623,27 +625,24 @@ router.get("/session/:id", access, async (req, res) => {
623
625
  res.redirect(backURL);
624
626
  });
625
627
 
626
- // change backend layout preference
628
+ // Ubah layout backend (per user / personalized: tiap user punya preferensi sendiri)
627
629
  router.get("/layout_changes/:layout", access, async (req, res) => {
628
- const selectedLayout = req.params.layout;
630
+ const selectedLayout = String(req.params.layout || "").replace(/\.ejs$/i, "");
629
631
  const backURL = req.header("Referer") || "/";
630
632
  try {
631
- // persist user preference
632
- await connection.update({
633
- table: "zuser",
634
- data: { layout: selectedLayout },
635
- where: { id: res.locals.userId },
636
- });
637
- // update session + locals for next requests
638
633
  if (req.session && req.session.user) {
639
634
  req.session.user.layout = selectedLayout;
640
635
  }
641
636
  res.locals.layout = selectedLayout;
642
- global.layout = selectedLayout;
643
637
  } catch (err) {
644
638
  debug(req, res, err);
645
639
  }
646
- res.redirect(backURL);
640
+
641
+ // pastikan perubahan session tersimpan sebelum redirect (tergantung session store)
642
+ if (req.session && typeof req.session.save === "function") {
643
+ return req.session.save(() => res.redirect(backURL));
644
+ }
645
+ return res.redirect(backURL);
647
646
  });
648
647
 
649
648
  //RESTART
@@ -671,6 +670,7 @@ router.get("/error", async (err, req, res, next) => {
671
670
  // render the error page
672
671
  res.status(err.status || 500);
673
672
  debug(req, res, err);
673
+ const layout = res.locals.layout || "finsrecipe";
674
674
  res.render("layouts/" + layout, {
675
675
  renderBody: "index/error.ejs",
676
676
  });
@@ -65,6 +65,7 @@ router.get('/', csrfProtection, async function (req, res, next) {
65
65
  const endBody = ejs.render(js, datas)
66
66
  datas.bodyHTML = bodyHTML
67
67
  moduleLib.addModule(req, res, endBody)
68
+ const layout = res.locals.layout || 'finsrecipe'
68
69
  res.render('layouts/' + layout, datas)
69
70
  })
70
71
 
package/lib/zapp.js CHANGED
@@ -1,93 +1,121 @@
1
1
  const myCache = require("./cache");
2
- module.exports = (req,res,next) => {
3
- res.locals.BACKEND_LAYOUTS = myCache.get("BACKEND_LAYOUTS");
4
- res.locals.renderHead = "";
5
- res.locals.renderBody = "";
6
- res.locals.renderEnd = "";
7
- res.locals.headHTML = '';
8
- res.locals.bodyHTML = '';
9
- res.locals.endHTML = '';
10
- res.locals.titleApp = process.env.APP_TITLE;
11
- res.locals.descriptionApp = process.env.APP_DESCRIPTION;
12
- res.locals.moduleHead = "";
13
- res.locals.relationsVariable = "";
14
- res.locals.moduleEnd = "";
15
- res.locals.menuApp = "home";
16
- res.locals.routeName = "index";
17
- res.locals.userId = -1;
18
- res.locals.csrfToken = "";
19
- res.locals.roleId = 0;
20
- res.locals.token = "guest";
21
- res.locals.companyId = 0;
22
- res.locals.userId = 0;
23
- res.locals.userAvatar = "/img/user.png";
24
- res.locals.zuser = {
25
- fullname: 'test'
26
- };
27
- res.locals.frameworkcss = "bootstrap5";
28
- res.locals.startup = 0;
29
- global.frameworkcss = "bootstrap5";
30
- global.LANGUAGE = require('./languages/lang_en');
31
- res.locals.socketUrl = process.env.APP_URL;
32
- res.locals.zcompanies = [];
33
- res.locals.backend_layouts = [];
34
- res.locals.isLogin = false;
35
- res.locals.objectStores = {};
36
- if (req.session) {
37
- let reqSession = req.session;
38
- if (reqSession.hasOwnProperty('user')) {
39
- const tUser = req.session.user;
40
- if (tUser && Object.prototype.hasOwnProperty.call(tUser, "id")) {
41
- res.locals.isLogin = true;
42
- res.locals.token = tUser.token;
43
- res.locals.roleId = tUser.role_id;
44
- res.locals.isLogin = true;
45
- res.locals.zuser = tUser;
46
- res.locals.userId = tUser.id;
47
- res.locals.companyId = tUser.company.id;
48
- res.locals.userAvatar = tUser.image ? tUser.image.indexOf("http") > -1 ? tUser.image : "/uploads/zuser/" + tUser.image : "/img/user.png";
49
- res.locals.zcompanies = tUser.companies;
50
- res.locals.zcompany = tUser.company;
51
- if(tUser.language){
52
- let objLanguage = {
53
- 1 : "lang_en",
54
- 2 : "lang_id",
55
- 3 : "lang_jp",
56
- 4 : "lang_fr"
57
- };
58
- global.LANGUAGE = require(`./languages/${objLanguage[tUser.language]}`);
2
+ const Util = require('./Util')
3
+ const zRole = require('./zRole')
4
+ module.exports = (req, res, next) => {
5
+ res.locals.BACKEND_LAYOUTS = myCache.get("BACKEND_LAYOUTS");
6
+ res.locals.renderHead = "";
7
+ res.locals.renderBody = "";
8
+ res.locals.renderEnd = "";
9
+ res.locals.headHTML = '';
10
+ res.locals.bodyHTML = '';
11
+ res.locals.endHTML = '';
12
+ res.locals.titleApp = process.env.APP_TITLE;
13
+ res.locals.descriptionApp = process.env.APP_DESCRIPTION;
14
+ res.locals.moduleHead = "";
15
+ res.locals.relationsVariable = "";
16
+ res.locals.moduleEnd = "";
17
+ res.locals.menuApp = "home";
18
+ res.locals.routeName = "index";
19
+ res.locals.userId = -1;
20
+ res.locals.csrfToken = "";
21
+ res.locals.roleId = 0;
22
+ res.locals.token = "guest";
23
+ res.locals.companyId = 0;
24
+ res.locals.userId = 0;
25
+ res.locals.userAvatar = "/img/user.png";
26
+ res.locals.zuser = {
27
+ fullname: 'test'
28
+ };
29
+ res.locals.frameworkcss = "bootstrap5";
30
+ res.locals.startup = 0;
31
+ global.frameworkcss = "bootstrap5";
32
+ global.LANGUAGE = require('./languages/lang_en');
33
+ res.locals.socketUrl = process.env.APP_URL;
34
+ res.locals.zcompanies = [];
35
+ res.locals.backend_layouts = [];
36
+ res.locals.isLogin = false;
37
+ res.locals.objectStores = {};
38
+ if (req.session) {
39
+ let reqSession = req.session;
40
+ if (reqSession.hasOwnProperty('user')) {
41
+ const tUser = req.session.user;
42
+ if (tUser && Object.prototype.hasOwnProperty.call(tUser, "id")) {
43
+ res.locals.isLogin = true;
44
+ res.locals.token = tUser.token;
45
+ res.locals.roleId = tUser.role_id;
46
+ res.locals.isLogin = true;
47
+ res.locals.zuser = tUser;
48
+ res.locals.userId = tUser.id;
49
+ res.locals.companyId = tUser.company.id;
50
+ res.locals.userAvatar = tUser.image ? tUser.image.indexOf("http") > -1 ? tUser.image : "/uploads/zuser/" + tUser.image : "/img/user.png";
51
+ res.locals.zcompanies = tUser.companies;
52
+ res.locals.zcompany = tUser.company;
53
+ if (tUser.language) {
54
+ let objLanguage = {
55
+ 1: "lang_en",
56
+ 2: "lang_id",
57
+ 3: "lang_jp",
58
+ 4: "lang_fr"
59
+ };
60
+ global.LANGUAGE = require(`./languages/${objLanguage[tUser.language]}`);
61
+ }
62
+ // Layout per user (personalized): dari preferensi user di session
63
+ if (tUser.layout) {
64
+ res.locals.layout = String(tUser.layout).replace(/\.ejs$/i, "");
65
+ }
66
+ }
59
67
  }
60
- if(tUser.layout){
61
- res.locals.layout = tUser.layout;
62
- //global.layout = tUser.layout;
63
- }
64
- console.log(global.layout)
65
- }
66
68
  }
67
- }
68
- global.COMPANY_ID = res.locals.companyId;
69
- global.USER_ID = res.locals.userId;
70
- res.locals.LANGUAGE = global.LANGUAGE;
69
+ // Default layout jika user belum pilih; tiap user bisa punya layout berbeda
70
+ res.locals.layout = res.locals.layout || "admin";
71
+ global.COMPANY_ID = res.locals.companyId;
72
+ global.USER_ID = res.locals.userId;
73
+ res.locals.LANGUAGE = global.LANGUAGE;
71
74
 
72
- const path = req.route && req.route.path ? req.route.path : req.path;
75
+ const path = req.route && req.route.path ? req.route.path : req.path;
76
+ const MYMODELS = myCache.get("MYMODELS");
77
+ // Mapping manual per route
78
+ let map = {
79
+ '/dashboard': {title: 'Dashboard', subtitle: 'Systems'},
80
+ '/users': {title: 'Users', subtitle: 'List'},
81
+ '/users/new': {title: 'Users', subtitle: 'Create'},
82
+ };
83
+ for (let key in MYMODELS) {
84
+ map[`/${key}`] = {title: MYMODELS[key].name, subtitle: 'List'}
85
+ map[`/${key}/create`] = {title: MYMODELS[key].name, subtitle: 'Create'}
86
+ map[`/${key}/update`] = {title: MYMODELS[key].name, subtitle: 'Update'}
87
+ map[`/${key}/view`] = {title: MYMODELS[key].name, subtitle: 'View'}
88
+ }
73
89
 
74
- const MYMODELS = myCache.get("MYMODELS");
75
- // Mapping manual per route
76
- let map = {
77
- '/dashboard': { title: 'Dashboard', subtitle: 'Systems' },
78
- '/users': { title: 'Users', subtitle: 'List' },
79
- '/users/new': { title: 'Users', subtitle: 'Create' },
80
- };
81
- for(let key in MYMODELS) {
82
- map[`/${key}`] = {title : MYMODELS[key].name, subtitle:'List'}
83
- map[`/${key}/create`] = {title : MYMODELS[key].name, subtitle:'Create'}
84
- map[`/${key}/update`] = {title : MYMODELS[key].name, subtitle:'Update'}
85
- map[`/${key}/view`] = {title : MYMODELS[key].name, subtitle:'View'}
86
- }
90
+ const info = map[path] || {};
91
+ res.locals.pagetitle = info.title || 'Dashboard';
92
+ res.locals.pagesubtitle = info.subtitle || '';
87
93
 
88
- const info = map[path] || {};
89
- res.locals.pagetitle = info.title || 'Dashboard';
90
- res.locals.pagesubtitle = info.subtitle || '';
94
+ const explode = req.path.split("/");
95
+ const route = explode[1];
96
+ let action = "index";
97
+ res.locals.menuApp = Util.capitalizeAfterSpace(route);
98
+ res.locals.routeName = route;
99
+ if (explode.length > 2) {
100
+ action = explode[2] || "index";
101
+ }
102
+ res.locals.titleApp = `${process.env.APP_TITLE} - ${route} ${action}`;
103
+ if (global.hasCacheLogo == 0) {
104
+ try {
105
+ global.hasCacheLogo = 1;
106
+ } catch (e) {
107
+ console.log(e);
108
+ }
109
+ }
110
+ // define role for route base on session or not
111
+ //define routes at zRole
112
+ if (zRole.routes.indexOf(route) > -1) {
113
+ req.route = route;
114
+ req.action = action;
115
+ zRole.access(req, res, next);
116
+ } else {
117
+ next();
118
+ }
91
119
 
92
- next();
120
+ //next();
93
121
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zet-lib",
3
- "version": "3.3.2",
3
+ "version": "3.3.3",
4
4
  "description": "zet is a library that part of zet generator.",
5
5
  "engines": {
6
6
  "node": ">=18"