@@ -94,24 +94,18 @@ export async function crawl(options: CrawlOptions = {}): Promise<void> {
9494 await repo . clearCrawlStatus ( refRecord . id ) ;
9595 }
9696
97- // Phase 1: Crawl brands (if not already crawled)
98- const brandsCrawled = await repo . isBrandsCrawled ( refRecord . id ) ;
99- if ( ! brandsCrawled ) {
100- log ( ' Phase 1: Crawling brands...' ) ;
101- const apiBrands = await fipeClient . getBrands ( ref . Codigo ) ;
102- const filteredBrands = options . brandCode
103- ? apiBrands . filter ( ( b ) => b . Value === options . brandCode )
104- : apiBrands ;
105-
106- for ( const b of filteredBrands ) {
107- const brand = await repo . upsertBrand ( b . Value , b . Label ) ;
108- await repo . upsertReferenceBrand ( refRecord . id , brand . id ) ;
109- }
110- await repo . markBrandsCrawled ( refRecord . id ) ;
111- log ( ` Crawled ${ filteredBrands . length } brands` ) ;
112- } else {
113- log ( ' Phase 1: Brands already crawled' ) ;
97+ // Phase 1: Crawl brands (always fetch - cheap API call)
98+ log ( ' Phase 1: Crawling brands...' ) ;
99+ const apiBrands = await fipeClient . getBrands ( ref . Codigo ) ;
100+ const filteredBrands = options . brandCode
101+ ? apiBrands . filter ( ( b ) => b . Value === options . brandCode )
102+ : apiBrands ;
103+
104+ for ( const b of filteredBrands ) {
105+ const brand = await repo . upsertBrand ( b . Value , b . Label ) ;
106+ await repo . upsertReferenceBrand ( refRecord . id , brand . id ) ;
114107 }
108+ log ( ` Crawled ${ filteredBrands . length } brands` ) ;
115109
116110 // Phase 2: Crawl models for each uncrawled brand
117111 const uncrawledBrands = await repo . getUncrawledReferenceBrands ( refRecord . id ) ;
@@ -143,7 +137,10 @@ export async function crawl(options: CrawlOptions = {}): Promise<void> {
143137 }
144138 }
145139
146- await repo . markReferenceBrandModelsCrawled ( brand . id ) ;
140+ // Only mark as crawled if we fetched ALL models (no filter)
141+ if ( ! options . modelCodes ) {
142+ await repo . markReferenceBrandModelsCrawled ( brand . id ) ;
143+ }
147144 } catch {
148145 // Models fetch failed - leave uncrawled for retry
149146 log ( ` Error crawling models for ${ brand . name } ` ) ;
0 commit comments