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

Commit

Permalink
fix(patients): fixes search when using special characters (#2012)
Browse files Browse the repository at this point in the history
  • Loading branch information
JDarke committed Apr 30, 2020
1 parent 2fe9c73 commit 1693e5f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -10,6 +10,7 @@
"@types/pouchdb-find": "~6.3.4",
"bootstrap": "~4.4.1",
"date-fns": "~2.12.0",
"escape-string-regexp": "~4.0.0",
"i18next": "~19.4.0",
"i18next-browser-languagedetector": "~4.1.0",
"i18next-xhr-backend": "~3.2.2",
Expand Down
9 changes: 9 additions & 0 deletions src/__tests__/clients/db/PatientRepository.test.ts
Expand Up @@ -39,6 +39,15 @@ describe('patient repository', () => {
await removeAllDocs()
})

it('should escape all special chars from search text', async () => {
await patients.put({ _id: 'id9999', code: 'P00001', fullName: 'test -]?}(){*[\\$+.^test' })

const result = await PatientRepository.search('test -]?}(){*[\\$+.^test')

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

it('should return all records that patient code matches search text', async () => {
// same full name to prove that it is finding by patient code
const expectedPatientCode = 'P00001'
Expand Down
4 changes: 3 additions & 1 deletion src/clients/db/PatientRepository.ts
@@ -1,3 +1,4 @@
import escapeStringRegexp from 'escape-string-regexp'
import shortid from 'shortid'
import Patient from '../../model/Patient'
import Repository from './Repository'
Expand All @@ -13,12 +14,13 @@ export class PatientRepository extends Repository<Patient> {
}

async search(text: string): Promise<Patient[]> {
const escapedString = escapeStringRegexp(text)
return super.search({
selector: {
$or: [
{
fullName: {
$regex: RegExp(text, 'i'),
$regex: RegExp(escapedString, 'i'),
},
},
{
Expand Down
18 changes: 8 additions & 10 deletions src/components/Sidebar.tsx
Expand Up @@ -55,22 +55,20 @@ const Sidebar = () => {
cursor: 'pointer',
fontSize: 'small',
borderBottomWidth: 0,
color:
(splittedPath[1].includes('patients') || splittedPath[1].includes('appointments')) &&
splittedPath.length > 2
? 'white'
: 'black',
borderTopWidth: 0,
color: 'black',
padding: '.6rem 1.25rem',
backgroundColor: 'rgba(245,245,245,1)',
}

const listSubItemStyle: CSSProperties = {
cursor: 'pointer',
fontSize: 'small',
borderBottomWidth: 0,
color:
(splittedPath[1].includes('patients') || splittedPath[1].includes('appointments')) &&
splittedPath.length < 3
? 'white'
: 'black',
borderTopWidth: 0,
color: 'black',
padding: '.6rem 1.25rem',
backgroundColor: 'rgba(245,245,245,1)',
}

const getDashboardLink = () => (
Expand Down

1 comment on commit 1693e5f

@vercel
Copy link

@vercel vercel bot commented on 1693e5f Apr 30, 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.