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
- Success Response
- Error Response
{
"data": {
"id": "number"
}
}
{
"statusCode": 400,
"message": "Error description",
"error": "Bad Request"
}
Business Logic and Validations
Required Field Validations
- Prescription Lines: Must have at least one line
- Line Fields: Each line must have
trade_product_idandtrade_qty > 0 - Instructions: Each line must have either
instruction(ID) orinstruction_name - Patient Phone: Required for patient identification/creation
- Doctor ID: Must be provided and valid
Database Validations
Product Validation
- Validates
trade_product_idexists inproduct_product - Must be active product with type 'product'
- Retrieves
product_template.nameastrade_medication
Instruction Processing
- If
instructionprovided: validates ID exists inrx_line_instruction - If
instruction_nameprovided: 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_idfor prescription
Doctor Validation
- Validates
doctor_idexists inres_users - Ensures associated partner has
is_clinic_doctor = true
Branch Validation
- Validates
selected_branch_idexists inres_company
Shipping Service Validation
- If
shipping_service_idprovided: validates it's a service type product - Adds shipping as additional prescription line
Example Usage
- Basic Prescription
- Full Prescription with Shipping
{
"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
}
{
"patient": {
"name": "Jane Smith",
"phone": "+1987654321",
"email": "jane.smith@email.com"
},
"lines": [
{
"trade_qty": 30,
"trade_product_id": 123,
"instruction_name": "Take one tablet daily with food",
"refills": 3
},
{
"trade_qty": 60,
"trade_product_id": 456,
"instruction": 789,
"refills": 1
}
],
"doctor_id": 101,
"selected_branch_id": 202,
"ship_to": "patient",
"shipping_service_id": 303,
"procedure": "Routine checkup",
"exp_date": "2025-12-31T00:00:00.000Z",
"notes": "Patient allergic to penicillin"
}
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