Skip to content

Commit

Permalink
integrated external APIs for job
Browse files Browse the repository at this point in the history
  • Loading branch information
Timothy-py committed Jun 6, 2024
1 parent d0cf2d5 commit 25bb51b
Show file tree
Hide file tree
Showing 7 changed files with 320 additions and 8 deletions.
108 changes: 108 additions & 0 deletions jobsfeed/empllo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
const { isJobExpired, isSameDay } = require('../utils/isSameDay');
const settings = require('../model/settings');
const axios = require('axios');
const Xjob = require('../model/xjobs');

const empllo = async () => {
try {
const API_URL = 'https://empllo.com/api/v1';

let LAST_EMPLLO_API_UPDATE = null;
let sameDay = false;

const settingDoc = await settings.findOne({ name: 'Settings' });
if (settingDoc) {
LAST_EMPLLO_API_UPDATE = settingDoc.empllo_api_update;
}

const pingAPI = await axios.get(`${API_URL}?limit=1`);
const get_last_update_at = new Date(pingAPI.data.updated_at * 1000);

if (LAST_EMPLLO_API_UPDATE != null) {
sameDay = isSameDay(LAST_EMPLLO_API_UPDATE, get_last_update_at);

const newSetting = await settings.create({
name: 'Settings',
empllo_api_update: get_last_update_at
});
await newSetting.save();
}

if (sameDay === false) {
console.log('RUNNING......');
// Fetch jobs
const query = await axios.get(`${API_URL}`).data;
const total_count = query.total_count;
const jobs = query.jobs;

jobs.forEach(async (job) => {
// check if job has expired
const expireDate = new Date(job.expiryDate * 1000);
const jobExpired = isJobExpired(expireDate);

if (jobExpired === false) {
const [
title,
// description,
companyName,
companyLogo,
minSalary,
maxSalary,
seniority,
categories,
publishedDate,
expiryDate,
applicationLink,
jobType,
workModel
] = [
job.title,
// jobs.excerpt,
job.companyName,
job.companyLogo,
job.minSalary,
job.maxSalary,
job.seniorityLevel,
job.tags,
job.pubDate,
job.expiryDate,
job.applicationLink,
job.jobType,
job.workModel
];
// create job
const newJob = await Xjob.create(
[
{
title,
description,
companyName,
companyLogo,
minSalary,
maxSalary,
seniority,
categories,
publishedDate,
expiryDate,
applicationLink,
jobType,
workModel,
source: 'empllo'
}
],
{ new: true }
);

await newJob[0].save();
}
});
}

return true;
} catch (error) {
console.log(error);
return false;
}
};

module.exports = empllo;
102 changes: 102 additions & 0 deletions jobsfeed/himalayas.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
const { isJobExpired, isSameDay } = require('../utils/isSameDay');
const settings = require('../model/settings');
const axios = require('axios');
const Xjob = require('../model/xjobs');

const himalayas = async () => {
try {
const API_URL = 'https://himalayas.app/jobs/api';

let LAST_HIMALAYAS_API_UPDATE = null;
let sameDay = false;

const settingDoc = await settings.findOne({ name: 'Settings' });
if (settingDoc) {
LAST_HIMALAYAS_API_UPDATE = settingDoc.himalayas_api_update;
}

const pingAPI = await axios.get(`${API_URL}?limit=1`);
const get_last_update_at = new Date(pingAPI.data.updated_at * 1000);

if (LAST_HIMALAYAS_API_UPDATE != null) {
sameDay = isSameDay(LAST_HIMALAYAS_API_UPDATE, get_last_update_at);

const newSetting = await settings.create({
name: 'Settings',
himalayas_api_update: get_last_update_at
});
await newSetting.save();
}

if (sameDay === false) {
console.log('RUNNING......');
// Fetch jobs
const query = await axios.get(`${API_URL}`).data;
const total_count = query.total_count;
const jobs = query.jobs;

jobs.forEach(async (job) => {
// check if job has expired
const expireDate = new Date(job.expiryDate * 1000);
const jobExpired = isJobExpired(expireDate);

if (jobExpired === false) {
const [
title,
description,
companyName,
companyLogo,
minSalary,
maxSalary,
seniority,
categories,
publishedDate,
expiryDate,
applicationLink
] = [
job.title,
job.excerpt,
job.companyName,
job.companyLogo,
job.minSalary,
job.maxSalary,
job.seniority,
job.categories,
job.pubDate,
job.expiryDate,
job.applicationLink
];
// create job
const newJob = await Xjob.create(
[
{
title,
description,
companyName,
companyLogo,
minSalary,
maxSalary,
seniority,
categories,
publishedDate,
expiryDate,
applicationLink,
source: 'himalayas'
}
],
{ new: true }
);

await newJob[0].save();
}
});
}

return true;
} catch (error) {
console.log(error);
return false;
}
};

