1+ import { VetResult } from "@/app/types/vet-result" ;
12
2- import { VetResult } from "@/app/types/vet-result" ;
3+ export async function verify (
4+ { firstName, lastName, licenseNumber } :
5+ { firstName : string , lastName : string , licenseNumber : string }
6+ ) : Promise < VetResult [ ] > {
37
4- export async function verify ( {
5- firstName,
6- lastName,
7- licenseNumber,
8- } : {
9- firstName : string ;
10- lastName : string ;
11- licenseNumber : string ;
12- } ) : Promise < VetResult [ ] > {
138 const queryParams = new URLSearchParams ( {
14- license : licenseNumber || "" ,
15- firstName : firstName || "" ,
16- lastName : lastName || "" ,
17- business : "" ,
18- city : "" ,
19- zipcode : "null" ,
9+ FirstName : firstName ,
10+ LastName : lastName ,
11+ LicenseNumber : licenseNumber
2012 } ) ;
2113
22-
23-
24- const html = await fetchHTML ( queryParams ) ;
25- const results = parseHTML ( html ) ;
26- return results ;
27-
28- // 🔹 Internal fetcher
29- async function fetchHTML ( params : URLSearchParams ) : Promise < string > {
30- const baseUrl = process . env . VERCEL_URL
31- ? `https://${ process . env . VERCEL_URL } `
32- : "http://localhost:3000" ;
33- const res = await fetch ( `${ baseUrl } /api/verify/alabama?${ params . toString ( ) } ` ) ;
34- if ( ! res . ok ) throw new Error ( "Failed to fetch" ) ;
35- return await res . text ( ) ;
36- }
37-
38- // 🔹 Internal parser
39- function parseHTML ( html : string ) : VetResult [ ] {
40- const parser = new DOMParser ( ) ;
41- const doc = parser . parseFromString ( html , "text/html" ) ;
42- const rows = Array . from ( doc . querySelectorAll ( "#myDataTable tbody tr" ) ) ;
43-
44- return rows
45- . filter ( ( row ) => {
46- const name = row . querySelector ( "td" ) ?. textContent ?. trim ( ) || "" ;
47- return name . includes ( " - DVM" ) ;
48- } )
49- . map ( ( row ) => {
50- const cells = row . querySelectorAll ( "td" ) ;
51- return {
52- name : cells [ 0 ] ?. textContent ?. trim ( ) || "" ,
53- licenseNumber : cells [ 1 ] ?. textContent ?. trim ( ) || "" ,
54- issuedDate : cells [ 2 ] ?. textContent ?. trim ( ) || "" ,
55- expirationDate : cells [ 3 ] ?. textContent ?. trim ( ) || "" ,
56- detailsUrl : cells [ 4 ] ?. querySelector ( "a" ) ?. getAttribute ( "href" ) || "" ,
57- reportUrl : cells [ 4 ] ?. querySelectorAll ( "a" ) [ 1 ] ?. getAttribute ( "href" ) || "" ,
58- status : "active" ,
59- expiration : cells [ 3 ] ?. textContent ?. trim ( ) || "" ,
60- } as VetResult ;
61- } ) ;
14+ try {
15+ const res = await fetch ( `https://supreme-guacamole-6wjw97ggrr535xgp-3000.app.github.dev/api/verify/alabama?${ queryParams . toString ( ) } ` ) ;
16+ if ( ! res . ok ) {
17+ throw new Error ( `Network response was not ok: ${ res . status } ` ) ;
18+ }
19+
20+ const html = await res . text ( ) ;
21+
22+ // Parse HTML (Alabama-specific for now)
23+ const rowRegex = / < t r [ ^ > ] * > ( .* ?) < \/ t r > / gs;
24+ const cellRegex = / < t d [ ^ > ] * > ( .* ?) < \/ t d > / gs;
25+
26+ const results : VetResult [ ] = [ ] ;
27+ const rows = html . match ( rowRegex ) || [ ] ;
28+
29+ for ( const row of rows ) {
30+ const cells = [ ...row . matchAll ( cellRegex ) ] . map ( match => match [ 1 ] . trim ( ) ) ;
31+
32+ // Only process rows with at least 4 cells
33+ if ( cells . length >= 4 ) {
34+ const [ name , license , , expiration ] = cells ;
35+
36+ // Filter: must have name, license, and expiration, and name must end with "DVM"
37+ if (
38+ name &&
39+ license &&
40+ expiration &&
41+ / \b D V M \b $ / i. test ( name )
42+ ) {
43+ results . push ( {
44+ name : name . replace ( / \s * - \s * D V M $ / i, "" ) , // Remove " - DVM" suffix for display
45+ licenseNumber : license ,
46+ expiration,
47+ status : "Active" , // Assuming status is always "Active" for this example
48+ } ) ;
49+ }
50+ }
51+ }
52+
53+ return results ;
54+
55+ } catch ( error ) {
56+ console . error ( "Error fetching Alabama vet data:" , error ) ;
57+ return [ ] ;
6258 }
63-
64-
65- // // 💾 Save to JSON locally
66- // const outputPath = path.join(process.cwd(), "data", "alabamaVets.json");
67- // fs.writeFileSync(outputPath, JSON.stringify(results, null, 2));
68- // console.log(`✅ Saved ${results.length} Alabama vet records to ${outputPath}`);
69-
70-
71- return results ;
72- }
59+ }
0 commit comments