index.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. const express = require('express');
  2. const path = require('path');
  3. const CrudModule = require('klapi/base/CrudModule');
  4. const OneToOne = require('klapi/base/OneToOne');
  5. const RepositorySystem = require('klapi/systems/RepositorySystem');
  6. const RoutingSystem = require('klapi/systems/RoutingSystem');
  7. const Actions = require('klapi/systems/ActionSystem');
  8. var db = require("../../db.js");
  9. //const FilterService = require(;'')
  10. var knex = require('knex')(db);
  11. let CategoriesScheme = {
  12. fields: [
  13. {
  14. key: "name",
  15. type: "text"
  16. },
  17. {
  18. key: "price",
  19. type: "decimal"
  20. },
  21. {
  22. key: "SKU",
  23. type: "text"
  24. },
  25. {
  26. key: "SKUName",
  27. type: "string",
  28. keyLength: 50,
  29. unique: true
  30. }
  31. ]
  32. };
  33. class ProductsModule extends CrudModule {
  34. constructor(){
  35. super('/products', [1,0,1,1,0], 'Products', '');
  36. this.repository = RepositorySystem.create('Products', CategoriesScheme);
  37. this.setRepo(this.repository);
  38. this.skus = new OneToOne('SKU', 'Products');
  39. this.init();
  40. }
  41. init(){
  42. let routes = [
  43. {
  44. type:"custom",
  45. method:"get",
  46. endpoint:"/prods",
  47. customFn: (req,res) => {
  48. // console.log("Req came")
  49. let category = 'Κουρτίνες';
  50. this.selectSKU(category)
  51. .then((products) => {
  52. res.send(products)
  53. })
  54. .catch((e) => {
  55. console.log("errio form here")
  56. res.send(412);
  57. })
  58. }
  59. }
  60. ];
  61. RoutingSystem.loadRoutes(routes, this.router);
  62. }
  63. /*selectSKU(category){
  64. //select all products --> from SKUs
  65. // for each product==SKU i ll select all prodcuts/buyes base on SKU
  66. //return knex.select().from('SKU').where({category:"Κουρτίνες"}).limit(10);
  67. return this.repository.knex().select().from('sku')
  68. .then((sku) => {
  69. return Promise.all(sku.map((product) => {
  70. return knex("products").select('price').count('id as Shops').where({sku:product.sku}).groupBy('price').orderBy('Shops','desc')//
  71. .then((suggested_item) => {
  72. let price = suggested_item[0].price;
  73. let paravates = suggested_item.filter((item) => item.price < price )
  74. let number_of_paraviaseis = paravates.reduce((accumulator,item) => accumulator+item.Shops,0)
  75. product['suggested_price'] = price;
  76. product['paraviaseis'] = number_of_paraviaseis
  77. return product;
  78. })
  79. }))
  80. })
  81. //return knex('products').where('');
  82. }*/
  83. }
  84. module.exports = ProductsModule;