Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 KB | None | 0 0
  1. j_total = 0
  2. const promoprueba = {
  3. id: 1,
  4. empresa_id: 2,
  5. titulo: 'Titulo de Prueba',
  6. texto: 'Texto de prueba',
  7. version: 'Version de prueba',
  8. validez: 'Validez',
  9. imgpromo: 'UnaURL',
  10. created_at: '2020-01-01',
  11. update_at: '2020-01-01',
  12. normal_price: 5000,
  13. special_price: 2000,
  14. address: 'Direccion de prueba',
  15. }
  16. const promoprueba2 = {
  17. id: 3,
  18. empresa_id: 1,
  19. titulo: 'Titulo de Prueba',
  20. texto: 'Texto de prueba',
  21. version: 'Version de prueba',
  22. validez: 'Validez',
  23. imgpromo: 'UnaURL',
  24. created_at: '2020-01-01',
  25. update_at: '2020-01-01',
  26. normal_price: 5000,
  27. special_price: 2000,
  28. address: 'Direccion de prueba',
  29. }
  30.  
  31. function Promo(id,empresa_id,titulo,texto,version,validez,imgpromo,created_at,updated_at,normal_price,special_price,logo,address) {
  32. this.id = id
  33. this.empresa_id = empresa_id
  34. this.titulo = titulo
  35. this.texto = texto
  36. this.version = version
  37. this.validez = validez
  38. this.imgpromo = imgpromo
  39. this.created_at = created_at
  40. this.updated_at = updated_at
  41. this.normal_price = normal_price
  42. this.special_price = special_price
  43. this.logo = logo
  44. this.address = address
  45. }
  46. // class
  47. class Empresa {
  48. constructor(promo, empresa_id) {
  49. this.turn = 0
  50. this.empresa_id = empresa_id
  51. this.promos = new Array()
  52. this.insertPromoInEmpresa(promo)
  53. }
  54. // Hay que determinar numero de existentes y a quien le toca
  55. takePromo() {
  56. if (this.turn < this.promos.length) {
  57. this.turn++
  58. } else {
  59. this.turn = 0
  60. }
  61. return this.promos[this.turn]
  62. }
  63. insertPromoInEmpresa(promo) {
  64.  
  65. this.promos.push(promo)
  66. }
  67. // Clean lapsed and removed
  68. updateAndCleanPromos() {
  69. var time_now = Date.now()
  70. for (let i = 0; i < this.promos.length; i++) {
  71. if (
  72. Date.parse(this.promos[i].validez) < time_now ||
  73. this.promos[i].version < 0
  74. ) {
  75. if (this.promos.length - 1 == turno) {
  76. //adapt turn
  77. if (turno > 0) {
  78. turno--
  79. }
  80. }
  81. this.promos.splice(i, 1) //remove
  82. checkEmpresaShouldExist()
  83. }
  84. }
  85. }
  86. checkEmpresaShouldExist() {
  87. if (this.promos.length == 0) {
  88. console.log("AQUI")
  89. delete this
  90. }
  91. }
  92. }
  93.  
  94. class Marketplace {
  95. constructor(){
  96. this.empresas=[];
  97. }
  98.  
  99. cleanUp() {
  100. let e = this.empresas;
  101. console.log(e)
  102. this.empresas.forEach(function (empresa,index) {
  103. e[index]=undefined
  104.  
  105. })
  106.  
  107.  
  108. }
  109. insertPromoInMarketplace (promo) {
  110. let empresa_id = promo.empresa_id
  111. if (this.empresas[j_total] instanceof Empresa) {
  112. this.empresas[j_total].insertPromoInEmpresa(promo)
  113. } else {
  114. this.empresas[j_total] = new Empresa(promo,empresa_id)
  115. }
  116. }
  117. }
  118. const m = new Marketplace()
  119.  
  120. m.insertPromoInMarketplace(promoprueba)
  121. m.insertPromoInMarketplace(promoprueba2)
  122. console.log(m)
  123. //m.empresas[0].promos = []
  124. m.cleanUp()
  125. console.log(m)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement