Skip to content

NestJS backend API for SensitiveMinds mobile app - patient management system for psychology research with JWT authentication, PostgreSQL database, and comprehensive visit tracking.

DanielSledz03/SensitiveMinds-Backend

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

13 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

SensitiveMinds Backend 🧠

NestJS TypeScript PostgreSQL TypeORM JWT Swagger

SensitiveMinds Backend is a dedicated API server created for the needs of a research project conducted by psychology students. Its purpose is to provide a secure and efficient backend infrastructure for the SensitiveMinds mobile application, enabling streamlined data collection about patients and conducted visits.

The backend was designed as a secure API for internal use among project participants. It provides comprehensive authentication, patient management, visit tracking, and data export capabilities while ensuring complete security and confidentiality of collected data.

🌟 Features

  • Modern Architecture: Built with NestJS 11.0.1 and TypeScript
  • Secure Authentication: JWT-based authentication with role-based access control
  • Database Management: PostgreSQL with TypeORM for efficient data handling
  • API Documentation: Comprehensive Swagger/OpenAPI documentation
  • Patient Management: Complete patient tracking and management system
  • Visit Management: Therapeutic visit management with exercise tracking
  • Role-Based Access: Manager and Researcher role system
  • Data Export: Secure data export functionality
  • Center Grouping: Organization of patients by therapeutic centers

πŸ› οΈ Tech Stack

  • Framework: NestJS 11.0.1
  • Language: TypeScript 5.7.3
  • Database: PostgreSQL 8.13.3 with TypeORM 0.3.21
  • Authentication: JWT 9.0.2, Passport, bcrypt 5.1.1
  • API Documentation: Swagger/OpenAPI 11.0.6
  • Validation: class-validator 0.14.1
  • Testing: Jest 29.7.0, Supertest 7.0.0
  • Linting: ESLint 9.18.0 with TypeScript support
  • Formatting: Prettier 3.4.2
  • Development: NestJS CLI 11.0.0

πŸ“ Project Structure

β”œβ”€β”€ src/                    # Source code
β”‚   β”œβ”€β”€ auth/              # Authentication module
β”‚   β”‚   β”œβ”€β”€ auth.controller.ts    # Auth endpoints
β”‚   β”‚   β”œβ”€β”€ auth.service.ts       # Auth business logic
β”‚   β”‚   β”œβ”€β”€ auth.module.ts        # Auth module configuration
β”‚   β”‚   β”œβ”€β”€ jwt-auth.guard.ts     # JWT authentication guard
β”‚   β”‚   └── dto/                  # Auth data transfer objects
β”‚   β”œβ”€β”€ users/             # User management module
β”‚   β”‚   β”œβ”€β”€ users.controller.ts   # User endpoints
β”‚   β”‚   β”œβ”€β”€ users.service.ts      # User business logic
β”‚   β”‚   β”œβ”€β”€ users.entity.ts       # User database entity
β”‚   β”‚   β”œβ”€β”€ users.module.ts       # User module configuration
β”‚   β”‚   └── roles.guard.ts        # Role-based access guard
β”‚   β”œβ”€β”€ patients/          # Patient management module
β”‚   β”‚   β”œβ”€β”€ patients.controller.ts # Patient endpoints
β”‚   β”‚   β”œβ”€β”€ patients.service.ts    # Patient business logic
β”‚   β”‚   β”œβ”€β”€ patients.entity.ts     # Patient database entity
β”‚   β”‚   β”œβ”€β”€ center.entity.ts       # Center database entity
β”‚   β”‚   β”œβ”€β”€ patients.module.ts     # Patient module configuration
β”‚   β”‚   └── dto/                   # Patient data transfer objects
β”‚   β”œβ”€β”€ visits/            # Visit management module
β”‚   β”‚   β”œβ”€β”€ visit.controller.ts    # Visit endpoints
β”‚   β”‚   β”œβ”€β”€ visit.service.ts       # Visit business logic
β”‚   β”‚   β”œβ”€β”€ visits.entity.ts       # Visit database entity
β”‚   β”‚   β”œβ”€β”€ visit.module.ts        # Visit module configuration
β”‚   β”‚   └── dto/                   # Visit data transfer objects
β”‚   β”œβ”€β”€ center/            # Center management module
β”‚   β”‚   β”œβ”€β”€ center.controller.ts   # Center endpoints
β”‚   β”‚   β”œβ”€β”€ center.service.ts      # Center business logic
β”‚   β”‚   └── center.module.ts       # Center module configuration
β”‚   β”œβ”€β”€ export/            # Data export functionality
β”‚   β”œβ”€β”€ app.module.ts      # Main application module
β”‚   β”œβ”€β”€ main.ts           # Application entry point
β”‚   β”œβ”€β”€ app.controller.ts  # Main application controller
β”‚   └── app.service.ts     # Main application service
β”œβ”€β”€ test/                  # Test files
β”œβ”€β”€ dist/                  # Compiled JavaScript output
β”œβ”€β”€ package.json           # Dependencies and scripts
β”œβ”€β”€ tsconfig.json          # TypeScript configuration
β”œβ”€β”€ nest-cli.json          # NestJS CLI configuration
└── .env                   # Environment variables (not in repo)

πŸš€ Getting Started

Prerequisites

  • Node.js >= 18
  • PostgreSQL >= 12
  • npm or yarn package manager

