const express = require('express'); const path = require('path'); const CrudModule = require('klapi/base/CrudModule'); const OneToOne = require('klapi/base/OneToOne'); const RepositorySystem = require('klapi/systems/RepositorySystem'); const RoutingSystem = require('klapi/systems/RoutingSystem'); const Actions = require('klapi/systems/ActionSystem'); var db = require("../../db.js"); //const FilterService = require(;'') var knex = require('knex')(db); let CategoriesScheme = { fields: [ { key: "name", type: "text" }, { key: "price", type: "decimal" }, { key: "SKU", type: "text" }, { key: "SKUName", type: "string", keyLength: 50, unique: true } ] }; class ProductsModule extends CrudModule { constructor(){ super('/products', [1,0,1,1,0], 'Products', ''); this.repository = RepositorySystem.create('Products', CategoriesScheme); this.setRepo(this.repository); this.skus = new OneToOne('SKU', 'Products'); this.init(); } init(){ let routes = [ { type:"custom", method:"get", endpoint:"/prods", customFn: (req,res) => { // console.log("Req came") let category = 'Κουρτίνες'; this.selectSKU(category) .then((products) => { res.send(products) }) .catch((e) => { console.log("errio form here") res.send(412); }) } } ]; RoutingSystem.loadRoutes(routes, this.router); } /*selectSKU(category){ //select all products --> from SKUs // for each product==SKU i ll select all prodcuts/buyes base on SKU //return knex.select().from('SKU').where({category:"Κουρτίνες"}).limit(10); return this.repository.knex().select().from('sku') .then((sku) => { return Promise.all(sku.map((product) => { return knex("products").select('price').count('id as Shops').where({sku:product.sku}).groupBy('price').orderBy('Shops','desc')// .then((suggested_item) => { let price = suggested_item[0].price; let paravates = suggested_item.filter((item) => item.price < price ) let number_of_paraviaseis = paravates.reduce((accumulator,item) => accumulator+item.Shops,0) product['suggested_price'] = price; product['paraviaseis'] = number_of_paraviaseis return product; }) })) }) //return knex('products').where(''); }*/ } module.exports = ProductsModule;