zet-lib 1.2.83 → 1.2.84

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/moduleLib.js CHANGED
@@ -485,4 +485,14 @@ m.selectpicker = function (req, res, elem) {
485
485
  res.locals.moduleEnd = end
486
486
  }
487
487
 
488
+ m.slugHTML = (req, res, content, at = 'end') => {
489
+ if (content) {
490
+ if (at == 'end') {
491
+ res.locals.moduleEnd = content
492
+ } else {
493
+ res.locals.moduleHead = content
494
+ }
495
+ }
496
+ }
497
+
488
498
  module.exports = m
package/lib/zAppRouter.js CHANGED
@@ -1576,4 +1576,31 @@ router.post('/zdropzone-attributes', async (req, res) => {
1576
1576
  }
1577
1577
  })
1578
1578
 
1579
+ router.post('/zhistory-data', async (req, res) => {
1580
+ let html = ''
1581
+ try {
1582
+ let id = req.body.id
1583
+ let table = req.body.table
1584
+ const relations = await zRoute.relations(req, res, table)
1585
+ const MYMODELS = zRoute.MYMODELS()
1586
+ const MYMODEL = MYMODELS[table]
1587
+ let results = await connection.results({
1588
+ table: 'zhistory',
1589
+ where: {
1590
+ module: table,
1591
+ module_id: id,
1592
+ },
1593
+ order_by: ['id asc'],
1594
+ })
1595
+ if (results.length > 0) {
1596
+ let users = myCache.get('users')
1597
+ html = await zRoute.history(req, res, relations, id, MYMODEL, users, results)
1598
+ }
1599
+ res.json(html)
1600
+ } catch (e) {
1601
+ console.log(e)
1602
+ res.json(e + '')
1603
+ }
1604
+ })
1605
+
1579
1606
  module.exports = router
package/lib/zRoute.js CHANGED
@@ -2576,8 +2576,37 @@ zRoute.viewFormsSync = async (req, res, MYMODEL, data = {}) => {
2576
2576
  })
2577
2577
  approval_history_table += `</ul></div>`
2578
2578
  }
2579
-
2580
2579
  forms.approval_history_table = approval_history_table
2580
+
2581
+ //get history / audit trail
2582
+ //add head css
2583
+ let contentModal = `<div class="modal" id="modal-history" tabindex="-1">
2584
+ <div class="modal-dialog modal-xl modal-dialog-scrollable">
2585
+ <div class="modal-content">
2586
+ <div class="modal-header">
2587
+ <h5 class="modal-title">History</h5>
2588
+ <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
2589
+ </div>
2590
+ <div class="modal-body" id="modal-body-history"></div>
2591
+ </div>
2592
+ </div>
2593
+ </div>
2594
+
2595
+ <span class="icon-small icons-primary" title="History" style="position: fixed;bottom: 10px;left: 400px" data-bs-toggle="modal" data-bs-target="#modal-history"><img class="icons-bg-white icon-image-large" src="/assets/icons/history.svg"></span>
2596
+ `
2597
+
2598
+ moduleLib.slugHTML(req, res, contentModal)
2599
+ let contentScript = `$(function(){
2600
+ ajaxPost("/zhistory-data",{id:${data.id},table:"${MYMODEL.table}"},(data) => {$("#modal-body-history").html(data)});
2601
+ $("#${MYMODEL.table}-delete").on("click", (() => {
2602
+ window.confirm("Sure to remove data ?") && ajaxDelete("/${MYMODEL.table}/delete", {id: "${data.id}"}, (function (e) {
2603
+ toastrForm(e), 1 == e.status && (location.href = "/${MYMODEL.table}")
2604
+ }))
2605
+ }))
2606
+
2607
+ })`
2608
+ moduleLib.addScript(req, res, contentScript)
2609
+
2581
2610
  return forms
2582
2611
  }
2583
2612
 
