# AyushEHR

## Overview

AyushEHR is a Node.js-based backend application developed for managing electronic health records in the Ayush healthcare domain. It provides a robust RESTful API for handling healthcare data, built with a modular structure and leveraging best practices in modern web development.

## Features

- **RESTful APIs**: Endpoints for managing various healthcare data.
- **Modular Structure**: Organized into controllers, models, routes, and services for better maintainability.
- **Environment-Based Configurations**: Supports development, production, and testing configurations.
- **Custom Libraries and Utilities**: For handling complex healthcare domain logic.
- **Logging**: Comprehensive logging for error tracking and monitoring.

## Technologies

- **Node.js**: JavaScript runtime for server-side programming.
- **Express**: Web framework for building the API.
- **Swagger**: API documentation tool.
- **Prisma**: ORM for database management.
- **Babel**: JavaScript transpiler for ES6+ support.
- **Logging**: For structured log capturing.

## Prerequisites

Before running this project, ensure you have the following installed:

- [Node.js](https://nodejs.org/) (version 14.x or later)
- [npm](https://www.npmjs.com/) (comes with Node.js)
- [PostgreSQL](https://sbp.enterprisedb.com/getfile.jsp?fileid=1259129) (recommended for Prisma)

## Installation

Follow these steps to set up the project locally:

1. **Clone the repository:**

   ```bash
   git clone <repository-url>
   cd AyushEHR-V3

2. **Install dependencies:**

   ```bash
   npm install

3. **Set up environment variables:**

   Create a `.env` file in the root directory and add the necessary environment variables. Example:

   `PORT=8503`

4. **Ensure PostgreSQL is running:**

   Download and install PostgreSQL from [this link](https://sbp.enterprisedb.com/getfile.jsp?fileid=1259129) if you haven't already. Make sure PostgreSQL is running and properly configured.

## Running the Application

Development Environment

To run the application in development mode (HTTP), run:

`npm run dev`

The server will run on [http://localhost:8503](http://localhost:8503) by default. You can change the port by setting the `PORT` environment variable in the `.env` file.

Production Environment

To run the application in production mode (HTTPS), ensure your .env file includes NODE_ENV=production and has paths to your SSL certificates (key.pem and cert.pem). Run the following command:

`npm run prod`

This command will start the server in production mode using HTTPS on the port defined by the PORT environment variable (default is 8503).

Ensure that your production environment has valid SSL certificate and key files, typically located in the root directory.

## Version Update

There are three commands which automatically updates the version of the project in your package.json. Ensure that you keep a clean working directory, i.e., dont have any uncommited changes before using these commands. So once commit is done, push the code, take pull and update the version and commit again.

1. **patch**
  ```bash
   npm version patch
  ```
  This updates the version in this manner 1.0.0 -> 1.0.1
  

2. **minor**
  ```bash
   npm version minor
  ```
  This updates the version in this manner 1.0.0 -> 1.1.0

3. **major**
  ```bash
   npm version patch
  ```
  This updates the version in this manner 1.0.0 -> 2.0.0

## Change Log

To view all the changes made in the project till date, use the below mentioned command line in the terminal of the project.
  ```bash
   auto-changelog
  ```


## API Documentation

The API is documented using Swagger. Once the application is running, you can access the Swagger UI at:

[http://localhost:8503/api-docs](http://localhost:8503/api-docs)

## Prisma Commands

Prisma is used for database schema management and operations. Below are the key Prisma commands you will use in this project:

- **Initialize Prisma in your project:**

  `npx prisma init`

  This command sets up Prisma in your project by creating a `prisma` directory with the necessary configuration files.

- **Run migrations:**

  `npx prisma migrate dev --name <migration-name>`

  This command creates a new migration file based on the changes in your Prisma schema and applies it to the database.

- **Generate Prisma Client:**

  `npx prisma generate`

  This command generates the Prisma Client, which is a type-safe database client for interacting with your database.

- **Reset the database and apply all migrations:**

  `npx prisma migrate reset`

  This command resets the database by dropping all tables and reapplying all migrations. Use this with caution as it will delete all existing data.

- **View the database schema:**

  `npx prisma db pull`

  This command pulls the current database schema into your Prisma project, which can be useful for keeping the schema file up to date with the database.

- **Open Prisma Studio:**

  `npx prisma studio`

  This command opens Prisma Studio, a web-based GUI to view and edit the data in your database.

## Project Structure

- **src/config**: Configuration files for various environments.
- **src/constants**: Stores application-wide constant values.
- **src/controllers**: Handles the core application logic and communicates with the services.
- **src/docs**: Stores API documentation and other project-related documentation.
- **src/libraries**: Custom libraries and third-party integrations.
- **src/logs**: Contains logs for tracking errors and monitoring application behavior.
- **src/models**: Database schemas and model definitions.
- **src/repository**: Abstraction layer for database operations.
- **src/routes**: Defines API endpoints and maps them to controllers.
- **src/utils**: Utility functions that provide reusable code across the project.

## Configuration

- **Babel**: Used to transpile ES modules. Ensure Babel is configured properly with `babel.config.js`.
- **Prisma**: ORM configured in prisma/schema.prisma.
- **Swagger**: Set up for interactive API documentation.
- **Environment Variables**: Configure in .env file.