Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- j_total = 0
- const promoprueba = {
- id: 1,
- empresa_id: 2,
- titulo: 'Titulo de Prueba',
- texto: 'Texto de prueba',
- version: 'Version de prueba',
- validez: 'Validez',
- imgpromo: 'UnaURL',
- created_at: '2020-01-01',
- update_at: '2020-01-01',
- normal_price: 5000,
- special_price: 2000,
- address: 'Direccion de prueba',
- }
- const promoprueba2 = {
- id: 3,
- empresa_id: 1,
- titulo: 'Titulo de Prueba',
- texto: 'Texto de prueba',
- version: 'Version de prueba',
- validez: 'Validez',
- imgpromo: 'UnaURL',
- created_at: '2020-01-01',
- update_at: '2020-01-01',
- normal_price: 5000,
- special_price: 2000,
- address: 'Direccion de prueba',
- }
- function Promo(id,empresa_id,titulo,texto,version,validez,imgpromo,created_at,updated_at,normal_price,special_price,logo,address) {
- this.id = id
- this.empresa_id = empresa_id
- this.titulo = titulo
- this.texto = texto
- this.version = version
- this.validez = validez
- this.imgpromo = imgpromo
- this.created_at = created_at
- this.updated_at = updated_at
- this.normal_price = normal_price
- this.special_price = special_price
- this.logo = logo
- this.address = address
- }
- // class
- class Empresa {
- constructor(promo, empresa_id) {
- this.turn = 0
- this.empresa_id = empresa_id
- this.promos = new Array()
- this.insertPromoInEmpresa(promo)
- }
- // Hay que determinar numero de existentes y a quien le toca
- takePromo() {
- if (this.turn < this.promos.length) {
- this.turn++
- } else {
- this.turn = 0
- }
- return this.promos[this.turn]
- }
- insertPromoInEmpresa(promo) {
- this.promos.push(promo)
- }
- // Clean lapsed and removed
- updateAndCleanPromos() {
- var time_now = Date.now()
- for (let i = 0; i < this.promos.length; i++) {
- if (
- Date.parse(this.promos[i].validez) < time_now ||
- this.promos[i].version < 0
- ) {
- if (this.promos.length - 1 == turno) {
- //adapt turn
- if (turno > 0) {
- turno--
- }
- }
- this.promos.splice(i, 1) //remove
- checkEmpresaShouldExist()
- }
- }
- }
- checkEmpresaShouldExist() {
- if (this.promos.length == 0) {
- console.log("AQUI")
- delete this
- }
- }
- }
- class Marketplace {
- constructor(){
- this.empresas=[];
- }
- cleanUp() {
- let e = this.empresas;
- console.log(e)
- this.empresas.forEach(function (empresa,index) {
- e[index]=undefined
- })
- }
- insertPromoInMarketplace (promo) {
- let empresa_id = promo.empresa_id
- if (this.empresas[j_total] instanceof Empresa) {
- this.empresas[j_total].insertPromoInEmpresa(promo)
- } else {
- this.empresas[j_total] = new Empresa(promo,empresa_id)
- }
- }
- }
- const m = new Marketplace()
- m.insertPromoInMarketplace(promoprueba)
- m.insertPromoInMarketplace(promoprueba2)
- console.log(m)
- //m.empresas[0].promos = []
- m.cleanUp()
- console.log(m)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement