const express = require('express'); const path = require('path'); const CrudModule = require('../../base/CrudModule'); const RepositorySystem = require('../../systems/RepositorySystem'); const RoutingSystem = require('../../systems/RoutingSystem'); const Actions = require('../../systems/ActionSystem'); const Filter = require('../Filters/FilterService'); let ProductScheme = { fields: [ { key: "name", type: "text" }, { key:"active", type: "boolean" }, { key: "price", type: "decimal" }, { key: "wprice", type: "decimal" }, { key: "quantity", type: "integer" }, { key: "weight", type: "decimal" }, { key: "description", type: "text" }, { key: "shortdescription", type: "text" }, { key: "reference", type: "text" }, { key: "ean13", type: "text" }, { key: "urlseo", type: "text" }, { key: "image", type: "text" } ] }; class ProductsModule extends CrudModule { constructor(){ super('/products', [2,0,3,4,0], 'Products', 'fa-shopping-basket'); this.repository = RepositorySystem.create('Products', ProductScheme); this.setRepo(this.repository); // this.users = RepositorySystem.create('Users'); // You can get any repo you want!! this.router = express.Router(); this.init(); this.Model = { ...this.Model, price: { Filter: { type: "range", options: [0, 200], name: "Price", field: 'price', order: 20 } }, name: { Filter: { type: "search", options: [], name: "Product Name", field: 'name', order: 10 } }, deleted: { disabled: true }, description: { Filter: { type: "editor", options: [], name: "Description", field: 'description', order: -1 } }, shortdescription: { Filter: { type: "editor", options: [], name: "Short description", field: 'shortdescription', order: -1 } }, reference: { Filter: { type: "search", options: [], name: "Reference Code", field: 'reference', order: -1 } }, active: { Filter: { type: "select", options: [0,1], name: "Active", field: 'active', order: -1 } } } } init() { let routes = [ { type: 'custom', method: 'get', endpoint: '/select/:name', customFn: () => {} }, { type: 'custom', method: 'get', endpoint: '/search/:search', customFn: (req, res) => { this.search(req, res); } } ]; RoutingSystem.loadRoutes(routes, this.router, '/products'); } search(req, res) { let searchParam = req.params.search; Filter.parseFilters(this.repository.raw(), req.filters).where((builder) => searchParam.split(" ").reduce((res, opt) => { return res.where((b) => b.where('name', 'like', '%' + opt + '%').orWhere('description', 'like', '%' + opt + '%')); }, builder) ) .then((data) => Actions.emit('getProduct', data)) .then((data) => res.send(data)); } } module.exports = ProductsModule;