lib/impl/exportSubmissions.js
function buildCSVsForSingleMergedForm(formSubmissionModel, params, cb) {
  var formId = params.formId;
  var formName = params.formName;
  var date = params.date;
  var mergedFields = params.mergedFields;
  var fieldHeader = params.fieldHeader;
  var singleFormQuery = params.singleFormQuery;
  var downloadUrl = params.downloadUrl;

  var fullSubmissionCSVString = "";
  //Form Name might not be unique but the ID will always be.
  var fileName = date + "-" + formId + "-" + (formName.split(' ').join('_'));
  //Query the submissions for the formId
  //LEAN
  //Select only the metadata and formFields in the submission.
  // Stream response
  // Build CSV string for each entry.
  // Add to the zip file.
  cb = _.once(cb);

  params.statusUpdaterFunction({
    message: "Beginning export of submissions for form ID: " + formId
  });

  var exportProgressInterval = setInterval(function() {
    params.statusUpdaterFunction({
      message: "Exporting submission " + params.exportCounter.numSubsExported + " of " + params.exportCounter.numSubmissionsToExport
    });
  }, 1000);

  //First, generate headers.
  fullSubmissionCSVString = csvHeaders.generateCSVHeaders(_.keys(mergedFields[formId]), mergedFields[formId], fieldHeader);

  var submissionQueryStream = formSubmissionModel.find(singleFormQuery).select({
    "formSubmittedAgainst.pages": 0,
    "formSubmittedAgainst.pageRules": 0,
    "formSubmittedAgainst.fieldRules": 0
  }).lean().stream();

  submissionQueryStream.on('data', function addSubmissionToCSV(submissionJSON) {
    //Merge the form fields
    fullSubmissionCSVString += processSingleSubmission({
      mergedFields: mergedFields,
      submission: submissionJSON,
      date: date,
      fieldHeader: fieldHeader,
      downloadUrl: downloadUrl
    });

    params.exportCounter.numSubsExported ++;
  }).on('error', function(err) {
    logger.error("Error streaming submissions ", err);
    clearInterval(exportProgressInterval);
    return cb(err);
  }).on('close', function() {
    clearInterval(exportProgressInterval);
    return cb(undefined, {
      formId: formId,
      fileName: fileName,
      csvString: fullSubmissionCSVString
    });
  });
}
function buildCSVsForSingleMergedForm(formSubmissionModel, params, cb) {
  var formId = params.formId;
  var formName = params.formName;
  var date = params.date;
  var mergedFields = params.mergedFields;
  var fieldHeader = params.fieldHeader;
  var singleFormQuery = params.singleFormQuery;
  var downloadUrl = params.downloadUrl;

  var fullSubmissionCSVString = "";
  //Form Name might not be unique but the ID will always be.
  var fileName = date + "-" + formId + "-" + (formName.split(' ').join('_'));
  //Query the submissions for the formId
  //LEAN
  //Select only the metadata and formFields in the submission.
  // Stream response
  // Build CSV string for each entry.
  // Add to the zip file.
  cb = _.once(cb);

  params.statusUpdaterFunction({
    message: "Beginning export of submissions for form ID: " + formId
  });

  var exportProgressInterval = setInterval(function() {
    params.statusUpdaterFunction({
      message: "Exporting submission " + params.exportCounter.numSubsExported + " of " + params.exportCounter.numSubmissionsToExport
    });
  }, 1000);

  //First, generate headers.
  fullSubmissionCSVString = csvHeaders.generateCSVHeaders(_.keys(mergedFields[formId]), mergedFields[formId], fieldHeader);

  var submissionQueryStream = formSubmissionModel.find(singleFormQuery).select({
    "formSubmittedAgainst.pages": 0,
    "formSubmittedAgainst.pageRules": 0,
    "formSubmittedAgainst.fieldRules": 0
  }).lean().stream();

  submissionQueryStream.on('data', function addSubmissionToCSV(submissionJSON) {
    //Merge the form fields
    fullSubmissionCSVString += processSingleSubmission({
      mergedFields: mergedFields,
      submission: submissionJSON,
      date: date,
      fieldHeader: fieldHeader,
      downloadUrl: downloadUrl
    });

    params.exportCounter.numSubsExported ++;
  }).on('error', function(err) {
    logger.error("Error streaming submissions ", err);
    clearInterval(exportProgressInterval);
    return cb(err);
  }).on('close', function() {
    clearInterval(exportProgressInterval);
    return cb(undefined, {
      formId: formId,
      fileName: fileName,
      csvString: fullSubmissionCSVString
    });
  });
}
function buildCSVsForSingleMergedForm ( formSubmissionModel , params , cb ) { var formId = params . formId ; var formName = params . formName ; var date = params . date ; var mergedFields = params . mergedFields ; var fieldHeader = params . fieldHeader ; var singleFormQuery = params . singleFormQuery ; var downloadUrl = params . downloadUrl ; var fullSubmissionCSVString = "" ; //Form Name might not be unique but the ID will always be. var fileName = date + "-" + formId + "-" + ( formName . split ( ' ' ) . join ( '_' ) ) ; //Query the submissions for the formId //LEAN //Select only the metadata and formFields in the submission. // Stream response // Build CSV string for each entry. // Add to the zip file. cb = _ . once ( cb ) ; params . statusUpdaterFunction ( { message : "Beginning export of submissions for form ID: " + formId } ) ; var exportProgressInterval = setInterval ( function ( ) { params . statusUpdaterFunction ( { message : "Exporting submission " + params . exportCounter . numSubsExported + " of " + params . exportCounter . numSubmissionsToExport } ) ; } , 1000 ) ; //First, generate headers. fullSubmissionCSVString = csvHeaders . generateCSVHeaders ( _ . keys ( mergedFields [ formId ] ) , mergedFields [ formId ] , fieldHeader ) ; var submissionQueryStream = formSubmissionModel . find ( singleFormQuery ) . select ( { "formSubmittedAgainst.pages" : 0 , "formSubmittedAgainst.pageRules" : 0 , "formSubmittedAgainst.fieldRules" : 0 } ) . lean ( ) . stream ( ) ; submissionQueryStream . on ( 'data' , function addSubmissionToCSV ( submissionJSON ) { //Merge the form fields fullSubmissionCSVString += processSingleSubmission ( { mergedFields : mergedFields , submission : submissionJSON , date : date , fieldHeader : fieldHeader , downloadUrl : downloadUrl } ) ; params . exportCounter . numSubsExported ++ ; } ) . on ( 'error' , function ( err ) { logger . error ( "Error streaming submissions " , err ) ; clearInterval ( exportProgressInterval ) ; return cb ( err ) ; } ) . on ( 'close' , function ( ) { clearInterval ( exportProgressInterval ) ; return cb ( undefined , { formId : formId , fileName : fileName , csvString : fullSubmissionCSVString } ) ; } ) ; }
buildCSVsForSingleMergedForm - Building a CSV representation of a submission based on the merged definiton of all of the versions of the form it was submitted against.

@param  {Model}  formSubmissionModel                           The Submission mongoose Model
@param  {type}   params
@param  {string} params.formId
@param  {function} params.statusUpdaterFunction                Function to update the status of a submission export.
@param  {string} params.date                                   Formatted date of export
@param  {string} params.formName                               Form Name
@param  {object} params.mergedFields                           Set of merged field definitions
@param  {string} params.fieldHeader                            Field header to use for export
@param  {object} params.singleFormQuery                        Query to search for submissions based in a form ID
@param  {number} params.exportCounter.numSubmissionsToExport   The total number of submissions to export
@param  {number} params.exportCounter.numSubsExported          The total number of submissions exported to date
@param  {string} params.downloadUrl                            The downloadUrl template to use when generating submission file URLs.
@param  {function} cb
buildCSVsForSingleMergedForm - Building a CSV representation of a submission based on the merged definiton of all of the versions of the form it was submitted against .