Page MenuHomec4science

answers.coffee
No OneTemporary

File Metadata

Created
Tue, Jun 4, 04:52

answers.coffee

class @Answer
constructor: (doc) ->
_.extend this, doc
@Answers = new Meteor.Collection("answers",
transform: (doc) ->
new Answer(doc)
)
Answers.before.insert BeforeInsertTimestampHook
Answers.before.update BeforeUpdateTimestampHook
Meteor.methods
"upsertAnswer": (answer) ->
check(answer.visitId, String)
visit = Visits.findOne
_id: answer.visitId
throw new Meteor.Error(403, "visit can't be found.") unless visit?
patient = Patients.findOne
_id: 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
throw new Meteor.Error(403, "question can't be found.") unless question?
questionnaire = Questionnaires.findOne
_id: question.questionnaireId
throw new Meteor.Error(403, "questionnaire can't be found.") unless questionnaire?
#TODO check if questionnaire is scheduled at visit
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
answerId = answer._id
else
answer = _.pick answer, 'visitId', 'questionId', 'value'
answerId = Answers.insert answer
if !patient.hasData
Patients.update patient._id,
$set: hasData: true
return answerId

Event Timeline