Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions libs/data/src/common/baserow/abstractBaserow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -24,7 +25,7 @@ export abstract class AbstractBaserow {

protected async _getTableData<T>(tableId: number): Promise<T[]> {
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
Expand All @@ -44,7 +45,7 @@ export abstract class AbstractBaserow {

protected async _getRowData<T>(tableId: number, rowId: number): Promise<T | null> {
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
Expand Down Expand Up @@ -75,17 +76,18 @@ 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<string, any>): Promise<void> {
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)
}
}

protected async _createRow(tableId: number, data: Record<string, any>): Promise<void> {
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)
}
Expand Down
28 changes: 23 additions & 5 deletions libs/data/src/common/baserow/trainingBaserow.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,42 @@
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<BaserowTraining[]> {
return this._getTableData<BaserowTraining>(this._tableId)
return (this._trainings = await this._getTableData<BaserowTraining>(this._tableId))
}

async patchOrCreate(training: Training, existingTrainings: BaserowTraining[]): Promise<void> {
const match = existingTrainings.find((baserowTraining) => baserowTraining['Id Ademe'] === training.id)
async patchOrCreate(training: Training): Promise<void> {
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<BaserowTraining> = {
'Id Ademe': training.id,
'Futures Sessions': training.session ? JSON.stringify(Array.isArray(training.session) ? training.session : [training.session]) : '',
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) {
Expand Down
11 changes: 11 additions & 0 deletions libs/data/src/common/baserow/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions libs/data/src/configBaserow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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()))
}
}
22 changes: 14 additions & 8 deletions libs/data/src/trainings/trainingFeatures.ts
Original file line number Diff line number Diff line change
@@ -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<Training[]> {
async loadTrainings(): Promise<void> {
const trainings = await this.getXmlData()
await this.updateDatabase(trainings)
return
}

private async getXmlData(): Promise<Training[]> {
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<void> {
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 []
}
}
Loading