Skip to content

Repository files navigation

CoupleBond Backend Service

Production-ready backend API for the CoupleBond mobile application. Handles authentication, couple pairing, activities, prayers, calendar events, and more.

πŸŽ‰ Recently Debugged & Improved!

This backend was completely debugged and refactored in November 2024. See COMPLETE_SUMMARY.md for details.

πŸš€ Quick Start

# 1. Install dependencies
npm install

# 2. Set up database
createdb cs262_couplebond
psql -U postgres -d cs262_couplebond -f migrations/001_couplebond_schema.sql

# 3. Configure environment (create .env file)
DATABASE_URL=postgres://postgres:YOUR_PASSWORD@localhost:5432/cs262_couplebond
JWT_SECRET=your_secret_key_here
PORT=4000

# 4. Run server
npm run dev

Server runs on http://localhost:4000

πŸ“š Documentation

πŸ”§ Tech Stack

  • Runtime: Node.js with TypeScript
  • Framework: Express.js
  • Database: PostgreSQL
  • Authentication: JWT + bcrypt
  • Features: Transactions, validation, error handling

πŸ“Š Database Schema

Tables: users, couples, pairing_codes, activities, photo_collages, calendar_events, prayer_items

Migration: migrations/001_couplebond_schema.sql

🌐 API Endpoints (37 total)

Auth

  • POST /api/auth/register - Create account
  • POST /api/auth/login - Login
  • GET /api/auth/me - Get current user

Profile

  • GET /api/user/profile - Get profile
  • PUT /api/user/profile - Update profile

Partner Pairing

  • POST /api/user/partner/generate-code - Generate pairing code
  • POST /api/user/partner/connect - Connect with partner
  • GET /api/user/partner - Get partner info
  • DELETE /api/user/partner/unmatch - Disconnect

Couple Management

  • POST /api/couple/create - Create couple
  • POST /api/couple/join - Join couple
  • GET /api/couple/me - Get couple info
  • DELETE /api/couple/leave - Leave couple

Activities

  • POST /api/activities - Create activity
  • GET /api/activities - Get all activities
  • GET /api/activities/:id - Get activity
  • PUT /api/activities/:id - Update activity
  • DELETE /api/activities/:id - Delete activity
  • POST /api/activities/:id/photos - Add photo
  • DELETE /api/activities/:activityId/photos/:photoId - Delete photo
  • GET /api/activities/timeline/all - Timeline view

Calendar

  • POST /api/calendar/events - Create event
  • GET /api/calendar/events - Get all events
  • GET /api/calendar/events/:id - Get event
  • PUT /api/calendar/events/:id - Update event
  • DELETE /api/calendar/events/:id - Delete event
  • GET /api/calendar/upcoming - Upcoming events
  • GET /api/calendar/anniversaries - Calculate anniversaries

Prayers

  • POST /api/prayers - Create prayer
  • GET /api/prayers - Get all prayers
  • GET /api/prayers/:id - Get prayer
  • PUT /api/prayers/:id - Update prayer
  • PUT /api/prayers/:id/toggle-answered - Toggle answered
  • DELETE /api/prayers/:id - Delete prayer

See API_TESTING_GUIDE.md for curl commands.

πŸ” Environment Variables

Create .env file:

# Database connection
DATABASE_URL=postgres://username:password@host:5432/database

# JWT secret (change in production!)
JWT_SECRET=your_super_secret_key

# Server port
PORT=4000

πŸ§ͺ Testing

# Health check
curl http://localhost:4000/api/health

# Register user
curl -X POST http://localhost:4000/api/auth/register \
  -H "Content-Type: application/json" \
  -d '{"email":"test@example.com","password":"pass123","name":"Test User"}'

Full testing guide: API_TESTING_GUIDE.md

πŸ“¦ Scripts

npm run dev      # Development mode with auto-restart
npm run build    # Compile TypeScript to JavaScript
npm start        # Production mode (requires build first)

🎯 Key Features

