zet-lib 1.5.23 → 1.5.25

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
@@ -707,6 +707,18 @@ router.post("/zzapproval", async (req, res) => {
707
707
  table: MYMODEL.table,
708
708
  where: { table: data.table, id_data: data.id_data },
709
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
+
710
722
  if (results.length) {
711
723
  let query = await zRoute.updateSQL(req, res, MYMODEL.table, data, {
712
724
  id: results[0].id,
@@ -761,10 +773,10 @@ router.post("/zzapproval", async (req, res) => {
761
773
  : `http://localhost:${port}/za/${post.token}`;
762
774
  let email = users[post.user_id].email;
763
775
 
764
- post.subject = `Need Approve ${data.title}`;
776
+ post.subject = `Need Approve ${titleEmail}`;
765
777
  post.buttonTitle = `Approve Now`;
766
778
  post.caption = `NEED APPROVE DOCUMENT`;
767
- post.title = data.title;
779
+ post.title = titleEmail;
768
780
  post.link = link;
769
781
  post.url = process.env.APP_URL;
770
782
  post.description = `Hai ${users[post.user_id].fullname} <br>
@@ -776,7 +788,7 @@ router.post("/zzapproval", async (req, res) => {
776
788
 
777
789
  //send notification
778
790
  //send toastr using socket.io
779
- io.to(users[item].token).emit("message", "Need Approve " + data.title);
791
+ io.to(users[item].token).emit("message", "Need Approve " + titleEmail);
780
792
  });
781
793
 
782
794
  knowings.map(async (item) => {
@@ -794,9 +806,9 @@ router.post("/zzapproval", async (req, res) => {
794
806
  : `http://localhost:${port}/za/${post.token}`;
795
807
  let email = users[post.user_id].email;
796
808
 
797
- post.subject = `Need acknowledge ${data.title}`;
809
+ post.subject = `Need acknowledge ${titleEmail}`;
798
810
  post.buttonTitle = `Acknowledge Now`;
799
- post.title = data.title
811
+ post.title = titleEmail
800
812
  post.caption = `NEED ACKNOWLEDGE DOCUMENT`;
801
813
 
802
814
  post.link = link;
@@ -810,7 +822,7 @@ router.post("/zzapproval", async (req, res) => {
810
822
 
811
823
  io.to(users[item].token).emit(
812
824
  "message",
813
- "Need Acknowledge " + data.title
825
+ "Need Acknowledge " + titleEmail
814
826
  );
815
827
  });
816
828
  }
@@ -828,10 +840,10 @@ router.post("/zzapproval", async (req, res) => {
828
840
 
829
841
  router.get("/za/:token", csrfProtection, async (req, res) => {
830
842
  let token = req.params.token;
831
- let data = {},
832
- data2 = {};
843
+ let data = {};
844
+ let data2 = {};
833
845
  let template = "";
834
- let result;
846
+ let result = {}
835
847
  let isClosed = false;
836
848
  let footers = "";
837
849
  let details = [];
@@ -856,6 +868,10 @@ router.get("/za/:token", csrfProtection, async (req, res) => {
856
868
  ],
857
869
  where: { "zapprovals_details.token": token },
858
870
  });
871
+ //console.log(results)
872
+ if(!result.id){
873
+ return res.send("Not Found!");
874
+ }
859
875
  let approvers = result.approvers;
860
876
  knowings = result.knowings ? result.knowings : [];
861
877
 
@@ -1009,6 +1025,15 @@ router.post("/za/:token", csrfProtection, async (req, res) => {
1009
1025
  }
1010
1026
 
1011
1027
  var result = results[0];
1028
+ //change title for subject for email
1029
+ let titleEmail = result.title || ""
1030
+ let rowData = await connection.result({table: result.table, where: {id: result.id_data}});
1031
+ data2 = await zRoute.viewTable(req, res, MYMODEL, rowData, true);
1032
+ for (var key in data2) {
1033
+ titleEmail = Util.replaceAll(titleEmail, "{{" + key + "}}", data2[key])
1034
+ }
1035
+ //end
1036
+
1012
1037
  if (result.approvers_status == 3 || result.approvers_status == 4) {
1013
1038
  return res.json(Util.jsonError("You have submited."));
1014
1039
  }
@@ -1032,6 +1057,8 @@ router.post("/za/:token", csrfProtection, async (req, res) => {
1032
1057
  let status_stats = [];
1033
1058
  let approversSigned = [];
1034
1059
  //{"stats":"0/2","status":[{"user":"12","status":2},{"user":"35","status":2}]}
1060
+ //3 approve
1061
+ //4 reject
1035
1062
  let last_status;
1036
1063
  details.map((item) => {
1037
1064
  if (item.type == 1) {
@@ -1061,7 +1088,7 @@ router.post("/za/:token", csrfProtection, async (req, res) => {
1061
1088
  }
1062
1089
  //{ comments: 'yyyyy', status: '6' }
1063
1090
  let approval_history = result.approval_history || [];
1064
- approval_history.push({comments:req.body.comments, status:req.body.status, user_id:result.user_id})
1091
+ approval_history.push({comments:req.body.comments, status:req.body.status, user_id:result.user_id, time:Util.now()})
1065
1092
  post.approval_history = Util.array_to_jsonb(approval_history)
1066
1093
  await connection.update({
1067
1094
  table: "zapprovals",
@@ -1070,7 +1097,7 @@ router.post("/za/:token", csrfProtection, async (req, res) => {
1070
1097
  });
1071
1098
  // 6 comment 4 reject
1072
1099
  //update ke table data yang dipakai
1073
- let status_approval_table = 2
1100
+ let status_approval_table = 2;
1074
1101
  if(req.body.status == 4){
1075
1102
  status_approval_table = 22
1076
1103
  } else {
@@ -1094,7 +1121,16 @@ router.post("/za/:token", csrfProtection, async (req, res) => {
1094
1121
 
1095
1122
  //send notification
1096
1123
  if (status == 3 || status == 4) {
1097
- let message = `Document ${result.title} has ${MYMODEL.widgets.status[status]};`;
1124
+ await connection.update({
1125
+ table : result.table,
1126
+ data:{
1127
+ approval_status: status == 3 ? 21 : 22
1128
+ },
1129
+ where : {
1130
+ id: result.id_data
1131
+ }
1132
+ })
1133
+ let message = `Document ${titleEmail} has been ${status == 3 ? "Approved" : "Rejected"};`;
1098
1134
  approvers.forEach(function (item) {
1099
1135
  io.to(users[item].token).emit("message", message);
1100
1136
  });
@@ -1104,7 +1140,6 @@ router.post("/za/:token", csrfProtection, async (req, res) => {
1104
1140
  //approversSigned
1105
1141
  if (result.typee == 2) {
1106
1142
  //get approvers has already sign
1107
-
1108
1143
  approversSigned.push(result.user_id);
1109
1144
  var newApprovers = approvers.filter((item) => {
1110
1145
  return !approversSigned.includes(item);
@@ -1131,7 +1166,7 @@ router.post("/za/:token", csrfProtection, async (req, res) => {
1131
1166
  let link = process.env.APP_URL + "/za/" + post.token;
1132
1167
  let email = users[item].username;
1133
1168
 
1134
- post.subject = `Need Approve ${result.title}`;
1169
+ post.subject = `Need Approve ${titleEmail}`;
1135
1170
  post.buttonTitle = `Approve Now`;
1136
1171
  post.title = result.title;
1137
1172
  post.link = link;
@@ -1188,16 +1223,15 @@ router.post("/za/:token", csrfProtection, async (req, res) => {
1188
1223
  link: "/za/" + post.token,
1189
1224
  status_label: "Unread",
1190
1225
  title: `Need Approve`,
1191
- description: data.title,
1226
+ description: titleEmail,
1192
1227
  token: post.token,
1193
1228
  },
1194
1229
  });
1195
1230
 
1196
-
1197
1231
  //send toastr using socket.io
1198
1232
  io.to(users[item].token).emit(
1199
1233
  "message",
1200
- "Need Approve for document : " + result.title
1234
+ "Need Approve for document : " + titleEmail
1201
1235
  );
1202
1236
  }
1203
1237
  }
@@ -1211,6 +1245,56 @@ router.post("/za/:token", csrfProtection, async (req, res) => {
1211
1245
  res.json(json);
1212
1246
  });
1213
1247
 
1248
+ router.post('/get_approval_comments', async(req,res) => {
1249
+ let data = []
1250
+ try {
1251
+ //console.log(req.body);
1252
+ let token = req.body.token;
1253
+ let row = await connection.result({
1254
+ select:"title_id",
1255
+ table : "zapprovals_details",
1256
+ where : {
1257
+ token: token
1258
+ }
1259
+ })
1260
+
1261
+ let result = await connection.result({
1262
+ select:"approval_history",
1263
+ table : "zapprovals",
1264
+ where : {
1265
+ id: row.title_id
1266
+ }
1267
+ })
1268
+
1269
+ //{"{\"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\"}"}
1270
+ let zuser = myCache.get('zuser') || {};
1271
+ let approval_history = result.approval_history || [];
1272
+ //console.log(approval_history)
1273
+ approval_history.forEach((item) => {
1274
+ // Safe access dengan optional chaining dan fallback
1275
+ const user = zuser[item.user_id];
1276
+ let name = (user && (user.fullname || user.username)) || 'Unknown User';
1277
+ let statusName = ''
1278
+ if(item.status === 6){
1279
+ statusName = 'Comment'
1280
+ } else if(item.status === 4){
1281
+ statusName = 'Reject'
1282
+ } else {
1283
+ statusName = 'Approve'
1284
+ }
1285
+ item.statusName = statusName;
1286
+ item.name = name
1287
+ data.push(item)
1288
+ })
1289
+
1290
+
1291
+ } catch (e) {
1292
+ console.log(e)
1293
+ }
1294
+
1295
+ res.json(data)
1296
+ })
1297
+
1214
1298
  router.get("/zdownload/zgenerator/:table", async (req, res) => {
1215
1299
  var table = req.params.table;
1216
1300
  var path = `${dirRoot}/public/uploads/zgenerator/${table}.json`;