Job Fetching Service for INeedAssessment
I'll create a service that fetches jobs from different regions (Overseas, Africa, Nigeria) based on the user's industry from the INeedAssessment interface. Here's a comprehensive solution:
Job Service Implementation
import axios from 'axios';
import { INeedAssessment } from './interfaces';
interface Job {
title: string;
company: string;
location: string;
description: string;
url: string;
postedDate: string;
source: string;
}
interface JobSearchParams {
industry: string;
location?: string;
experience?: string;
skills?: string[];
}
class JobSearchService {
private apiKeys = {
adzuna: process.env.ADZUNA_API_KEY,
indeed: process.env.INDEED_API_KEY,
linkedin: process.env.LINKEDIN_API_KEY,
};
private baseUrls = {
adzuna: 'https://api.adzuna.com/v1/api/jobs',
indeed: 'https://api.indeed.com/ads/apisearch',
linkedin: 'https://api.linkedin.com/v2/jobs',
};
async searchJobs(assessment: INeedAssessment): Promise<Job[]> {
const { industry, priorExperience, skills } = assessment;
const searchParams: JobSearchParams = {
industry,
experience: priorExperience,
skills,
};
// Determine search locations based on user's needs
const locations = this.determineSearchLocations(assessment);
const jobs: Job[] = [];
// Search in each determined location
for (const location of locations) {
searchParams.location = location;
const locationJobs = await this.fetchJobsFromAllSources(searchParams);
jobs.push(...locationJobs);
}
return jobs;
}
private determineSearchLocations(assessment: INeedAssessment): string[] {
// Custom logic to determine search locations based on assessment
// This is a simplified version - you can expand based on your requirements
const locations = ['Nigeria']; // Default to Nigeria
// If user has international experience or skills that are in demand globally
if (assessment.priorExperience === 'international' ||
(assessment.skills && assessment.skills.some(skill =>
['global', 'international', 'multinational'].includes(skill.toLowerCase())))) {
locations.push('Overseas');
}
// For certain industries, we might want to search across Africa
const africanIndustries = [
'agriculture', 'mining', 'oil and gas',
'telecommunications', 'financial services'
];
if (assessment.industry &&
africanIndustries.some(ind =>
assessment.industry.toLowerCase().includes(ind))) {
locations.push('Africa');
}
return [...new Set(locations)]; // Remove duplicates
}
private async fetchJobsFromAllSources(params: JobSearchParams): Promise<Job[]> {
try {
const [adzunaJobs, indeedJobs, linkedinJobs] = await Promise.all([
this.fetchFromAdzuna(params),
this.fetchFromIndeed(params),
this.fetchFromLinkedIn(params),
]);
return [...adzunaJobs, ...indeedJobs, ...linkedinJobs];
} catch (error) {
console.error('Error fetching jobs from sources:', error);
return [];
}
}
private async fetchFromAdzuna(params: JobSearchParams): Promise<Job[]> {
try {
const { industry, location, experience, skills } = params;
const response = await axios.get(`${this.baseUrls.adzuna}/search/1`, {
params: {
app_id: this.apiKeys.adzuna,
app_key: this.apiKeys.adzuna,
what: industry,
where: location,
results_per_page: 20,
sort_by: 'date',
...(skills && skills.length > 0 && { what_or: skills.join(',') }),
},
});
return response.data.results.map(job => ({
title: job.title,
company: job.company.display_name,
location: job.location.display_name,
description: job.description,
url: job.redirect_url,
postedDate: job.created,
source: 'Adzuna',
}));
} catch (error) {
console.error('Error fetching from Adzuna:', error);
return [];
}
}
private async fetchFromIndeed(params: JobSearchParams): Promise<Job[]> {
try {
const { industry, location, experience } = params;
const response = await axios.get(this.baseUrls.indeed, {
params: {
publisher: this.apiKeys.indeed,
q: industry,
l: location,
sort: 'date',
limit: 20,
...(experience && { fromage: this.getExperienceDays(experience) }),
},
});
return response.data.results.map(job => ({
title: job.jobtitle,
company: job.company,
location: job.formattedLocation,
description: job.snippet,
url: job.url,
postedDate: job.date,
source: 'Indeed',
}));
} catch (error) {
console.error('Error fetching from Indeed:', error);
return [];
}
}
private async fetchFromLinkedIn(params: JobSearchParams): Promise<Job[]> {
try {
const { industry, location, skills } = params;
const response = await axios.get(`${this.baseUrls.linkedin}?q=search`, {
headers: {
'Authorization': `Bearer ${this.apiKeys.linkedin}`,
'X-Restli-Protocol-Version': '2.0.0',
},
params: {
keywords: industry,
locationName: location,
...(skills && { facets: `["skills", "${skills.join(',')}"]` }),
count: 20,
sort: 'date',
},
});
return response.data.elements.map(job => ({
title: job.title,
company: job.companyName,
location: job.formattedLocation,
description: job.description.text,
url: job.detailUrl,
postedDate: job.postedAt,
source: 'LinkedIn',
}));
} catch (error) {
console.error('Error fetching from LinkedIn:', error);
return [];
}
}
private getExperienceDays(experience: string): string {
// Convert experience years to days for Indeed API
const years = parseInt(experience);
if (isNaN(years)) return 'any';
const days = years * 365;
return days.toString();
}
}
export default JobSearchService;
Usage Example
import JobSearchService from './jobSearchService';
import { INeedAssessment } from './interfaces';
// Example usage
const assessment: INeedAssessment = {
industry: 'Software Development',
priorExperience: '5 years',
experienceYears: '5',
skills: ['JavaScript', 'TypeScript', 'React'],
jobDescription: 'Looking for senior developer positions',
createdAt: new Date(),
updatedAt: new Date(),
};
const jobService = new JobSearchService();
async function findJobs() {
try {
const jobs = await jobService.searchJobs(assessment);
console.log(`Found ${jobs.length} jobs:`);
jobs.forEach(job => {
console.log(`- ${job.title} at ${job.company} (${job.location})`);
});
} catch (error) {
console.error('Error searching for jobs:', error);
}
}
findJobs();
Key Features
- Multi-Source Job Search: Fetches jobs from Adzuna, Indeed, and LinkedIn APIs
- Location Intelligence: Determines search locations (Nigeria, Africa, Overseas) based on user's assessment
- Industry-Specific Search: Uses the industry field from INeedAssessment to find relevant jobs
- Experience Filtering: Considers the user's prior experience when searching
- Skills Matching: Incorporates user's skills into the job search
- Standardized Output: Returns all jobs in the requested format
Implementation Notes
-
You'll need to:
- Set up API keys for each job provider
- Handle rate limiting and API quotas
- Implement proper error handling and retries
- Consider caching results to improve performance
-
The service can be extended to:
- Add more job sources
- Implement more sophisticated location determination logic
- Add filtering and sorting options
- Include salary information when available
-
For production use, you might want to:
- Add request throttling
- Implement circuit breakers for API failures
- Add logging and monitoring
- Consider using a queue system for large volumes of searches
Would you like me to elaborate on any specific part of this implementation?