diff --git a/app/client/views/patients/patient_visit.coffee b/app/client/views/patients/patient_visit.coffee index bdad9ed..59bd085 100644 --- a/app/client/views/patients/patient_visit.coffee +++ b/app/client/views/patients/patient_visit.coffee @@ -1,77 +1,61 @@ AutoForm.hooks uploadPhysioRecordForm: onSubmit: (insertDoc, updateDoc, currentDoc) -> PhysioRecords.update insertDoc.physioRecordId, $set: 'metadata.visitId': currentDoc._id 'metadata.sensor': insertDoc.sensor 'metadata.deviceName': insertDoc.deviceName @done() false Template.patientVisit.helpers #this templateData visit: -> v = Visits.findOne(@visitId) if v? v.validatedDoc() else v - #this visit - canStart: -> - patient = Template.parentData().patient - !@endedAt? and !patient.runningVisitId? - #this visit showEmpaticaRecorder: -> @recordPhysicalData and Meteor.isCordova #this visit empaticaSessionId: -> @_id #this visit uploadFormSchema: -> schema = sensor: type: String label: "Sensor" deviceName: type: String label: "Device name" optional: true physioRecordId: type: String label: " " autoform: afFieldInput: type: 'fileUpload' collection: 'PhysioRecords' label: 'Choose file' new SimpleSchema(schema) -Template.patientVisit.events - "click .startVisit": (evt) -> - Meteor.call "startVisit", @_id, (error) -> - throwError error if error? - return - "click .stopVisit": (evt) -> - Meteor.call "stopVisit", @_id, (error) -> - throwError error if error? - return - - Template.questionnaireRow.helpers questionnaireCSS: -> return "valid" if @questionnaire.answered "invalid" Template.questionnaireRow.events #this: {questionnaire, visit, patient} "click .answerQuestionnaire": (evt, tmpl) -> if !@patient.runningVisitId? or @patient.runningVisitId isnt @visit._id alert("This visit must be running to answer it's questionnaires.") else Modal.show('questionnaireWizzard', @) false diff --git a/app/client/views/patients/patient_visit.html b/app/client/views/patients/patient_visit.html index 63e5bf3..0938bdc 100644 --- a/app/client/views/patients/patient_visit.html +++ b/app/client/views/patients/patient_visit.html @@ -1,97 +1,76 @@ diff --git a/app/client/views/patients/patient_visits.html b/app/client/views/patients/patient_visits.html index 3215b4c..85fbe1e 100644 --- a/app/client/views/patients/patient_visits.html +++ b/app/client/views/patients/patient_visits.html @@ -1,66 +1,63 @@ diff --git a/app/lib/collections/answers.coffee b/app/lib/collections/answers.coffee index 18f599b..92bcde7 100644 --- a/app/lib/collections/answers.coffee +++ b/app/lib/collections/answers.coffee @@ -1,56 +1,54 @@ 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 Answers.allow insert: (userId, doc) -> false update: (userId, doc, fieldNames, modifier) -> false remove: (userId, doc) -> false 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? - throw new Meteor.Error(403, "visit must be started first") unless visit.startedAt? - throw new Meteor.Error(403, "visit must be running, this visit ended already") unless !visit.endedAt? 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 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 answer._id else answer = _.pick answer, 'visitId', 'questionId', 'value' _id = Answers.insert answer _id diff --git a/app/lib/collections/visits.coffee b/app/lib/collections/visits.coffee index 3194fbe..9d36c6b 100644 --- a/app/lib/collections/visits.coffee +++ b/app/lib/collections/visits.coffee @@ -1,131 +1,74 @@ class @Visit constructor: (doc) -> _.extend this, doc study: -> return null unless @studyId? Studies.findOne _id: @studyId studyDesign: -> return null unless @studyDesignId? StudyDesigns.findOne _id: @studyDesignId - isRunning: -> - @startedAt? and !@endedAt? - isScheduled: -> - !@startedAt? and !@endedAt? - isFinished: -> - @startedAt? and @endedAt? - - statusText: -> - if @isFinished() - "#{fullDateTime(@startedAt)} - #{fullDateTime(@endedAt)}" - else if @isRunning() - "running since #{fullDateTime(@startedAt)}" - else if @isScheduled() - "scheduled" - questionnaires: -> qIds = @questionnaireIds or [] qs = Questionnaires.find _id: {$in: qIds} qs physioRecords: -> PhysioRecords.find 'metadata.visitId': @_id validatedDoc: -> valid = true physioValid = true if @recordPhysicalData? and @physioRecords().count() is 0 valid = false physioValid = false @physioValid = physioValid validatedQuestionnaires = @getValidatedQuestionnaires() if valid _.some validatedQuestionnaires, (quest) -> if quest.answered is false valid = false !valid @validatedQuestionnaires = validatedQuestionnaires @valid = valid @ getValidatedQuestionnaires: -> visit = @ quests = @questionnaires().map (quest) -> quest.answered = true quest.numAnswered = 0 questions = Questions.find questionnaireId: quest._id .map (question) -> answers = Answers.find visitId: visit._id questionId: question._id .fetch() question.answered = answers.length > 0 or question.type is "markdown" question.answers = answers if question.answered quest.numAnswered += 1 else quest.answered = false question quest.questions = questions quest.numQuestions = questions.length quest.answered = false if questions.length is 0 quest quests @Visits = new Meteor.Collection("visits", transform: (doc) -> new Visit(doc) ) Visits.before.insert BeforeInsertTimestampHook Visits.before.update BeforeUpdateTimestampHook - -Meteor.methods - startVisit: (visitId) -> - check(visitId, String) - visit = Visits.findOne 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) - - throw new Meteor.Error(403, "another visit is already running for this patient") if patient.runningVisitId? - - Visits.update visit._id, - $set: - startedAt: Date.now() - startedBy: Meteor.userId() - Patients.update patient._id, - $set: - runningVisitId: visit._id - - stopVisit: (visitId) -> - check(visitId, String) - visit = Visits.findOne 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) - - if !patient.runningVisitId? or patient.runningVisitId isnt visit._id - throw new Meteor.Error(403, "this visit isn't running") if patient.runningVisitId? - - Visits.update visit._id, - $set: - endedAt: Date.now() - endedBy: Meteor.userId() - Patients.update patient._id, - $unset: - runningVisitId: visit._id