Skip to content

Sakemo/sgvs-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

72 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🏒 SGVS API - Simplified Sales Management System

Status Java Version Spring Boot PostgreSQL License


πŸ“‹ Description

SGVS API is a robust RESTful backend application built with Spring Boot that provides complete management capabilities for sales, inventory, customers, and expenses. Designed to deliver a "CEO experience" with business intelligence, the API integrates real-time inflation tracking and delivers analytical reports to help small and medium-sized businesses optimize their financial operations.


API Response in Insomnia

Here's an example of an authenticated request successfully returning a product list:

GET http://localhost:8081/api/v1/products
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...

βœ… Status: 200 OK

{
  "data": [
    {
      "id": 1,
      "name": "Product A",
      "sku": "SKU-001",
      "cost": 50.00,
      "price": 99.90,
      "profitMargin": 99.80,
      "stock": 150,
      "category": "Electronics"
    },
    {
      "id": 2,
      "name": "Product B",
      "sku": "SKU-002",
      "cost": 30.00,
      "price": 79.90,
      "profitMargin": 166.33,
      "stock": 45
    }
  ],
  "meta": {
    "total": 2,
    "page": 0,
    "limit": 20
  }
}

πŸ’‘ Tip: All responses follow consistent RESTful patterns with appropriate HTTP status codes (200 OK, 400 Bad Request, 401 Unauthorized, 500 Server Error).


πŸ›  Technologies Used

Backend

  • Java 21 β€” Modern language with cutting-edge features
  • Spring Boot 3.5.3 β€” Web framework and security
  • Spring Data JPA β€” Object-relational mapping (ORM)
  • Spring Security + JWT β€” Stateless authentication with tokens
  • OAuth2 Google β€” Integrated social login

Database

  • PostgreSQL β€” Robust relational database
  • Hibernate β€” ORM for automatic persistence

External Integration

  • Google OAuth 2.0 β€” Authentication via Google

Tools & Quality

  • Maven β€” Dependency management and build automation
  • Lombok β€” Reduces boilerplate (getters, setters, constructors)
  • JUnit 5 + Mockito β€” Unit testing with coverage
  • Docker β€” Containerization for deployment

✨ Key Features

πŸ” Authentication & Security

  • βœ… User registration and login with robust validation
  • βœ… JWT authentication with configurable token expiration
  • βœ… Google OAuth 2.0 login with integrated profile
  • βœ… Role-based access control (ADMIN, USER)

πŸ“¦ Product Management

  • βœ… Complete CRUD operations on products
  • βœ… Automatic profit margin calculation
  • βœ… Inventory control (global, per-item, or disabled)
  • βœ… Categorization and unique SKU
  • βœ… ABC report (top-selling products)

πŸ‘₯ Customer Management

  • βœ… Complete CRUD for customers with contact validation
  • βœ… Advanced filters and pagination
  • βœ… Purchase history tracking
  • βœ… Support for generic customer (sale without specific customer)

πŸ’° Sales Management

  • βœ… Sales recording with multiple items
  • βœ… Automatic calculation of totals (gross and net)
  • βœ… Per-item discount control
  • βœ… Automatic inventory updates
  • βœ… Sales reports by period

πŸ’Έ Expense Management

  • βœ… Registration of operational expenses
  • βœ… Restocking expenses (automatically update inventory)
  • βœ… Expense categorization
  • βœ… Advanced filters and reports

πŸ“Š Dashboard & Reports

  • βœ… Summary metric cards (revenue, profit, inventory)
  • βœ… Sales evolution charts
  • βœ… Product ABC report
  • βœ… Integration with Central Bank inflation indices

🌍 Global Settings

  • βœ… Inventory control modes (global, per-item, or disabled)
  • βœ… Currency and localization
  • βœ… Business parameters (default margins, etc.)

πŸŽ“ The Process and Learnings

The Process

We started by designing the database schema based on a real-world sales management business model. The greatest challenge was balancing ease of use with business robustness β€” many startups fail because they over-engineer simple things.

The first major milestone was implementing the Products and Sales API with precise profit calculations. We faced initial challenges with:

  1. Automatic inventory updates β€” We needed to synchronize sales and restocking expenses without creating inconsistencies. Solution: transactions with @Transactional in Spring.
  2. Efficient pagination β€” Thousands of records loading slowly. Solution: database indices on PostgreSQL + optimized queries with @Query in JPA.
  3. Inflation integration β€” We needed to fetch Central Bank indices but with safe fallback. Solution: scheduled task that updates cache periodically.

What I Learned

This project forced me to deeply understand:

  • Spring Security: Configuring JWT correctly is non-trivial. I now understand why many projects fail with token leaks.
  • Transaction Management: Operations modifying multiple tables require careful isolation. I learned when to use @Transactional and isolation levels.
  • API Design: Consistent naming, versioning (/api/v1/), and standardized responses make ALL the difference in maintainability.
  • Testing Strategy: Unit tests in services with Mockito revealed bugs that manual testing never caught, especially in calculation logic.
  • Infrastructure: Docker + PostgreSQL in production is different from h2 in tests. I learned about connectionPool, timeout, and real performance.

