const fetch = require('node-fetch'); const { LotDb } = require("./lotDb"); const lotDb = new LotDb(); const { SaleDb } = require("./saleDb"); const saleDb = new SaleDb(); const moment = require('moment-timezone'); const Agent = class { constructor() { this.ApiAgentURL = "http://host.docker.internal:3020/api"; } async getSaleInfos(url) { return new Promise((resolve, reject) => { url = encodeURIComponent(url); fetch(this.ApiAgentURL+'/sale/getSaleInfos/'+url) .then(response => response.json()) .then(data => { resolve(data); }) .catch(error => { reject(error); }); }); } async prepareSale(id) { return new Promise(async (resolve, reject) => { let Sale = await saleDb.get(id); const DateSale = moment.tz(Sale.date, "Europe/Paris"); const NowParis = moment.tz(new Date(),"Europe/Paris") if (NowParis.isBefore(DateSale)){ let url = Sale.url url = encodeURIComponent(url); fetch(this.ApiAgentURL+'/sale/getLotList/'+url) .then(response => response.json()) .then( async data => { for (let lot of data) { lot.sale_id = Sale._id await lotDb.post(lot); } resolve(data); }) .catch(error => { reject(error); }); }else{ console.log("Sale started or finished"); resolve([]); } }); } async followSale(id) { return new Promise(async (resolve, reject) => { let Sale = await saleDb.get(id); let url = Sale.url url = encodeURIComponent(url); fetch(this.ApiAgentURL+'/sale/followSale/'+url) .then(response => response.json()) .then(async data => { // set the Sale status to following Sale.status = "following"; Sale = await saleDb.put(id, Sale); resolve(data); }) .catch(error => { reject(error); }); }); } } module.exports = {Agent};