@eka-care/patient-ts-sdk
Version:
TypeScript SDK for Trinity Patient Profile Management System
140 lines (139 loc) • 4.29 kB
JavaScript
/**
* Patient CRUD operations
*/
/**
* Patient CRUD methods
*/
export class PatientMethods {
constructor(client) {
this.basePath = '/profiles/v1/patient';
this.indexedDBUpdateCallback = null;
this.client = client;
}
/**
* Set callback for updating IndexedDB after patient operations
*/
setIndexedDBUpdateCallback(callback) {
this.indexedDBUpdateCallback = callback;
}
/**
* Create a new patient profile
*
* @param data Patient creation data
* @returns Created patient or just the OID if is_developer flag is set
*
* @example
* ```typescript
* const patient = await sdk.patients.create({
* oid: 'unique-id',
* wid: 'workspace-id',
* gen: 'M',
* dob: '1990-01-01',
* fn: 'John',
* ln: 'Doe',
* mobile: '1234567890',
* email: 'john@example.com'
* });
* ```
*/
async create(data) {
const response = await this.client.post(this.basePath, data);
// Update IndexedDB if callback is available and we have patient data
if (this.indexedDBUpdateCallback && typeof response.data === 'object' && 'oid' in response.data) {
try {
const localPatient = {
oid: response.data.oid,
u_ate: 'u_ate' in response.data ? response.data.u_ate : Date.now(),
fln: 'fln' in response.data ? response.data.fln : data.fln,
mobile: 'mobile' in response.data ? response.data.mobile : data.mobile,
username: 'username' in response.data ? response.data.username : data.username
};
await this.indexedDBUpdateCallback(localPatient);
}
catch (error) {
console.warn('Failed to update IndexedDB after patient creation:', error);
}
}
return response.data;
}
/**
* Get a patient by ID
*
* @param id Patient OID
* @returns Patient profile
*
* @example
* ```typescript
* const patient = await sdk.patients.get('patient-oid');
* ```
*/
async get(id) {
const response = await this.client.get(`${this.basePath}/${id}`);
return response.data;
}
/**
* Update a patient profile
*
* @param id Patient OID
* @param data Update data
* @returns Updated patient profile
*
* @example
* ```typescript
* const updated = await sdk.patients.update('patient-oid', {
* fn: 'Jane',
* email: 'jane@example.com'
* });
* ```
*/
async update(id, data) {
const response = await this.client.patch(`${this.basePath}/${id}`, data);
// Update IndexedDB if callback is available
if (this.indexedDBUpdateCallback && response.data) {
try {
const localPatient = {
oid: response.data.oid,
u_ate: 'u_ate' in response.data ? response.data.u_ate : Date.now(),
fln: response.data.fln,
mobile: response.data.mobile,
username: response.data.username
};
await this.indexedDBUpdateCallback(localPatient);
}
catch (error) {
console.warn('Failed to update IndexedDB after patient update:', error);
}
}
return response.data;
}
/**
* Delete a patient profile
*
* @param id Patient OID
* @returns Success response
*
* @example
* ```typescript
* await sdk.patients.delete('patient-oid');
* ```
*/
async delete(id) {
const response = await this.client.delete(`${this.basePath}/${id}`);
return response.data;
}
/**
* Get patients by username
*
* @param username Username to search for
* @returns Array of patients (typically one)
*
* @example
* ```typescript
* const patients = await sdk.patients.getByUsername('john.doe');
* ```
*/
async getByUsername(username) {
const response = await this.client.get(`${this.basePath}/${username}/username`);
return response.data;
}
}