zet-lib 1.2.18 → 1.2.19

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 +199 -147
  2. package/package.json +1 -1
package/lib/zRoute.js CHANGED
@@ -713,7 +713,7 @@ zRoute.dataTableFilter = (MYMODEL, relations, filter) => {
713
713
  break
714
714
 
715
715
  case 'datepicker':
716
- dataTable[key] = `<input type="text" class="form-control search datepicker" value="${value}" id="data_table_${key}" >`
716
+ dataTable[key] = `<input type="text" class="form-control search" value="${value}" id="data_table_${key}" >`
717
717
  types[key] = 'input'
718
718
  break
719
719
 
@@ -1670,174 +1670,226 @@ zRoute.dataTableSave = async (routeName, userId, body) => {
1670
1670
  dynamic list Data Table
1671
1671
  */
1672
1672
  zRoute.listDataTable = async (req, res, objData = {}) => {
1673
- const body = req.body
1674
- const MYMODEL = objData.MYMODEL || {}
1675
- const fields = Object.prototype.hasOwnProperty.call(objData, 'fields') ? objData.fields : req.body.fields
1676
- const relations = await zRoute.relations(req, res, MYMODEL.table)
1677
- const select = Object.prototype.hasOwnProperty.call(objData, 'select') ? objData.select : Util.selectParser(fields, MYMODEL)
1678
- const columns = Object.prototype.hasOwnProperty.call(objData, 'columns') ? objData.columns : body.columns
1679
- const asDate = []
1680
- for (let key in MYMODEL.widgets) {
1681
- if (MYMODEL.widgets[key].name == 'datetime') {
1682
- asDate.push(key)
1673
+ const room = res.locals.token || 'aa'
1674
+ try {
1675
+ const body = req.body
1676
+ const MYMODEL = objData.MYMODEL || {}
1677
+ const fields = Object.prototype.hasOwnProperty.call(objData, 'fields') ? objData.fields : req.body.fields
1678
+ const relations = await zRoute.relations(req, res, MYMODEL.table)
1679
+ const select = Object.prototype.hasOwnProperty.call(objData, 'select') ? objData.select : Util.selectParser(fields, MYMODEL)
1680
+ const columns = Object.prototype.hasOwnProperty.call(objData, 'columns') ? objData.columns : body.columns
1681
+ const asDate = []
1682
+ const asJSONb = []
1683
+ for (let key in MYMODEL.widgets) {
1684
+ if (MYMODEL.widgets[key].name == 'datetime') {
1685
+ asDate.push(key)
1686
+ }
1687
+ if (MYMODEL.widgets[key].name == 'datepicker') {
1688
+ asDate.push(key)
1689
+ }
1690
+ if (MYMODEL.widgets[key].name == 'dropdown_multi') {
1691
+ asJSONb.push(key)
1692
+ }
1693
+ if (MYMODEL.widgets[key].name == 'dropdown_checkbox') {
1694
+ asJSONb.push(key)
1695
+ }
1683
1696
  }
1684
- if (MYMODEL.widgets[key].name == 'datepicker') {
1685
- asDate.push(key)
1697
+ let whereArray = []
1698
+ if (objData.hasOwnProperty('whereArray')) {
1699
+ whereArray = objData['whereArray']
1686
1700
  }
1701
+ whereArray.push({
1702
+ field: 'company_id',
1703
+ option: '=',
1704
+ value: res.locals.companyId,
1705
+ operator: 'AND',
1706
+ })
1707
+
1708
+ columns.forEach(function (item) {
1709
+ if (item.search.value) {
1710
+ let astype = 'text'
1711
+ if (asDate.includes(fields[item.data])) {
1712
+ astype = 'date'
1713
+ }
1714
+ if (asJSONb.includes(fields[item.data])) {
1715
+ astype = 'jsonb'
1716
+ }
1717
+ if (astype === 'jsonb') {
1718
+ whereArray.push({
1719
+ field: fields[item.data],
1720
+ option: ' ',
1721
+ value: ` @> '["${item.search.value}"]'`,
1722
+ operator: 'AND',
1723
+ type: 'inline',
1724
+ })
1725
+ } else {
1726
+ whereArray.push({
1727
+ field: fields[item.data],
1728
+ option: MYMODEL.options[fields[item.data]],
1729
+ value: item.search.value,
1730
+ operator: 'AND',
1731
+ type: astype,
1732
+ })
1733
+ }
1734
+ }
1735
+ })
1736
+ const orderColumn = fields[body.order[0].column] == 'actionColumn' ? 'id' : fields[body.order[0].column] == 'no' ? 'id' : fields[body.order[0].column] == 'actionColum' ? 'id' : fields[body.order[0].column]
1737
+ const rows = await connection.results({
1738
+ select: select,
1739
+ table: MYMODEL.table,
1740
+ whereArray: whereArray,
1741
+ limit: body.length,
1742
+ offset: body.start,
1743
+ orderBy: [orderColumn, body.order[0].dir],
1744
+ })
1745
+ const count = await connection.result({
1746
+ select: 'count(id) as count',
1747
+ table: MYMODEL.table,
1748
+ whereArray: whereArray,
1749
+ })
1750
+ let datas = []
1751
+ const zRole = objData.zRole || require('./zRole')
1752
+ const levels = objData.hasOwnProperty('levels') ? objData.levels : zRole.myLevel(req, res, MYMODEL.table)
1753
+ if (objData.hasOwnProperty('datas')) {
1754
+ datas = objData.datas(body, rows, fields, relations, levels)
1755
+ } else {
1756
+ rows.forEach(function (row, index) {
1757
+ let arr = []
1758
+ fields.forEach(function (item) {
1759
+ if (item == 'no') {
1760
+ arr.push(index + 1 + parseInt(body.start))
1761
+ } else if (item == 'actionColumn') {
1762
+ let buttons = objData.hasOwnProperty('actionButtons') ? objData.actionButtons(levels, row, MYMODEL.table) : zRoute.actionButtons(levels, row, MYMODEL.table)
1763
+ arr.push(buttons)
1764
+ } else {
1765
+ let data = objData.hasOwnProperty('datas') ? objData.data(item, row[item], MYMODEL, relations) : zRoute.dataTableData(item, row[item], MYMODEL, relations)
1766
+ arr.push(data)
1767
+ }
1768
+ })
1769
+ datas.push(arr)
1770
+ })
1771
+ }
1772
+
1773
+ const data = {
1774
+ draw: body.draw,
1775
+ recordsTotal: count.count || 0,
1776
+ recordsFiltered: count.count || 0,
1777
+ data: datas,
1778
+ }
1779
+ //save grid filter async
1780
+ zRoute.dataTableSave(MYMODEL.routeName, res.locals.userId, body)
1781
+ res.json(data)
1782
+ } catch (err) {
1783
+ console.log(err)
1784
+ io.to(room).emit('error', err + '')
1687
1785
  }
1688
- let whereArray = []
1689
- if (objData.hasOwnProperty('whereArray')) {
1690
- whereArray = objData['whereArray']
1691
- }
1692
- whereArray.push({
1693
- field: 'company_id',
1694
- option: '=',
1695
- value: res.locals.companyId,
1696
- operator: 'AND',
1697
- })
1786
+ }
1698
1787
 
1699
- columns.forEach(function (item) {
1700
- if (item.search.value) {
1701
- let astype = 'text'
1702
- if (asDate.includes(fields[item.data])) {
1703
- astype = 'date'
1788
+ zRoute.listData = async (req, res, MYMODEL, zRole, actionButtonsFn) => {
1789
+ const room = res.locals.token || 'aa'
1790
+ try {
1791
+ actionButtonsFn = actionButtonsFn || function () {}
1792
+ const relations = await zRoute.relations(req, res, MYMODEL.table)
1793
+ const body = req.body
1794
+ const fields = body.fields
1795
+ const select = Util.selectParser(fields, MYMODEL)
1796
+ const asDate = []
1797
+ const asJSONb = []
1798
+ for (let key in MYMODEL.widgets) {
1799
+ if (MYMODEL.widgets[key].name == 'datetime') {
1800
+ asDate.push(key)
1801
+ }
1802
+ if (MYMODEL.widgets[key].name == 'datepicker') {
1803
+ asDate.push(key)
1804
+ }
1805
+ if (MYMODEL.widgets[key].name == 'dropdown_multi') {
1806
+ asJSONb.push(key)
1704
1807
  }
1808
+ if (MYMODEL.widgets[key].name == 'dropdown_checkbox') {
1809
+ asJSONb.push(key)
1810
+ }
1811
+ }
1812
+ let whereArray = []
1813
+ const columns = body.columns
1814
+ if (MYMODEL.keys.includes('company_id')) {
1705
1815
  whereArray.push({
1706
- field: fields[item.data],
1707
- option: MYMODEL.options[fields[item.data]],
1708
- value: item.search.value,
1816
+ field: 'company_id',
1817
+ option: '=',
1818
+ value: res.locals.companyId,
1709
1819
  operator: 'AND',
1710
- type: astype,
1711
1820
  })
1712
1821
  }
1713
- })
1714
- const orderColumn = fields[body.order[0].column] == 'actionColumn' ? 'id' : fields[body.order[0].column] == 'no' ? 'id' : fields[body.order[0].column] == 'actionColum' ? 'id' : fields[body.order[0].column]
1715
- const rows = await connection.results({
1716
- select: select,
1717
- table: MYMODEL.table,
1718
- whereArray: whereArray,
1719
- limit: body.length,
1720
- offset: body.start,
1721
- orderBy: [orderColumn, body.order[0].dir],
1722
- })
1723
- const count = await connection.result({
1724
- select: 'count(id) as count',
1725
- table: MYMODEL.table,
1726
- whereArray: whereArray,
1727
- })
1728
- let datas = []
1729
- const zRole = objData.zRole || require('./zRole')
1730
- const levels = objData.hasOwnProperty('levels') ? objData.levels : zRole.myLevel(req, res, MYMODEL.table)
1731
- if (objData.hasOwnProperty('datas')) {
1732
- datas = objData.datas(body, rows, fields, relations, levels)
1733
- } else {
1822
+ columns.forEach(function (item) {
1823
+ if (item.search.value) {
1824
+ let astype = 'text'
1825
+ if (asDate.includes(fields[item.data])) {
1826
+ astype = 'date'
1827
+ }
1828
+ if (asJSONb.includes(fields[item.data])) {
1829
+ astype = 'jsonb'
1830
+ }
1831
+ if (astype === 'jsonb') {
1832
+ whereArray.push({
1833
+ field: fields[item.data],
1834
+ option: ' ',
1835
+ value: ` @> '["${item.search.value}"]'`,
1836
+ operator: 'AND',
1837
+ type: 'inline',
1838
+ })
1839
+ } else {
1840
+ whereArray.push({
1841
+ field: fields[item.data],
1842
+ option: MYMODEL.options[fields[item.data]],
1843
+ value: item.search.value,
1844
+ operator: 'AND',
1845
+ type: astype,
1846
+ })
1847
+ }
1848
+ }
1849
+ })
1850
+ const orderColumn = fields[body.order[0].column] == 'actionColumn' ? 'id' : fields[body.order[0].column] == 'no' ? 'id' : fields[body.order[0].column] == 'actionColum' ? 'id' : fields[body.order[0].column]
1851
+ const rows = await connection.results({
1852
+ select: select,
1853
+ table: MYMODEL.table,
1854
+ whereArray: whereArray,
1855
+ limit: body.length,
1856
+ offset: body.start,
1857
+ orderBy: [orderColumn, body.order[0].dir],
1858
+ })
1859
+ const count = await connection.result({
1860
+ select: 'count(id) as count',
1861
+ table: MYMODEL.table,
1862
+ whereArray: whereArray,
1863
+ })
1864
+ let datas = []
1865
+ const levels = zRole.myLevel(req, res, MYMODEL.table)
1734
1866
  rows.forEach(function (row, index) {
1735
1867
  let arr = []
1736
1868
  fields.forEach(function (item) {
1737
1869
  if (item == 'no') {
1738
1870
  arr.push(index + 1 + parseInt(body.start))
1739
1871
  } else if (item == 'actionColumn') {
1740
- let buttons = objData.hasOwnProperty('actionButtons') ? objData.actionButtons(levels, row, MYMODEL.table) : zRoute.actionButtons(levels, row, MYMODEL.table)
1872
+ let buttons = !actionButtonsFn(levels, row, MYMODEL.table) ? zRoute.actionButtons(levels, row, MYMODEL.table) : actionButtonsFn(levels, row, MYMODEL.table)
1741
1873
  arr.push(buttons)
1742
1874
  } else {
1743
- let data = objData.hasOwnProperty('datas') ? objData.data(item, row[item], MYMODEL, relations) : zRoute.dataTableData(item, row[item], MYMODEL, relations)
1744
- arr.push(data)
1875
+ arr.push(zRoute.dataTableData(item, row[item], MYMODEL, relations))
1745
1876
  }
1746
1877
  })
1747
1878
  datas.push(arr)
1748
1879
  })
1880
+ const data = {
1881
+ draw: body.draw,
1882
+ recordsTotal: count.count || 0,
1883
+ recordsFiltered: count.count || 0,
1884
+ data: datas,
1885
+ }
1886
+ //save grid filter async
1887
+ zRoute.dataTableSave(MYMODEL.routeName, res.locals.userId, body)
1888
+ res.json(data)
1889
+ } catch (err) {
1890
+ console.log(err)
1891
+ io.to(room).emit('error', err + '')
1749
1892
  }
1750
-
1751
- const data = {
1752
- draw: body.draw,
1753
- recordsTotal: count.count || 0,
1754
- recordsFiltered: count.count || 0,
1755
- data: datas,
1756
- }
1757
- //save grid filter async
1758
- zRoute.dataTableSave(MYMODEL.routeName, res.locals.userId, body)
1759
- res.json(data)
1760
- }
1761
-
1762
- zRoute.listData = async (req, res, MYMODEL, zRole, actionButtonsFn) => {
1763
- actionButtonsFn = actionButtonsFn || function () {}
1764
- const relations = await zRoute.relations(req, res, MYMODEL.table)
1765
- const body = req.body
1766
- const fields = body.fields
1767
- const select = Util.selectParser(fields, MYMODEL)
1768
- const asDate = []
1769
- for (let key in MYMODEL.widgets) {
1770
- if (MYMODEL.widgets[key].name == 'datetime') {
1771
- asDate.push(key)
1772
- }
1773
- if (MYMODEL.widgets[key].name == 'datepicker') {
1774
- asDate.push(key)
1775
- }
1776
- }
1777
- let whereArray = []
1778
- const columns = body.columns
1779
- if (MYMODEL.keys.includes('company_id')) {
1780
- whereArray.push({
1781
- field: 'company_id',
1782
- option: '=',
1783
- value: res.locals.companyId,
1784
- operator: 'AND',
1785
- })
1786
- }
1787
- columns.forEach(function (item) {
1788
- if (item.search.value) {
1789
- let astype = 'text'
1790
- if (asDate.includes(fields[item.data])) {
1791
- astype = 'date'
1792
- }
1793
- whereArray.push({
1794
- field: fields[item.data],
1795
- option: MYMODEL.options[fields[item.data]],
1796
- value: item.search.value,
1797
- operator: 'AND',
1798
- type: astype,
1799
- })
1800
- }
1801
- })
1802
- const orderColumn = fields[body.order[0].column] == 'actionColumn' ? 'id' : fields[body.order[0].column] == 'no' ? 'id' : fields[body.order[0].column] == 'actionColum' ? 'id' : fields[body.order[0].column]
1803
- const rows = await connection.results({
1804
- select: select,
1805
- table: MYMODEL.table,
1806
- whereArray: whereArray,
1807
- limit: body.length,
1808
- offset: body.start,
1809
- orderBy: [orderColumn, body.order[0].dir],
1810
- })
1811
- const count = await connection.result({
1812
- select: 'count(id) as count',
1813
- table: MYMODEL.table,
1814
- whereArray: whereArray,
1815
- })
1816
- let datas = []
1817
- const levels = zRole.myLevel(req, res, MYMODEL.table)
1818
- rows.forEach(function (row, index) {
1819
- let arr = []
1820
- fields.forEach(function (item) {
1821
- if (item == 'no') {
1822
- arr.push(index + 1 + parseInt(body.start))
1823
- } else if (item == 'actionColumn') {
1824
- let buttons = !actionButtonsFn(levels, row, MYMODEL.table) ? zRoute.actionButtons(levels, row, MYMODEL.table) : actionButtonsFn(levels, row, MYMODEL.table)
1825
- arr.push(buttons)
1826
- } else {
1827
- arr.push(zRoute.dataTableData(item, row[item], MYMODEL, relations))
1828
- }
1829
- })
1830
- datas.push(arr)
1831
- })
1832
- const data = {
1833
- draw: body.draw,
1834
- recordsTotal: count.count || 0,
1835
- recordsFiltered: count.count || 0,
1836
- data: datas,
1837
- }
1838
- //save grid filter async
1839
- zRoute.dataTableSave(MYMODEL.routeName, res.locals.userId, body)
1840
- res.json(data)
1841
1893
  }
1842
1894
 
1843
1895
  zRoute.actionButtons = (levels, row, table, callback = null) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zet-lib",
3
- "version": "1.2.18",
3
+ "version": "1.2.19",
4
4
  "description": "zet is a library that part of zet generator.",
5
5
  "engines": {
6
6
  "node": ">=18"