Installation

  1. Clone the repository

    git clone <repository-url>
    cd sensitive-minds-backend
  2. Install dependencies

    npm install
  3. Set up environment variables

    Create a .env file in the root directory with the following variables:

    # JWT Configuration
    JWT_SECRET=your-super-secret-jwt-key-here
    
    # Database Configuration
    PGHOST=localhost
    PGPORT=5432
    PGUSER=your-database-user
    PGPASSWORD=your-database-password
    PGDATABASE=sensitive_minds
    
    # Application Configuration
    PORT=3000
    EXPORT_ALL_PASSWORD=your-export-password

    ⚠️ IMPORTANT: Never commit the .env file to the repository - it contains sensitive data!

  4. Set up the database

    Create a PostgreSQL database and update the connection details in your .env file.

πŸ”§ Configuration

Environment Variables

The application uses the following environment variables:

  • JWT_SECRET: Secret key for JWT token signing
  • PGHOST: PostgreSQL host address
  • PGPORT: PostgreSQL port (default: 5432)
  • PGUSER: PostgreSQL username
  • PGPASSWORD: PostgreSQL password
  • PGDATABASE: PostgreSQL database name
  • PORT: Application port (default: 3000)
  • EXPORT_ALL_PASSWORD: Password for data export functionality

Database Configuration

The application uses TypeORM with PostgreSQL. The database connection is configured in src/app.module.ts:

TypeOrmModule.forRoot({
  type: 'postgres',
  host: process.env.PGHOST,
  port: parseInt(process.env.PGPORT, 10) || 5432,
  username: process.env.PGUSER,
  password: process.env.PGPASSWORD,
  database: process.env.PGDATABASE,
  entities: [__dirname + '/**/*.entity{.ts,.js}'],
  synchronize: true, // Use migrations in production
});

πŸ“ Available Scripts

  • npm run start - Start the application in production mode
  • npm run start:dev - Start the application in development mode with hot reload
  • npm run start:debug - Start the application in debug mode
  • npm run build - Build the application for production
  • npm run test - Run unit tests
  • npm run test:e2e - Run end-to-end tests
  • npm run test:cov - Run tests with coverage report
  • npm run lint - Run ESLint for code quality
  • npm run format - Format code with Prettier

πŸ”Œ API Endpoints

The API provides the following main endpoints:

Authentication

  • POST /auth/login - User login
  • POST /auth/register - User registration (admin only)

Users

  • GET /users - Get all users (admin only)
  • GET /users/:id - Get user by ID
  • PUT /users/:id - Update user
  • DELETE /users/:id - Delete user

Patients

  • GET /patients - Get all patients
  • GET /patients/:id - Get patient by ID
  • POST /patients - Create new patient
  • PUT /patients/:id - Update patient
  • DELETE /patients/:id - Delete patient

Visits

  • GET /visits - Get all visits
  • GET /visits/:id - Get visit by ID
  • POST /visits - Create new visit
  • PUT /visits/:id - Update visit
  • DELETE /visits/:id - Delete visit

Centers

  • GET /centers - Get all centers

πŸ“Š Data Models

User Entity

  • id: Unique identifier
  • fullName: User's full name
  • username: Unique username
  • password: Hashed password
  • role: User role (MANAGER or RESEARCHER)
  • patients: Associated patients
  • researchers: Associated researchers (for managers)
  • manager: Associated manager (for researchers)

Patient Entity

  • id: Unique identifier
  • name: Patient name
  • age: Patient age
  • bedNumber: Bed number
  • gender: Patient gender
  • center: Associated therapeutic center
  • roomNumber: Room number
  • user: Associated researcher
  • visits: Patient's visits

Visit Entity

  • id: Unique identifier
  • date: Visit date
  • consentGiven: Consent status
  • exercises: Exercise completion data
    • pastMemory: Memory exercise completion
    • arithmetic: Arithmetic exercise data
    • reading: Reading exercise completion
    • stroopTest: Stroop test data
  • notes: Visit notes
  • patient: Associated patient
  • createdBy: User who created the visit

πŸ”’ Security Features

  • JWT Authentication: Secure token-based authentication
  • Password Hashing: bcrypt for password security
  • Role-Based Access Control: Manager and Researcher roles
  • Input Validation: Comprehensive data validation
  • SQL Injection Protection: TypeORM parameterized queries
  • Environment Variable Protection: Sensitive data isolation

πŸ“š API Documentation

The API documentation is automatically generated using Swagger/OpenAPI and is available at:

http://localhost:3000/api

The documentation includes:

  • All available endpoints
  • Request/response schemas
  • Authentication requirements
  • Interactive testing interface

πŸ§ͺ Testing

The project includes comprehensive testing setup:

# Run unit tests
npm run test

# Run tests in watch mode
npm run test:watch

# Run tests with coverage
npm run test:cov

# Run end-to-end tests
npm run test:e2e

πŸš€ Deployment

Production Deployment

  1. Build the application

    npm run build
  2. Set up production environment variables

  3. Configure database migrations (recommended for production)

  4. Deploy using your preferred method:

    • Docker containers
    • Cloud platforms (AWS, Google Cloud, Azure)
    • Traditional servers

Docker Deployment

FROM node:18-alpine

WORKDIR /app

COPY package*.json ./
RUN npm ci --only=production

COPY dist ./dist

EXPOSE 3000

CMD ["node", "dist/main"]

πŸ”§ Development

Code Quality

The project uses ESLint and Prettier for code quality and formatting:

# Check for linting errors
npm run lint

# Format code
npm run format

Database Migrations

For production environments, it's recommended to use TypeORM migrations instead of synchronize: true:

# Generate migration
npm run typeorm migration:generate -- -n MigrationName

# Run migrations
npm run typeorm migration:run

πŸ“„ License

This project is private and proprietary. All rights reserved.

πŸ“ž Contact

SensitiveMinds Backend


Built with ❀️ for efficient therapy management

Secure backend solutions for modern healthcare

About

NestJS backend API for SensitiveMinds mobile app - patient management system for psychology research with JWT authentication, PostgreSQL database, and comprehensive visit tracking.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published