diff --git a/app/lib/collections/answers.coffee b/app/lib/collections/answers.coffee index e2cd933..5e698f0 100644 --- a/app/lib/collections/answers.coffee +++ b/app/lib/collections/answers.coffee @@ -1,63 +1,58 @@ -class @Answer - constructor: (doc) -> - _.extend this, doc - -@Answers = new Meteor.Collection("answers", - transform: (doc) -> - new Answer(doc) -) +@Answers = new Meteor.Collection("answers") Answers.before.insert BeforeInsertTimestampHook Answers.before.update BeforeUpdateTimestampHook Meteor.methods "upsertAnswer": (answer) -> check(answer.visitId, String) - visit = Visits.findOne - _id: answer.visitId + visit = Visits.findOne answer.visitId throw new Meteor.Error(403, "visit can't be found.") unless visit? - patient = Patients.findOne - _id: visit.patientId + patient = Patients.findOne visit.patientId throw new Meteor.Error(403, "patient can't be found.") unless patient? throw new Meteor.Error(433, "you are not allowed to upsert answers") unless Roles.userIsInRole(@userId, ['admin']) or (Roles.userIsInRole(@userId, 'therapist') and patient.therapistId is @userId) check(answer.questionId, String) - question = Questions.findOne - _id: answer.questionId + question = Questions.findOne answer.questionId throw new Meteor.Error(403, "question can't be found.") unless question? - questionnaire = Questionnaires.findOne - _id: question.questionnaireId + questionnaire = Questionnaires.findOne question.questionnaireId throw new Meteor.Error(403, "questionnaire can't be found.") unless questionnaire? - #TODO check if questionnaire is scheduled at visit + #check if questionnaire is scheduled at visit + found = false + visit.questionnaireIds.some (questionnaireId) -> + if questionnaireId is questionnaire._id + found = true + found + throw new Meteor.Error(403, "questionnaire is not scheduled at this visit.") unless found + answerId = null if answer._id? a = Answers.findOne _.pick answer, 'visitId', 'questionId', '_id' throw new Meteor.Error(403, "answer to update can't be found.") unless answer? Answers.update answer._id, - $set: - value: answer.value + $set: value: answer.value answerId = answer._id else #check if an answer exists already - a = Answers.findOne _.pick answer, 'visitId', 'questionId' + a = Answers.findOne _.pick(answer, 'visitId', 'questionId') if a? console.log "\n\nError: There already exists an answer for this visitId and questionId." console.log a throw new Meteor.Error(403, "Error: There already exists an answer for this visitId and questionId.") answer = _.pick answer, 'visitId', 'questionId', 'value' answerId = Answers.insert answer if !patient.hasData Patients.update patient._id, $set: hasData: true if !visit.date? Visits.update visit._id, $set: date: Date.now() return answerId