Skip to content

About

A RESTful API backend for a community-driven platform where users share life challenges, get AI-powered insights, and connect with others. Built with Express 5, TypeScript, and MongoDB (Mongoose).

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

16 Commits

Folders and files

Repository files navigation

⚙️ LifeSolve AI — Backend

The RESTful API powering LifeSolve AI's community platform.

LifeSolve AI is a community-driven platform where people share life challenges, receive instant AI-powered guidance, and connect with others who genuinely understand. This repository contains the Express + TypeScript backend that handles all server-side logic.


📖 Description

The LifeSolve AI Backend provides every server-side feature of the platform — problem management, comments, reactions, direct messaging, notifications, authentication, analytics, and streaming AI responses powered by Groq. It exposes a clean REST API consumed by the LifeSolve AI Frontend.


🎯 Overview

The backend is organized around domain modules and provides:

Area What it does
📝 Problems CRUD, search, filter, sort, pagination for community posts
💬 Comments & Reactions Full comment CRUD + like/love/sad reaction toggles
🤖 AI Integration Streaming AI responses in three modes (solver, chat, quick-chat)
⚡ AI Quick Chat Ephemeral endpoint — answers are never saved to the database
✉️ Messaging Real-time private conversations between users
🔔 Notifications Event-driven notifications for comments, reactions, messages
🔐 Authentication Bearer token session validation via Better Auth
📊 Statistics Platform-wide and per-user analytics with daily breakdowns

🎯 Purpose

The backend exists to give LifeSolve AI a secure, scalable, and real-time foundation:

  • Serve AI responses with streaming (SSE) for a smooth chat experience.
  • Offer an ephemeral quick-chat mode for the floating widget so anyone can ask the AI anything without any data being persisted.
  • Persist user content — problems, comments, conversations, notifications — in MongoDB.
  • Protect user data with session-based authentication on every mutating route.

✨ Key Features

Feature Description
Problem CRUD Full create, read, update, delete with search, filter, pagination
Comment System Create, edit, delete comments on problems
Reactions Like, Love, Sad toggle with per-user tracking
AI Solver Streaming AI responses for structured problem-solving (saved sessions)
AI Chat Streaming conversational AI for general support (saved sessions)
AI Quick Chat Streaming AI for the floating widget — no DB writes, no auth required
Direct Messages Private conversations with user discovery
Notifications 5 event types: problem posted, resolved, comment, reaction, message
User Stats Per-user post/comment counts with 7-day activity
Platform Stats Global counts for posts, comments, messages, AI chats, users
Auth Middleware Session-based authentication via Better Auth tokens
Health Check API health endpoint

🛠️ Technology Stack

Technology Purpose
Express 5 Web framework
TypeScript Type safety
Mongoose 9 MongoDB ODM
MongoDB Database (Atlas)
Groq API AI inference (OpenAI-compatible, streaming)
Better Auth Session management (MongoDB adapter)
CORS Cross-Origin Resource Sharing
tsx TypeScript execution in development

📁 Project Structure

backend/
├── api/                            # Vercel serverless entry point
├── src/
│   ├── index.ts                    # Entry point — DNS, dotenv, DB connect, listen
│   ├── app.ts                      # Express app — CORS, JSON, routes, error handler
│   ├── config/
│   │   └── db.ts                   # MongoDB connection via Mongoose
│   ├── types/
│   │   └── index.ts                # AuthUser & AuthRequest type definitions
│   ├── middleware/
│   │   └── auth.ts                 # Bearer token authentication middleware
│   ├── lib/
│   │   └── createNotification.ts   # Utility to persist notifications
│   ├── models/
│   │   ├── Problem.ts              # Problem schema (title, description, category, etc.)
│   │   ├── Comment.ts              # Comment schema (problemId, userId, content)
│   │   ├── Message.ts              # Conversation + Message schemas (messaging)
│   │   ├── Notification.ts         # Notification schema (userId, type, message)
│   │   └── ChatSession.ts          # AI Chat Session schema (solver + chat)
│   ├── controllers/
│   │   ├── problemController.ts    # Problem CRUD + stats
│   │   ├── commentController.ts    # Comment CRUD
│   │   ├── reactionController.ts   # Toggle like/love/sad
│   │   ├── aiController.ts         # AI solver, chat, sessions, quick-chat
│   │   ├── messageController.ts    # Users, conversations, messages
│   │   └── notificationController.ts # Fetch, mark read, mark all read
│   └── routes/
│       ├── problems.ts             # Problems, comments, reactions, stats
│       ├── ai.ts                   # AI solver, chat, sessions + quick-chat
│       ├── messages.ts             # Users, conversations, messages
│       └── notifications.ts        # Notifications
├── vercel.json                     # Vercel deployment config
├── tsconfig.json                   # TypeScript configuration
└── package.json                    # Dependencies & scripts

🔌 API Endpoints

Health

Method Path Auth Description
GET /api/health — Health check (status + timestamp)

AI — /api/ai

Method Path Auth Description
POST /api/ai/quick-chat — Ephemeral AI chat (streaming, nothing saved)
POST /api/ai/solver Bearer AI problem solver (streaming)
GET /api/ai/solver/sessions Bearer List solver sessions
GET /api/ai/solver/sessions/:id Bearer Get solver session detail
POST /api/ai/chat Bearer AI chat assistant (streaming)
GET /api/ai/chat/sessions Bearer List chat sessions
GET /api/ai/chat/sessions/:id Bearer Get chat session detail
DELETE /api/ai/chat/sessions/:id Bearer Delete chat session

POST /api/ai/quick-chat accepts { messages: [{ role, content }] } and streams an answer back. It requires no authentication and performs no database writes — the conversation history is sent by the client and kept only in memory for the duration of the request.

Problems — /api/problems

Method Path Auth Description
GET /api/problems — List problems (search, category, sort, page, limit)
GET /api/problems/:id — Get single problem by ID
POST /api/problems Bearer Create a new problem
PUT /api/problems/:id Bearer Update problem (owner only)
DELETE /api/problems/:id Bearer Delete problem (owner only)
GET /api/problems/my Bearer Get current user's problems
GET /api/problems/stats/my Bearer Get current user's stats
GET /api/problems/stats/overview — Get platform-wide stats

Reactions — /api/problems/:id/react

Method Path Auth Description
POST /api/problems/:id/react Bearer Toggle like/love/sad reaction

Comments — /api/problems/:id/comments

Method Path Auth Description
GET /api/problems/:id/comments — List comments for a problem
POST /api/problems/:id/comments Bearer Create a comment
PUT /api/problems/:id/comments/:commentId Bearer Update comment (owner only)
DELETE /api/problems/:id/comments/:commentId Bearer Delete comment (owner only)

Messages — /api

Method Path Auth Description
GET /api/users Bearer List all users (except current)
GET /api/messages/conversations Bearer List current user's conversations
POST /api/messages/conversations Bearer Get or create conversation with a user
GET /api/messages/conversations/:id Bearer Get messages in a conversation
POST /api/messages/conversations/:id Bearer Send a message

Notifications — /api

Method Path Auth Description
GET /api/notifications Bearer Get notifications (paginated)
GET /api/notifications/unread-count Bearer Get unread notification count
POST /api/notifications/:id/read Bearer Mark notification as read
POST /api/notifications/read-all Bearer Mark all notifications as read

🗄️ Database Models

Problem

Field Type Details
title String Required, trimmed
shortDescription String Required, trimmed
fullDescription String Required, trimmed
category String Required
priority String Enum: Low, Medium, High, Emergency
images String[] Array of image URLs
userId String Required (Better Auth user ID)
userName String Required
userImage String Optional
status String Enum: open, resolved (default: open)
reactions Object { likes: String[], loves: String[], sads: String[] }

Comment

Field Type Details
problemId ObjectId Ref: Problem, indexed
userId String Required
userName String Required
userImage String Optional
content String Required, trimmed

Conversation

Field Type Details
participants String[2] Exactly 2 user IDs, validated
lastMessage String Optional, message preview
lastMessageBy String User ID of last sender
lastMessageAt Date Timestamp of last message

Message

Field Type Details
conversationId ObjectId Ref: Conversation, indexed
senderId String Required
senderName String Required
senderImage String Optional
receiverId String Required
receiverName String Required
content String Required, trimmed
read Boolean Default: false

Notification

Field Type Details
userId String Required, indexed
type String Enum: problem_created, problem_resolved, new_comment, new_reaction, new_message
title String Required
message String Required
referenceId String Required (links to related entity)
read Boolean Default: false

ChatSession

Field Type Details
userId String Required, indexed
sessionType String Enum: solver, chat, indexed
title String Default: "New Session"
messages SubDoc[] Array of { role, content } with timestamps

The quick-chat mode intentionally uses no model — it never touches the database.


💻 Installation

Prerequisites

  • Node.js 18+
  • npm or yarn
  • MongoDB instance (local or Atlas)
  • Groq API key (for AI features)

Setup

# Clone the repository
git clone <repository-url>
cd backend

# Install dependencies
npm install

# Create environment file
cp .env.example .env

# Edit .env with your configuration
# Required variables:
#   MONGODB_URI
#   GROQ_API_KEY (for AI features)
# Optional:
#   PORT
#   CORS_ORIGIN

# Start development server with hot reload
npm run dev

The server will start at http://localhost:5000.

Available Scripts

Script Command Description
npm run dev tsx watch src/index.ts Development with hot reload
npm run build tsc Compile TypeScript to dist/
npm start node dist/index.js Run compiled production build

Environment Variables

Variable Required Default Description
PORT No 5000 Server port
MONGODB_URI Yes — MongoDB connection string
CORS_ORIGIN No http://localhost:3000 Allowed CORS origin
GROQ_API_KEY Yes (for AI) — Groq API key for AI features

🔐 Authentication Flow

  1. The frontend handles user registration/login via Better Auth (handled by Next.js API routes)
  2. Better Auth stores sessions in the session MongoDB collection
  3. All protected API routes require an Authorization: Bearer <token> header
  4. The authenticate middleware validates the token, checks expiry, and attaches req.user
  5. User IDs are used throughout the system for ownership and notification routing

The /api/ai/quick-chat endpoint is intentionally public so the floating chat widget works for both visitors and logged-in users — it performs no DB writes and stores nothing.


🔔 Notification Triggers

Notifications are automatically created by controllers when these events occur:

Event Triggered By Receiver
Problem created createProblem Post author
Problem resolved updateProblem (status → resolved) Post author
New comment createComment Problem owner (if not self)
New reaction toggleReaction Problem owner (if not self)
New message sendMessage Message recipient

🩺 Health Check

GET /api/health
Response: { "status": "ok", "timestamp": "2024-01-01T00:00:00.000Z" }

🤝 Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

📄 License

This project is private and not licensed for public distribution.

About

A RESTful API backend for a community-driven platform where users share life challenges, get AI-powered insights, and connect with others. Built with Express 5, TypeScript, and MongoDB (Mongoose).

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages