index.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 OneToOne = require('../../base/OneToOne');
  8. let TaxScheme = {
  9. fields: [
  10. {
  11. key: "name",
  12. type: "text"
  13. },
  14. {
  15. key: "percentage",
  16. type: "decimal"
  17. }
  18. ]
  19. };
  20. let ProductScheme = {
  21. fields: [
  22. {
  23. key: "untaxed_price",
  24. type: "decimal"
  25. }
  26. ]
  27. };
  28. class TaxModule extends CrudModule {
  29. constructor(){
  30. super('/tax', [3,0,3,4,0], 'Tax');
  31. this.repository = RepositorySystem.create('Tax', TaxScheme);
  32. this.products = RepositorySystem.create('Tax', ProductScheme);
  33. this.setRepo(this.repository);
  34. //new OneToOne('Tax', 'Products');
  35. // this.users = RepositorySystem.create('Users'); // You can get any repo you want!!
  36. this.router = express.Router();
  37. this.init();
  38. }
  39. // Add logic to change price on get product!
  40. init() {
  41. // Actions.on('modelProducts', (model) => {
  42. // return this.repository.getPage(0,1000)
  43. // .then((taxes) => {
  44. // return {
  45. // ...model,
  46. // "tax_id": {
  47. // disabled: true
  48. // },
  49. // "tax": {
  50. // Filter: {
  51. // type: "exact",
  52. // options: taxes.reduce((data,a) => {return {...data, [a.id]: a.name }}, {}),
  53. // name: "Tax",
  54. // field: 'tax_id',
  55. // order: 60
  56. // }
  57. // }
  58. // }
  59. // })
  60. // });
  61. Actions.on('postProducts', (prod) => {
  62. if(!prod["Tax_id"]) return prod;
  63. if(!prod["price"]) return prod;
  64. return this.repository.get('id', prod['Tax_id'])
  65. .then((taxes) => {
  66. if (!taxes.length)
  67. return prod;
  68. let tax = taxes[0];
  69. prod.untaxed_price = prod.price;
  70. prod.price = prod.untaxed_price * tax.percentage;
  71. return ;
  72. })
  73. });
  74. RoutingSystem.loadRoutes([], this.router, '/Tax');
  75. }
  76. decorate(product) {
  77. return this.repository.get('id', product.tax_id)
  78. .then((taxes) => {
  79. if(taxes.length) {
  80. product.tax = taxes[0] ? taxes[0].name : "-";
  81. }
  82. return product;
  83. });
  84. }
  85. }
  86. module.exports = TaxModule;