zet-lib 1.0.10 → 1.0.12

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/index.js CHANGED
@@ -10,7 +10,6 @@ module.exports = {
10
10
  Form: require('./Form'),
11
11
  io: require('./io'),
12
12
  Mail: require('./Mail'),
13
- menuGenerator: require('./menuGenerator'),
14
13
  Modal: require('./Modal'),
15
14
  moduleLib: require('./moduleLib'),
16
15
  tableForm: require('./tableForm'),
@@ -18,7 +17,6 @@ module.exports = {
18
17
  zComponent: require('./zComponent'),
19
18
  zFn: require('./zFn'),
20
19
  zFunction: require('./zFunction'),
21
- zMenuCollections: require('./zMenuCollections'),
22
20
  zMenuRouter: require('./zMenuRouter'),
23
21
  zReport: require('./zReport'),
24
22
  zRoute: require('./zRoute'),
package/lib/zCache.js CHANGED
@@ -151,7 +151,7 @@ zCache.ZFUNCTIONS = async() => {
151
151
  let obj = Util.arrayToObject(results, "title");
152
152
  await myCache.set("ZFUNCTIONS", obj);
153
153
  //add to version after set
154
- zCache.updateVersions("ZFUNCTIONS")
154
+ zCache.updateVersions("ZFUNCTIONS");
155
155
  }
156
156
  };
157
157
 
@@ -275,10 +275,6 @@ zCache.MENU_SYSTEMS = () => {
275
275
  text: "Custom Report",
276
276
  href: "zreport",
277
277
  },
278
- {
279
- text: "Menu Collections",
280
- href: "zmenu_collections",
281
- },
282
278
  {
283
279
  text: "Log Errors",
284
280
  href: "zerror",
package/lib/zRole.js CHANGED
@@ -95,7 +95,6 @@ a.menuAccess = (res, menu) => {
95
95
  return true;
96
96
  }
97
97
  }
98
-
99
98
  return false;
100
99
  };
101
100
 
