const express = require('express'); const path = require('path'); const OneToOne = require('../../base/OneToOne'); const OneToMany = require('../../base/OneToMany'); const CrudModule = require('../../base/CrudModule'); const RepositorySystem = require('../../systems/RepositorySystem'); const RoutingSystem = require('../../systems/RoutingSystem'); const Actions = require('../../systems/ActionSystem'); // const OrderHookup = require('./OrderHookup'); const AddressMiddleware = require('./AddressesFilter'); let AddressesScheme = { fields: [ { key: "name", type: "text" }, { key: "phone", type: "text" }, { key: "address", type: "text" }, { key: "address2", type: "text" }, { key: "postalcode", type: "text" }, { key: "city", type: "text" }, { key: "state", type: "text" }, { key: "country", type: "text" }, { key: "comments", type: "text" } ] }; let OrdersScheme = { fields: [ { key: "Address", type: "text" } ] }; class AddressesModule extends CrudModule { // this.repository ready! constructor(){ super('/addresses', [1,1,1,1,1], 'Addresses', '', AddressMiddleware); this.repository = RepositorySystem.create('Addresses', AddressesScheme); this.orders = RepositorySystem.create('Orders', OrdersScheme); this.setRepo(this.repository); new OneToMany('Addresses', 'Users') new OneToOne('Addresses', 'Orders'); Actions.on("verifyOrder" , this.verify.bind(this)); } verify(order) { if (!order["Addresses_id"]) throw "No address id"; let id = order["Addresses_id"]; return this.repository.get('id', id) .then((res) => { if(res.length) { order.address = JSON.stringify(res[0]); return order; } throw 'No such address with id: ' + id; }); } } module.exports = AddressesModule;