File size: 2,877 Bytes
0936134
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
'use strict';

angular.module('rippleDemonstrator')
  .factory('PatientService', function ($http, Patient) {

    var all = function () {
      return $http.get('/api/patients').then(function (result) {
        var patients = [];

        angular.forEach(result.data, function (patient) {
          patient = new Patient(patient);
          patients.push(patient);
        });

        return patients;
      });
    };

    var get = function (id, source) {
      return $http.get('/api/patients/' + id + '?source=' + source).then(function (result) {
        return new Patient(result.data);
      });
    };

    var create = function (patient) {
      var sendPatient = {
        address1: patient.address1,
        address2: patient.address2,
        address3: patient.address3,
        address4: patient.address4,
        address5: patient.address5,
        born: patient.born,
        department: patient.department,
        firstname: patient.firstname,
        gPPractice: patient.gPPractice,
        gender: patient.gender,
        id: patient.id,
        lastname: patient.lastname,
        nhsNo: patient.nhsNo,
        phone: patient.phone,
        postCode: patient.postCode,
        title: patient.title
      };

      return $http.put('/api/patients', sendPatient);
    };

    var update = function (patient, updatedDiagnosis) {
      var diagnosis = _.findWhere(patient.diagnoses, {
        id: updatedDiagnosis.id
      });
      angular.extend(diagnosis, updatedDiagnosis);
    };

    var swapArrayElements = function (arr, indexA, indexB) {
      var temp = arr[indexA];
      arr[indexA] = arr[indexB];
      arr[indexB] = temp;
    };

    var summaries = function () {
      return all().then(function (patients) {
        var summaries = {};

        summaries.age = _.chain(patients)
          .filter(function (patient) {
            return !!patient.age;
          })
          .countBy(function (patient) {
            return patient.ageRange;
          })
          .map(function (value, key) {
            return {
              series: key,
              value: value
            };
          })

        .reverse()
          .value();

        swapArrayElements(summaries.age, 3, 4);

        summaries.department = _.chain(patients)
          .filter(function (patient) {
            return !!patient.department;
          })
          .countBy(function (patient) {
            return patient.department;
          })
          .map(function (value, key) {
            return {
              series: key,
              value: value
            };
          })
          .sortBy(function (value) {
            return value.department;
          })
          .value();

        return summaries;
      });
    };

    return {
      all: all,
      get: get,
      update: update,
      summaries: summaries,
      create: create
    };

  });