βœ… Transaction Safety - All multi-step operations atomic βœ… Permission Checks - Users can only access their couple's data βœ… Input Validation - All user input validated βœ… Standardized Responses - Consistent { success, data, message } format βœ… Error Handling - Comprehensive error catching with proper status codes βœ… Security - JWT auth, bcrypt passwords, SQL injection prevention βœ… Performance - Database indexes, connection pooling, optimized queries

πŸ“ Project Structure

Service/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ db.ts                    # Database connection & transactions
β”‚   β”œβ”€β”€ index.ts                 # Express app setup
β”‚   β”œβ”€β”€ middleware/
β”‚   β”‚   └── auth.ts             # JWT authentication
β”‚   └── routes/
β”‚       β”œβ”€β”€ auth.ts             # Auth endpoints
β”‚       β”œβ”€β”€ user.ts             # User profile & partner pairing
β”‚       β”œβ”€β”€ couple.ts           # Couple management
β”‚       β”œβ”€β”€ activities.ts       # Activities & photos
β”‚       β”œβ”€β”€ calendar.ts         # Calendar events
β”‚       └── prayers.ts          # Prayer items
β”œβ”€β”€ migrations/
β”‚   └── 001_couplebond_schema.sql  # Database schema
β”œβ”€β”€ package.json
β”œβ”€β”€ tsconfig.json
└── .env (create this)

πŸ› Troubleshooting

"Connection refused"

  • Check PostgreSQL is running: pg_ctl status
  • Verify DATABASE_URL is correct

"relation does not exist"

  • Run migration: psql -U postgres -d your_db -f migrations/001_couplebond_schema.sql

"Invalid token"

  • Token expired (7 days) - login again
  • JWT_SECRET changed - regenerate tokens

"No couple found"

  • User must be paired first using generate-code + connect flow

πŸ”„ Migration from Old Schema

If you have an existing database with old migrations:

See MIGRATION_GUIDE.md for step-by-step instructions.

🀝 For React Native Developers

Response Format

// Success
{
  success: true,
  data: { ... },
  message?: string
}

// Error
{
  success: false,
  message: string
}

Authentication

Include token in all requests (except register/login):

headers: {
  'Authorization': `Bearer ${token}`,
  'Content-Type': 'application/json'
}

Status Codes

  • 200/201: Success
  • 400: Validation error
  • 401: Need to login
  • 404: Not found
  • 409: Already exists
  • 500: Server error

πŸ“ž Support

Questions? Check the documentation:

  1. COMPLETE_SUMMARY.md - Overview
  2. DEBUG_AND_IMPROVEMENTS_COMPLETE.md - Detailed report
  3. API_TESTING_GUIDE.md - API reference

πŸ† Status

βœ… Production Ready

  • All bugs fixed
  • All features implemented
  • Fully documented
  • Security hardened
  • Performance optimized

Related Repositories


Backend last updated: November 2024

  • game, player, playergame - for the monopoly game data

If you need to recreate the users table, run:

node setup-azure-db.js

5. Start the Service

npm run dev

The service will run on http://localhost:4000 (or the PORT specified in .env).

You should see:

[db] connected
Server running on port 4000, listening on all interfaces

Testing the API

Test the health endpoint:

Invoke-RestMethod -Uri http://localhost:4000/api/health

Test registration:

$body = @{ email="test@example.com"; password="password123" } | ConvertTo-Json
Invoke-RestMethod -Method Post -Uri "http://localhost:4000/api/auth/register" -ContentType "application/json" -Body $body

API Endpoints

Authentication

  • POST /api/auth/register - Register a new user
  • POST /api/auth/login - Login with email/password
  • GET /api/auth/me - Get current user (requires JWT token)

User

  • GET /api/user/profile - Get user profile (requires JWT token)

Health Check

  • GET /api/health - Check if service is running
  • GET / - Root endpoint

Common Issues

Connection Timeout

If you get ETIMEDOUT errors, your IP address needs to be added to the Azure firewall rules.

Authentication Failed

Make sure you're using the correct database credentials in your .env file.

Port Already in Use

If port 4000 is already in use, change the PORT in your .env file.

About

This one will eventually hold your data service application.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages