Skip to content
This repository has been archived by the owner on Jan 9, 2023. It is now read-only.

Commit

Permalink
feat(patients): escape string in appointments search (#2031)
Browse files Browse the repository at this point in the history
Fix #1997
Fix #2003
Fix #1999
Fix #2029
  • Loading branch information
JDarke committed May 2, 2020
1 parent 63f0706 commit 254273f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
17 changes: 16 additions & 1 deletion src/__tests__/clients/db/AppointmentRepository.test.ts
@@ -1,5 +1,5 @@
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 +24,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
@@ -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

1 comment on commit 254273f

@vercel
Copy link

@vercel vercel bot commented on 254273f May 2, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.