module.exports = himalayas;
17 changes: 17 additions & 0 deletions model/settings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const mongoose = require('mongoose');

const settingSchema = new mongoose.Schema({
name: {
type: String,
default: 'Settings',
required: false
},
himalayas_api_update: {
type: String,
required: false
}
});

const Settings = mongoose.model('Settings', settingSchema);

module.exports = Settings;
60 changes: 60 additions & 0 deletions model/xjobs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
const mongoose = require('mongoose');

const xjobSchema = new mongoose.Schema({
title: {
type: String,
required: false
},
description: {
type: String,
required: false
},
companyName: {
type: String,
required: false
},
companyLogo: {
type: String,
required: false
},
minSalary: {
type: Number,
required: false
},
maxSalary: {
type: Number,
required: false
},
seniority: {
type: Array,
required: false
},
categories: {
type: Array,
required: false
},
publishedDate: {
type: String,
required: false
},
expiryDate: {
type: String,
required: false
},
applicationLink: {
type: String,
required: false
},
jobType: {
type: String,
required: false
},
workModel: {
type: String,
required: false
}
});

const Xjob = mongoose.model('Xjob', xjobSchema);

module.exports = Xjob;
9 changes: 7 additions & 2 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ const jobRouter = require('./routes/jobs');
const adminRouter = require('./routes/admin');
const usersRouter = require('./routes/users');
const reportRouter = require('./routes/report');
const himalayas = require('./jobsfeed/himalayas');
const empllo = require('./jobsfeed/empllo');

dotenv.config({ path: './.env/config.env' });
const PORT = process.env.PORT;
Expand Down Expand Up @@ -54,7 +56,7 @@ app.use('/api/v1/recruiter', recruiterRouter);
app.use('/api/v1/blog', blogRouter);
app.use('/api/v1/company', companyRouter);
app.use('/api/v1/jobs', jobRouter);
app.use('/api/v1/admins', adminRouter);
app.use('/api/v1/admins', adminRouter);
app.use('/api/v1/users', usersRouter);
app.use('/api/v1/reports', reportRouter);

Expand All @@ -64,7 +66,7 @@ swaggerDocs(app, PORT);
app.use(errorHandler);

// catch 404 and forward to error handler
app.use(function (req, res, next) {
app.use(function (req, res, next) {
res.status(404).json({
success: false,
status: 'Resource Not Found',
Expand All @@ -86,4 +88,7 @@ app.use(function (err, req, res, next) {
// start job expiry cronjob
jobExpiryCron.start();

// himalayas();
// empllo();

module.exports = app;
6 changes: 0 additions & 6 deletions utils/JoiValidator.js

This file was deleted.

26 changes: 26 additions & 0 deletions utils/isSameDay.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const isSameDay = (date1, date2) => {
const givenDate = new Date(date1);
const currentDate = new Date(date2);

return (
givenDate.getUTCFullYear() === currentDate.getUTCFullYear() &&
givenDate.getUTCMonth() === currentDate.getUTCMonth() &&
givenDate.getUTCDate() === currentDate.getUTCDate()
);
};

const isJobExpired = (expiryDate) => {
const givenDate = new Date(expiryDate);
const currentDate = new Date();

// Normalize both dates to midnight to compare only the date parts
givenDate.setUTCHours(0, 0, 0, 0);
currentDate.setUTCHours(0, 0, 0, 0);

return givenDate < currentDate;
};

module.exports = {
isSameDay,
isJobExpired
};

0 comments on commit 25bb51b

Please sign in to comment.