Skip to content

Commit

Permalink
Merge branch 'teach/add-attend-score-behav-to-mysql-outputs' into rel…
Browse files Browse the repository at this point in the history
…ease/v3.31.0
  • Loading branch information
esurface committed Jul 23, 2024
2 parents 53bbfd6 + b6402f1 commit d8bf659
Showing 1 changed file with 71 additions and 2 deletions.
73 changes: 71 additions & 2 deletions server/src/modules/mysql-js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,14 +206,31 @@ module.exports = {
}
const result = await saveToMysql(knex, sourceDb,flatDoc, tablenameSuffix, tableName, docType, primaryKey, createFunction)
log.info('Processed: ' + JSON.stringify(result))
} else {
} else if (doc.type === 'response') {
const flatDoc = await prepareFlatData(doc, sanitized);
tableName = null;
docType = 'response';
primaryKey = 'ID'
createFunction = function (t) {
t.engine('InnoDB')
t.string(primaryKey, 36).notNullable().primary();
t.string(primaryKey, 200).notNullable().primary();
t.string('caseId', 36) // .index('response_caseId_IDX');
t.string('participantID', 36) //.index('case_instances_ParticipantID_IDX');
t.string('caseEventId', 36) // .index('eventform_caseEventId_IDX');
t.tinyint('complete');
t.string('archived', 36); // TODO: "sqlMessage":"Incorrect integer value: '' for column 'archived' at row 1
}
const result = await saveToMysql(knex, sourceDb,flatDoc, tablenameSuffix, tableName, docType, primaryKey, createFunction)
log.info('Processed: ' + JSON.stringify(result))
} else {
const flatDoc = await prepareFlatData(doc, sanitized);
tableName = flatDoc.type;
console.log("tableName: " + tableName)
docType = 'response';
primaryKey = 'ID'
createFunction = function (t) {
t.engine('InnoDB')
t.string(primaryKey, 200).notNullable().primary();
t.string('caseId', 36) // .index('response_caseId_IDX');
t.string('participantID', 36) //.index('case_instances_ParticipantID_IDX');
t.string('caseEventId', 36) // .index('eventform_caseEventId_IDX');
Expand All @@ -223,6 +240,7 @@ module.exports = {
const result = await saveToMysql(knex, sourceDb,flatDoc, tablenameSuffix, tableName, docType, primaryKey, createFunction)
log.info('Processed: ' + JSON.stringify(result))
}

await knex.destroy()
}
}
Expand Down Expand Up @@ -329,9 +347,56 @@ const generateFlatResponse = async function (formResponse, sanitized) {
if (formResponse.form.id === '') {
formResponse.form.id = 'blank'
}
console.log('formResponse.form.id: ' + formResponse.form.id)
flatFormResponse['formId'] = formResponse.form.id
}

if (formResponse.type === 'attendance' || formResponse.type === 'behavior' || formResponse.type === 'scores' ||
formResponse.form.id === 'student-registration' ||
formResponse.form.id === 'class-registration') {

function hackFunctionToRemoveUserProfileId (formResponse) {
// This is a very special hack function to remove userProfileId
// It needs to be replaced with a proper solution that resolves duplicate variables.
if (formResponse.userProfileId) {
for (let item of formResponse.items) {
for (let input of item.inputs) {
if (input.name === 'userProfileId') {
delete formResponse.userProfileId;
}
}
}
}
return formResponse;
}

formResponse = hackFunctionToRemoveUserProfileId(formResponse);

if (formResponse.type === 'attendance') {
flatFormResponse['attendanceList'] = formResponse.attendanceList
} else if (formResponse.type === 'behavior') {
// flatFormResponse['studentBehaviorList'] = formResponse.studentBehaviorList
const studentBehaviorList = formResponse.studentBehaviorList.map(record => {
const student = {}
Object.keys(record).forEach(key => {
if (key === 'behavior') {
student[key + '.formResponseId'] = record[key]['formResponseId']
student[key + '.internal'] = record[key]['internal']
student[key + '.internalPercentage'] = record[key]['internalPercentage']
// console.log("special processing for behavior: " + JSON.stringify(student) )
} else {
// console.log("key: " + key + " record[key]: " + record[key])
student[key] = record[key]
}
})
return student
})
flatFormResponse['studentBehaviorList'] = studentBehaviorList
} else if (formResponse.type === 'scores') {
flatFormResponse['scoreList'] = formResponse.scoreList
}
}

for (let item of formResponse.items) {
for (let input of item.inputs) {
let sanitize = false;
Expand Down Expand Up @@ -664,6 +729,7 @@ async function convert_response(knex, doc, groupId, tableName) {
const caseEventId = doc.caseEventId

var formID = doc.formId || data['formid']
console.log("formID: " + formID)
if (formID) {
// thanks to https://stackoverflow.com/a/14822579
const find = '-'
Expand Down Expand Up @@ -820,6 +886,9 @@ async function saveToMysql(knex, sourceDb, doc, tablenameSuffix, tableName, docT
data = await convert_issue(knex, doc, groupId, tableName)
break;
case 'response':
case 'attendance':
case 'behavior':
case 'scores':
data = await convert_response(knex, doc, groupId, tableName)
// Check if table exists and create if needed:
tableName = data['formID_sanitized'] + tablenameSuffix
Expand Down

0 comments on commit d8bf659

Please sign in to comment.