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,67 @@
"use strict";
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 || '';
}
async authenticate() {
try {
const response = await 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:', axiosError.response?.data || axiosError.message);
throw new Error('Failed to authenticate with France Travail API');
}
}
async ensureValidToken() {
if (!this.accessToken || (this.tokenExpiration && Date.now() >= this.tokenExpiration)) {
await this.authenticate();
}
}
async getJobOffers(params) {
await this.ensureValidToken();
try {
const response = await axios_1.default.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;
console.error('Failed to fetch job offers:', axiosError.response?.data || axiosError.message);
throw new Error('Failed to fetch job offers from France Travail API');
}
}
}
exports.default = new FranceTravailService();