/
weekly_check_in.js
187 lines (149 loc) · 6.47 KB
/
weekly_check_in.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
'use strict';
/**
* @ngdoc function
* @name livewellApp.controller:WeeklyCheckInCtrl
* @description
* # WeeklyCheckInCtrl
* Controller of the livewellApp
*/
angular.module('livewellApp').controller('WeeklyCheckInCtrl', function($scope, $location, $routeParams, Questions, Guid, UserDetails, Pound, Database) {
$scope.pageTitle = 'Weekly Check In';
var phq_questions = Questions.query('phq9');
var amrs_questions = Questions.query('amrs');
//combine questions into one group for the page
$scope.questionGroups = [phq_questions, amrs_questions];
//allows you to pass a question index url param into the question group directive
$scope.questionIndex = parseInt($routeParams.questionIndex) - 1 || 0;
$scope.skippable = false;
//overrides questiongroup default submit action to send data to PR
$scope.submit = function() {
var _SAVE_LOCATION = 'livewell_survey_data';
$scope.responseArray[$scope.currentIndex] = $('form').serializeArray()[0];
var responses = _.flatten($scope.responseArray);
var sessionID = Guid.create();
var clinicalStatus = JSON.parse(localStorage['clinicalStatus']);
_.each(responses, function(el) {
var payload = {
userId: UserDetails.find,
survey: $scope.pageTitle,
questionDataLabel: el.name,
questionValue: el.value,
sessionGUID: sessionID,
savedAt: new Date(),
clinicalStatus: clinicalStatus
};
(new PurpleRobot()).emitReading(_SAVE_LOCATION, payload).execute();
console.log(payload);
});
var responsePayload = {
sessionID: sessionID,
responses: responses
};
console.log('WEEKLY_PAYLOAD');
console.log(JSON.stringify(responsePayload, 2));
Pound.add('weeklyCheckIn', responsePayload);
var lWR = responses;
var phq8Sum = parseInt(lWR[0].value) + parseInt(lWR[1].value) + parseInt(lWR[2].value) + parseInt(lWR[3].value) + parseInt(lWR[4].value) + parseInt(lWR[5].value) + parseInt(lWR[6].value) + parseInt(lWR[7].value);
var amrsSum = parseInt(lWR[8].value) + parseInt(lWR[9].value) + parseInt(lWR[10].value) + parseInt(lWR[11].value) + parseInt(lWR[12].value);
if (_.where(JSON.parse(localStorage.team, { role: 'Psychiatrist' })[0] != undefined)) {
$scope.psychiatristEmail = _.where(JSON.parse(localStorage.team), {
role: 'Psychiatrist'
})[0].email;
} else {
$scope.psychiatristEmail = '';
}
if (_.where(JSON.parse(localStorage.team), { role: 'Coach' })[0] != undefined) {
$scope.coachEmail = _.where(JSON.parse(localStorage.team), {
role: 'Coach'
})[0].email;
} else {
$scope.coachEmail = ''
}
// Make new PR reading for weekly clinical reachout...
if (amrsSum >= 10) {
(new PurpleRobot()).emitReading('livewell_clinicalreachout', {
call: 'coach',
message: 'Altman Mania Rating Scale >= 10',
clinicalStatus: clinicalStatus
}).execute();
(new PurpleRobot()).emitReading('livewell_email', {
coachEmail: $scope.coachEmail,
message: 'Altman Mania Rating Scale >= 10',
clinicalStatus: clinicalStatus
}).execute();
}
if (amrsSum >= 16) {
(new PurpleRobot()).emitReading('livewell_clinicalreachout', {
call: 'psychiatrist',
message: 'Altman Mania Rating Scale >= 16',
clinicalStatus: clinicalStatus
}).execute();
(new PurpleRobot()).emitReading('livewell_email', {
psychiatristEmail: $scope.psychiatristEmail,
message: 'Altman Mania Rating Scale >= 16',
clinicalStatus: clinicalStatus
}).execute();
}
if (phq8Sum >= 15) {
(new PurpleRobot()).emitReading('livewell_clinicalreachout', {
call: 'coach',
message: 'PHQ8 >= 15',
clinicalStatus: clinicalStatus
}).execute();
(new PurpleRobot()).emitReading('livewell_email', {
coachEmail: $scope.coachEmail,
message: 'PHQ8 >= 15',
clinicalStatus: clinicalStatus
}).execute();
}
if (phq8Sum >= 20) {
(new PurpleRobot()).emitReading('livewell_clinicalreachout', {
call: 'psychiatrist',
message: 'PHQ8 >= 20',
clinicalStatus: clinicalStatus
}).execute();
(new PurpleRobot()).emitReading('livewell_email', {
psychiatristEmail: $scope.psychiatristEmail,
message: 'PHQ8 >= 20',
clinicalStatus: clinicalStatus
}).execute();
}
var dbObject = {
created: Date.now(),
sessionID: sessionID,
phq_composite: phq8Sum,
amrs_composite: amrsSum
};
for (var i = 0; i < responsePayload['responses'].length; i++) {
var response = responsePayload['responses'][i];
dbObject[response['name']] = parseInt(response['value']);
}
Database.insertWithCallback('weekly_check_in', dbObject, function() {
console.log('000001');
Database.updateWeeklyReachout(function() {
console.log('000002');
Database.filter('clinical_weekly_reachout', 'updated', IDBKeyRange.lowerBound(0), "prev", function(cursor) {
console.log("GOT MESSAGE");
$scope.reachoutMessage = null;
if (cursor != null) {
$scope.reachoutMessage = cursor.value.message
}
console.log("NEW WEEKLY REVIEW MESSAGE: " + $scope.reachoutMessage);
$scope.$apply();
if ($scope.reachoutMessage != null) {
$("#continue").modal();
$('#continue').on('hide.bs.modal', function (e) {
console.log('HIDE');
console.log($location);
$location.path("/ews");
$scope.$apply();
});
} else {
$location.path("/ews");
$scope.$apply();
}
});
});
});
}
});