index.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. const express = require('express');
  2. const path = require('path');
  3. const CrudModule = require('../../base/CrudModule');
  4. const RepositorySystem = require('../../systems/RepositorySystem');
  5. const Actions = require('../../systems/ActionSystem');
  6. const RoutingSystem = require('../../systems/RoutingSystem');
  7. const OneToOne = require('../../base/OneToOne');
  8. let ShippingScheme = {
  9. fields: [
  10. {
  11. key: "name",
  12. type: "text"
  13. },
  14. {
  15. key: "price",
  16. type: "decimal"
  17. }
  18. ]
  19. };
  20. let OrderScheme = {
  21. fields: [
  22. {
  23. key: "shipping_cost",
  24. type: "decimal"
  25. }
  26. ]
  27. };
  28. class ShippingModule extends CrudModule {
  29. constructor(){
  30. super('/shipping', [3,0,3,4,0], 'Shipping');
  31. this.repository = RepositorySystem.create('Shipping', ShippingScheme);
  32. this.ordersRepo = RepositorySystem.create('Orders', OrderScheme);
  33. this.setRepo(this.repository);
  34. new OneToOne('Shipping', 'Orders');
  35. // this.users = RepositorySystem.create('Users'); // You can get any repo you want!!
  36. this.router = express.Router();
  37. this.init();
  38. }
  39. init() {
  40. RoutingSystem.loadRoutes([], this.router, '/shipping');
  41. Actions.on('verifyOrder', (order) => {
  42. if(!order['Shipping_id'])
  43. throw new Error("Shipping ID Must be specified");
  44. return this.repository.get('id', order['Shipping_id'])
  45. .then((ships) => {
  46. if(!ships.length)
  47. throw new Error("Shipping id doesnt exist!");
  48. let ship = ships[0];
  49. order.shipping_cost = ship.price;
  50. return order;
  51. });
  52. });
  53. }
  54. }
  55. module.exports = ShippingModule;