Changeset 492 for Dev/trunk/src/server
- Timestamp:
- 03/09/14 14:23:42 (11 years ago)
- Location:
- Dev/trunk/src/server
- Files:
-
- 1 added
- 5 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 } -
Dev/trunk/src/server/config/check-couchdb.js
r487 r492 1 1 var Q = require('q') 2 2 , _ = require('underscore') 3 , tv4 = require('tv4')3 , validator = require('../util/validator') 4 4 , CouchDB = require('../util/couch').CouchDB 5 5 ; … … 10 10 var server = new CouchDB(couchServerURL,dbName); 11 11 var designRe = /^_design\//; 12 13 var codes = {}; 14 function codeUnique(code) { 15 if ( code in codes ) { 16 return false; 17 } else { 18 codes[code] = true; 19 return true; 20 } 21 } 22 12 23 return server.get('/_all_docs') 13 24 .then(function(allDocs){ … … 20 31 return server.get(doc.id) 21 32 .then(function(doc){ 22 var valid = tv4.validateResult(doc,schema); 33 var valid = validator(doc,schema); 34 if ( doc.type === "Question" && !codeUnique(doc.code) ) { 35 valid.valid = false; 36 valid.error = "Question code "+doc.code+" is not unique."; 37 } 23 38 result[doc._id] = valid; 24 39 return result; -
Dev/trunk/src/server/config/couchdb-design-docs.js
r487 r492 125 125 }, 126 126 127 "qed/_design/surveyRuns": { 128 __configAction: "replace", 129 _id: "_design/surveys", 130 language: "javascript", 131 views: { 132 by_dates: { 133 map: function(doc){ 134 if ( doc.type !== 'SurveyRun' ) { return; } 135 var startDate = doc.startDate || ""; 136 var endDate = doc.endDate || {}; 137 emit([startDate,endDate,doc.liveName||null],doc); 138 } 139 } 140 } 141 }, 142 127 143 "qed/_design/responses": { 128 144 __configAction: "replace", -
Dev/trunk/src/server/config/couchdb-schema.json
r487 r492 36 36 "_rev": { "type": "string" }, 37 37 "categories": { "type": "array", "items": { "type": "string" } }, 38 "code": { "type": "string" },38 "code": { "type": "string", "minLength": 1 }, 39 39 "content": { "type": "array", "items": { "$ref": "#/definitions/content/any" } }, 40 40 "description": { "type": "string" }, … … 52 52 "_id": { "type": "string" }, 53 53 "_rev": { "type": "string" }, 54 "description": { "type": "string" }, 54 55 "publicationDate": { "type": "string", "format": "datetime" }, 55 56 "questions": { "type": "array", "items": { "$ref": "#/definitions/docs/Question" } }, … … 67 68 "description": { "type": "string" }, 68 69 "endDate": { "type": "string", "format": "datetime" }, 70 "liveName": { "type": "string" }, 69 71 "mode": { "type": "string", "enum": [ "open", "closed" ] }, 70 "secret": { "type": "string", "minLength": 8 }, 72 "respondentCanDeleteOwnResponse": { "type": "boolean" }, 73 "secret": { "type": "string", "minLength": 1 }, 71 74 "startDate": { "type": "string", "format": "datetime" }, 72 75 "survey": { "$ref": "#/definitions/docs/Survey" }, … … 83 86 "_rev": { "type": "string" }, 84 87 "answers": { "type": "object" }, 88 "email": { "type": "string", "format": "email" }, 85 89 "publicationDate": { "type": "string", "format": "datetime" }, 86 "secret": { "type": "string", "minLength": 8},90 "secret": { "type": "string", "minLength": 1 }, 87 91 "surveyRunId": { "type": "string" } 88 92 }, -
Dev/trunk/src/server/config/upgrade-couchdb.js
r487 r492 1 1 var Q = require('q') 2 2 , _ = require('underscore') 3 , tv4 = require('tv4')4 3 , CouchDB = require('../util/couch').CouchDB 5 4 ;
Note: See TracChangeset
for help on using the changeset viewer.