Skip to main content

Post Prescription

Endpoint Overview

This API endpoint provides a detailed overview of the endpoint responsible for creating a prescription within the RX module of the Juleb pharmacy management system. This endpoint handles the creation of prescriptions with associated prescription lines, patient information, doctor validation, and shipping services.

Endpoint Details

  • Method: POST
  • URL: /api/v1/rx
  • Summary: Create a prescription
  • Authentication: Bearer Token required
  • Content-Type: application/json

Parameters

  • Path Parameters: None
  • Query Parameters: None

Request Body

The request body contains the prescription information including patient details, prescription lines, and optional shipping information.

{
"patient": {
"name": "string",
"phone": "string",
"email": "string"
},
"lines": [
{
"refills": "number",
"trade_qty": "number",
"trade_product_id": "number",
"instruction": "number",
"instruction_name": "string"
}
],
"procedure": "string",
"exp_date": "Date",
"doctor_id": "number",
"selected_branch_id": "number",
"ship_to": "patient | clinic",
"shipping_service_id": "number",
"notes": "string"
}

Field Descriptions

Patient Information (PatientInputDto)

  • name (required): Patient full name
  • phone (required): Patient phone number (unique identifier)
  • email (optional): Patient email address

Prescription Lines (CreatePrescriptionLineDto[])

  • refills (optional): Number of refills allowed
  • trade_qty (required): Quantity of medication (must be > 0)
  • trade_product_id (required): Product ID from product_product table
  • instruction (optional): Instruction ID from rx_line_instruction table
  • instruction_name (optional): Instruction name (creates new if doesn't exist)
note

Each line must have either instruction (ID) or instruction_name. At least one prescription line is required.

Main Prescription Fields

  • procedure (optional): Medical procedure description
  • exp_date (optional): Prescription expiration date
  • doctor_id (required): Doctor ID from res_users table
  • selected_branch_id (optional): Branch ID from res_company table
  • ship_to (optional): Shipping destination - either "patient" or "clinic"
  • shipping_service_id (optional): Shipping service product ID
  • notes (optional): Additional prescription notes

Response

  • 201 Created: The prescription has been successfully created
  • 400 Bad Request: Validation error (missing required fields, invalid data)
  • 401 Unauthorized: Authentication required

Response Body

{
"data": {
"id": "number"
}
}

Business Logic and Validations

Required Field Validations

  1. Prescription Lines: Must have at least one line
  2. Line Fields: Each line must have trade_product_id and trade_qty > 0
  3. Instructions: Each line must have either instruction (ID) or instruction_name
  4. Patient Phone: Required for patient identification/creation
  5. Doctor ID: Must be provided and valid

Database Validations

Product Validation

  • Validates trade_product_id exists in product_product
  • Must be active product with type 'product'
  • Retrieves product_template.name as trade_medication

Instruction Processing

  • If instruction provided: validates ID exists in rx_line_instruction
  • If instruction_name provided: creates new instruction if doesn't exist

Patient Management

  • Searches for existing patient by phone with is_patient = true
  • Creates new patient if not found
  • Sets patient_id for prescription

Doctor Validation

  • Validates doctor_id exists in res_users
  • Ensures associated partner has is_clinic_doctor = true

Branch Validation

  • Validates selected_branch_id exists in res_company

Shipping Service Validation

  • If shipping_service_id provided: validates it's a service type product
  • Adds shipping as additional prescription line

Example Usage

{
"patient": {
"name": "John Doe",
"phone": "+1234567890",
"email": "john.doe@email.com"
},
"lines": [
{
"trade_qty": 30,
"trade_product_id": 123,
"instruction_name": "Take one tablet daily",
"refills": 2
}
],
"doctor_id": 456,
"selected_branch_id": 789
}

Error Handling

Common Error Cases

  • Missing prescription lines: At least one prescription line is required
  • Invalid product: Product not found or inactive
  • Missing instruction: Each line must have either instruction ID or instruction name
  • Invalid doctor: Doctor not found or not a clinic doctor
  • Invalid branch: Branch not found
  • Invalid shipping service: Shipping service product not found
  • Incomplete patient information: Patient phone is required
  • Invalid quantity: Trade quantity must be greater than 0