phase 1 backend

This commit is contained in:
el 2025-05-24 01:01:01 +02:00
parent a0897c2d38
commit 9653e55453
26 changed files with 3225 additions and 0 deletions

View file

@ -0,0 +1,55 @@
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ingestJobOffers = void 0;
const client_1 = require("@prisma/client");
const FranceTravailService_1 = __importDefault(require("../services/FranceTravailService"));
const prisma = new client_1.PrismaClient();
const ingestJobOffers = (req, res) => __awaiter(void 0, void 0, void 0, function* () {
var _a, _b, _c, _d, _e;
try {
const jobOffers = yield FranceTravailService_1.default.getJobOffers({ range: '0-149' });
for (const offre of jobOffers.resultats) {
const mappedOffer = {
id: offre.id,
title: offre.intitule,
description: offre.description,
publicationDate: new Date(offre.dateCreation),
romeCode: offre.romeCode,
romeLabel: offre.romeLibelle,
locationLabel: ((_a = offre.lieuTravail) === null || _a === void 0 ? void 0 : _a.libelle) || null,
postalCode: ((_b = offre.lieuTravail) === null || _b === void 0 ? void 0 : _b.codePostal) || null,
departmentCode: ((_c = offre.lieuTravail) === null || _c === void 0 ? void 0 : _c.codeDepartement) || null,
cityName: ((_d = offre.lieuTravail) === null || _d === void 0 ? void 0 : _d.ville) || null,
companyName: ((_e = offre.entreprise) === null || _e === void 0 ? void 0 : _e.nom) || null,
contractType: offre.typeContrat,
contractLabel: offre.libelleTypeContrat,
};
yield prisma.jobOffer.upsert({
where: { id: mappedOffer.id },
update: mappedOffer,
create: mappedOffer,
});
}
res.status(200).json({
message: 'Job offers ingested successfully',
count: jobOffers.resultats.length,
});
}
catch (error) {
console.error('Error ingesting job offers:', error);
res.status(500).json({ error: 'Failed to ingest job offers' });
}
});
exports.ingestJobOffers = ingestJobOffers;

View file

@ -0,0 +1,43 @@
import { Request, Response } from 'express';
import { PrismaClient } from '@prisma/client';
import FranceTravailService from '../services/FranceTravailService';
const prisma = new PrismaClient();
export const ingestJobOffers = async (req: Request, res: Response) => {
try {
const jobOffers = await FranceTravailService.getJobOffers({ range: '0-149' });
for (const offre of jobOffers.resultats) {
const mappedOffer = {
id: offre.id,
title: offre.intitule,
description: offre.description,
publicationDate: new Date(offre.dateCreation),
romeCode: offre.romeCode,
romeLabel: offre.romeLibelle,
locationLabel: offre.lieuTravail?.libelle || null,
postalCode: offre.lieuTravail?.codePostal || null,
departmentCode: offre.lieuTravail?.codeDepartement || null,
cityName: offre.lieuTravail?.ville || null,
companyName: offre.entreprise?.nom || null,
contractType: offre.typeContrat,
contractLabel: offre.libelleTypeContrat,
};
await prisma.jobOffer.upsert({
where: { id: mappedOffer.id },
update: mappedOffer,
create: mappedOffer,
});
}
res.status(200).json({
message: 'Job offers ingested successfully',
count: jobOffers.resultats.length,
});
} catch (error) {
console.error('Error ingesting job offers:', error);
res.status(500).json({ error: 'Failed to ingest job offers' });
}
};

View file

@ -0,0 +1,78 @@
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.searchLocalJobOffers = void 0;
const client_1 = require("@prisma/client");
const prisma = new client_1.PrismaClient();
const searchLocalJobOffers = (req, res) => __awaiter(void 0, void 0, void 0, function* () {
try {
const page = parseInt(req.query.page) || 1;
const limit = parseInt(req.query.limit) || 10;
const skip = (page - 1) * limit;
const take = limit;
const sortBy = req.query.sortBy || 'publicationDate';
const sortOrder = req.query.sortOrder || 'desc';
const keyword = req.query.keyword;
const location = req.query.location;
const contractType = req.query.contractType;
const where = {};
if (keyword) {
where.OR = [
{ title: { contains: keyword, mode: 'insensitive' } },
{ description: { contains: keyword, mode: 'insensitive' } },
];
}
if (location) {
where.AND = [
...(where.AND || []),
{
OR: [
{ locationLabel: { contains: location, mode: 'insensitive' } },
{ postalCode: { contains: location, mode: 'insensitive' } },
{ cityName: { contains: location, mode: 'insensitive' } },
{ departmentCode: { contains: location, mode: 'insensitive' } },
],
},
];
}
if (contractType) {
where.AND = [
...(where.AND || []),
{ contractType: contractType },
];
}
const orderBy = {};
if (sortBy) {
orderBy[sortBy] = sortOrder === 'asc' ? 'asc' : 'desc';
}
else {
orderBy.publicationDate = 'desc'; // Tri par défaut
}
const jobs = yield prisma.jobOffer.findMany({
skip,
take,
where,
orderBy,
});
const total = yield prisma.jobOffer.count({ where });
res.status(200).json({
jobs,
total,
page,
limit,
});
}
catch (error) {
console.error('Error searching job offers:', error);
res.status(500).json({ error: 'Failed to search job offers' });
}
});
exports.searchLocalJobOffers = searchLocalJobOffers;