πŸš€ How to Run the Project

If you just want to USE the project, just click on the URL https://sgvs-ui.onrender.com/

TO DEVELOPERS

Prerequisites

You'll need to have installed:

  • Java 21+ (Download)
  • Maven 3.8+ (usually comes with IDEs like IntelliJ or Eclipse)
  • PostgreSQL 12+ (Download)
  • Git to clone the repository

Step 1: Clone the Repository

git clone https://github.com/your-username/sgvs-api.git
cd sgvs-api

Step 2: Configure Environment Variables

  1. Copy the example file:
cp .env.example .env
  1. Edit the .env file and set real values:
# Database
DB_PASSWORD=your-postgres-password-here

# JWT Secret (generate a random string with at least 32 characters)
# Tip: use `openssl rand -base64 32` to generate automatically
JWT_SECRET_KEY=your-random-32-character-minimum-secret-key-here

⚠️ IMPORTANT: Never commit real passwords! Use environment variables in production.

Step 3: Configure PostgreSQL Database

  1. Create the database:
CREATE DATABASE sgvs_db;
CREATE USER postgres WITH PASSWORD 'your-postgres-password-here';
GRANT ALL PRIVILEGES ON DATABASE sgvs_db TO postgres;
  1. Or use Docker (recommended):
docker run --name sgvs-postgres \
  -e POSTGRES_PASSWORD=your-password \
  -e POSTGRES_DB=sgvs_db \
  -p 5432:5432 \
  -d postgres:latest

Step 4: Export Environment Variables

On Linux/macOS:

export DB_PASSWORD='your-postgres-password-here'
export JWT_SECRET_KEY='your-random-32-character-minimum-secret-key-here'
export GOOGLE_CLIENT_ID='your-google-client-id'
export GOOGLE_CLIENT_SECRET='your-google-client-secret'

On Windows (PowerShell):

$env:DB_PASSWORD = 'your-postgres-password-here'
$env:JWT_SECRET_KEY = 'your-random-32-character-minimum-secret-key-here'
$env:GOOGLE_CLIENT_ID = 'your-google-client-id'
$env:GOOGLE_CLIENT_SECRET = 'your-google-client-secret'

Step 5: Run the Application

# With Maven wrapper (recommended)
./mvnw spring-boot:run

# Or with globally installed Maven
mvn spring-boot:run

The application will start at: http://localhost:8081

Step 6: Verify It's Running

curl http://localhost:8081/actuator/health

# Expected response:
# {"status":"UP"}

Step 7: Create Your First Account (Optional)

curl -X POST http://localhost:8081/api/auth/register \
  -H "Content-Type: application/json" \
  -d '{
    "username": "your-username",
    "email": "your-email@example.com",
    "password": "secure-password-here"
  }'

πŸ”Œ Environment Variables (.env.example)

The .env.example file is already included in the repository. Copy and configure:

# Database
DB_PASSWORD=your-secure-password-here

# JWT Security
# Generate with: openssl rand -base64 32
JWT_SECRET_KEY=your-32-character-minimum-secret-key-here

# (Optional) Google OAuth
# GOOGLE_CLIENT_ID=your-client-id.apps.googleusercontent.com
# GOOGLE_CLIENT_SECRET=your-client-secret
# GOOGLE_REDIRECT_URI=http://localhost:8081/api/auth/google/callback

πŸ“¦ Build & Deploy

Build for Production

mvn clean package -DskipTests

This generates an executable JAR in target/sgvs-api-0.0.1-SNAPSHOT.jar.

Run the JAR

java -jar target/sgvs-api-0.0.1-SNAPSHOT.jar

With Docker

docker build -t sgvs-api:latest .
docker run -p 8081:8081 \
  -e DB_PASSWORD='your-password' \
  -e JWT_SECRET_KEY='your-secret' \
  sgvs-api:latest

πŸ§ͺ Testing

Run unit tests:

# Run all tests
mvn test

# Run with coverage
mvn test jacoco:report

Tests cover main services: ProductService, SaleService, CustomerService, ExpenseService.


πŸ“š Documentation & Resources

  • API Docs: [Swagger UI] (coming soon)
  • Roadmap: See README.md for complete phases
  • Project Structure:
    • controller β€” REST Controllers
    • src/main/java/com/flick/business/core/service/ β€” Business logic
    • entity β€” JPA Entities
    • repository β€” Data access

🀝 Contributing

This is a portfolio project. If you find bugs or have suggestions, open an issue or pull request.


πŸ“„ License

This project is under the MIT license. See the LICENSE file for details.


πŸ‘¨β€πŸ’» Author

Developed with ❀️ as a modern full-stack business management project.

Frontend (React + Vite): sgvs-ui


πŸ”— Useful Links

About

Spring Boot REST API for flick.business, handling authentication, sales, inventory, customers, expenses, reports, and dashboard insights with JWT security and PostgreSQL.

Topics

Resources

Stars

Watchers

Forks

Contributors