source: Dev/trunk/src/server/api/surveyRuns.js @ 527

Last change on this file since 527 was 527, checked in by hendrikvanantwerpen, 11 years ago
  • Dropped request module for a simple one we wrote ourselves using the native Node modules. Hopefully this one will not suffer from the problem that sometimes empty bodies are returned even when the statuscode and content-length of the request are ok & present.
  • Handle exceptions better in HTTPResult chain, they were hoisted in unknown responses before. Should we not include them in default processing, because they are special?
File size: 4.0 KB
RevLine 
[525]1var 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
8module.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),
17        util.makeDocsGet('SurveyRun'));
18    app.post('/',
19        util.ensureMIME(util.JSON_MIME),
20        function(req,res) {
21            var doc = req.body;
22            var hr;
23            if ( !util.isDocPublished(doc.survey) ) {
24                hr = new HTTPResult(400,{error:"Cannot run unpublished survey."});
25            } else {
26                hr = cryptoken()
27                .then(function(token) {
28                    doc.secret = token;
29                    return util.postDocument('SurveyRun',doc);
30                });
31            }
[527]32            hr.handle({'-1': util.handleException})
33              .handle(res.send.bind(res));
[525]34        });
35    app.get('/:id',
36        util.ensureMIME(util.JSON_MIME),
37        util.makeDocGet_id('SurveyRun'));
38    app.put('/:id',
39        util.ensureMIME(util.JSON_MIME),
40        function (req,res) {
41            var id = req.params.id;
42            var doc = req.body;
43            var rev = etags.parse(req.header('If-Match'))[0] || (doc && doc._rev);
44            var hr;
45            if ( !util.isDocPublished(doc.survey) ) {
46                hr = new HTTPResult(400,{error:"Cannot run unpublished survey."});
47            } else if ( typeof doc.secret === 'undefined' ) {
48                hr = cryptoken()
49                .then(function(token) {
50                    doc.secret = token;
51                    return util.putDocument(id,rev,'SurveyRun',doc);
52                });
53            } else {
54                hr = util.putDocument(id,rev,'SurveyRun',doc);
55            }
[527]56            hr.handle({'-1': util.handleException})
57              .handle(res.send.bind(res));
[525]58        });
59    app.delete('/:id',
60        util.ensureMIME(util.JSON_MIME),
61        function(req,res) {
62            var id = req.params.id;
63            var doc = req.body;
64            var rev = etags.parse(req.header('If-Match'))[0] || (doc && doc._rev);
65            responses.getResponsesBySurveyRunId(id)
66            .handle({
67                200: function(responses) {
68                    if ( responses.length > 0 ) {
69                        return new HTTPResult(403,{error:"Cannot delete run that has responses."});
70                    } else {
71                        return util.deleteDocument(id,rev);
72                    }
73                }
[527]74            })
75            .handle({'-1': util.handleException})
76            .handle(res.send.bind(res));
[525]77        });
78    app.get('/:id/responses',
79        util.ensureMIME(util.JSON_MIME),
80        function(req,res) {
81            var id = req.params.id;
82            responses.getResponsesBySurveyRunId(id)
[527]83            .handle({'-1': util.handleException})
[525]84            .handle(res.send.bind(res));
85        });
86    app.get('/:id/responses.csv',
87        util.ensureMIME(util.CSV_MIME),
88        function(req, res) {
89            var id = req.params.id;
90            responses.getResponsesBySurveyRunId(id)
91            .handle({
[527]92                '-1': function(result) {
93                    util.handleException(result)
94                    .handle(res.send.bind(res));
95                },
[525]96                200: function(responses) {
97                    var answers = _.map(responses,function(response){
98                        return response.answers;
99                    });
100                    res.set({
101                        'Content-Disposition': 'attachment; filename=surveyRun-'+id+'-responses.csv'
102                    });
103                    res.status(200);
104                    util.writeObjectsToCSVStream(answers, res);
105                    res.end();
106                },
107                default: res.send.bind(res)
108            });
109        });
110    return exports;
111};
Note: See TracBrowser for help on using the repository browser.