zet-lib 1.5.22 → 1.5.24

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.
@@ -91,9 +91,12 @@ router.get('/preview/:token', async (req, res) => {
91
91
  let row = await connection.result({table: results[0].table, where: {id: results[0].id_data}});
92
92
  data2 = await zRoute.viewTable(req, res, MYMODEL, row, true);
93
93
  template = data.template;
94
+ let title = data.title || ""
94
95
  for (var key in data2) {
95
96
  template = Util.replaceAll(template, "{{" + key + "}}", data2[key])
97
+ title = Util.replaceAll(title, "{{" + key + "}}", data2[key])
96
98
  }
99
+ data.title = title;
97
100
  var details = await connection.results({table: "zapprovals_details", where: {title_id: results[0].id}});
98
101
  if (details.length) {
99
102
  footers = await zRoute.approversFooter(details);
package/lib/zAppRouter.js CHANGED
@@ -647,8 +647,10 @@ router.post("/zzapproval", async (req, res) => {
647
647
  "id"
648
648
  );
649
649
  try {
650
+ //console.log(req.body)
650
651
  let userId = res.locals.userId;
651
652
  let data = zRoute.post(req, res, MYMODEL)[MYMODEL.table];
653
+ //console.log(data)
652
654
  let validator = zRoute.validator(data, MYMODEL);
653
655
  if (validator.status == 0) return res.json(validator.message);
654
656
  if (data.status == 2) {
@@ -705,6 +707,18 @@ router.post("/zzapproval", async (req, res) => {
705
707
  table: MYMODEL.table,
706
708
  where: { table: data.table, id_data: data.id_data },
707
709
  });
710
+
711
+ //change title for subject for email
712
+ let titleEmail = data.title || ""
713
+ let rowData = await connection.result({table: results[0].table, where: {id: results[0].id_data}});
714
+ data2 = await zRoute.viewTable(req, res, MYMODEL, rowData, true);
715
+ for (var key in data2) {
716
+ titleEmail = Util.replaceAll(titleEmail, "{{" + key + "}}", data2[key])
717
+ }
718
+ //console.log(titleEmail)
719
+ //end
720
+
721
+
708
722
  if (results.length) {
709
723
  let query = await zRoute.updateSQL(req, res, MYMODEL.table, data, {
710
724
  id: results[0].id,
@@ -754,30 +768,27 @@ router.post("/zzapproval", async (req, res) => {
754
768
  token: Util.uuid(),
755
769
  };
756
770
  await zRoute.insertSQL(req, res, "zapprovals_details", post);
757
- let link =
758
- env == "production"
759
- ? process.env.APP_URL + "/za/" + post.token
760
- : `http://localhost:${port}/za/${post.token}`;
771
+ let link = env == "production"
772
+ ? process.env.APP_URL + "/za/" + post.token
773
+ : `http://localhost:${port}/za/${post.token}`;
761
774
  let email = users[post.user_id].email;
762
775
 
763
- post.subject = `Need Approve ${data.title}`;
776
+ post.subject = `Need Approve ${titleEmail}`;
764
777
  post.buttonTitle = `Approve Now`;
765
778
  post.caption = `NEED APPROVE DOCUMENT`;
766
- post.title = data.title;
779
+ post.title = titleEmail;
767
780
  post.link = link;
768
781
  post.url = process.env.APP_URL;
769
782
  post.description = `Hai ${users[post.user_id].fullname} <br>
770
-
771
-
772
- We have some document for you.<br>
773
- Please click the link above to view the document.`;
783
+ We have some document for you.<br>
784
+ Please click the link above to view the document.`;
774
785
 
775
786
  post.note = `If you click the link, it's will be automatically acknowledged`;
776
787
  Mail.approval(req, res, post, email);
777
788
 
778
789
  //send notification
779
790
  //send toastr using socket.io
780
- io.to(users[item].token).emit("message", "Need Approve " + data.title);
791
+ io.to(users[item].token).emit("message", "Need Approve " + titleEmail);
781
792
  });
782
793
 
783
794
  knowings.map(async (item) => {
@@ -795,29 +806,28 @@ router.post("/zzapproval", async (req, res) => {
795
806
  : `http://localhost:${port}/za/${post.token}`;
796
807
  let email = users[post.user_id].email;
797
808
 
798
- post.subject = `Need acknowledge ${data.title}`;
809
+ post.subject = `Need acknowledge ${titleEmail}`;
799
810
  post.buttonTitle = `Acknowledge Now`;
800
- post.title = data.title
811
+ post.title = titleEmail
801
812
  post.caption = `NEED ACKNOWLEDGE DOCUMENT`;
802
813
 
803
814
  post.link = link;
804
815
  post.url = process.env.APP_URL;
805
816
  post.description = `Hai ${users[post.user_id].fullname} <br>
806
-
807
-
808
- We have some document for you.<br>
809
- Please click the link above to view the document.`;
817
+ We have some document for you.<br>
818
+ Please click the link above to view the document.`;
810
819
 
811
820
  post.note = `If you click the link, it's will be automatically acknowledged`;
812
821
  Mail.approval(req, res, post, email);
813
822
 
814
823
  io.to(users[item].token).emit(
815
824
  "message",
816
- "Need Acknowledge " + data.title
825
+ "Need Acknowledge " + titleEmail
817
826
  );
818
827
  });
819
828
  }
820
829
  } catch (err) {
830
+ console.log(err)
821
831
  if (Object.prototype.hasOwnProperty.call(err, "sqlMessage")) {
822
832
  json = Util.flashError(err.sqlMessage);
823
833
  } else {
@@ -858,6 +868,11 @@ router.get("/za/:token", csrfProtection, async (req, res) => {
858
868
  ],
859
869
  where: { "zapprovals_details.token": token },
860
870
  });
871
+
872
+ if(!result.length){
873
+ return res.send("Not Found");
874
+ }
875
+ result = result[0];
861
876
  let approvers = result.approvers;
862
877
  knowings = result.knowings ? result.knowings : [];
863
878
 
@@ -935,10 +950,13 @@ router.get("/za/:token", csrfProtection, async (req, res) => {
935
950
  where: { id: result.id_data },
936
951
  });
937
952
  data = await zRoute.viewTable(req, res, MYMODEL, row);
953
+ let title = result.title || ""
938
954
  template = result.template;
939
955
  for (var key in data) {
940
956
  template = Util.replaceAll(template, "{{" + key + "}}", data[key]);
957
+ title = Util.replaceAll(title, "{{" + key + "}}", data[key])
941
958
  }
959
+ result.title = title;
942
960
  footers = await zRoute.approversFooter(details);
943
961
 
944
962
  //send activity
@@ -1008,6 +1026,15 @@ router.post("/za/:token", csrfProtection, async (req, res) => {
1008
1026
  }
1009
1027
 
1010
1028
  var result = results[0];
1029
+ //change title for subject for email
1030
+ let titleEmail = result.title || ""
1031
+ let rowData = await connection.result({table: result.table, where: {id: result.id_data}});
1032
+ data2 = await zRoute.viewTable(req, res, MYMODEL, rowData, true);
1033
+ for (var key in data2) {
1034
+ titleEmail = Util.replaceAll(titleEmail, "{{" + key + "}}", data2[key])
1035
+ }
1036
+ //end
1037
+
1011
1038
  if (result.approvers_status == 3 || result.approvers_status == 4) {
1012
1039
  return res.json(Util.jsonError("You have submited."));
1013
1040
  }
@@ -1058,34 +1085,42 @@ router.post("/za/:token", csrfProtection, async (req, res) => {
1058
1085
  if (status == 4) {
1059
1086
  post.status = 4;
1060
1087
  }
1088
+ //{ comments: 'yyyyy', status: '6' }
1089
+ let approval_history = result.approval_history || [];
1090
+ approval_history.push({comments:req.body.comments, status:req.body.status, user_id:result.user_id, time:Util.now()})
1091
+ post.approval_history = Util.array_to_jsonb(approval_history)
1061
1092
  await connection.update({
1062
1093
  table: "zapprovals",
1063
1094
  data: post,
1064
1095
  where: { id: result.ide },
1065
1096
  });
1097
+ // 6 comment 4 reject
1098
+ //update ke table data yang dipakai
1099
+ let status_approval_table = 2
1100
+ if(req.body.status == 4){
1101
+ status_approval_table = 22
1102
+ } else {
1103
+ if(result.approvers.length ==1){
1104
+ status_approval_table =21;
1105
+ } else {
1106
+ status_approval_table =11;
1107
+ }
1108
+ }
1066
1109
 
1067
- /* //send activity
1068
- await connection.insert({
1069
- table: "zactivity",
1070
- data: {
1071
- company_id: result.company_id,
1072
- created_at: Util.now(),
1073
- created_by: result.user_id,
1074
- updated_by: result.user_id,
1075
- user_id: result.user_id,
1076
- table: MYMODEL.table,
1077
- id_data: result.ide,
1078
- status: status,
1079
- status_label: MYMODEL.widgets.status.fields[status],
1080
- title: MYMODEL.widgets.status.fields[status],
1081
- description: comments,
1082
- data: JSON.stringify(post),
1083
- },
1084
- });*/
1110
+ await connection.update({
1111
+ table : result.table,
1112
+ data:{
1113
+ approval_history:Util.array_to_jsonb(approval_history),
1114
+ approval_status:status_approval_table
1115
+ },
1116
+ where : {
1117
+ id: result.id_data
1118
+ }
1119
+ })
1085
1120
 
1086
1121
  //send notification
1087
1122
  if (status == 3 || status == 4) {
1088
- let message = `Document ${result.title} has ${MYMODEL.widgets.status[status]};`;
1123
+ let message = `Document ${titleEmail} has ${MYMODEL.widgets.status[status]};`;
1089
1124
  approvers.forEach(function (item) {
1090
1125
  io.to(users[item].token).emit("message", message);
1091
1126
  });
@@ -1095,7 +1130,6 @@ router.post("/za/:token", csrfProtection, async (req, res) => {
1095
1130
  //approversSigned
1096
1131
  if (result.typee == 2) {
1097
1132
  //get approvers has already sign
1098
-
1099
1133
  approversSigned.push(result.user_id);
1100
1134
  var newApprovers = approvers.filter((item) => {
1101
1135
  return !approversSigned.includes(item);
@@ -1122,7 +1156,7 @@ router.post("/za/:token", csrfProtection, async (req, res) => {
1122
1156
  let link = process.env.APP_URL + "/za/" + post.token;
1123
1157
  let email = users[item].username;
1124
1158
 
1125
- post.subject = `Need Approve ${result.title}`;
1159
+ post.subject = `Need Approve ${titleEmail}`;
1126
1160
  post.buttonTitle = `Approve Now`;
1127
1161
  post.title = result.title;
1128
1162
  post.link = link;
@@ -1179,35 +1213,16 @@ router.post("/za/:token", csrfProtection, async (req, res) => {
1179
1213
  link: "/za/" + post.token,
1180
1214
  status_label: "Unread",
1181
1215
  title: `Need Approve`,
1182
- description: data.title,
1216
+ description: titleEmail,
1183
1217
  token: post.token,
1184
1218
  },
1185
1219
  });
1186
1220
 
1187
- //send todolist
1188
- /*await connection.insert({
1189
- table: "ztodolist",
1190
- data: {
1191
- company_id: result.company_id,
1192
- created_at: Util.now(),
1193
- created_by: employee_user[result.user_id].user_id,
1194
- updated_by: employee_user[result.user_id].user_id,
1195
- user_id: item,
1196
- table: MYMODEL.table,
1197
- id_data: result.ide,
1198
- status: 1,
1199
- link: "/za/" + post.token,
1200
- status_label: "Unread",
1201
- title: `Need Approve`,
1202
- description: result.title,
1203
- users: JSON.stringify(approvers),
1204
- },
1205
- });*/
1206
1221
 
1207
1222
  //send toastr using socket.io
1208
1223
  io.to(users[item].token).emit(
1209
1224
  "message",
1210
- "Need Approve for document : " + result.title
1225
+ "Need Approve for document : " + titleEmail
1211
1226
  );
1212
1227
  }
1213
1228
  }
@@ -1221,6 +1236,55 @@ router.post("/za/:token", csrfProtection, async (req, res) => {
1221
1236
  res.json(json);
1222
1237
  });
1223
1238
 
1239
+ router.post('/get_approval_comments', async(req,res) => {
1240
+ let data = []
1241
+ try {
1242
+ //console.log(req.body);
1243
+ let token = req.body.token;
1244
+ let row = await connection.result({
1245
+ select:"title_id",
1246
+ table : "zapprovals_details",
1247
+ where : {
1248
+ token: token
1249
+ }
1250
+ })
1251
+
1252
+ let result = await connection.result({
1253
+ select:"approval_history",
1254
+ table : "zapprovals",
1255
+ where : {
1256
+ id: row.title_id
1257
+ }
1258
+ })
1259
+
1260
+ //{"{\"status\": \"6\", \"user_id\": \"192\", \"comments\": \"yayayay\"}","{\"status\": \"6\", \"user_id\": \"192\", \"comments\": \"kurang\"}","{\"status\": \"6\", \"user_id\": \"192\", \"comments\": \"oke\"}","{\"status\": \"4\", \"user_id\": \"192\", \"comments\": \"reject\"}","{\"status\": \"6\", \"user_id\": \"192\", \"comments\": \"dsfsdf\"}"}
1261
+ let zuser = myCache.get('zuser') || {};
1262
+ let approval_history = result.approval_history || [];
1263
+ approval_history.forEach((item) => {
1264
+ // Safe access dengan optional chaining dan fallback
1265
+ const user = zuser[item.user_id];
1266
+ let name = (user && (user.fullname || user.username)) || 'Unknown User';
1267
+ let statusName = ''
1268
+ if(item.status === 6){
1269
+ statusName = 'Comment'
1270
+ } else if(item.status === 4){
1271
+ statusName = 'Reject'
1272
+ } else {
1273
+ statusName = 'Approve'
1274
+ }
1275
+ item.statusName = statusName;
1276
+ item.name = name
1277
+ data.push(item)
1278
+ })
1279
+
1280
+
1281
+ } catch (e) {
1282
+ console.log(e)
1283
+ }
1284
+
1285
+ res.json(data)
1286
+ })
1287
+
1224
1288
  router.get("/zdownload/zgenerator/:table", async (req, res) => {
1225
1289
  var table = req.params.table;
1226
1290
  var path = `${dirRoot}/public/uploads/zgenerator/${table}.json`;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zet-lib",
3
- "version": "1.5.22",
3
+ "version": "1.5.24",
4
4
  "description": "zet is a library that part of zet generator.",
5
5
  "engines": {
6
6
  "node": ">=18"