import { updateBid } from '../../system/apis/bid.js'; import configs from '../../system/config.js'; import { extractPriceNumber, removeFalsyValues } from '../../system/utils.js'; import { ProductBid } from '../product-bid.js'; export class LawsonsProductBid extends ProductBid { constructor({ ...prev }) { super(prev); } async handleUpdateBid({ lot_id, close_time, name, current_price, reserve_price }) { const response = await updateBid(this.id, { lot_id, close_time, name, current_price, reserve_price: Number(reserve_price) || 0 }); if (response) { this.lot_id = response.lot_id; this.close_time = response.close_time; this.start_bid_time = response.start_bid_time; } } async getReversePrice() { try { // Kiểm tra xem có context của trang web không, nếu không thì trả về null if (!this.page_context) return null; await this.page_context.waitForSelector('.select-dropdown-value.text-truncate', { timeout: 2000 }); const price = await this.page_context.evaluate(() => { const el = document.querySelector('.select-dropdown-value.text-truncate'); return el ? el.innerText : null; }); return price ? extractPriceNumber(price) : null; } catch (error) { console.log(error); // Nếu có lỗi xảy ra trong quá trình lấy thời gian, trả về null return null; } } update = async () => { try { if (!this.page_context) return; const result = await this.waitApiInfo(); const reservePrice = await this.getReversePrice(); console.log({ reservePrice }); if (!result) return; // 📌 Loại bỏ các giá trị không hợp lệ và bổ sung thông tin cần thiết const data = removeFalsyValues( { lot_id: String(result?.itemView.lotId) || null, reserve_price: reservePrice, current_price: result?.currentBidAmount || null, close_time: new Date(result.endTime).toUTCString() || null, name: result?.itemView?.title || null, }, ['close_time'], ); console.log(`🚀 [${this.id}] Processed data ready for update`); // 📌 Gửi dữ liệu cập nhật lên hệ thống await this.handleUpdateBid(data); } catch (error) { console.log('Error Update', error.message); } }; // Hàm con để fetch trong context trình duyệt fetchFromPage = async (url) => { return await this.page_context.evaluate(async (url) => { try { const res = await fetch(url, { method: 'GET', headers: { 'Content-Type': 'application/json' }, }); return await res.json(); } catch (err) { return { error: err.message }; } }, url); }; async waitApiInfo() { if (!this.page_context) { console.error(`❌ [${this.id}] Error: page_context is undefined.`); return null; } const infoUrl = configs.WEB_CONFIGS.LAWSONS.API_DETAIL_INFO(this.model); const detailUrl = configs.WEB_CONFIGS.LAWSONS.API_DETAIL_PRODUCT(this.model); const [info, detailData] = await Promise.all([this.fetchFromPage(infoUrl), this.fetchFromPage(detailUrl)]); return { ...info, ...detailData }; } action = async () => { try { const page = this.page_context; // 📌 Kiểm tra nếu trang chưa tải đúng URL thì điều hướng đến URL mục tiêu if (!page.url() || !page.url().includes(this.url)) { console.log(`🔄 [${this.id}] Navigating to target URL: ${this.url}`); await this.gotoLink(); } } catch (error) { console.error(`🚨 [${this.id}] Error navigating the page: ${error}`); } }; }