index.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. const express = require('express');
  2. const path = require('path');
  3. const OneToOne = require('../../base/OneToOne');
  4. const OneToMany = require('../../base/OneToMany');
  5. const CrudModule = require('../../base/CrudModule');
  6. const RepositorySystem = require('../../systems/RepositorySystem');
  7. const RoutingSystem = require('../../systems/RoutingSystem');
  8. const Actions = require('../../systems/ActionSystem');
  9. // const OrderHookup = require('./OrderHookup');
  10. const AddressMiddleware = require('./AddressesFilter');
  11. let AddressesScheme = {
  12. fields: [
  13. {
  14. key: "name",
  15. type: "text"
  16. },
  17. {
  18. key: "phone",
  19. type: "text"
  20. },
  21. {
  22. key: "address",
  23. type: "text"
  24. },
  25. {
  26. key: "address2",
  27. type: "text"
  28. },
  29. {
  30. key: "postalcode",
  31. type: "text"
  32. },
  33. {
  34. key: "city",
  35. type: "text"
  36. },
  37. {
  38. key: "state",
  39. type: "text"
  40. },
  41. {
  42. key: "country",
  43. type: "text"
  44. },
  45. {
  46. key: "comments",
  47. type: "text"
  48. }
  49. ]
  50. };
  51. let OrdersScheme = {
  52. fields: [
  53. {
  54. key: "Address",
  55. type: "text"
  56. }
  57. ]
  58. };
  59. class AddressesModule extends CrudModule { // this.repository ready!
  60. constructor(){
  61. super('/addresses', [1,1,1,1,1], 'Addresses', '', AddressMiddleware);
  62. this.repository = RepositorySystem.create('Addresses', AddressesScheme);
  63. this.orders = RepositorySystem.create('Orders', OrdersScheme);
  64. this.setRepo(this.repository);
  65. new OneToMany('Addresses', 'Users')
  66. new OneToOne('Addresses', 'Orders');
  67. Actions.on("verifyOrder" , this.verify.bind(this));
  68. }
  69. verify(order) {
  70. if (!order["Addresses_id"])
  71. throw "No address id";
  72. let id = order["Addresses_id"];
  73. return this.repository.get('id', id)
  74. .then((res) => {
  75. if(res.length) {
  76. order.address = JSON.stringify(res[0]);
  77. return order;
  78. }
  79. throw 'No such address with id: ' + id;
  80. });
  81. }
  82. }
  83. module.exports = AddressesModule;