Skip to content

SabarishAV/beacon-backend

Repository files navigation

Beacon Backend

πŸ”¦ BEACON β€” Backend API

Blind Enhancement and Assistive Cap with Optical Navigation


πŸ“– About

BEACON is a companion system designed to support visually impaired individuals by working in tandem with a smart wearable cap. The cap is equipped with a camera module (ESP32-CAM) that performs real-time object detection, crosswalk recognition, currency identification, and proximity alerts β€” all powered by AI models running on a separate camera service.

This repository contains the backend API server β€” the central nervous system that ties the entire BEACON ecosystem together. It handles:

  • πŸ” User authentication & management β€” Registration, login, and JWT-secured sessions
  • πŸ†˜ Emergency alerting β€” Automated fall detection emails and manual SOS alerts sent to emergency contacts with Google Maps location links
  • πŸ“‡ Emergency contact management β€” Full CRUD with priority ordering for contacts who receive alerts
  • πŸ“‹ Task scheduling β€” Repeatable and one-time task reminders with trigger tracking to help users manage daily routines
  • πŸ“¨ Message queues β€” Bi-directional message relay between the camera module and the mobile app, enabling real-time data transmission

πŸ—οΈ System Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   BEACON Cap    β”‚     β”‚  BEACON Backend  β”‚     β”‚  BEACON Mobile  β”‚
β”‚  (ESP32-CAM +   │◄───►│    (This Repo)   │◄───►│   (Expo App)    β”‚
β”‚   AI Service)   β”‚     β”‚                  β”‚     β”‚                 β”‚
β”‚                 β”‚     β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚     β”‚  β€’ Voice UI     β”‚
β”‚  β€’ Object Det.  β”‚     β”‚  β”‚ Fastify    β”‚  β”‚     β”‚  β€’ Navigation   β”‚
β”‚  β€’ Crosswalk    β”‚     β”‚  β”‚ REST API   β”‚  β”‚     β”‚  β€’ Task Mgmt    β”‚
β”‚  β€’ Currency     β”‚     β”‚  β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€  β”‚     β”‚  β€’ SOS Trigger  β”‚
β”‚  β€’ Proximity    β”‚     β”‚  β”‚ PostgreSQL β”‚  β”‚     β”‚                 β”‚
β”‚                 β”‚     β”‚  β”‚ (Prisma)   β”‚  β”‚     β”‚                 β”‚
β”‚                 β”‚     β”‚  β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€  β”‚     β”‚                 β”‚
β”‚                 β”‚     β”‚  β”‚ Nodemailer β”‚  β”‚     β”‚                 β”‚
β”‚                 β”‚     β”‚  β”‚ (Alerts)   β”‚  β”‚     β”‚                 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

The backend acts as the bridge between the smart cap and the mobile application. The camera service pushes detection results into message queues via the API, and the mobile app consumes them to deliver voice-guided audio feedback to the user.


πŸ› οΈ Tech Stack

Layer Technology
Runtime Node.js with TypeScript
Framework Fastify v5 β€” high-performance HTTP server
ORM Prisma v7 β€” type-safe database client
Database PostgreSQL
Auth JWT (jsonwebtoken) + bcrypt password hashing
Email Nodemailer (Gmail SMTP) β€” HTML-styled emergency alerts
Docs Swagger / OpenAPI 3.0 with Swagger UI (/docs)
Logging Pino with pino-pretty transport
Code Quality ESLint + Prettier + Husky pre-commit hooks + lint-staged
Build tsx (dev) / tsc (production)

πŸ“‘ API Endpoints

All endpoints except public routes require a Bearer JWT token in the Authorization header.

πŸ”“ Authentication β€” /auth

Method Endpoint Description Auth
POST /auth/register Register a new user ❌
POST /auth/login Login and receive a JWT token ❌

πŸ†˜ Emergency Alerts β€” /emergency

Method Endpoint Description Auth
POST /emergency Send a generic emergency email with location βœ…
POST /emergency/alerts/fall Send fall-detection alert to primary emergency contact βœ…
POST /emergency/alerts/manual Manually trigger an SOS alert with GPS coordinates βœ…

πŸ“‡ Emergency Contacts β€” /emergency-contact

