CareStack EHR Node.js SDK Documentation
    Preparing search index...

    Service class for handling patient-related operations such as retrieving, creating, updating, and deleting patients.

    The service provides methods for:

    • Retrieving all patients
    • Fetching a patient by ID
    • Checking patient existence
    • Creating a new patient
    • Updating an existing patient
    • Searching for patients using filters
    • Deleting a patient

    Hierarchy

    • BaseService
      • PatientService
    Index

    Constructors

    • Parameters

      • config: ClientConfig

      Returns PatientService

    Accessors

    Methods

    • Creates a new patient record in the system.

      Parameters

      • patient: PatientDTO

        Object containing patient details:

        Fields:

        • idNumber - Unique patient identification number - mandatory
        • idType - Type of identification (e.g., Aadhaar, Passport) - mandatory
        • abhaAddress - ABHA address linked to the patient - mandatory
        • patientType - Category/type of patient - mandatory
        • firstName - Patient's first name (3–20 characters, only alphabets/spaces/dots allowed) - mandatory
        • middleName - Patient's middle name (optional, alphabets/spaces/dots allowed) - optional
        • lastName - Patient's last name (3–20 characters, only alphabets/spaces/dots allowed) - mandatory
        • birthDate - Date of birth in ISO format (YYYY-MM-DD) - mandatory
        • gender - Gender of the patient (Male/Female/Other) - mandatory
        • mobileNumber - Valid mobile number (should not contain masked characters like *) - mandatory
        • emailId - Patient’s valid email address - mandatory
        • address - Residential address (5–50 characters) - mandatory
        • pincode - 6-digit postal code - mandatory
        • state - State or union territory (from predefined enum list) - mandatory
        • wantsToLinkWhatsapp - Whether the patient wants to link WhatsApp number - optional
        • photo - Base64 or URL string of the patient’s photo - optional
        • resourceType - Type of FHIR resource (usually "Patient") - mandatory
        • resourceId - Existing resource ID if updating - optional

      Returns Promise<CreateUpdatePatientResponse>

      • Returns an object containing:
      • type : Type of the response (e.g., "Patient")
      • message : Descriptive message or status of the operation
      • resourceId : Unique identifier of the updated patient resource
      • validationErrors : Array of validation errors (if any)
      • resource : Full updated patient object

      If the input data is invalid.

      If the API request fails.

      // Input:
      const data = await patient.create({
      idNumber: "123456789012",
      idType: PatientIdTypeEnum.AADHAAR,
      abhaAddress: "abha55578@example.com",
      patientType: PatientTypeEnum.REGULAR,
      firstName: "John",
      lastName: "Doe",
      birthDate: "1990-01-01",
      gender: 'male',
      mobileNumber: "+911234563291",
      emailId: "john@example.com",
      address: "123 Main St, Anytown, Anystate",
      pincode: "560001",
      state: StatesAndUnionTerritories.KARNATAKA,
      resourceType: "Patient"
      });
      console.log(data);


      // Expected Output:
      {
      message: 'Successfully created the Patient profile',
      type: 'Patient',
      resourceId: '791708f6-de6d-439e-bece-041e5d64e463',
      validationErrors: [],
      fhirProfileId: '',
      resource: {
      firstName: 'John',
      lastName: 'Doe',
      birthDate: '1990-01-01',
      gender: 'male',
      mobileNumber: '*********3291',
      emailId: 'john@example.com',
      address: '123 Main St, Anytown, Anystate, 12345',
      pincode: '560001',
      resourceType: 'Patient',
      resourceId: '791708f6-de6d-439e-bece-041e5d64e463',
      organizationId: 'ACHALA_HEALTH_01',
      state: 'Andhra Pradesh',
      idNumber: '1234567000',
      idType: 'ABHA',
      photo: '',
      wantsToLinkWhatsapp: true,
      abhaAddress: 'abha55578@example.com',
      patientType: 'OLD',
      lastUpdated: '2025-07-31T11:12:08.014760704Z'
      }
      }
    • Delete a patient

      Parameters

      • id: string

        Patient's unique identifier

      Returns Promise<any>

    • Checks whether a patient exists in the system using their Resource ID.

      Parameters

      • id: string

        Unique identifier (Resource ID) of the patient to check - mandatory

      Returns Promise<boolean>

      A promise that resolves to:

      • true if the patient exists

      If the id is null, undefined, or an empty string.

      If the API request fails or an unexpected error occurs.

      // Input:
      const doesExist = await patient.exists("123e4567-e89b-12d3-a456-426614174000");
      console.log(doesExist);


      // Expected Output:
      true
    • Retrieves a paginated list of all patients.

      Parameters

      • OptionalpageSize: number

        Number of patients to fetch in a single page - optional (Only effective in the first request. Default is 10 if not provided)

      • OptionalnextPage: string

        Cursor to fetch the next set of patients - optional (Use the value from the previous response's nextPage for pagination)

      Returns Promise<GetPatientResponse>

      • Returns an object containing:
      • type: Type of the response (e.g., "success", "error")
      • message: Descriptive message or status of the operation
      • requestResource: Actual patient data or list of patients
      • totalNumberOfRecords: Total number of patients in the system
      • nextPageLink: Token to retrieve the next page of results, if more records exist

      Throws an error if the request fails or response is invalid.

      // Input:
      const patients = await patient.findAll(20);
      const morePatients = await patient.findAll(undefined, patients.nextPage);

      // Expected Output:
      {
      data: {
      Entry: [
      {
      firstName: 'arya',
      lastName: 'bunny',
      // ... other fields
      },
      {
      firstName: 'Neha',
      lastName: 'Verma',
      // ... other fields
      }
      // ... more entries
      ],
      link: {
      nextPage: 'X2NvdW50PTEwJl9zb3J0PS1fb'
      },
      total: 900
      }
      }
    • Retrieves a list of patients using a combination of optional filter criteria.

      Parameters

      • filters: PatientFiltersDTO

        Filters to apply for fetching patients.

        Fields:

        • firstName - Patient's first name (3–20 characters, alphabets/spaces/dots only) - optional
        • lastName - Patient's last name (3–20 characters, alphabets/spaces/dots only) - optional
        • email - Patient’s email address (must be a valid format) - optional
        • birthDate - Patient's date of birth in ISO format (YYYY-MM-DD) - optional
        • gender - Gender of the patient (Male/Female/Other) - optional
        • phone - Patient’s phone number (must be valid and unmasked) - optional
        • state - Patient’s state or union territory - optional
        • pincode - 6-digit postal code - optional
        • organizationId - ID of the organization associated with the patient - optional
        • pageSize - Number of records to fetch per page (defaults to 10 if not specified) - optional
        • identifier - Unique patient identifier (e.g., ABHA ID, ABHA Address) - optional
        • fromDate - Start date (inclusive) for filtering based on last updated records - optional
        • toDate - End date (inclusive) for filtering based on last updated records - optional

        Note:

        • All filter fields are optional, but at least one should ideally be provided to avoid fetching unfiltered results.
        • fromDate and toDate should be valid ISO date strings and are used together to define a last-updated range.
        • nextPage token is used for pagination and is effective only on subsequent requests (not the first one). Note: All filter parameters are only effective for the first request. Subsequent requests using the nextPage parameter will return the records based on filters as specified in the initial request.
      • OptionalnextPage: string

        Token for retrieving the next page of results from the previous response.

      Returns Promise<GetPatientResponse>

      • Returns an object containing:
        • type: Type of the response (e.g., "Patient")
        • message: Descriptive message or status of the operation
        • requestResource: Patient data of the requested resource ID
        • totalNumberOfRecords: Total number of matched patients

      If any filter value is invalid.

      If the API request fails.

      // Input:
      const filters = {
      identifier: "joelpauljoel@sbx",
      state : "Telangana",
      fromDate": "2025-03-04T13:33:51.397512770Z",
      toDate": "2025-03-05T13:33:51.397512770Z",
      }
      const response = await patient.findByFilters(filters);
      console.log(response)


      // Expected Output:
      {
      data: {
      Entry: [
      {
      firstName: 'Joel',
      middleName: 'Paul',
      lastName: 'Jeripothula',
      abhaAddress: 'joelpauljoel@sbx',
      state: 'Telangana'
      // ... other fields
      },
      {
      firstName: 'Neha',
      lastName: 'Verma',
      abhaAddress: 'joelpauljoel@sbx',
      state: 'Telangana'
      // ... other fields
      }
      // ... more entries
      ],
      total: 2
      }
      }
    • Fetches the details of a patient using Resource ID.

      Parameters

      • id: string

        Unique identifier (Resource ID) of the patient to fetch - mandatory

      Returns Promise<GetPatientResponse>

      • Returns an object containing:
      • type: Type of the response (e.g., "Patient")
      • message: Descriptive message or status of the operation
      • requestResource: Patient data of the requested resource ID
      • totalNumberOfRecords: Total number of matched patients

      If the id is null, undefined, or empty.

      If the API request fails.

      // Input:
      const patient = await patient.findById("123e4567-e89b-12d3-a456-426614174000");
      console.log(patient);


      // Expected Output:
      {
      type: 'Patient',
      message: 'Patient Found !!!',
      requestResource: {
      firstName: 'Sreenivasa',
      birthDate: '2025-07-23',
      gender: 'male',
      mobileNumber: '*********0000',
      emailId: '',
      address: 'DUMMY_ADDRESS',
      pincode: '123456',
      resourceType: 'Patient',
      resourceId: '123e4567-e89b-12d3-a456-426614174000',
      organizationId: 'ACHALA_HEALTH_01',
      state: 'Telangana',
      idNumber: 'BGSG-0000123456',
      idType: 'UHID',
      photo: '',
      wantsToLinkWhatsapp: true,
      abhaAddress: '',
      patientType: 'NEW',
      lastUpdated: '2025-07-23T09:51:13.201881128Z'
      },
      totalNumberOfRecords: 1
      }
    • Parameters

      • filters: Record<string, any>

      Returns Promise<Record<string, any>>

    • Updates existing patient data in the system.

      Parameters

      • updatePatientData: UpdatePatientDTO

        Object containing fields to update:

        Fields:

        • resourceId - Unique resource ID of the patient (used for identification) - mandatory
        • resourceType - Type of FHIR resource (usually "Patient") - mandatory
        • emailId - Updated email address (must be a valid email) - optional
        • mobileNumber - Updated mobile number (must be valid, should not include masked characters like "*") - optional

      Returns Promise<CreateUpdatePatientResponse>

      • Returns an object containing:
      • type : Type of the response (e.g., "Patient")
      • message : Descriptive message or status of the operation
      • resourceId : Unique identifier of the updated patient resource
      • validationErrors : Array of validation errors (if any)
      • resource : Full updated patient object

      If the input data is invalid.

      If the API request fails.

      // Input:
      await patient.update({
      "resourceType": "Patient",
      "resourceId": "791708f6-de6d-439e-bece-041e5d64e463",
      "emailId": "VijayaLatha123@gmail.com",
      "mobileNumber": "+919182853291"
      });


      // Expected Output:
      {
      message: 'Details Updated successfully',
      type: 'Patient',
      resource: {
      firstName: 'Vijaya',
      middleName: 'A',
      lastName: 'Doe',
      birthDate: '1980-01-01',
      gender: 'male',
      mobileNumber: '*********3291',
      emailId: 'VijayaLatha123@gmail.com',
      address: '123 Main St, Anytown, Anystate, 12345',
      pincode: '12345',
      resourceType: 'Patient',
      resourceId: '791708f6-de6d-439e-bece-041e5d64e463',
      organizationId: 'ACHALA_HEALTH_01',
      state: 'Andhra Pradesh',
      idNumber: '1234567000',
      idType: 'ABHA',
      photo: '',
      wantsToLinkWhatsapp: true,
      abhaAddress: 'abha55578@example.com',
      patientType: 'OLD',
      lastUpdated: '2025-07-31T11:16:36.550237457Z'
      }
      }