import sql from "../db/db"; import { Job } from "../types/job"; export const saveJob = async (job: Job): Promise => { try { const result = await sql` INSERT INTO public.jobs ( title, company, location, company_link, job_link, provider, status, job_timestamp ) VALUES ( ${job.title}, ${job.company}, ${job.location}, ${job.companyLink}, ${job.jobLink}, ${job.provider}, ${job.status}, ${job.jobPostTime} ) ON CONFLICT (job_link) DO UPDATE SET title = EXCLUDED.title, company = EXCLUDED.company, company_link = EXCLUDED.company_link, location = EXCLUDED.location, job_timestamp = EXCLUDED.job_timestamp RETURNING id `; const id = result[0]?.id ?? null; console.log(`Saved job with id ${id} to the database.`); return id; } catch (error) { console.error("Error saving job to the database:", error); return null; } };