index.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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: "sku",
  15. type: "string",
  16. unique: true,
  17. keyLength: 50
  18. },
  19. {
  20. key: "link",
  21. type: "text"
  22. },
  23. {
  24. key: "price",
  25. type: "decimal"
  26. },
  27. {
  28. key: "title",
  29. type: "text"
  30. },
  31. {
  32. key: "category",
  33. type: "text"
  34. }
  35. ]
  36. };
  37. class SKUModule extends CrudModule {
  38. constructor(){
  39. super('/skus', [1,0,1,1,0], 'SKU', '', );
  40. this.repository = RepositorySystem.create('SKU', CategoriesScheme);
  41. this.setRepo(this.repository);
  42. this.categories = new OneToOne('Categories', 'SKU');
  43. this.init();
  44. Actions.on('getSKU', (sku) => {
  45. return this.selectSKU(sku);
  46. })
  47. Actions.on('ArraygetSKU',(skus) => {
  48. return this.selectSKU(skus);
  49. })
  50. Actions.on('getProducts',(product) => {
  51. return this.repository.get('sku',product.SKU)
  52. .then((sku) => {
  53. product['title'] = sku[0].title;
  54. return product;
  55. })
  56. })
  57. Actions.on('ArraygetProducts',(product) => {
  58. return this.repository.get('sku',product.SKU)
  59. .then((sku) => {
  60. product['title'] = sku[0].title;
  61. return product;
  62. })
  63. })
  64. }
  65. init(){
  66. let routes = [
  67. {
  68. type:"custom",
  69. method:"get",
  70. endpoint:"/notifications",
  71. customFn: (req,res) => {
  72. this.notifications()
  73. .then((prod) => {
  74. res.send(prod)
  75. })
  76. .catch((e) => {
  77. console.log("errio form here",e)
  78. res.send(e);
  79. })
  80. }
  81. }
  82. ];
  83. RoutingSystem.loadRoutes(routes, this.router);
  84. }
  85. selectSKU(product){
  86. return knex("Products").select('price').count('id as Shops').where({sku:product.sku}).groupBy('price').orderBy('Shops','desc')//
  87. .then((suggested_item) => {
  88. let price = suggested_item[0].price;
  89. let paravates = suggested_item.filter((item) => item.price < price )
  90. let number_of_paraviaseis = paravates.reduce((accumulator,item) => accumulator+item.Shops,0)
  91. product['suggested_price'] = price;
  92. product['paraviaseis'] = number_of_paraviaseis;
  93. return product;
  94. })
  95. }
  96. notifications(){
  97. return knex('SKU').select('*').limit(48)
  98. .then((skus) => Promise.all(skus.map((sku) => {
  99. let suggested_price;
  100. return this.selectSKU(sku)
  101. .then((sku) => {
  102. suggested_price = sku['suggested_price'];
  103. return sku['suggested_price']})
  104. .then((suggested_price) =>{
  105. return knex('Products').select("*").where('price','<',suggested_price).where({sku:sku.sku}) })
  106. .then((paravates) => paravates.map((item) => {
  107. item['actual_price'] = suggested_price;
  108. item['discount'] = (suggested_price - item.price)/suggested_price;
  109. item.link = sku.link;
  110. return item;
  111. }))
  112. .then((products) => Actions.emitArray('getProducts',products))
  113. }))
  114. .then((result) => [].concat.apply([], result))
  115. )
  116. }
  117. }
  118. module.exports = SKUModule;