const express = require('express'); const path = require('path'); const CrudModule = require('../../base/CrudModule'); const RepositorySystem = require('../../systems/RepositorySystem'); const RoutingSystem = require('../../systems/RoutingSystem'); const Actions = require('../../systems/ActionSystem'); let GlobalsScheme = { fields: [ { key: "name", type: "text" }, { key: "en", type: "text" }, { key: "gr", type: "text" } ] }; class GlobalModule extends CrudModule { constructor(){ super('/globals', [3,0,3,4,0], 'Globals', '', GlobalModule.Middleware); this.repository = RepositorySystem.create('Globals', GlobalsScheme); this.setRepo(this.repository); this.router = express.Router(); this.init(); } init() { let routes = [ { type: 'custom', method: 'get', endpoint: '/dictionary', customFn: this.getDictionary.bind(this) } ]; RoutingSystem.loadRoutes(routes, this.router, '/', GlobalModule.Middleware); Actions.on('ArraygetGlobals', this.parse.bind(this)); Actions.on('getGlobals', this.parse.bind(this)); Actions.on('postGlobals', this.prepare.bind(this)); } getDictionary(req,res) { console.log("ASD"); this.repository.getPage(1, 10000) .then((data) => { return data.reduce((initial, item) => { let value = item[req.language]; initial[item.name] = value; return initial; }, {}); }) .then((data) => res.send(data)); } prepare(glob, req, res) { glob[req.language] = glob.value; delete glob.value; return glob; } parse(glob, req, res) { let value = glob[req.language]; return { name: glob.name, value } } } GlobalModule.Middleware = (req, res, next) => { let lang = req.headers.language || 'gr'; req.language = lang; next(); }; module.exports = GlobalModule;