index.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. const express = require('express');
  2. const path = require('path');
  3. const CrudModule = require('../../base/CrudModule');
  4. const RepositorySystem = require('../../systems/RepositorySystem');
  5. const RoutingSystem = require('../../systems/RoutingSystem');
  6. const Actions = require('../../systems/ActionSystem');
  7. const Filter = require('../Filters/FilterService');
  8. let ProductScheme = {
  9. fields: [
  10. {
  11. key: "name",
  12. type: "text"
  13. },
  14. {
  15. key:"active",
  16. type: "boolean"
  17. },
  18. {
  19. key: "price",
  20. type: "decimal"
  21. },
  22. {
  23. key: "wprice",
  24. type: "decimal"
  25. },
  26. {
  27. key: "quantity",
  28. type: "integer"
  29. },
  30. {
  31. key: "weight",
  32. type: "decimal"
  33. },
  34. {
  35. key: "description",
  36. type: "text"
  37. },
  38. {
  39. key: "shortdescription",
  40. type: "text"
  41. },
  42. {
  43. key: "reference",
  44. type: "text"
  45. },
  46. {
  47. key: "ean13",
  48. type: "text"
  49. },
  50. {
  51. key: "urlseo",
  52. type: "text"
  53. },
  54. {
  55. key: "image",
  56. type: "text"
  57. }
  58. ]
  59. };
  60. class ProductsModule extends CrudModule {
  61. constructor(){
  62. super('/products', [2,0,3,4,0], 'Products', 'fa-shopping-basket');
  63. this.repository = RepositorySystem.create('Products', ProductScheme);
  64. this.setRepo(this.repository);
  65. // this.users = RepositorySystem.create('Users'); // You can get any repo you want!!
  66. this.router = express.Router();
  67. this.init();
  68. this.Model = {
  69. ...this.Model,
  70. price: {
  71. Filter: {
  72. type: "range",
  73. options: [0, 200],
  74. name: "Price",
  75. field: 'price',
  76. order: 20
  77. }
  78. },
  79. name: {
  80. Filter: {
  81. type: "search",
  82. options: [],
  83. name: "Product Name",
  84. field: 'name',
  85. order: 10
  86. }
  87. },
  88. deleted: { disabled: true },
  89. description: {
  90. Filter: {
  91. type: "editor",
  92. options: [],
  93. name: "Description",
  94. field: 'description',
  95. order: -1
  96. }
  97. },
  98. shortdescription: {
  99. Filter: {
  100. type: "editor",
  101. options: [],
  102. name: "Short description",
  103. field: 'shortdescription',
  104. order: -1
  105. }
  106. },
  107. reference: {
  108. Filter: {
  109. type: "search",
  110. options: [],
  111. name: "Reference Code",
  112. field: 'reference',
  113. order: -1
  114. }
  115. },
  116. active: {
  117. Filter: {
  118. type: "select",
  119. options: [0,1],
  120. name: "Active",
  121. field: 'active',
  122. order: -1
  123. }
  124. }
  125. }
  126. }
  127. init() {
  128. let routes = [
  129. {
  130. type: 'custom',
  131. method: 'get',
  132. endpoint: '/select/:name',
  133. customFn: () => {}
  134. },
  135. {
  136. type: 'custom',
  137. method: 'get',
  138. endpoint: '/search/:search',
  139. customFn: (req, res) => {
  140. this.search(req, res);
  141. }
  142. }
  143. ];
  144. RoutingSystem.loadRoutes(routes, this.router, '/products');
  145. }
  146. search(req, res) {
  147. let searchParam = req.params.search;
  148. Filter.parseFilters(this.repository.raw(), req.filters).where((builder) =>
  149. searchParam.split(" ").reduce((res, opt) => {
  150. return res.where((b) => b.where('name', 'like', '%' + opt + '%').orWhere('description', 'like', '%' + opt + '%'));
  151. }, builder)
  152. )
  153. .then((data) => Actions.emit('getProduct', data))
  154. .then((data) => res.send(data));
  155. }
  156. }
  157. module.exports = ProductsModule;