-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(feat) : Add patient identifier validator
- Loading branch information
1 parent
50e4df0
commit afcf39e
Showing
10 changed files
with
225 additions
and
12 deletions.
There are no files selected for viewing
107 changes: 107 additions & 0 deletions
107
projects/ngx-formentry/src/form-entry/directives/patient-identifier.directive.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import { Directive, Input } from '@angular/core'; | ||
import { | ||
AbstractControl, | ||
AsyncValidator, | ||
NG_ASYNC_VALIDATORS, | ||
ValidationErrors | ||
} from '@angular/forms'; | ||
import { Observable, of } from 'rxjs'; | ||
import { | ||
catchError, | ||
debounceTime, | ||
distinctUntilChanged, | ||
map, | ||
switchMap | ||
} from 'rxjs/operators'; | ||
import { HttpClient, HttpParams } from '@angular/common/http'; | ||
import { LeafNode } from '../form-factory/form-node'; | ||
import { Identifier } from '../value-adapters/types'; | ||
import { Messages } from '../utils/messages'; | ||
|
||
@Directive({ | ||
selector: '[ofePatientIdentifierValidator]', | ||
providers: [ | ||
{ | ||
provide: NG_ASYNC_VALIDATORS, | ||
useExisting: PatientIdentifierValidatorDirective, | ||
multi: true | ||
} | ||
] | ||
}) | ||
export class PatientIdentifierValidatorDirective implements AsyncValidator { | ||
@Input('ofePatientIdentifierValidator') node: LeafNode; | ||
@Input() currentPatientId?: string; | ||
|
||
constructor(private http: HttpClient) {} | ||
|
||
validate( | ||
control: AbstractControl | ||
): Promise<ValidationErrors | null> | Observable<ValidationErrors | null> { | ||
const identifier = control.value; | ||
const currentPatientExistingIdentifier: Array<Identifier> = | ||
this.node.form.valueProcessingInfo?.patientIdentifiers ?? []; | ||
|
||
if ( | ||
currentPatientExistingIdentifier.some( | ||
(id) => id.identifier === identifier | ||
) | ||
) { | ||
// Disable the control, since the identifier is already assigned to the patient | ||
control.disable(); | ||
return of(null); | ||
} | ||
|
||
// If the identifier is less than 3 characters, no need to validate | ||
if (!identifier || identifier.length < 3) { | ||
return of(null); | ||
} | ||
|
||
return of(identifier).pipe( | ||
debounceTime(500), | ||
distinctUntilChanged(), | ||
switchMap((id) => this.validateIdentifier(id)), | ||
map((response) => { | ||
if ( | ||
response.isAssigned && | ||
response.patientId !== this.currentPatientId | ||
) { | ||
return { | ||
identifierTaken: { | ||
message: Messages.identifierTaken | ||
} | ||
}; | ||
} | ||
return null; | ||
}), | ||
catchError(() => | ||
of({ | ||
identifierError: { | ||
message: Messages.identifierError | ||
} | ||
}) | ||
) | ||
); | ||
} | ||
|
||
private validateIdentifier(identifier: string): Observable<any> { | ||
// For testing purposes, change openmrsBase to the OpenMRS server you are using | ||
const baseUrl = window?.['openmrsBase'] + '/ws/rest/v1' + '/'; | ||
const apiUrl = `${baseUrl}patient`; | ||
|
||
const params = new HttpParams().set('q', identifier); | ||
|
||
return this.http.get(apiUrl, { params }).pipe( | ||
map((response: any) => { | ||
const results = response.results || []; | ||
return { | ||
isAssigned: results.length > 0, | ||
patientId: results.length > 0 ? results[0].uuid : undefined | ||
}; | ||
}), | ||
catchError((error) => { | ||
console.error('Error searching for patient:', error); | ||
throw error; | ||
}) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
projects/ngx-formentry/src/form-entry/value-adapters/types/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export interface Identifier { | ||
uuid?: string; | ||
identifier: string; | ||
identifierType: OpenmrsResource; | ||
location: OpenmrsResource; | ||
} | ||
|
||
interface OpenmrsResource { | ||
display: string; | ||
uuid: string; | ||
links?: Array<{ rel: string; uri: string }>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters