Skip to content

Commit

Permalink
Merge pull request #308 from RADAR-base/fix-script
Browse files Browse the repository at this point in the history
Check if questionnaire exists before pulling from redcap
  • Loading branch information
mpgxvii authored Mar 28, 2024
2 parents 2b3b8a7 + 386ac00 commit 4dbdc1e
Showing 1 changed file with 44 additions and 21 deletions.
65 changes: 44 additions & 21 deletions utilities/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,42 +11,59 @@ async function publishQuestionnaires(
formNames,
) {
for (const form of formNames) {
const formData = createFormRequestBody(redcapToken, form);
await publishSingleQuestionnaire(redcapUrl, langConvention, form, formData);
await publishSingleQuestionnaire(redcapUrl, langConvention, form, redcapToken);

console.log(`Finished publishing questionnaire: ${form}`);
console.log(`Finished trying to publish questionnaire: ${form}`);
}
}

async function publishSingleQuestionnaire(
redcapUrl,
langConvention,
formName,
formData,
redcapToken
) {
var lang = langConvention || '';
var redcapUrl = redcapUrl || '';

// Publish to Github
const githubFilename = formName + '/' + formName + '_armt' + lang + '.json';
const formAsString = await pullFromRedcap(redcapUrl, formName, formData);

await githubClient
.postToGithub(githubFilename, formAsString)
.catch(e => console.log(e));
// Check form exists
const formExists = await checkFormExists(redcapUrl, formName, redcapToken);
if (!formExists) {
console.log(`Form ${formName} does not exist in REDCap`);
return;
}
else {
console.log(`Form ${formName} exists in REDCap, pulling data...`);
// If form exists, pull from REDCap
const formAsString = await pullFromRedcap(redcapUrl, formName, redcapToken);
console.log(`Pulled data for form ${formName}, publishing to Github...`);
await githubClient
.postToGithub(githubFilename, formAsString)
.catch(e => console.log(e));
}
}

console.log('Done uploading to github!');
function checkFormExists(redcapUrl, formName, redcapToken) {
const formData = createFormRequestBody(redcapToken);
formData.append('content', 'instrument');
return sendPostRequest(redcapUrl, formData)
.then(function(response) {
const instruments = response['data'].map(instrument => instrument['instrument_name']);
return instruments.includes(formName);
})
.catch(function(error) {
console.log(error);
});
}

function pullFromRedcap(redcapUrl, formName, formData) {
return axios({
method: 'post',
url: redcapUrl,
data: formData,
headers: {
'Content-Type': `multipart/form-data; boundary=${formData._boundary}`,
},
})
function pullFromRedcap(redcapUrl, formName, redcapToken) {
const formData = createFormRequestBody(redcapToken);
formData.append('content', 'metadata');
formData.append('forms[0]', formName);
return sendPostRequest(redcapUrl, formData)
.then(function(response) {
var formParsed = redcapParser.REDCapConverter(
cleanupJson(response['data']),
Expand All @@ -63,13 +80,19 @@ function cleanupJson(json) {
return JSON.parse(JSON.stringify(json).replace(/(\r?\n|\r)/gm, '\n'));
}

function createFormRequestBody(redcapToken, formName) {
function sendPostRequest(url, form) {
return axios.post(url, form, {
headers: {
'Content-Type': `multipart/form-data; boundary=${form._boundary}`,
},
});
}

function createFormRequestBody(redcapToken) {
var form = new FormData();
form.append('token', redcapToken);
form.append('content', 'metadata');
form.append('format', 'json');
form.append('returnFormat', 'json');
form.append('forms[0]', formName);

return form;
}
Expand Down

0 comments on commit 4dbdc1e

Please sign in to comment.