zet-lib 1.3.26 → 1.3.27

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.
Files changed (2) hide show
  1. package/lib/zRoute.js +124 -58
  2. package/package.json +1 -1
package/lib/zRoute.js CHANGED
@@ -1384,6 +1384,7 @@ zRoute.selectAllowedField = (req, res, MYMODEL, arr = []) => {
1384
1384
  }
1385
1385
 
1386
1386
  zRoute.excelQuery = async (req, res, MYMODEL, paramsObject = {}) => {
1387
+ const table = MYMODEL.table
1387
1388
  const cacheRole = myCache.get('ROLES')[res.locals.roleId].params
1388
1389
  const levels = cacheRole[MYMODEL.table] || []
1389
1390
  if (!levels.includes('export')) {
@@ -1393,20 +1394,28 @@ zRoute.excelQuery = async (req, res, MYMODEL, paramsObject = {}) => {
1393
1394
 
1394
1395
  let virtuals = []
1395
1396
  let customs = []
1396
- let asDate = []
1397
- for (var key in MYMODEL.widgets) {
1398
- if (MYMODEL.widgets[key].name === 'virtual') {
1399
- virtuals.push(key)
1400
- }
1401
- if (MYMODEL.widgets[key].name === 'custom') {
1402
- customs.push(key)
1403
- }
1397
+ const asDate = []
1398
+ const asJSONb = []
1399
+ const asArray = []
1400
+ for (let key in MYMODEL.widgets) {
1404
1401
  if (MYMODEL.widgets[key].name == 'datetime') {
1405
1402
  asDate.push(key)
1406
1403
  }
1407
1404
  if (MYMODEL.widgets[key].name == 'datepicker') {
1408
1405
  asDate.push(key)
1409
1406
  }
1407
+ if (MYMODEL.widgets[key].name == 'dropdown_multi') {
1408
+ asJSONb.push(key)
1409
+ }
1410
+ if (MYMODEL.widgets[key].name == 'dragdrop') {
1411
+ asJSONb.push(key)
1412
+ }
1413
+ if (MYMODEL.widgets[key].name == 'dropdown_checkbox') {
1414
+ asJSONb.push(key)
1415
+ }
1416
+ if (MYMODEL.widgets[key].name == 'json_array' || MYMODEL.widgets[key].name == 'array' || MYMODEL.widgets[key].name == 'dropzone') {
1417
+ asArray.push(key)
1418
+ }
1410
1419
  }
1411
1420
  //check directory for import not export
1412
1421
  const dir = `${dirRoot}/public/excel/tmp`
@@ -1441,7 +1450,8 @@ zRoute.excelQuery = async (req, res, MYMODEL, paramsObject = {}) => {
1441
1450
  let difference = allfields.filter((x) => !fields.includes(x))
1442
1451
  arr = [...fields, ...difference].filter((x) => !virtuals.includes(x))
1443
1452
  arr = arr.filter((item) => !customs.includes(item))
1444
- select = `"${arr.join('","')}"`
1453
+ //select = `"${arr.join('","')}"`
1454
+ select = Util.selectParser(arr, MYMODEL)
1445
1455
  }
1446
1456
  let whereArray = []
1447
1457
  if (paramsObject.hasOwnProperty('whereArray')) {
@@ -1451,48 +1461,90 @@ zRoute.excelQuery = async (req, res, MYMODEL, paramsObject = {}) => {
1451
1461
  whereArray.push(paramsObject.whereArray)
1452
1462
  }
1453
1463
  }
1464
+
1465
+ whereArray.push({
1466
+ field: `${table}.company_id`,
1467
+ option: '=',
1468
+ value: res.locals.companyId,
1469
+ operator: 'AND',
1470
+ })
1471
+
1454
1472
  let columns = body.columns
1455
1473
  columns.forEach(function (item) {
1456
1474
  if (item.search.value) {
1457
1475
  let astype = 'text'
1458
- if (asDate.includes(body.fields[item.data])) {
1476
+ if (asDate.includes(fields[item.data])) {
1459
1477
  astype = 'date'
1460
1478
  }
1461
- whereArray.push({
1462
- field: body.fields[item.data],
1463
- option: MYMODEL.options[body.fields[item.data]],
1464
- value: item.search.value,
1465
- operator: 'AND',
1466
- type: astype,
1467
- })
1479
+ if (asJSONb.includes(fields[item.data])) {
1480
+ astype = 'jsonb'
1481
+ }
1482
+ if (asArray.includes(fields[item.data])) {
1483
+ astype = 'array'
1484
+ }
1485
+ if (astype === 'jsonb') {
1486
+ whereArray.push({
1487
+ field: Util.fieldWithTable(fields[item.data], MYMODEL, true),
1488
+ option: ' ',
1489
+ value: ` @> '["${item.search.value}"]'`,
1490
+ operator: 'AND',
1491
+ type: 'inline',
1492
+ })
1493
+ } else if (astype === 'array') {
1494
+ whereArray.push({
1495
+ field: '',
1496
+ option: '',
1497
+ value: ` ${Util.fieldWithTable(fields[item.data], MYMODEL, true)}::text ILIKE '${item.search.value}' `,
1498
+ operator: 'AND',
1499
+ type: 'inline',
1500
+ })
1501
+ } else {
1502
+ whereArray.push({
1503
+ field: Util.fieldWithTable(fields[item.data], MYMODEL, true),
1504
+ option: MYMODEL.options[fields[item.data]],
1505
+ value: item.search.value,
1506
+ operator: 'AND',
1507
+ type: astype,
1508
+ })
1509
+ }
1468
1510
  }
1469
1511
  })
1470
- const orderColumn = body.fields[body.order[0].column] == 'actionColumn' ? 'id' : body.fields[body.order[0].column] == 'no' ? 'id' : body.fields[body.order[0].column] == 'actionColum' ? 'id' : body.fields[body.order[0].column]
1512
+
1513
+ const orderColumn = fields[body.order[0].column] == 'actionColumn' ? `${table}.id` : fields[body.order[0].column] == 'no' ? `${table}.id` : fields[body.order[0].column] == 'actionColum' ? `${table}.id` : fields[body.order[0].column]
1471
1514
  if (zstandart) {
1472
- select = '*'
1515
+ select = Util.selectParser(allfields, MYMODEL)
1473
1516
  }
1474
- const obj = {
1517
+ let gridData = {
1475
1518
  select: select,
1476
- table: MYMODEL.table,
1519
+ table: table,
1477
1520
  whereArray: whereArray,
1478
- orderBy: [orderColumn, body.order[0].dir],
1521
+ order_by: [`${orderColumn} ${body.order[0].dir}`],
1479
1522
  }
1480
1523
  if (!zall) {
1481
- obj.limit = body.length
1482
- obj.offset = body.start
1524
+ gridData.limit = body.length
1525
+ gridData.offset = body.start
1483
1526
  } else {
1484
- obj.limit = 50000
1485
- obj.offset = 0
1527
+ gridData.limit = 5000
1528
+ gridData.offset = 0
1529
+ }
1530
+ let joins = Util.tableWithJoin(MYMODEL)
1531
+ if (joins.length > 0) {
1532
+ gridData.joins = joins
1486
1533
  }
1487
- rows = await connection.results(obj)
1534
+ rows = await connection.results(gridData)
1488
1535
  } else {
1489
- rows = await connection.results({
1536
+ let gridData = {
1490
1537
  select: Util.selectParser(fields, MYMODEL),
1491
- table: MYMODEL.table,
1538
+ table: table,
1492
1539
  limit: reqQuery.pageSize,
1493
1540
  offset: parseInt(reqQuery.pageSize) - 1,
1494
1541
  orderBy: ['id', 'desc'],
1495
- })
1542
+ }
1543
+ let joins = Util.tableWithJoin(MYMODEL)
1544
+ if (joins.length > 0) {
1545
+ gridData.joins = joins
1546
+ }
1547
+ rows = await connection.results({ gridData })
1496
1548
  }
1497
1549
  if (zall) {
1498
1550
  fields = arr
@@ -1510,13 +1562,24 @@ zRoute.excelQuery = async (req, res, MYMODEL, paramsObject = {}) => {
1510
1562
  }
1511
1563
 
1512
1564
  // for excels
1513
- zRoute.excel = async (req, res, MYMODEL, fields, rows, callback, fileName) => {
1565
+ zRoute.excel = async (req, res, MYMODEL, myfields, rows, callback, fileName) => {
1514
1566
  const cacheRole = myCache.get('ROLES')[res.locals.roleId].params
1515
1567
  const levels = cacheRole[MYMODEL.table] || []
1516
1568
  if (!levels.includes('export')) {
1517
1569
  res.json('no access')
1518
1570
  return false
1519
1571
  }
1572
+ let joins = Util.tableWithJoin(MYMODEL)
1573
+ let notJoins = []
1574
+ if (joins.length > 0) {
1575
+ notJoins = MYMODEL.joins.list || []
1576
+ }
1577
+ let fields = []
1578
+ myfields.map((item) => {
1579
+ if (!notJoins.includes(item)) {
1580
+ fields.push(item)
1581
+ }
1582
+ })
1520
1583
 
1521
1584
  //if any other custom value then callback needed
1522
1585
  callback = callback || function () {}
@@ -1566,18 +1629,20 @@ zRoute.excel = async (req, res, MYMODEL, fields, rows, callback, fileName) => {
1566
1629
  let selectRelations = ''
1567
1630
  let aliasResults = {}
1568
1631
  for (let key in MYMODEL.widgets) {
1569
- if (MYMODEL.widgets[key].name == 'relation' || MYMODEL.widgets[key].name == 'typeahead') {
1570
- if (MYMODEL.widgets[key].import_field && MYMODEL.widgets[key].import_field != 'id') {
1571
- aliasResults[key] = Util.arrayToObject(
1572
- await connection.results({
1573
- select: `id, ${MYMODEL.widgets[key].import_field}`,
1574
- table: MYMODEL.widgets[key].table,
1575
- }),
1576
- 'id'
1577
- )
1578
- //console.log(aliasResults[key])
1579
- aliasRelations.push(key)
1580
- selectRelations += `, (select ${MYMODEL.widgets[key].import_field} from "${MYMODEL.widgets[key].table}" where ${MYMODEL.widgets[key].table}.id = ${MYMODEL.table}.${key} limit 1) as ${key} `
1632
+ if (!notJoins.includes(key)) {
1633
+ if (MYMODEL.widgets[key].name == 'relation' || MYMODEL.widgets[key].name == 'typeahead') {
1634
+ if (MYMODEL.widgets[key].import_field && MYMODEL.widgets[key].import_field != 'id') {
1635
+ aliasResults[key] = Util.arrayToObject(
1636
+ await connection.results({
1637
+ select: `id, ${MYMODEL.widgets[key].import_field}`,
1638
+ table: MYMODEL.widgets[key].table,
1639
+ }),
1640
+ 'id'
1641
+ )
1642
+ //console.log(aliasResults[key])
1643
+ aliasRelations.push(key)
1644
+ selectRelations += `, (select ${MYMODEL.widgets[key].import_field} from "${MYMODEL.widgets[key].table}" where ${MYMODEL.widgets[key].table}.id = ${MYMODEL.table}.${key} limit 1) as ${key} `
1645
+ }
1581
1646
  }
1582
1647
  }
1583
1648
  }
@@ -1591,20 +1656,22 @@ zRoute.excel = async (req, res, MYMODEL, fields, rows, callback, fileName) => {
1591
1656
  let usersObj = await zRoute.getUsers()
1592
1657
  if (Object.prototype.hasOwnProperty.call(MYMODEL, 'widgets')) {
1593
1658
  for (let key in MYMODEL.widgets) {
1594
- let widget = MYMODEL.widgets[key]
1595
- if (widget.name == 'password') {
1596
- passwords.push(key)
1597
- } else if (widget.name == 'select') {
1598
- tableObj[key] = widget.fields
1599
- } else if (widget.name == 'switch') {
1600
- tableObj[key] = widget.fields
1601
- } else if (widget.name == 'relation' || widget.name == 'typeahead') {
1602
- var rowsarr = await connection.results({
1603
- table: widget.table,
1604
- select: widget.fields[0] + ',' + widget.fields[1] + ' as zname ',
1605
- })
1606
- //save to object
1607
- relations[key] = Util.arrayToObject(rowsarr, 'id')
1659
+ if (!notJoins.includes(key)) {
1660
+ let widget = MYMODEL.widgets[key]
1661
+ if (widget.name == 'password') {
1662
+ passwords.push(key)
1663
+ } else if (widget.name == 'select') {
1664
+ tableObj[key] = widget.fields
1665
+ } else if (widget.name == 'switch') {
1666
+ tableObj[key] = widget.fields
1667
+ } else if (widget.name == 'relation' || widget.name == 'typeahead') {
1668
+ var rowsarr = await connection.results({
1669
+ table: widget.table,
1670
+ select: widget.fields[0] + ',' + widget.fields[1] + ' as zname ',
1671
+ })
1672
+ //save to object
1673
+ relations[key] = Util.arrayToObject(rowsarr, 'id')
1674
+ }
1608
1675
  }
1609
1676
  }
1610
1677
  }
@@ -2073,7 +2140,6 @@ zRoute.listDataTable = async (req, res, objData = {}) => {
2073
2140
  datas.push(arr)
2074
2141
  })
2075
2142
  }
2076
-
2077
2143
  const data = {
2078
2144
  draw: body.draw,
2079
2145
  recordsTotal: count.count || 0,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zet-lib",
3
- "version": "1.3.26",
3
+ "version": "1.3.27",
4
4
  "description": "zet is a library that part of zet generator.",
5
5
  "engines": {
6
6
  "node": ">=18"