const express = require('express'); const path = require('path'); const CrudModule = require('../../base/CrudModule'); const RepositorySystem = require('../../systems/RepositorySystem'); const Actions = require('../../systems/ActionSystem'); const RoutingSystem = require('../../systems/RoutingSystem'); const OneToOne = require('../../base/OneToOne'); let ShippingScheme = { fields: [ { key: "name", type: "text" }, { key: "price", type: "decimal" } ] }; let OrderScheme = { fields: [ { key: "shipping_cost", type: "decimal" } ] }; class ShippingModule extends CrudModule { constructor(){ super('/shipping', [3,0,3,4,0], 'Shipping'); this.repository = RepositorySystem.create('Shipping', ShippingScheme); this.ordersRepo = RepositorySystem.create('Orders', OrderScheme); this.setRepo(this.repository); new OneToOne('Shipping', 'Orders'); // this.users = RepositorySystem.create('Users'); // You can get any repo you want!! this.router = express.Router(); this.init(); } init() { RoutingSystem.loadRoutes([], this.router, '/shipping'); Actions.on('verifyOrder', (order) => { if(!order['Shipping_id']) throw new Error("Shipping ID Must be specified"); return this.repository.get('id', order['Shipping_id']) .then((ships) => { if(!ships.length) throw new Error("Shipping id doesnt exist!"); let ship = ships[0]; order.shipping_cost = ship.price; return order; }); }); } } module.exports = ShippingModule;