@@ -129,7 +128,7 @@ a.access = (req, res, next) => {
129
128
  if (isAccess) {
130
129
  next();
131
130
  } else {
132
- if(a.isAccess(res.locals.roleId,"zrole","index")) {
131
+ if(a.isAccess(res.locals.roleId,'zrole','index')) {
133
132
  req.session.sessionFlash = Util.flashError(LANGUAGE.no_access);
134
133
  res.redirect(`${process.env.APP_AFTER_LOGIN}?setup=role`)
135
134
  } else {
@@ -139,5 +138,108 @@ a.access = (req, res, next) => {
139
138
  }
140
139
  };
141
140
 
141
+ a.menu = (req, res) => {
142
+ let jsonArray = [];
143
+ if (!res.locals.isLogin) return jsonArray;
144
+ let companyId = res.locals.companyId;
145
+ let userId = res.locals.userId;
146
+ let jsonMenu = myCache.get("MENU");
147
+ let arr = [];
148
+ if (jsonMenu && jsonMenu.hasOwnProperty(companyId)) {
149
+ arr = jsonMenu[companyId] || [];
150
+ }
151
+ arr.map((me) => {
152
+ if (a.menuAccess(res, me.href)) {
153
+ let obj = a.addItemMenu(me);
154
+ if (me.hasOwnProperty("children")) {
155
+ obj.children = a.childrenMenu(res, me.children);
156
+ if(obj.children.length) {
157
+ jsonArray.push(obj);
158
+ }
159
+ } else {
160
+ jsonArray.push(obj);
161
+ }
162
+ }
163
+ });
164
+
165
+ return jsonArray;
166
+ };
167
+
168
+ a.menuSystems = (req,res) => {
169
+ let arr = [];
170
+ let menus = myCache.get("MENU_SYSTEMS");
171
+ let children = [];
172
+ menus["users"].forEach(function (item) {
173
+ if (a.menuAccess(res, item.href)) {
174
+ children.push(item);
175
+ }
176
+ });
177
+ if(children.length == 0) {
178
+ delete menus.users;
179
+ } else {
180
+ let userManagament = {
181
+ text: "Users",
182
+ icon: "tabler-users-plus",
183
+ children: children
184
+ };
185
+ arr.push(userManagament);
186
+ }
187
+ children = [];
188
+ menus["systems"].forEach(function (item) {
189
+ if (a.menuAccess(res, item.href)) {
190
+ children.push(item);
191
+ }
192
+ });
193
+ if(children.length == 0) {
194
+ delete menus.systems;
195
+ } else {
196
+ let settings = {
197
+ text: "Systems",
198
+ icon: "tabler-settings-check",
199
+ children: children
200
+ };
201
+ arr.push(settings);
202
+ }
203
+ return arr;
204
+ };
205
+
206
+ a.menus = (req, res) => {
207
+ let arr = a.menu(req, res);
208
+ return [...arr, ...a.menuSystems(req, res)];
209
+ };
210
+
211
+ a.addItemMenu = (obj) => {
212
+ let newObj = {};
213
+ for (const key in obj) {
214
+ if (key != "children") {
215
+ newObj[key] = obj[key];
216
+ }
217
+ }
218
+ return newObj;
219
+ };
220
+
221
+ a.childrenMenu = (res, arr) => {
222
+ let myArr = [];
223
+ let nArr = arr.reduce((result, item) => [...result, item.href],[]);
224
+ let isAccess = a.menuAccess(res, nArr);
225
+ if (isAccess) {
226
+ //stupid way
227
+ arr.map((item) => {
228
+ let obj = {};
229
+ if(a.isAccess(res.locals.roleId,item.href,"index")) {
230
+ obj = a.addItem(item);
231
+ if (item.hasOwnProperty("children") && item.children.length) {
232
+ let child = [];
233
+ child.push(a.children(res, item.children));
234
+ if (child[0]) {
235
+ obj.children = child[0];
236
+ }
237
+ }
238
+ myArr.push(obj)
239
+ }
240
+ });
241
+ }
242
+ return myArr;
243
+ };
142
244
 
143
245
  module.exports = a;
package/lib/zRoute.js CHANGED
@@ -3225,4 +3225,36 @@ zRoute.modelsCacheRenew = (table, companyId) => {
3225
3225
  }
3226
3226
  };
3227
3227
 
3228
+ zRoute.makeFunctionsSystem = () => {
3229
+ let obj = myCache.get("ZFUNCTIONS");
3230
+ let content = ``;
3231
+ for(let key in obj) {
3232
+ if(obj[key].systems == 1) {
3233
+ content += obj[key].code;
3234
+ content += Util.newLine;
3235
+ }
3236
+ }
3237
+ let templateSystem = `const {zRole, zCache, myCache, Util, io, moment, Mail} = require('zet-lib');
3238
+ module.exports = (req, res, next) => {`;
3239
+ let fileSystem = `${dirRoot}/components/zFunctionsSystem.js`;
3240
+ templateSystem += content;
3241
+ templateSystem += ` next();
3242
+ };
3243
+ `;
3244
+
3245
+ Util.writeFile(fileSystem, templateSystem);
3246
+ if(process.env.NODE_ENV === 'production') {
3247
+ setTimeout(function () {
3248
+ pm2.connect(function (err) {
3249
+ if (err) {
3250
+ console.log(err.toString());
3251
+ }
3252
+ pm2.restart(process.env.app.pm2, (err, proc) => {
3253
+ //io.to(room).emit("message","Restart done")
3254
+ });
3255
+ });
3256
+ }, 3000)
3257
+ }
3258
+ }
3259
+
3228
3260
  module.exports = zRoute;
package/lib/zapp.js CHANGED
@@ -1,6 +1,4 @@
1
1
  const config = require('dotenv').config();
2
- const dirRoot = process.env.dirRoot;
3
- const menuGenerator = require('./menuGenerator');
4
2
  module.exports = (req,res,next) => {
5
3
  res.locals.renderHead = "";
6
4
  res.locals.renderBody = "";
@@ -21,8 +19,7 @@ module.exports = (req,res,next) => {
21
19
  res.locals.userId = 0;
22
20
  res.locals.userAvatar = "/img/user.png";
23
21
  res.locals.zuser = {
24
- fullname: 'test',
25
- role: {name: "test"}
22
+ fullname: 'test'
26
23
  };
27
24
  res.locals.frameworkcss = "bootstrap5";
28
25
  res.locals.startup = 0;
@@ -32,17 +29,11 @@ module.exports = (req,res,next) => {
32
29
  res.locals.zcompanies = [];
33
30
  res.locals.isLogin = false;
34
31
  res.locals.objectStores = {};
35
- res.locals.MENU = [];
36
- res.locals.MENU_SYSTEMS = [];
37
- res.locals.MENU_ALL = [];
38
32
  if (req.session) {
39
33
  let reqSession = req.session;
40
34
  if (reqSession.hasOwnProperty('user')) {
41
35
  const tUser = req.session.user;
42
36
  if (tUser && Object.prototype.hasOwnProperty.call(tUser, "id")) {
43
- tUser.role = {
44
- name: "Admin"
45
- };
46
37
  res.locals.isLogin = true;
47
38
  res.locals.token = tUser.token;
48
39
  res.locals.roleId = tUser.role_id;
@@ -64,17 +55,10 @@ module.exports = (req,res,next) => {
64
55
  global.layout = "two";
65
56
  }
66
57
  }
67
- res.locals.MENU = menuGenerator.menu(req, res);
68
- res.locals.MENU_SYSTEMS = menuGenerator.systems(req, res);
69
- res.locals.MENU_ALL = menuGenerator.menuPlus(req, res);
70
58
  }
71
59
  global.COMPANY_ID = res.locals.companyId;
72
60
  global.USER_ID = res.locals.userId;
73
61
  res.locals.LANGUAGE = global.LANGUAGE;
74
- res.locals.currency = {};
75
- res.locals.settings = {};
76
- res.locals.sessionFlash = req.session.sessionFlash;
77
- delete req.session.sessionFlash;
78
- res.locals.sessionFlashc = req.session.sessionFlashc;
62
+
79
63
  next();
80
64
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zet-lib",
3
- "version": "1.0.10",
3
+ "version": "1.0.12",
4
4
  "description": "zet is a library that part of zet generator.",
5
5
  "engines": {
6
6
  "node": ">=18"
@@ -1,236 +0,0 @@
1
- /**
2
- * Created by ANDHIE on 2/28/2021.
3
- */
4
- const myCache = require("./cache");
5
- const zRole = require("./zRole");
6
- const connection = require("./connection");
7
- const fs = require("fs-extra");
8
- const config = require('dotenv').config();
9
- const dirRoot = process.env.dirRoot;
10
- const pm2 = require('pm2');
11
- const menuGenerator = {};
12
-
13
- menuGenerator.addItem = (obj) => {
14
- let newObj = {};
15
- for (const key in obj) {
16
- if (key != "children") {
17
- newObj[key] = obj[key];
18
- }
19
- }
20
- return newObj;
21
- };
22
-
23
- menuGenerator.children = (res, arr) => {
24
- let myArr = [];
25
- let nArr = arr.reduce((result, item) => [...result, item.href],[]);
26
- let isAccess = zRole.menuAccess(res, nArr);
27
- if (isAccess) {
28
- //stupid way
29
- arr.map((item) => {
30
- let obj = {};
31
- if(zRole.isAccess(res.locals.roleId,item.href,"index")) {
32
- obj = menuGenerator.addItem(item);
33
- if (item.hasOwnProperty("children") && item.children.length) {
34
- let child = [];
35
- child.push(menuGenerator.children(res, item.children));
36
- if (child[0]) {
37
- obj.children = child[0];
38
- }
39
- }
40
- myArr.push(obj)
41
- }
42
- });
43
- }
44
- return myArr;
45
- };
46
-
47
- menuGenerator.menu = (req, res) => {
48
- let jsonArray = [];
49
- if (!res.locals.isLogin) return jsonArray;
50
- let companyId = res.locals.companyId;
51
- let userId = res.locals.userId;
52
- let jsonMenu = myCache.get("MENU");
53
- let arr = [];
54
- if (jsonMenu && jsonMenu.hasOwnProperty(companyId)) {
55
- arr = jsonMenu[companyId] || [];
56
- } else {
57
- arr = menuGenerator.arr; //set to default menu
58
- }
59
- arr.map((me) => {
60
- if (zRole.menuAccess(res, me.href)) {
61
- let obj = menuGenerator.addItem(me);
62
- if (me.hasOwnProperty("children")) {
63
- obj.children = menuGenerator.children(res, me.children);
64
- if(obj.children.length) {
65
- jsonArray.push(obj);
66
- }
67
- } else {
68
- jsonArray.push(obj);
69
- }
70
- }
71
- });
72
-
73
- return jsonArray;
74
- };
75
-
76
- menuGenerator.systems = (req,res) => {
77
- let arr = [];
78
- let menus = myCache.get("MENU_SYSTEMS");
79
- let children = [];
80
- menus["users"].forEach(function (item) {
81
- if (zRole.menuAccess(res, item.href)) {
82
- children.push(item);
83
- }
84
- });
85
- if(children.length == 0) {
86
- delete menus.users;
87
- } else {
88
- let userManagament = {
89
- text: "Users",
90
- icon: "tabler-users-plus",
91
- children: children
92
- };
93
- arr.push(userManagament);
94
- }
95
- children = [];
96
- menus["systems"].forEach(function (item) {
97
- if (zRole.menuAccess(res, item.href)) {
98
- children.push(item);
99
- }
100
- });
101
- if(children.length == 0) {
102
- delete menus.systems;
103
- } else {
104
- let settings = {
105
- text: "Systems",
106
- icon: "tabler-settings-check",
107
- children: children
108
- };
109
- arr.push(settings);
110
- }
111
- return arr;
112
- };
113
-
114
- menuGenerator.menuPlus = (req, res) => {
115
- let arr = menuGenerator.menu(req, res);
116
- return [...arr, ...menuGenerator.systems(req, res)];
117
- };
118
-
119
- menuGenerator.html = (req, res) => {
120
- let html = "";
121
- let arr = menuGenerator.menu(req, res);
122
- let span = "";
123
- let dropdown = "";
124
- let test = "";
125
- arr.map((me) => {
126
- let href = me.href == "" ? "#" : "/" + me.href;
127
- if (me.hasOwnProperty("children") && me.children.length) {
128
- dropdown = `class="dropdown"`;
129
- span = `<span class="caret"></span>`;
130
- test = `class="test"`;
131
- } else {
132
- dropdown = "", span = "", test = "";
133
- }
134
- html += `<li ${dropdown}>`;
135
- html += `<a href="${href}" title="${me.title}" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false"><i class="${me.icon} fa-fw"></i> ${me.text}${span}</a>`;
136
-
137
- if (me.hasOwnProperty("children") && me.children.length) {
138
- html += `<ul class="dropdown-menu">`;
139
- let children = me.children;
140
-
141
- //1 child
142
- children.map((item) => {
143
- if (item.hasOwnProperty("children")) {
144
- dropdown = `class="dropdown-submenu"`;
145
- span = `<span class="caret"></span>`;
146
- test = `class="test"`;
147
- } else {
148
- dropdown = "", span = "", test = "";
149
- }
150
-
151
- href = item.href == "" ? "#" : "/" + item.href;
152
- html += `<li ${dropdown}><a ${test} title="${item.title}" tabindex="-1" href="${href}"><i class="${item.icon} fa-fw"></i> ${item.text}${span}</a>`;
153
-
154
- //2 child
155
- if (item.hasOwnProperty("children") && me.children.length) {
156
- html += `<ul class="dropdown-menu">`;
157
- item.children.map((child) => {
158
- if (child.hasOwnProperty("children")) {
159
- dropdown = `class="dropdown-submenu"`;
160
- span = `<span class="caret"></span>`;
161
- test = `class="test"`;
162
- } else {
163
- dropdown = "", span = "", test = "";
164
- }
165
-
166
- href = child.href == "" ? "#" : "/" + child.href;
167
- html += `<li ${dropdown}><a title="${child.title}" ${test} tabindex="-1" href="${href}"><i class="${child.icon} fa-fw"></i> ${child.text}${span}</a>`;
168
-
169
-
170
- if (child.hasOwnProperty("children") && me.children.length) {
171
- html += `<ul class="dropdown-menu">`;
172
- child.children.map((child2) => {
173
- href = child2.href == "" ? "#" : "/" + child2.href;
174
-
175
- html += `<li><a title="${child2.title}" tabindex="-1" href="${href}"><i class="${child2.icon} fa-fw"></i> ${child2.text}</a>`;
176
- });
177
-
178
- //end 3 child
179
- html += '</ul>';
180
- }
181
- });
182
- //end 2 child
183
- html += '</ul>';
184
- }
185
- });
186
- //end 1 child
187
- html += `</ul>`;
188
- }
189
- html += `<li>`
190
- });
191
- return html;
192
- };
193
-
194
-
195
- /*
196
- Do not remove for menu
197
- */
198
- menuGenerator.generateCollections = async() => {
199
- let scripts = `
200
- module.exports = (req,res,next) => {
201
-
202
- `;
203
- let results = await connection.results({
204
- table : "zmenu_collections",
205
- where : {
206
- active : 1
207
- }
208
- });
209
- let arrFn = [];
210
- if(results.length) {
211
- for(let i = 0;i<results.length;i++) {
212
- scripts += `function menu${i}() {
213
- ${results[i].code}
214
- }
215
- menu${i}();
216
- `;
217
- }
218
- }
219
- scripts += ` next();
220
- }`;
221
- fs.writeFileSync(`${dirRoot}/components/zMenuCollections.js`, scripts, 'utf-8');
222
- if (process.env.environment == "production") {
223
- setTimeout(function () {
224
- pm2.connect(function (err) {
225
- if (err) {
226
- console.log(err.toString());
227
- }
228
- pm2.restart(process.env.app.pm2, (err, proc) => {
229
- //io.to(room).emit("message","Restart done")
230
- });
231
- });
232
- }, 3000)
233
- }
234
- };
235
-
236
- module.exports = menuGenerator;
@@ -1,62 +0,0 @@
1
- module.exports = (req, res, next) => {
2
-
3
- function menu0() {
4
- function mainMenu(obj) {
5
- let html = "", span = "", dropdown = "", test = "";
6
- const parent = (obj, classname = "") => {
7
- let href = obj.href ? "/" + obj.href : "#";
8
- let hassub = "", sidebarlink = "", aClass = "", span = obj.text;
9
- if (obj.hasOwnProperty("children")) {
10
- hassub = "has-sub";
11
- sidebarlink = "sidebar-link";
12
- aClass = `class="${sidebarlink}"`;
13
- span = `<span>${obj.text}</span>`;
14
- }
15
- let html = '';
16
- let icon = obj.icon ? obj.icon.replace('tabler-', '') : '';
17
- let aicon = '';
18
- if(icon && icon != "empty"){
19
- aicon = icon ? `<img src="/assets/icons/${icon}.svg" class="tabler-icons icons-black-color" >` : `<i class="${obj.icon}"></i>`;
20
- }
21
- html += `<li class="${classname}">
22
- <a href="${href}" ${aClass} >
23
- ${aicon}
24
- ${span}
25
- </a>`;
26
- if (hassub != "") {
27
- html += child(obj.children);
28
- }
29
- html += `</li>`;
30
-
31
- return html;
32
- };
33
-
34
- const child = (arr) => {
35
- let html = '<div class="submenu"><ul>';
36
- arr.map((item) => {
37
- html += parent(item, "submenu-item");
38
- });
39
- html += `</ul></div>`;
40
- return html;
41
- };
42
- let arr = obj.items;
43
- html += `<ul class="main-menu">`;
44
- arr.map((item) => {
45
- let classname = "";
46
- if (item.hasOwnProperty("children")) {
47
- classname = "has-submenu ";
48
- } else {
49
- classname = "";
50
- }
51
- html += parent(item, classname);
52
- });
53
- html += `</ul>`;
54
- return html;
55
- }
56
-
57
- const html = mainMenu({items: res.locals.MENU_ALL, options: {class: "sidebar-nav"}});
58
- res.locals.menu_vertical = html;
59
- }
60
- menu0();
61
- next();
62
- };