Skip to content

Commit

Permalink
build(scripts): add parser to handle form titles with t-lang elements…
Browse files Browse the repository at this point in the history
… when creating csv files

If no `t-lang` tag, use form title as is
Find and use the t-lang tag with 'en'
if no 'en', then use the first t-lang tag
Refs #3735
  • Loading branch information
Evans Dianga committed Jan 12, 2025
1 parent 5c21cba commit 521a565
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions server/src/scripts/generate-csv-data-set/generate-csv-data-set.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@ const spawn = require('child_process').spawn
const exec = util.promisify(require('child_process').exec)
const fs = require('fs-extra');
const sanitize = require('sanitize-filename');
const axios = require('axios')
const axios = require('axios');
const jsdom = require("jsdom");
const writeFile = util.promisify(fs.writeFile);
const log = require('tangy-log').log

const { JSDOM } = jsdom;
class NodeJSDOMParser {
parseFromString(s, contentType = 'text/html') {
return new JSDOM(s, {contentType}).window.document;
}
}
async function getUser1HttpInterface() {
const body = await axios.post('http://localhost/login', {
username: process.env.T_USER1,
Expand Down Expand Up @@ -98,9 +104,15 @@ async function generateCsvDataSet(groupId = '', formIds = [], outputPath = '', y
} else {
state.csvs.find(csv => csv.formId === formId).inProgress = true
await writeState(state)
const formTitle = formInfo
? formInfo.title.replace(/ /g, '_')
: formId
let formTitle;
if(!formInfo.title) formTitle = formId;
if(!formInfo.title.includes('t-lang')) formTitle = formInfo.title.replace(/ /g, '_');
if(formInfo.title.includes('t-lang')) {
const titleDomString = new NodeJSDOMParser().parseFromString(formInfo.title, 'text/html')
const formTitleEnglish = titleDomString.querySelector('t-lang[en]')
formInfo.title = formTitleEnglish ? formTitleEnglish.textContent : titleDomString.querySelector('t-lang').textContent
formTitle = formInfo.title.replace(/ /g, '_')
}
const groupFormname = sanitize(groupLabel + '-' + formTitle, options)
const fileName = `${groupFormname}${excludePii ? '-sanitized' : ''}-${Date.now()}.csv`.replace(/'/g, "_")
const csvOutputPath = `/csv/${fileName.replace(/['",]/g, "_")}`
Expand Down

0 comments on commit 521a565

Please sign in to comment.