Ignore:
Timestamp:
03/05/14 22:44:48 (11 years ago)
Author:
hendrikvanantwerpen
Message:

Completed migration to API, without CouchDB proxy.

Move to API is now completed. The full API is password protected, a very
limited API is exposed for respondents, which works with secrets that
are passed in URLs.

Serverside the HTTPResult class was introduced, which is similar to
Promises, but specifically for HTTP. It carries a status code and
response and makes it easier to extract parts of async handling in
separate functions.

Fixed a bug in our schema (it seems optional attributes don't exist but
a required list does). Verification of our schema by grunt-tv4 didn't
work yet. Our schema is organized the wrong way (this is fixable),
but the json-schema schema has problems with simple types and $refs.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • Dev/trunk/src/client/qed-client/response.js

    r477 r487  
    44    "./app/Path",
    55    "./lib/async",
    6     "./model/classes/Response",
    7     "./model/classes/SurveyRun",
     6    "./model/classes/responses",
     7    "./model/classes/surveyRuns",
    88    "./pages/response",
    9     "./store",
    109    "dojo/_base/json",
    1110    "dojo/date",
     
    1615    "./stddeps",
    1716    "dojo/domReady!"
    18 ], function(Content, Page, Path, async, Response, SurveyRun, ResponsePage, store, json, date, locale, hash, parser, request) {
     17], function(Content, Page, Path, async, responses, surveyRuns, ResponsePage, json, date, locale, hash, parser, request) {
    1918    parser.parse();
    2019    Content.startup();
     
    2625    }
    2726
    28     var path = new Path('/:surveyRunId');
     27    var path = new Path('/:type/:id');
    2928    var params = path.match(hash());
    3029    params.options = params.options || {};
    31 
    32     if ( !params || !params.surveyRunId ) {
     30   
     31    if ( params && params.type === 'surveyRuns' ) {
     32        var response = responses.create();
     33        response.surveyRunId = params.id;
     34        responses.postWithSecret(response,params.options.secret)
     35        .then(setContent,function(err){ error(err.error); });
     36    } else if ( params && params.type === 'responses' ) {
     37        responses.getWithSecret(params.id,params.options.secret)
     38        .then(setContent,function(err){ error(err.error); });
     39    } else {
    3340        error("Something is wrong with the URL, don't know what survey to show you. Sorry.");
    34         return;
    3541    }
    3642
    37     var surveyRunId = params.surveyRunId;
    38 
    39     function checkDates(surveyRun) {
    40         var now = new Date();
    41         var startDate = SurveyRun.StartDate.get(surveyRun);
    42         var endDate = SurveyRun.EndDate.get(surveyRun);
    43         if ( startDate && date.compare(startDate,now) > 0 ) {
    44             error("This survey will start on "+locale.format(startDate,'date'));
    45             throw false;
    46         }
    47         if ( endDate && date.compare(now,endDate) > 0 ) {
    48             error("This survey ended on "+locale.format(endDate,'date'));
    49             throw false;
    50         }
     43    function setContent(response) {
     44        hash(Path.format("/responses/"+responses.getId(response),
     45                         {secret:response.secret}));
     46        Content.set(new ResponsePage({
     47            response: response
     48        }));
    5149    }
    5250
    53     request.get('/api/surveyRuns/'+surveyRunId,{handleAs:'json'})
    54     .then(function(surveyRun){
    55         checkDates(surveyRun);
    56         if ( params.options.id ) {
    57             return params.options.id;
    58         } else {
    59             if ( surveyRun.mode === "open") {
    60                 var response = Response.create();
    61                 response.surveyRunId = surveyRunId;
    62                 return request.post('/api/responses',{
    63                     handleAs:'json',
    64                     data:json.toJson(response),
    65                     headers:{"Content-Type": "application/json"}
    66                 }).then(function(res){
    67                     return res.id;
    68                 });
    69             } else {
    70                 error("Cannot respond to closed survey without response id. Sorry.");
    71                 throw false;
    72             }
    73         }
    74         return surveyRun;
    75     },function(){
    76         error("No running survey found for the given id. Sorry.");
    77         throw false;
    78     })
    79     .then(function(responseId){
    80         hash(Path.format("/"+surveyRunId,{ id: responseId }));
    81         Content.set(new ResponsePage({
    82             surveyRunId: surveyRunId,
    83             options: {
    84                 id: responseId
    85             }
    86         }));
    87     });
    8851});
Note: See TracChangeset for help on using the changeset viewer.