Skip to content

Commit

Permalink
fix(src/clients/db/appointmentrepository.ts): escape regex search chars
Browse files Browse the repository at this point in the history
Added escapeStringRegex to AppointmentRepository, and associated test suite in
src/__tests__/clients/db/AppointmentRepository.test.ts.  This escapes regex chars included in the
search field in patient info > appointments area

fix HospitalRun#2029
  • Loading branch information
JDarke committed May 1, 2020
1 parent d8be269 commit 8b2c863
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"@testing-library/react-hooks": "~3.2.1",
"@types/enzyme": "^3.10.5",
"@types/jest": "~25.2.0",
"@types/lodash": "^4.14.150",
"@types/lodash": "~4.14.150",
"@types/node": "~13.13.0",
"@types/pouchdb": "~6.4.0",
"@types/react": "~16.9.17",
Expand Down
18 changes: 17 additions & 1 deletion src/__tests__/clients/db/AppointmentRepository.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import AppointmentRepository from 'clients/db/AppointmentRepository'
import { appointments } from 'config/pouchdb'
import { appointments, patients } from 'config/pouchdb'

import Appointment from 'model/Appointment'

const uuidV4Regex = /^[A-F\d]{8}-[A-F\d]{4}-4[A-F\d]{3}-[89AB][A-F\d]{3}-[A-F\d]{12}$/i
Expand All @@ -24,6 +25,21 @@ describe('Appointment Repository', () => {
})
})

describe('searchPatientAppointments', () => {
it('should escape all special chars from search text', async () => {
await patients.put({ _id: 'id2222' })
await appointments.put({ _id: 'id3333', patientId: 'id2222', location: 'id-]?}(){*[$+.^\\' })

const result = await AppointmentRepository.searchPatientAppointments(
'id2222',
'id-]?}(){*[$+.^\\',
)

expect(result).toHaveLength(1)
expect(result[0].id).toEqual('id3333')
})
})

describe('save', () => {
it('should create an id that is a uuid', async () => {
const newAppointment = await AppointmentRepository.save({
Expand Down
8 changes: 5 additions & 3 deletions src/clients/db/AppointmentRepository.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import escapeStringRegexp from 'escape-string-regexp'
import Appointment from 'model/Appointment'
import { appointments } from 'config/pouchdb'
import Repository from './Repository'
Expand All @@ -9,6 +10,7 @@ export class AppointmentRepository extends Repository<Appointment> {

// Fuzzy search for patient appointments. Used for patient appointment search bar
async searchPatientAppointments(patientId: string, text: string): Promise<Appointment[]> {
const escapedString = escapeStringRegexp(text)
return super.search({
selector: {
$and: [
Expand All @@ -19,17 +21,17 @@ export class AppointmentRepository extends Repository<Appointment> {
$or: [
{
location: {
$regex: RegExp(text, 'i'),
$regex: RegExp(escapedString, 'i'),
},
},
{
reason: {
$regex: RegExp(text, 'i'),
$regex: RegExp(escapedString, 'i'),
},
},
{
type: {
$regex: RegExp(text, 'i'),
$regex: RegExp(escapedString, 'i'),
},
},
],
Expand Down

0 comments on commit 8b2c863

Please sign in to comment.