@eka-care/patient-ts-sdk
Version:
TypeScript SDK for Trinity Patient Profile Management System
188 lines (187 loc) • 5.52 kB
JavaScript
/**
* Patient search and lookup operations
*/
import { DataLoaderService } from '../services/data-loader';
import { IndexedDBService } from '../services/indexeddb';
/**
* Search and lookup methods
*/
export class SearchMethods {
constructor(client, config) {
this.basePath = '/profiles/v1/patient';
this.indexedDB = null;
this.dataLoader = null;
this.config = null;
this.isSyncComplete = false;
this.client = client;
this.config = config || null;
// Initialize local search components if enabled
if (config && config.workspaceId) {
this.indexedDB = new IndexedDBService(config.workspaceId);
this.dataLoader = new DataLoaderService(config);
}
}
/**
* Bulk get patients by OID list
*
* @param oidList Comma-separated list of patient OIDs
* @returns Array of patients
*
* @example
* ```typescript
* const patients = await sdk.search.bulkGet('oid1,oid2,oid3');
* // or
* const patients = await sdk.search.bulkGet(['oid1', 'oid2', 'oid3']);
* ```
*/
async bulkGet(oidList) {
const oidListParam = Array.isArray(oidList) ? oidList.join(',') : oidList;
const response = await this.client.get(`${this.basePath}/bulk`, {
oid_list: oidListParam
});
return response.data;
}
/**
* Get patients by mobile number
*
* @param mobile Mobile number to search for
* @returns Array of patients with matching mobile number
*
* @example
* ```typescript
* const patients = await sdk.search.getByMobile('1234567890');
* ```
*/
async getByMobile(mobile) {
const response = await this.client.get(`${this.basePath}/by-mobile`, {
mob: mobile
});
return response.data;
}
/**
* Search patients using various criteria
*
* @param params Search parameters
* @returns Array of matching patients
*
* @example
* ```typescript
* // Search by prefix (local search if available)
* const patients = await sdk.search.search({ prefix: 'jo', limit: 15 });
* ```
*/
async search(params, forceApiSearch = false) {
// Validate parameters
if (!params.prefix) {
throw new Error('prefix is required for search');
}
// Check if we should use local search
if (!forceApiSearch && this.indexedDB) {
if (!this.isSyncComplete) {
throw new Error('Data is still syncing. Please wait for sync to complete or use forceApiSearch=true');
}
try {
const localResults = await this.indexedDB.searchByPrefix(params.prefix, params.limit || 50);
return this.convertLocalToPatients(localResults);
}
catch (error) {
console.warn('Local search failed, falling back to API:', error);
}
}
// API search
const queryParams = {
prefix: params.prefix,
limit: params.limit || 50
};
if (params.select) {
queryParams.select = params.select;
}
const response = await this.client.get(`${this.basePath}/search`, queryParams);
return response.data;
}
/**
* Search by general prefix
*
* @param prefix Search prefix
* @param limit Maximum number of results (default: 50, max: 50)
* @param select Optional comma-separated list of fields to return
* @returns Array of matching patients
*/
// SEARCH PATIENT BY PREFIX
async searchByPrefix(prefix, limit = 50, select) {
return this.search({ prefix, limit, select });
}
/**
* Initialize local search functionality
*/
async initializeLocalSearch() {
if (this.indexedDB) {
await this.indexedDB.init();
}
if (this.dataLoader) {
await this.dataLoader.init();
}
}
/**
* Set sync completion status
*/
setSyncComplete(complete) {
this.isSyncComplete = complete;
}
/**
* Check if sync is complete
*/
isSyncCompleted() {
return this.isSyncComplete;
}
/**
* Check if local search data is available
*/
async hasLocalSearchData() {
if (!this.indexedDB) {
return false;
}
return await this.indexedDB.hasData();
}
/**
* Clear all local search data
*/
async clearLocalSearchData() {
if (this.dataLoader) {
await this.dataLoader.clearLocalData();
}
this.isSyncComplete = false;
}
/**
* Get data loader service for direct access
*/
getDataLoader() {
return this.dataLoader;
}
/**
* Convert local minified patients to full Patient objects
*/
convertLocalToPatients(localPatients) {
return localPatients.map(local => ({
oid: local.oid,
wid: this.config?.workspaceId || '',
ps: 'P',
u_ate: local.u_ate,
fln: local.fln,
mobile: local.mobile,
username: local.username
}));
}
/**
* Cleanup resources
*/
destroy() {
if (this.indexedDB) {
this.indexedDB.close();
}
if (this.dataLoader) {
this.dataLoader.close();
}
this.isSyncComplete = false;
}
}