diff --git a/libs/data/src/common/baserow/abstractBaserow.ts b/libs/data/src/common/baserow/abstractBaserow.ts index 5527b3321..5ff0ab09d 100644 --- a/libs/data/src/common/baserow/abstractBaserow.ts +++ b/libs/data/src/common/baserow/abstractBaserow.ts @@ -11,6 +11,7 @@ export abstract class AbstractBaserow { protected readonly __dirname = path.dirname(fileURLToPath(import.meta.url)) private readonly _apiToken = this._setBaserowToken() private readonly _baseUrl = 'https://api.baserow.io/api' + private readonly _url = `${this._baseUrl}/database/rows/table` protected readonly _themeTableId = ConfigBaserow.THEME_ID protected readonly _operatorTableId = ConfigBaserow.OPERATOR_ID protected readonly _geographicAreasTableId = ConfigBaserow.GEOGRAPHIC_AREAS_ID @@ -24,7 +25,7 @@ export abstract class AbstractBaserow { protected async _getTableData(tableId: number): Promise { try { - const response = await axios.get(`${this._baseUrl}/database/rows/table/${tableId}/?user_field_names=true`, this._axiosHeader) + const response = await axios.get(`${this._url}/${tableId}/?user_field_names=true`, this._axiosHeader) await this._delay(100) // eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-member-access let results = response.data.results @@ -44,7 +45,7 @@ export abstract class AbstractBaserow { protected async _getRowData(tableId: number, rowId: number): Promise { try { - const response = await axios.get(`${this._baseUrl}/database/rows/table/${tableId}/${rowId}/?user_field_names=true`, this._axiosHeader) + const response = await axios.get(`${this._url}/${tableId}/${rowId}/?user_field_names=true`, this._axiosHeader) // eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-member-access return response.data @@ -75,9 +76,10 @@ export abstract class AbstractBaserow { throw Error('Baserow token not found.') } + // eslint-disable-next-line @typescript-eslint/no-explicit-any protected async _patchRow(tableId: number, rowId: number, data: Record): Promise { try { - await axios.patch(`${this._baseUrl}/database/rows/table/${tableId}/${rowId}/?user_field_names=true`, data, this._axiosHeader) + await axios.patch(`${this._url}/${tableId}/${rowId}/?user_field_names=true`, data, this._axiosHeader) } catch (error) { console.error(`Error patching row ${rowId} in table ${tableId}:`, error) } @@ -85,7 +87,7 @@ export abstract class AbstractBaserow { protected async _createRow(tableId: number, data: Record): Promise { try { - await axios.post(`${this._baseUrl}/database/rows/table/${tableId}/?user_field_names=true`, data, this._axiosHeader) + await axios.post(`${this._url}/${tableId}/?user_field_names=true`, data, this._axiosHeader) } catch (error) { console.error(`Error creating row in table ${tableId}:`, error) } diff --git a/libs/data/src/common/baserow/trainingBaserow.ts b/libs/data/src/common/baserow/trainingBaserow.ts index 56a08a418..a65bfc727 100644 --- a/libs/data/src/common/baserow/trainingBaserow.ts +++ b/libs/data/src/common/baserow/trainingBaserow.ts @@ -1,16 +1,22 @@ import { AbstractBaserow } from './abstractBaserow' import { BaserowTraining } from './types' import { Training } from '../../trainings/types' +import ConfigBaserow from '../../configBaserow' export class TrainingBaserow extends AbstractBaserow { - private readonly _tableId = 620771 + private readonly _tableId = ConfigBaserow.TRAINING_ID + private _trainings: BaserowTraining[] = [] async getAll(): Promise { - return this._getTableData(this._tableId) + return (this._trainings = await this._getTableData(this._tableId)) } - async patchOrCreate(training: Training, existingTrainings: BaserowTraining[]): Promise { - const match = existingTrainings.find((baserowTraining) => baserowTraining['Id Ademe'] === training.id) + async patchOrCreate(training: Training): Promise { + if (!this._trainings.length) { + throw new Error('TrainingBaserow: No trainings loaded. Call getAll() first.') + } + + const match = this._trainings.find((baserowTraining) => baserowTraining['Id Ademe'] === training.id) const payload: Partial = { 'Id Ademe': training.id, @@ -18,7 +24,19 @@ export class TrainingBaserow extends AbstractBaserow { Titre: training.Libellé_de_la_formation, Promesse: training.Chapeau_pour_site_web, 'Url ADEME': training.Lien_URL_vers_la_fiche_programme, - Objectifs: training.Objectifs_de_la_formation + Objectifs: training.Objectifs_de_la_formation, + Thématique: training.Thème._, + Modalité: training.Modalité_de_dispense._ + ' | ' + training.Modalités_et_moyens_pédagogiques, + 'Codes Sections': Array.isArray(training.Id_section_code) ? training.Id_section_code.map((item) => item.id).join(', ') : '', + Cible: training.Public_cible, + Programme: training.Programme, + Prérequis: training.Prérequis, + Tarif: training.Tarif_net_de_taxes, + Durée: training.Durée_totale_en_heures, + 'Nombre de jours': training.Nombre_de_jours_de_formation, + 'Nombre de sessions à venir': Array.isArray(training.session) ? training.session.length : training.session ? 1 : 0, + 'Nombre de participants par session': + 'De ' + training.Nombre_de_participants_minimum + ' à ' + training.Nombre_de_participants_maximum } if (match && match.id) { diff --git a/libs/data/src/common/baserow/types.ts b/libs/data/src/common/baserow/types.ts index 5008991d4..71b14df09 100644 --- a/libs/data/src/common/baserow/types.ts +++ b/libs/data/src/common/baserow/types.ts @@ -140,6 +140,17 @@ export interface BaserowTraining extends Id { Promesse: string 'Url ADEME': string Objectifs: string + Thématique: string + 'Nombre de sessions à venir': number + 'Nombre de participants par session': string + Modalité: string + 'Codes Sections': string + Cible: string + Programme: string + Prérequis: string + Tarif: string + Durée: string + 'Nombre de jours': string } export interface ProgramTechField { diff --git a/libs/data/src/configBaserow.ts b/libs/data/src/configBaserow.ts index 574a837b9..04187e8be 100644 --- a/libs/data/src/configBaserow.ts +++ b/libs/data/src/configBaserow.ts @@ -10,6 +10,7 @@ export default class ConfigBaserow extends ConfigCommon { private static _TABLE_ID_PROJECT = 305253 private static _TABLE_ID_TESTIMONIES = 399896 private static _TABLE_ID_THEME = 305258 + private static _TABLE_ID_TRAINING = 620771 public static get TOKEN() { return this.getEnvValue('BASEROW_TOKEN') @@ -50,4 +51,8 @@ export default class ConfigBaserow extends ConfigCommon { public static get THEME_ID() { return parseInt(this.getEnvValue('BASEROW_TABLE_ID_THEME', this._TABLE_ID_THEME.toString())) } + + public static get TRAINING_ID() { + return parseInt(this.getEnvValue('BASEROW_TABLE_ID_TRAINING', this._TABLE_ID_TRAINING.toString())) + } } diff --git a/libs/data/src/trainings/trainingFeatures.ts b/libs/data/src/trainings/trainingFeatures.ts index 14d5ab1c1..60c8b9e2f 100644 --- a/libs/data/src/trainings/trainingFeatures.ts +++ b/libs/data/src/trainings/trainingFeatures.ts @@ -1,23 +1,29 @@ import { parseStringPromise } from 'xml2js' import axios from 'axios' import { Training } from './types' -import { BaserowTraining } from '../common/baserow/types' import { TrainingBaserow } from '../common/baserow/trainingBaserow' export class TrainingFeatures { - async loadTrainings(): Promise { + async loadTrainings(): Promise { + const trainings = await this.getXmlData() + await this.updateDatabase(trainings) + return + } + + private async getXmlData(): Promise { const xmlPath = 'https://formations.ademe.fr/tmp/flux_formations_agir.xml' const xmlContent = await axios.get(xmlPath).then((res) => res.data) const parsed = await parseStringPromise(xmlContent, { mergeAttrs: true, explicitArray: false }) - const formations: Training[] = parsed.formations.module || [] + return parsed.formations.module || [] + } + + private async updateDatabase(trainings: Training[]): Promise { const trainingBaserow = new TrainingBaserow() - const existingTrainings: BaserowTraining[] = await trainingBaserow.getAll() - for (const formation of formations) { - await trainingBaserow.patchOrCreate(formation, existingTrainings) + await trainingBaserow.getAll() + for (const training of trainings) { + await trainingBaserow.patchOrCreate(training) await new Promise((res) => setTimeout(res, 200)) } - - return [] } }