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 OneToOne = require('../../base/OneToOne'); let TaxScheme = { fields: [ { key: "name", type: "text" }, { key: "percentage", type: "decimal" } ] }; let ProductScheme = { fields: [ { key: "untaxed_price", type: "decimal" } ] }; class TaxModule extends CrudModule { constructor(){ super('/tax', [3,0,3,4,0], 'Tax'); this.repository = RepositorySystem.create('Tax', TaxScheme); this.products = RepositorySystem.create('Tax', ProductScheme); this.setRepo(this.repository); //new OneToOne('Tax', 'Products'); // this.users = RepositorySystem.create('Users'); // You can get any repo you want!! this.router = express.Router(); this.init(); } // Add logic to change price on get product! init() { // Actions.on('modelProducts', (model) => { // return this.repository.getPage(0,1000) // .then((taxes) => { // return { // ...model, // "tax_id": { // disabled: true // }, // "tax": { // Filter: { // type: "exact", // options: taxes.reduce((data,a) => {return {...data, [a.id]: a.name }}, {}), // name: "Tax", // field: 'tax_id', // order: 60 // } // } // } // }) // }); Actions.on('postProducts', (prod) => { if(!prod["Tax_id"]) return prod; if(!prod["price"]) return prod; return this.repository.get('id', prod['Tax_id']) .then((taxes) => { if (!taxes.length) return prod; let tax = taxes[0]; prod.untaxed_price = prod.price; prod.price = prod.untaxed_price * tax.percentage; return ; }) }); RoutingSystem.loadRoutes([], this.router, '/Tax'); } decorate(product) { return this.repository.get('id', product.tax_id) .then((taxes) => { if(taxes.length) { product.tax = taxes[0] ? taxes[0].name : "-"; } return product; }); } } module.exports = TaxModule;