Method Endpoint Description Auth
POST /emergency-contact Create a new emergency contact βœ…
GET /emergency-contact/:id Get a specific emergency contact βœ…
GET /emergency-contact/list List contacts with pagination βœ…
PATCH /emergency-contact/:id Update an emergency contact βœ…
DELETE /emergency-contact/:id Delete an emergency contact βœ…

πŸ“‹ Tasks β€” /tasks

Method Endpoint Description Auth
POST /tasks Create a new task (one-time or recurring) βœ…
GET /tasks List all tasks for the authenticated user βœ…
GET /tasks/due Fetch tasks due at the current time βœ…
PATCH /tasks/:id Toggle task active/inactive status βœ…
POST /tasks/trigger/:id Mark a task as triggered (update lastTriggered) βœ…
DELETE /tasks/:id Delete a task βœ…

πŸ“¨ Message Queues β€” /message-queues

Method Endpoint Description Auth
POST /message-queues Create a new message in the queue βœ…
POST /message-queues/public Create a message (public β€” used by cam service) ❌
GET /message-queues List all messages for the authenticated user βœ…
DELETE /message-queues/:id Delete a message from the queue βœ…

πŸ“˜ Interactive API docs are available at http://localhost:8080/docs when the server is running.


πŸ—„οΈ Database Schema

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚    Users     │────<β”‚  EmergencyContacts   β”‚
β”‚              β”‚     β”‚                     β”‚
β”‚  id (cuid)   β”‚     β”‚  id, userId, name   β”‚
β”‚  email       β”‚     β”‚  email, mobileNo    β”‚
β”‚  name        β”‚     β”‚  position (priority)β”‚
β”‚  password    β”‚     β”‚  createdAt          β”‚
β”‚  createdAt   β”‚     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β”‚              β”‚
β”‚              │────<β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚              β”‚     β”‚       Tasks         β”‚
β”‚              β”‚     β”‚                     β”‚
β”‚              β”‚     β”‚  id, userId, name   β”‚
β”‚              β”‚     β”‚  scheduledTime      β”‚
β”‚              β”‚     β”‚  specificDate       β”‚
β”‚              β”‚     β”‚  isActive           β”‚
β”‚              β”‚     β”‚  isRepeating        β”‚
β”‚              β”‚     β”‚  repeatDays[]       β”‚
β”‚              β”‚     β”‚  lastTriggered      β”‚
β”‚              β”‚     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β”‚              β”‚
β”‚              │────<β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚              β”‚     β”‚   MessageQueue      β”‚
β”‚              β”‚     β”‚                     β”‚
β”‚              β”‚     β”‚  id, userId         β”‚
β”‚              β”‚     β”‚  message            β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

All relationships cascade on delete β€” removing a user automatically cleans up their contacts, tasks, and messages.


πŸš€ Getting Started

Prerequisites

  • Node.js β‰₯ 18
  • PostgreSQL database
  • Gmail account with an App Password for email alerts

1. Clone the repository

git clone https://github.com/SabarishAV/beacon-backend.git
cd beacon-backend

2. Install dependencies

npm install

3. Configure environment variables

Copy the example env file and fill in your values:

cp .env.example .env
PORT=8080
DATABASE_URL=postgresql://user:password@localhost:5432/beacon
EMAIL_USERNAME=your-email@gmail.com
EMAIL_APP_PASSWORD=your-gmail-app-password
JWT_SECRET=your-secret-key

4. Set up the database

# Generate Prisma client
npm run prisma:generate

# Run database migrations
npm run prisma:migrate

5. Start the development server

npm run dev

The server will start at http://localhost:8080 with hot-reload enabled.


πŸ“œ Available Scripts

Script Description
npm run dev Start dev server with hot-reload (tsx watch)
npm run build Compile TypeScript to JavaScript
npm run lint Run ESLint on all source files
npm run lint:fix Run ESLint with auto-fix
npm run format Format all source files with Prettier
npm run prisma:generate Generate Prisma client from schema
npm run prisma:studio Open Prisma Studio (visual DB editor)
npm run prisma:migrate Run pending database migrations
npm run prisma:reset Reset database and re-run all migrations
npm run seed:up Run database seeders
npm run seed:down Reverse database seeders

πŸ“ Project Structure

beacon_backend/
β”œβ”€β”€ prisma/
β”‚   β”œβ”€β”€ schema.prisma          # Database schema definition
β”‚   β”œβ”€β”€ migrations/            # SQL migration files
β”‚   └── seeders/               # Database seed scripts
β”‚       β”œβ”€β”€ up.ts
β”‚       └── down.ts
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ server.ts              # Application entry point
β”‚   β”œβ”€β”€ config/
β”‚   β”‚   β”œβ”€β”€ logger.ts          # Pino logger configuration
β”‚   β”‚   β”œβ”€β”€ prisma.ts          # Prisma client singleton
β”‚   β”‚   └── swagger.ts         # Swagger/OpenAPI setup
β”‚   β”œβ”€β”€ constants/
β”‚   β”‚   β”œβ”€β”€ bcrypt.ts          # Salt rounds configuration
β”‚   β”‚   └── route.ts           # Public routes whitelist
β”‚   β”œβ”€β”€ controllers/
β”‚   β”‚   β”œβ”€β”€ auth-controller.ts
β”‚   β”‚   β”œβ”€β”€ emergency-controller.ts
β”‚   β”‚   β”œβ”€β”€ emergency-contact-controllers.ts
β”‚   β”‚   β”œβ”€β”€ message-queue-controller.ts
β”‚   β”‚   └── tasks-controller.ts
β”‚   β”œβ”€β”€ middlewares/
β”‚   β”‚   β”œβ”€β”€ error.ts           # Global error handler (Prisma + custom errors)
β”‚   β”‚   └── jwt.ts             # JWT authentication pre-handler
β”‚   β”œβ”€β”€ repository/
β”‚   β”‚   β”œβ”€β”€ user-repository.ts
β”‚   β”‚   β”œβ”€β”€ emergency-contact-repository.ts
β”‚   β”‚   β”œβ”€β”€ message-queue-repository.ts
β”‚   β”‚   └── tasks-repository.ts
β”‚   β”œβ”€β”€ routes/
β”‚   β”‚   β”œβ”€β”€ auth-route.ts
β”‚   β”‚   β”œβ”€β”€ emergency-route.ts
β”‚   β”‚   β”œβ”€β”€ emergency-contact-routes.ts
β”‚   β”‚   β”œβ”€β”€ message-queue-route.ts
β”‚   β”‚   └── task-route.ts
β”‚   └── utils/
β”‚       β”œβ”€β”€ date.ts            # Date formatting helpers
β”‚       β”œβ”€β”€ jwt.ts             # JWT sign/verify utilities
β”‚       β”œβ”€β”€ mail.ts            # Email templates (fall alert, SOS)
β”‚       β”œβ”€β”€ pagination.ts      # Pagination helper
β”‚       β”œβ”€β”€ time.ts            # Time utilities
β”‚       β”œβ”€β”€ errors/            # Custom error classes
β”‚       β”‚   β”œβ”€β”€ AppError.ts
β”‚       β”‚   β”œβ”€β”€ BadRequestError.ts
β”‚       β”‚   β”œβ”€β”€ ForbiddenError.ts
β”‚       β”‚   β”œβ”€β”€ NotFoundError.ts
β”‚       β”‚   └── ValidationError.ts
β”‚       └── swagger-schemas/   # OpenAPI schema definitions
β”œβ”€β”€ .env.example               # Environment variable template
β”œβ”€β”€ .prettierrc                # Prettier configuration
β”œβ”€β”€ eslint.config.mjs          # ESLint configuration
β”œβ”€β”€ tsconfig.json              # TypeScript configuration
β”œβ”€β”€ tsup.config.ts             # tsup bundler configuration
└── package.json

🧩 Related Repositories

Repository Description
beacon-mobile React Native (Expo) mobile application β€” voice UI, navigation, task manager
beacon-camera Python AI service β€” object detection, crosswalk & currency recognition

πŸ“„ License

MIT


Built with ❀️ for accessibility

About

Backend API for BEACON, an assistive smart-cap ecosystem for visually impaired users. Built with Fastify, TypeScript, Prisma, and PostgreSQL to provide authentication, emergency alerts, task scheduling, contact management, and real-time message routing.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages