diff --git a/backend/app/services/ebay_scraper_service.ts b/backend/app/services/ebay_scraper_service.ts index 3addf91..b94c288 100644 --- a/backend/app/services/ebay_scraper_service.ts +++ b/backend/app/services/ebay_scraper_service.ts @@ -67,7 +67,7 @@ export default class EbayScraperService { /** Đóng browser dùng chung (gọi khi command/worker tắt). */ static async close(): Promise { if (this._browser) { - await this._browser.close().catch(() => {}) + await this._browser.close().catch(() => { }) this._browser = null } } @@ -108,7 +108,7 @@ export default class EbayScraperService { page = await browser.newPage() await page.setUserAgent( 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 ' + - '(KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36' + '(KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36' ) await page.setExtraHTTPHeaders({ 'Accept-Language': 'en-US,en;q=0.9' }) // Ẩn dấu hiệu automation để giảm khả năng bị eBay chặn. @@ -127,7 +127,7 @@ export default class EbayScraperService { // QUAN TRỌNG: eBay chặn truy cập trực tiếp vào /sch/i.html (trả Error Page). // Phải vào homepage trước để lấy cookie/session, rồi mới điều hướng tới search. const origin = new URL(url).origin - await page.goto(origin + '/', { waitUntil: 'domcontentloaded', timeout }).catch(() => {}) + await page.goto(origin + '/', { waitUntil: 'domcontentloaded', timeout }).catch(() => { }) await wait(1500) await page.goto(url, { waitUntil: 'networkidle2', timeout }) @@ -142,9 +142,9 @@ export default class EbayScraperService { html.includes('Something went wrong on our end') if (blocked) { // Bị chặn -> quay lại homepage rồi vào lại search. - await page.goto(new URL(url).origin + '/', { waitUntil: 'domcontentloaded', timeout }).catch(() => {}) + await page.goto(new URL(url).origin + '/', { waitUntil: 'domcontentloaded', timeout }).catch(() => { }) await wait(1500) - await page.goto(url, { waitUntil: 'networkidle2', timeout }).catch(() => {}) + await page.goto(url, { waitUntil: 'networkidle2', timeout }).catch(() => { }) retries++ continue } @@ -190,6 +190,7 @@ export default class EbayScraperService { const idMatch = it.link_detail.match(/\/itm\/(\d+)/) const id = idMatch ? idMatch[1] : it.listingId if (!id) return null + if (!it.title?.includes(sku)) return null const { amount, currency } = this.parsePrice(it.priceText) if (!Number.isFinite(amount) || amount <= 0) return null @@ -226,7 +227,7 @@ export default class EbayScraperService { logger.error({ err, url }, `eBay scrape lỗi cho SKU ${sku}`) return [] } finally { - if (page) await page.close().catch(() => {}) + if (page) await page.close().catch(() => { }) } } diff --git a/backend/app/services/ebay_service.ts b/backend/app/services/ebay_service.ts index c8a14c1..bd150d3 100644 --- a/backend/app/services/ebay_service.ts +++ b/backend/app/services/ebay_service.ts @@ -46,10 +46,10 @@ export default class EbayService { const [sale, sold] = await Promise.all([ hasApiCreds ? this.tryFetch('sale', () => - this.getAccessToken(clientId!, clientSecret!, baseUrl, SCOPE_BASE).then((token) => - this.searchActiveListings(token, baseUrl, sku, conditionId, marketplace) - ) + this.getAccessToken(clientId!, clientSecret!, baseUrl, SCOPE_BASE).then((token) => + this.searchActiveListings(token, baseUrl, sku, conditionId, marketplace) ) + ) : Promise.resolve([] as Array>), this.tryFetch('sold', () => this.getSoldListings(clientId, clientSecret, baseUrl, sku, conditionId, marketplace) @@ -144,7 +144,7 @@ export default class EbayService { marketplace: string ) { const response = await fetch( - `${baseUrl.replace(/\/$/, '')}/buy/browse/v1/item_summary/search?q=${encodeURIComponent(sku)}&filter=conditionIds:{${conditionId}}&limit=100`, + `${baseUrl.replace(/\/$/, '')}/buy/browse/v1/item_summary/search?q=${encodeURIComponent(sku)}&filter=conditionIds:{${conditionId}}&limit=50`, { headers: { Authorization: `Bearer ${token}`, diff --git a/backend/tests/services/ai_suggestion.spec.ts b/backend/tests/services/ai_suggestion.spec.ts index 9d80612..72862e3 100644 --- a/backend/tests/services/ai_suggestion.spec.ts +++ b/backend/tests/services/ai_suggestion.spec.ts @@ -20,7 +20,7 @@ const Product = (await import('#models/product')).default const EbayService = (await import('#services/ebay_service')).default async function runSyncServiceTest() { - const product = await Product.query().first() + const product = await Product.query().where('id', 182).first() if (!product) { throw new Error('Không có sản phẩm nào trong database để test AI suggest') }