[525] | 1 | var express = require("express") |
---|
| 2 | , HTTPResult = require("../util/http-result") |
---|
| 3 | , cryptoken = require("../util/crypto-token") |
---|
| 4 | , etags = require("../util/etags") |
---|
| 5 | , _ = require('underscore') |
---|
| 6 | ; |
---|
| 7 | |
---|
| 8 | module.exports = function(couch,schema) { |
---|
| 9 | var exports = {}; |
---|
| 10 | |
---|
| 11 | var util = require('./util')(couch,schema); |
---|
| 12 | var responses = require('./responses')(couch,schema); |
---|
| 13 | var app = exports.app = express(); |
---|
| 14 | |
---|
| 15 | app.get('/', |
---|
| 16 | util.ensureMIME(util.JSON_MIME), |
---|
[531] | 17 | function(req,res) { |
---|
| 18 | var qs = {reduce:false,include_docs:true}; |
---|
| 19 | var now = new Date().toISOString(); |
---|
| 20 | var hr; |
---|
| 21 | if ( 'current' in req.query ) { |
---|
| 22 | // we make the assumption that there will be fewer |
---|
| 23 | // runs with no or a future enddate than with a |
---|
| 24 | // startdate in the past, i.e. we assume fewer future |
---|
| 25 | // surveys are planned than past surveys are run. |
---|
| 26 | hr = couch.get('_design/surveyRuns/_view/by_end_date',{ |
---|
| 27 | query: {startkey:now,reduce:false,include_docs:true} |
---|
| 28 | }).handle({ |
---|
| 29 | '-1': _.identity, |
---|
| 30 | 200: function(result) { |
---|
| 31 | return _.filter( |
---|
| 32 | util.handleRowDocs(result), |
---|
| 33 | function(doc){ |
---|
| 34 | return !doc.startDate || |
---|
| 35 | doc.startDate <= now; |
---|
| 36 | }); |
---|
| 37 | }, |
---|
| 38 | default: util.handleUnknownResponse |
---|
| 39 | }); |
---|
| 40 | } else if ( 'future' in req.query ) { |
---|
| 41 | hr = couch.get('_design/surveyRuns/_view/by_start_date',{ |
---|
| 42 | query: {startkey:now,reduce:false,include_docs:true} |
---|
| 43 | }).handle({ |
---|
| 44 | '-1': _.identity, |
---|
| 45 | 200: util.handleRowDocs, |
---|
| 46 | default: util.handleUnknownResponse |
---|
| 47 | }); |
---|
| 48 | } else if ( 'past' in req.query ) { |
---|
| 49 | hr = couch.get('_design/surveyRuns/_view/by_end_date',{ |
---|
| 50 | query: {endkey:now,reduce:false,include_docs:true} |
---|
| 51 | }).handle({ |
---|
| 52 | '-1': _.identity, |
---|
| 53 | 200: util.handleRowDocs, |
---|
| 54 | default: util.handleUnknownResponse |
---|
| 55 | }); |
---|
| 56 | } else { |
---|
| 57 | hr = util.getDocumentsOfType('SurveyRun'); |
---|
| 58 | } |
---|
| 59 | hr.handle({'-1': util.handleException}) |
---|
| 60 | .handle(res.send.bind(res)); |
---|
| 61 | }); |
---|
[525] | 62 | app.post('/', |
---|
| 63 | util.ensureMIME(util.JSON_MIME), |
---|
| 64 | function(req,res) { |
---|
| 65 | var doc = req.body; |
---|
| 66 | var hr; |
---|
| 67 | if ( !util.isDocPublished(doc.survey) ) { |
---|
| 68 | hr = new HTTPResult(400,{error:"Cannot run unpublished survey."}); |
---|
| 69 | } else { |
---|
| 70 | hr = cryptoken() |
---|
| 71 | .then(function(token) { |
---|
| 72 | doc.secret = token; |
---|
| 73 | return util.postDocument('SurveyRun',doc); |
---|
| 74 | }); |
---|
| 75 | } |
---|
[527] | 76 | hr.handle({'-1': util.handleException}) |
---|
| 77 | .handle(res.send.bind(res)); |
---|
[525] | 78 | }); |
---|
| 79 | app.get('/:id', |
---|
| 80 | util.ensureMIME(util.JSON_MIME), |
---|
| 81 | util.makeDocGet_id('SurveyRun')); |
---|
| 82 | app.put('/:id', |
---|
| 83 | util.ensureMIME(util.JSON_MIME), |
---|
| 84 | function (req,res) { |
---|
| 85 | var id = req.params.id; |
---|
| 86 | var doc = req.body; |
---|
| 87 | var rev = etags.parse(req.header('If-Match'))[0] || (doc && doc._rev); |
---|
| 88 | var hr; |
---|
| 89 | if ( !util.isDocPublished(doc.survey) ) { |
---|
| 90 | hr = new HTTPResult(400,{error:"Cannot run unpublished survey."}); |
---|
| 91 | } else if ( typeof doc.secret === 'undefined' ) { |
---|
| 92 | hr = cryptoken() |
---|
| 93 | .then(function(token) { |
---|
| 94 | doc.secret = token; |
---|
| 95 | return util.putDocument(id,rev,'SurveyRun',doc); |
---|
| 96 | }); |
---|
| 97 | } else { |
---|
| 98 | hr = util.putDocument(id,rev,'SurveyRun',doc); |
---|
| 99 | } |
---|
[527] | 100 | hr.handle({'-1': util.handleException}) |
---|
| 101 | .handle(res.send.bind(res)); |
---|
[525] | 102 | }); |
---|
| 103 | app.delete('/:id', |
---|
| 104 | util.ensureMIME(util.JSON_MIME), |
---|
| 105 | function(req,res) { |
---|
| 106 | var id = req.params.id; |
---|
| 107 | var doc = req.body; |
---|
| 108 | var rev = etags.parse(req.header('If-Match'))[0] || (doc && doc._rev); |
---|
| 109 | responses.getResponsesBySurveyRunId(id) |
---|
| 110 | .handle({ |
---|
| 111 | 200: function(responses) { |
---|
| 112 | if ( responses.length > 0 ) { |
---|
| 113 | return new HTTPResult(403,{error:"Cannot delete run that has responses."}); |
---|
| 114 | } else { |
---|
| 115 | return util.deleteDocument(id,rev); |
---|
| 116 | } |
---|
| 117 | } |
---|
[527] | 118 | }) |
---|
| 119 | .handle({'-1': util.handleException}) |
---|
| 120 | .handle(res.send.bind(res)); |
---|
[525] | 121 | }); |
---|
| 122 | app.get('/:id/responses', |
---|
| 123 | util.ensureMIME(util.JSON_MIME), |
---|
| 124 | function(req,res) { |
---|
| 125 | var id = req.params.id; |
---|
| 126 | responses.getResponsesBySurveyRunId(id) |
---|
[527] | 127 | .handle({'-1': util.handleException}) |
---|
[525] | 128 | .handle(res.send.bind(res)); |
---|
| 129 | }); |
---|
| 130 | app.get('/:id/responses.csv', |
---|
| 131 | util.ensureMIME(util.CSV_MIME), |
---|
| 132 | function(req, res) { |
---|
| 133 | var id = req.params.id; |
---|
| 134 | responses.getResponsesBySurveyRunId(id) |
---|
| 135 | .handle({ |
---|
[527] | 136 | '-1': function(result) { |
---|
| 137 | util.handleException(result) |
---|
| 138 | .handle(res.send.bind(res)); |
---|
| 139 | }, |
---|
[525] | 140 | 200: function(responses) { |
---|
| 141 | var answers = _.map(responses,function(response){ |
---|
| 142 | return response.answers; |
---|
| 143 | }); |
---|
| 144 | res.set({ |
---|
| 145 | 'Content-Disposition': 'attachment; filename=surveyRun-'+id+'-responses.csv' |
---|
| 146 | }); |
---|
| 147 | res.status(200); |
---|
| 148 | util.writeObjectsToCSVStream(answers, res); |
---|
| 149 | res.end(); |
---|
| 150 | }, |
---|
| 151 | default: res.send.bind(res) |
---|
| 152 | }); |
---|
| 153 | }); |
---|
| 154 | return exports; |
---|
| 155 | }; |
---|