View file

@ -0,0 +1,74 @@
import { Request, Response } from 'express';
import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient();
export const searchLocalJobOffers = async (req: Request, res: Response) => {
try {
const page = parseInt(req.query.page as string) || 1;
const limit = parseInt(req.query.limit as string) || 10;
const skip = (page - 1) * limit;
const take = limit;
const sortBy = req.query.sortBy as string || 'publicationDate';
const sortOrder = req.query.sortOrder as string || 'desc';
const keyword = req.query.keyword as string;
const location = req.query.location as string;
const contractType = req.query.contractType as string;
console.log('Keyword:', keyword);
console.log('Location:', location);
const where: any = {};
if (keyword) {
where.OR = [
{ title: { contains: keyword, mode: 'insensitive' } },
{ description: { contains: keyword, mode: 'insensitive' } },
];
}
if (location) {
where.AND = [
...(where.AND || []),
{
OR: [
{ locationLabel: { contains: location, mode: 'insensitive' } },
{ postalCode: { contains: location, mode: 'insensitive' } },
{ cityName: { contains: location, mode: 'insensitive' } },
{ departmentCode: { contains: location, mode: 'insensitive' } },
],
},
];
}
if (contractType) {
where.AND = [
...(where.AND || []),
{ contractType: contractType },
];
}
const orderBy: any = {};
if (sortBy) {
orderBy[sortBy] = sortOrder === 'asc' ? 'asc' : 'desc';
} else {
orderBy.publicationDate = 'desc'; // Tri par défaut
}
const jobs = await prisma.jobOffer.findMany({
skip,
take,
where,
orderBy,
});
const total = await prisma.jobOffer.count({ where });
res.status(200).json({
jobs,
total,
page,
limit,
});
} catch (error) {
console.error('Error searching job offers:', error);
res.status(500).json({ error: 'Failed to search job offers' });
}
};

18
backend/src/index.js Normal file
View file

@ -0,0 +1,18 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const express_1 = __importDefault(require("express"));
const dotenv_1 = __importDefault(require("dotenv"));
const jobIngestionRoutes_1 = __importDefault(require("./routes/jobIngestionRoutes"));
const jobSearchRoutes_1 = __importDefault(require("./routes/jobSearchRoutes"));
dotenv_1.default.config();
const app = (0, express_1.default)();
const PORT = process.env.PORT || 3000;
app.use(express_1.default.json());
app.use(jobIngestionRoutes_1.default);
app.use(jobSearchRoutes_1.default);
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});

18
backend/src/index.ts Normal file
View file

@ -0,0 +1,18 @@
import express from 'express';
import dotenv from 'dotenv';
import jobIngestionRoutes from './routes/jobIngestionRoutes';
import jobSearchRoutes from './routes/jobSearchRoutes';
dotenv.config();
const app = express();
const PORT = process.env.PORT || 3000;
app.use(express.json());
app.use(jobIngestionRoutes);
app.use(jobSearchRoutes);
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});

View file

@ -0,0 +1,10 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const express_1 = __importDefault(require("express"));
const jobIngestionController_1 = require("../controllers/jobIngestionController");
const router = express_1.default.Router();
router.post('/api/ingest-jobs', jobIngestionController_1.ingestJobOffers);
exports.default = router;

View file

@ -0,0 +1,8 @@
import express from 'express';
import { ingestJobOffers } from '../controllers/jobIngestionController';
const router = express.Router();
router.post('/api/ingest-jobs', ingestJobOffers);
export default router;

View file

@ -0,0 +1,10 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const express_1 = __importDefault(require("express"));
const jobSearchController_1 = require("../controllers/jobSearchController");
const router = express_1.default.Router();
router.get('/api/jobs', jobSearchController_1.searchLocalJobOffers);
exports.default = router;

View file

