Changeset 492 for Dev/trunk/src/server/app.js
- Timestamp:
- 03/09/14 14:23:42 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
Dev/trunk/src/server/app.js
r490 r492 8 8 , CouchDB = require('./util/couch').CouchDB 9 9 , _ = require("underscore") 10 , tv4 = require("tv4")10 , validator = require("./util/validator") 11 11 , HTTPResult = require("./util/http-result") 12 12 , etags = require("./util/etags") … … 151 151 return new HTTPResult(500,{error: error.reason}); 152 152 } 153 function handleUnknownError(error) {153 function handleUnknownError(error) { 154 154 return new HTTPResult(500, {error: "Unknown error", innerError: error}); 155 155 } … … 159 159 function handleRowDocs(result) { 160 160 return _.map(result.rows, function(item) { return item.doc; }); 161 } 162 function handleRowFirstDoc(result) { 163 if ( result.rows.length > 0 ) { 164 return result.rows[0].doc; 165 } else { 166 return new HTTPResult(404,{error:"No document found."}); 167 } 168 } 169 function handleRowFirstValue(result) { 170 if ( result.rows.length > 0 ) { 171 return result.rows[0].value; 172 } else { 173 return new HTTPResult(404,{error:"No document found."}); 174 } 175 } 176 function handleRowDictOfDocs(result) { 177 return _.reduce(result.rows, function(dict,item) { 178 dict[item.key] = item.doc; 179 return dict; 180 }, {}); 181 } 182 function handleRowDictOfValues(result) { 183 return _.reduce(result.rows, function(dict,item) { 184 dict[item.key] = item.value; 185 return dict; 186 }, {}); 161 187 } 162 188 … … 199 225 function putDocument(id,rev,type,doc) { 200 226 var priv = stripAndReturnPrivates(doc); 201 if ( doc.type === type && tv4.validateResult(doc, schema)) {227 if ( doc.type === type && validator(doc, schema).valid ) { 202 228 var opts = rev ? {headers:{'If-Match':'"'+rev+'"'}} : {}; 203 229 return HTTPResult.fromResponsePromise(couch.put(id,doc,opts).response, … … 236 262 function postDocument(type,doc) { 237 263 var priv = stripAndReturnPrivates(doc); 238 if ( doc.type === type && tv4.validateResult(doc, schema)) {264 if ( doc.type === type && validator(doc, schema).valid ) { 239 265 return HTTPResult.fromResponsePromise(couch.post(doc).response, 240 266 handleUnknownError) … … 321 347 .handle({ 322 348 200: handleRowDocs, 349 default: handleUnknownResponse 350 }); 351 } 352 function getQuestionsAndCodes() { 353 var url = '_design/questions/_view/by_code'; 354 var query = {include_docs:true}; 355 return HTTPResult.fromResponsePromise(couch.get(url,{query:query}).response, 356 handleUnknownError) 357 .handle({ 358 200: handleRowDictOfDocs, 323 359 default: handleUnknownResponse 324 360 }); … … 760 796 return new HTTPResult(403,{error: "Secrets are not the same."}); 761 797 } else { 762 return deleteDocument(id,rev,doc); 798 return getDocument(doc.surveyRunId,[],'SurveyRun') 799 .handle({ 800 200: function(surveyRun) { 801 if ( surveyRun.respondentCanDeleteOwnResponse === true ) { 802 return deleteDocument(id,rev,doc); 803 } else { 804 return new HTTPResult(403,{error:"Not allowed to delete response."}); 805 } 806 } 807 }); 763 808 } 764 809 }
Note: See TracChangeset
for help on using the changeset viewer.