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

Commit

Permalink
feat(viewpatients): add a new field 'index', paging in next direction
Browse files Browse the repository at this point in the history
New 'index' field is a combination of 'fullName' and 'code' so that uniqueness is guaranteed and
paging logic can relay on this field. A new field in PageRequest to store a reference field for
getting next page.

feat #1969, feat #1967
  • Loading branch information
akshay-ap committed May 3, 2020
1 parent 8727f38 commit d1c55e7
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 19 deletions.
4 changes: 4 additions & 0 deletions src/clients/db/PageRequest.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
export default interface PageRequest {
number: number | undefined
size: number | undefined
nextPageInfo: { [key: string]: string | null } | undefined
direction: 'previous' | 'next' | null
}
export const UnpagedRequest: PageRequest = {
number: undefined,
size: undefined,
nextPageInfo: undefined,
direction: null,
}
3 changes: 2 additions & 1 deletion src/clients/db/PatientRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class PatientRepository extends Repository<Patient> {
constructor() {
super(patients)
patients.createIndex({
index: { fields: ['fullName', 'code'] },
index: { fields: ['index'] },
})
}

Expand Down Expand Up @@ -86,6 +86,7 @@ export class PatientRepository extends Repository<Patient> {
async save(entity: Patient): Promise<Patient> {
const patientCode = getPatientCode()
entity.code = patientCode
entity.index = (entity.fullName ? entity.fullName : '') + patientCode
return super.save(entity)
}
}
Expand Down
52 changes: 42 additions & 10 deletions src/clients/db/Repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,27 +47,59 @@ export default class Repository<T extends AbstractDBModel> {
const selector: any = {
_id: { $gt: null },
}

sort.sorts.forEach((s) => {
selector[s.field] = { $gt: null }
})
if (pageRequest.direction === 'next') {
sort.sorts.forEach((s) => {
selector[s.field] = {
$gte:
pageRequest.nextPageInfo && pageRequest.nextPageInfo[s.field]
? pageRequest.nextPageInfo[s.field]
: null,
}
})
} else if (pageRequest.direction === 'previous') {
sort.sorts.forEach((s) => {
s.direction = s.direction === 'asc' ? 'desc' : 'asc'
selector[s.field] = {
$lte:
pageRequest.nextPageInfo && pageRequest.nextPageInfo[s.field]
? pageRequest.nextPageInfo[s.field]
: null,
}
})
}

const result = await this.db.find({
selector,
sort: sort.sorts.length > 0 ? sort.sorts.map((s) => ({ [s.field]: s.direction })) : undefined,
limit: pageRequest.size,
skip:
pageRequest.number && pageRequest.size ? (pageRequest.number - 1) * pageRequest.size : 0,
limit: pageRequest.size ? pageRequest.size + 1 : undefined,
skip: pageRequest.direction === 'previous' ? pageRequest.size : undefined,
})
const mappedResult = result.docs.map(mapDocument)

if (pageRequest.direction === 'previous') {
mappedResult.reverse()
}
const nextPageInfo: { [key: string]: string } = {}

if (mappedResult.length > 0) {
sort.sorts.forEach((s) => {
nextPageInfo[s.field] = mappedResult[mappedResult.length - 1][s.field]
})
}

const pagedResult: Page<T> = {
content: mappedResult,
hasNext: pageRequest.size !== undefined && mappedResult.length === pageRequest.size,
hasPrevious: pageRequest.number !== undefined && pageRequest.number > 1,
content:
pageRequest.size !== undefined && mappedResult.length === pageRequest.size + 1
? mappedResult.slice(0, mappedResult.length - 1)
: mappedResult,
hasNext: pageRequest.size !== undefined && mappedResult.length === pageRequest.size + 1,
// hasPrevious: pageRequest.number !== undefined && pageRequest.number > 1,
hasPrevious: false,
pageRequest: {
size: pageRequest.size,
number: pageRequest.number,
nextPageInfo,
direction: null,
},
}
return pagedResult
Expand Down
1 change: 1 addition & 0 deletions src/model/Patient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ export default interface Patient extends AbstractDBModel, Name, ContactInformati
allergies?: Allergy[]
diagnoses?: Diagnosis[]
notes?: Note[]
index: string
}
2 changes: 2 additions & 0 deletions src/patients/edit/EditPatient.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ const EditPatient = () => {
{
...patient,
fullName: getPatientName(patient.givenName, patient.familyName, patient.suffix),
index:
getPatientName(patient.givenName, patient.familyName, patient.suffix) + patient.code,
},
onSuccessfulSave,
),
Expand Down
16 changes: 8 additions & 8 deletions src/patients/list/ViewPatients.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ const ViewPatients = () => {
const [userPageRequest, setUserPageRequest] = useState<PageRequest>({
size: 1,
number: 1,
nextPageInfo: { index: null },
direction: 'next',
})

const setNextPageRequest = () => {
Expand All @@ -36,6 +38,8 @@ const ViewPatients = () => {
const newPageRequest: PageRequest = {
number: p.number + 1,
size: p.size,
nextPageInfo: patients.pageRequest?.nextPageInfo,
direction: 'next',
}
return newPageRequest
}
Expand All @@ -49,6 +53,8 @@ const ViewPatients = () => {
return {
number: p.number - 1,
size: p.size,
nextPageInfo: patients.pageRequest?.nextPageInfo,
direction: 'previous',
}
}
return p
Expand All @@ -60,20 +66,14 @@ const ViewPatients = () => {

useEffect(() => {
const sortRequest: SortRequest = {
sorts: [
{ field: 'fullName', direction: 'asc' },
{ field: 'code', direction: 'asc' },
],
sorts: [{ field: 'index', direction: 'asc' }],
}
dispatch(searchPatients(debouncedSearchText, sortRequest, userPageRequest))
}, [dispatch, debouncedSearchText, userPageRequest])

useEffect(() => {
const sortRequest: SortRequest = {
sorts: [
{ field: 'fullName', direction: 'asc' },
{ field: 'code', direction: 'asc' },
],
sorts: [{ field: 'index', direction: 'asc' }],
}
dispatch(fetchPatients(sortRequest, userPageRequest))

Expand Down

0 comments on commit d1c55e7

Please sign in to comment.