@ -0,0 +1,8 @@
import express from 'express';
import { searchLocalJobOffers } from '../controllers/jobSearchController';
const router = express.Router();
router.get('/api/jobs', searchLocalJobOffers);
export default router;

View file

@ -0,0 +1,81 @@
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const axios_1 = __importDefault(require("axios"));
class FranceTravailService {
constructor() {
this.accessToken = null;
this.tokenExpiration = null;
this.realm = '/partenaire';
this.clientId = process.env.FRANCE_TRAVAIL_CLIENT_ID || '';
this.clientSecret = process.env.FRANCE_TRAVAIL_CLIENT_SECRET || '';
this.tokenUrl = process.env.FRANCE_TRAVAIL_TOKEN_URL || '';
this.apiUrl = process.env.FRANCE_TRAVAIL_API_URL || '';
this.scope = process.env.FRANCE_TRAVAIL_SCOPE || '';
}
authenticate() {
return __awaiter(this, void 0, void 0, function* () {
var _a;
try {
const response = yield axios_1.default.post(this.tokenUrl, null, {
params: {
realm: this.realm,
grant_type: 'client_credentials',
client_id: this.clientId,
client_secret: this.clientSecret,
scope: this.scope,
},
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
});
this.accessToken = response.data.access_token;
this.tokenExpiration = Date.now() + response.data.expires_in * 1000;
}
catch (error) {
const axiosError = error;
console.error('Authentication failed:', ((_a = axiosError.response) === null || _a === void 0 ? void 0 : _a.data) || axiosError.message);
throw new Error('Failed to authenticate with France Travail API');
}
});
}
ensureValidToken() {
return __awaiter(this, void 0, void 0, function* () {
if (!this.accessToken || (this.tokenExpiration && Date.now() >= this.tokenExpiration)) {
yield this.authenticate();
}
});
}
getJobOffers(params) {
return __awaiter(this, void 0, void 0, function* () {
var _a;
yield this.ensureValidToken();
try {
const response = yield axios_1.default.get(this.apiUrl, {
headers: {
Authorization: `Bearer ${this.accessToken}`,
},
params: Object.assign(Object.assign({}, params), { range: (params === null || params === void 0 ? void 0 : params.range) || '0-9' }),
});
return response.data;
}
catch (error) {
const axiosError = error;
console.error('Failed to fetch job offers:', ((_a = axiosError.response) === null || _a === void 0 ? void 0 : _a.data) || axiosError.message);
throw new Error('Failed to fetch job offers from France Travail API');
}
});
}
}
exports.default = new FranceTravailService();

View file

@ -0,0 +1,74 @@
import axios, { AxiosError } from 'axios';
class FranceTravailService {
private clientId: string;
private clientSecret: string;
private tokenUrl: string;
private apiUrl: string;
private scope: string;
private accessToken: string | null = null;
private tokenExpiration: number | null = null;
private realm: string = '/partenaire';
constructor() {
this.clientId = process.env.FRANCE_TRAVAIL_CLIENT_ID || '';
this.clientSecret = process.env.FRANCE_TRAVAIL_CLIENT_SECRET || '';
this.tokenUrl = process.env.FRANCE_TRAVAIL_TOKEN_URL || '';
this.apiUrl = process.env.FRANCE_TRAVAIL_API_URL || '';
this.scope = process.env.FRANCE_TRAVAIL_SCOPE || '';
}
private async authenticate(): Promise<void> {
try {
const response = await axios.post(this.tokenUrl, null, {
params: {
realm: this.realm,
grant_type: 'client_credentials',
client_id: this.clientId,
client_secret: this.clientSecret,
scope: this.scope,
},
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
});
this.accessToken = response.data.access_token;
this.tokenExpiration = Date.now() + response.data.expires_in * 1000;
} catch (error) {
const axiosError = error as AxiosError;
console.error('Authentication failed:', axiosError.response?.data || axiosError.message);
throw new Error('Failed to authenticate with France Travail API');
}
}
private async ensureValidToken(): Promise<void> {
if (!this.accessToken || (this.tokenExpiration && Date.now() >= this.tokenExpiration)) {
await this.authenticate();
}
}
public async getJobOffers(params?: any): Promise<any> {
await this.ensureValidToken();
try {
const response = await axios.get(this.apiUrl, {
headers: {
Authorization: `Bearer ${this.accessToken}`,
},
params: {
...params,
range: params?.range || '0-9', // Default range for pagination
},
});
return response.data;
} catch (error) {
const axiosError = error as AxiosError;
console.error('Failed to fetch job offers:', axiosError.response?.data || axiosError.message);
throw new Error('Failed to fetch job offers from France Travail API');
}
}
}
export default new FranceTravailService();