ceiba-scores/scripts/directus-to-markdown/index.js

82 lines
2.5 KiB
JavaScript

import DirectusToMarkdown from '@resilien/directus-to-markdown'
import urlslug from 'url-slug'
const getTranslation = (translations, lang, field) => {
const match = translations.filter(translation => translation.languages_code == lang || translation.languages_id == lang)
return match.length > 0 ? match[0][field] : undefined
}
const formatQuestions = (questions, lang) => {
const formated = []
for (const [i, question] of questions.entries()) {
formated.push({
id: question.questions_id.id,
sort: question.sort,
title: getTranslation(question.questions_id.translations, lang, 'title'),
answers: formatAnswers(question.questions_id.answers, lang)
})
}
return formated
}
const formatAnswers = (answers, lang) => {
const formated = []
for (const [i, answer] of answers.entries()) {
formated.push({
id: answer.answers_id.id,
sort: answer.sort,
title: getTranslation(answer.answers_id.translations, lang, 'title'),
weight: answer.answers_id.weight
})
}
return formated
}
const formatScore = (score, lang) => {
score.title = getTranslation(score.translations, lang, 'title')
delete score.translations
return score
}
const formatResults = (results, lang) => {
const formated = []
for (const [i, result] of results.entries()) {
formated.push({
id: result.results_id.id,
sort: result.sort,
min: result.results_id.min,
max: result.results_id.max,
facteur: getTranslation(result.results_id.translations, lang, 'facteur'),
effets: getTranslation(result.results_id.translations, lang, 'effets'),
pde: getTranslation(result.results_id.translations, lang, 'pde'),
pde_qtra: result.results_id.pde_qtra,
})
}
return formated
}
const config = {
collections: {
scores: {
readByQueryOption: {
fields: ['*', 'translations.*', 'questions.sort', 'questions.questions_id.*', 'questions.questions_id.translations.*', 'questions.questions_id.answers.*', 'questions.questions_id.answers.answers_id.*', 'questions.questions_id.answers.answers_id.translations.*', 'results.*', 'results.results_id.*', 'results.results_id.translations.*']
},
pathBuilder: (score) => {
const lang = 'fr-FR'
score = formatScore(score, lang)
score.questions = formatQuestions(score.questions, lang)
score.results = formatResults(score.results, lang)
return `./content`;
},
deleteFields: ['path']
}
}
}
new DirectusToMarkdown(config).export();