1
1
import { patients } from 'config/pouchdb'
2
2
import PatientRepository from 'clients/db/PatientRepository'
3
3
import Patient from 'model/Patient'
4
- import { fromUnixTime } from 'date-fns'
4
+ import shortid from 'shortid'
5
+ import { getTime , isAfter } from 'date-fns'
6
+
7
+ const uuidV4Regex = / ^ [ A - F \d ] { 8 } - [ A - F \d ] { 4 } - 4 [ A - F \d ] { 3 } - [ 8 9 A B ] [ A - F \d ] { 3 } - [ A - F \d ] { 12 } $ / i
5
8
6
9
async function removeAllDocs ( ) {
7
10
// eslint-disable-next-line
@@ -36,22 +39,22 @@ describe('patient repository', () => {
36
39
await removeAllDocs ( )
37
40
} )
38
41
39
- it ( 'should return all records that friendly ids match search text' , async ( ) => {
40
- // same full name to prove that it is finding by friendly id
41
- const expectedFriendlyId = 'P00001'
42
- await patients . put ( { _id : 'someId1' , friendlyId : expectedFriendlyId , fullName : 'test test' } )
43
- await patients . put ( { _id : 'someId2' , friendlyId : 'P00002' , fullName : 'test test' } )
42
+ it ( 'should return all records that patient code matches search text' , async ( ) => {
43
+ // same full name to prove that it is finding by patient code
44
+ const expectedPatientCode = 'P00001'
45
+ await patients . put ( { _id : 'someId1' , code : expectedPatientCode , fullName : 'test test' } )
46
+ await patients . put ( { _id : 'someId2' , code : 'P00002' , fullName : 'test test' } )
44
47
45
- const result = await PatientRepository . search ( expectedFriendlyId )
48
+ const result = await PatientRepository . search ( expectedPatientCode )
46
49
47
50
expect ( result ) . toHaveLength ( 1 )
48
- expect ( result [ 0 ] . friendlyId ) . toEqual ( expectedFriendlyId )
51
+ expect ( result [ 0 ] . code ) . toEqual ( expectedPatientCode )
49
52
} )
50
53
51
54
it ( 'should return all records that fullName contains search text' , async ( ) => {
52
- await patients . put ( { _id : 'id3333' , friendlyId : 'P00002' , fullName : 'blh test test blah' } )
53
- await patients . put ( { _id : 'id4444' , friendlyId : 'P00001' , fullName : 'test test' } )
54
- await patients . put ( { _id : 'id5555' , friendlyId : 'P00003' , fullName : 'not found' } )
55
+ await patients . put ( { _id : 'id3333' , code : 'P00002' , fullName : 'blh test test blah' } )
56
+ await patients . put ( { _id : 'id4444' , code : 'P00001' , fullName : 'test test' } )
57
+ await patients . put ( { _id : 'id5555' , code : 'P00003' , fullName : 'not found' } )
55
58
56
59
const result = await PatientRepository . search ( 'test test' )
57
60
@@ -61,8 +64,8 @@ describe('patient repository', () => {
61
64
} )
62
65
63
66
it ( 'should match search criteria with case insensitive match' , async ( ) => {
64
- await patients . put ( { _id : 'id6666' , friendlyId : 'P00001' , fullName : 'test test' } )
65
- await patients . put ( { _id : 'id7777' , friendlyId : 'P00002' , fullName : 'not found' } )
67
+ await patients . put ( { _id : 'id6666' , code : 'P00001' , fullName : 'test test' } )
68
+ await patients . put ( { _id : 'id7777' , code : 'P00002' , fullName : 'not found' } )
66
69
67
70
const result = await PatientRepository . search ( 'TEST TEST' )
68
71
@@ -92,30 +95,41 @@ describe('patient repository', () => {
92
95
await removeAllDocs ( )
93
96
} )
94
97
95
- it ( 'should generate an id that is a timestamp for the patient' , async ( ) => {
98
+ it ( 'should generate an id that is a uuid for the patient' , async ( ) => {
96
99
const newPatient = await PatientRepository . save ( {
97
100
fullName : 'test test' ,
98
101
} as Patient )
99
102
100
- expect ( fromUnixTime ( parseInt ( newPatient . id , 10 ) ) . getTime ( ) > 0 ) . toBeTruthy ( )
103
+ expect ( uuidV4Regex . test ( newPatient . id ) ) . toBeTruthy ( )
101
104
} )
102
105
103
- it ( 'should generate a friendly id ' , async ( ) => {
106
+ it ( 'should generate a patient code ' , async ( ) => {
104
107
const newPatient = await PatientRepository . save ( {
105
108
fullName : 'test1 test1' ,
106
109
} as Patient )
107
110
108
- expect ( newPatient . friendlyId ) . toEqual ( 'P00001' )
111
+ expect ( shortid . isValid ( newPatient . code ) ) . toBeTruthy ( )
109
112
} )
110
113
111
- it ( 'should sequentially generate a friendly id' , async ( ) => {
112
- await patients . put ( { _id : 'id9999' , friendlyId : 'P00001' } )
114
+ it ( 'should generate a timestamp for created date and last updated date' , async ( ) => {
115
+ const newPatient = await PatientRepository . save ( {
116
+ fullName : 'test1 test1' ,
117
+ } as Patient )
118
+
119
+ expect ( newPatient . createdAt ) . toBeDefined ( )
120
+ expect ( newPatient . updatedAt ) . toBeDefined ( )
121
+ } )
113
122
123
+ it ( 'should override the created date and last updated date even if one was passed in' , async ( ) => {
124
+ const unexpectedTime = new Date ( 2020 , 2 , 1 ) . toISOString ( )
114
125
const newPatient = await PatientRepository . save ( {
115
- fullName : 'test3 test3' ,
126
+ fullName : 'test1 test1' ,
127
+ createdAt : unexpectedTime ,
128
+ updatedAt : unexpectedTime ,
116
129
} as Patient )
117
130
118
- expect ( newPatient . friendlyId ) . toEqual ( 'P00002' )
131
+ expect ( newPatient . createdAt ) . not . toEqual ( unexpectedTime )
132
+ expect ( newPatient . updatedAt ) . not . toEqual ( unexpectedTime )
119
133
} )
120
134
} )
121
135
@@ -164,6 +178,28 @@ describe('patient repository', () => {
164
178
expect ( updatedPatient . fullName ) . toEqual ( existingPatient . fullName )
165
179
expect ( updatedPatient . givenName ) . toEqual ( 'givenName' )
166
180
} )
181
+
182
+ it ( 'should update the last updated date' , async ( ) => {
183
+ const time = new Date ( 2020 , 1 , 1 ) . toISOString ( )
184
+ await patients . put ( { _id : 'id2222222' , createdAt : time , updatedAt : time } )
185
+ const existingPatient = await PatientRepository . find ( 'id2222222' )
186
+
187
+ const updatedPatient = await PatientRepository . saveOrUpdate ( existingPatient )
188
+
189
+ expect (
190
+ isAfter ( new Date ( updatedPatient . updatedAt ) , new Date ( updatedPatient . createdAt ) ) ,
191
+ ) . toBeTruthy ( )
192
+ expect ( updatedPatient . updatedAt ) . not . toEqual ( existingPatient . updatedAt )
193
+ } )
194
+
195
+ it ( 'should not update the created date' , async ( ) => {
196
+ const time = getTime ( new Date ( 2020 , 1 , 1 ) )
197
+ await patients . put ( { _id : 'id111111' , createdAt : time , updatedAt : time } )
198
+ const existingPatient = await PatientRepository . find ( 'id111111' )
199
+ const updatedPatient = await PatientRepository . saveOrUpdate ( existingPatient )
200
+
201
+ expect ( updatedPatient . createdAt ) . toEqual ( existingPatient . createdAt )
202
+ } )
167
203
} )
168
204
169
205
describe ( 'delete' , ( ) => {
0 commit comments