@@ -3659,6 +3688,23 @@ zRoute.updateSQL = async (req, res, table, data, whereData) => {
3659
3688
  delete data.approval_history
3660
3689
  if (result.lock != 1) {
3661
3690
  const myresult = await connection.update({ table: table, where: whereData, data: data })
3691
+ //save to history
3692
+ connection.insert({
3693
+ table: 'zhistory',
3694
+ data: {
3695
+ module_id: result.id,
3696
+ module: MYMODEL.table,
3697
+ data_1: JSON.stringify(result),
3698
+ data_2: JSON.stringify(myresult),
3699
+ data_1_date: result.updated_at,
3700
+ data_2_date: data.updated_at,
3701
+ created_by: userId,
3702
+ updated_by: userId,
3703
+ created_at: Util.now(),
3704
+ updated_at: Util.now(),
3705
+ company_id: res.locals.companyId,
3706
+ },
3707
+ })
3662
3708
  zRoute.modelsCacheRenew(table, res.locals.companyId)
3663
3709
  return myresult
3664
3710
  } else {
@@ -4271,4 +4317,112 @@ zRoute.typeaheadpost = async (req, res) => {
4271
4317
  res.json(result.zname)
4272
4318
  }
4273
4319
 
4320
+ zRoute.history = async (req, res, relations, id, MYMODEL, users, results = []) => {
4321
+ //html
4322
+ let html = ''
4323
+ let unhistories = ['created_by', 'updated_by', 'created_at', 'updated_at', 'company_id', 'id']
4324
+ let arrHistories = []
4325
+ let arrDataAttributes = []
4326
+ let objectType = ['dropzone', 'dropdown_multi', 'dropdown_checkbox', 'array', 'json_array', 'table', 'json']
4327
+ const widgets = MYMODEL.widgets
4328
+ let fields = Object.keys(MYMODEL.labels)
4329
+ if (results.length == 0) {
4330
+ html = 'No History...'
4331
+ } else {
4332
+ for (const result of results) {
4333
+ let obj = {}
4334
+ let data1 = result.data_1
4335
+ let data2 = result.data_2
4336
+
4337
+ for (const key in data1) {
4338
+ if (!unhistories.includes(key)) {
4339
+ if (fields.includes(key)) {
4340
+ if (data1[key] || data2[key]) {
4341
+ let name = MYMODEL.widgets[key].name
4342
+ if (objectType.includes(name)) {
4343
+ if (JSON.stringify(data1[key]) != JSON.stringify(data2[key])) {
4344
+ obj[key] = [data1[key], data2[key]]
4345
+ }
4346
+ } else {
4347
+ if (data1[key] != data2[key]) {
4348
+ obj[key] = [data1[key], data2[key]]
4349
+ }
4350
+ }
4351
+ }
4352
+ }
4353
+ }
4354
+ }
4355
+ if (Object.keys(obj).length > 0) {
4356
+ arrHistories.push(obj)
4357
+ arrDataAttributes.push({
4358
+ date1: result.data_1_date,
4359
+ date2: result.data_2_date,
4360
+ by1: result.data_1.created_by,
4361
+ by2: result.created_by,
4362
+ })
4363
+ }
4364
+ }
4365
+
4366
+ let index = 0
4367
+ for (const item of arrHistories) {
4368
+ let obj1 = {}
4369
+ let obj2 = {}
4370
+ for (let key in item) {
4371
+ obj1[key] = item[key][0]
4372
+ obj2[key] = item[key][1]
4373
+ }
4374
+ let viewFormsSync1 = await zRoute.viewFormsSync(req, res, MYMODEL, obj1)
4375
+ let viewFormsSync2 = await zRoute.viewFormsSync(req, res, MYMODEL, obj2)
4376
+ let html1 = ''
4377
+ let html2 = ''
4378
+ for (let key in item) {
4379
+ html1 += viewFormsSync1.build[key]
4380
+ html2 += viewFormsSync2.build[key]
4381
+ }
4382
+
4383
+ html += `<div class="row">
4384
+ <!-- timeline item 1 left dot -->
4385
+ <div class="col-auto text-center flex-column d-none d-sm-flex">
4386
+ <div class="row h-50">
4387
+ <div class="col border-end">&nbsp;</div>
4388
+ <div class="col">&nbsp;</div>
4389
+ </div>
4390
+ <h5 class="m-2">
4391
+ <span class="badge rounded-pill bg-light border">&nbsp;</span>
4392
+ </h5>
4393
+ <div class="row h-50">
4394
+ <div class="col border-end order">&nbsp;</div>
4395
+ <div class="col">&nbsp;</div>
4396
+ </div>
4397
+ </div>
4398
+ <!-- timeline item 1 event content -->
4399
+ <div class="col py-2">
4400
+ <div class="row">
4401
+ <div class="col-md-6">
4402
+ <div class="card">
4403
+ <div class="card-body">
4404
+ <div class="float-end text-muted text-end">${Util.dateFormat(arrDataAttributes[index].date1, 'DD MMM YYYY HH:mm') || ''}<br><small>${users[arrDataAttributes[index].by1].fullname}</small></div>
4405
+ <h4 class="card-title text-muted">Semula</h4>
4406
+ <div class="card-text">${html1}</div>
4407
+ </div>
4408
+ </div>
4409
+ </div>
4410
+ <div class="col-md-6">
4411
+ <div class="card">
4412
+ <div class="card-body">
4413
+ <div class="float-end text-muted text-end">${Util.dateFormat(arrDataAttributes[index].date2, 'DD MMM YYYY HH:mm') || ''} <br><small>${users[arrDataAttributes[index].by2].fullname}</small></div>
4414
+ <h4 class="card-title text-success">Menjadi </h4>
4415
+ <div class="card-text">${html2}</div>
4416
+ </div>
4417
+ </div>
4418
+ </div>
4419
+ </div>
4420
+ </div>
4421
+ </div>`
4422
+
4423
+ index++
4424
+ }
4425
+ }
4426
+ return html
4427
+ }
4274
4428
  module.exports = zRoute
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zet-lib",
3
- "version": "1.2.83",
3
+ "version": "1.2.84",
4
4
  "description": "zet is a library that part of zet generator.",
5
5
  "engines": {
6
6
  "node": ">=18"