How amansrivstv/discount-service-unifize works
Project type and purpose
• This is a single-service, full-stack microservice (backend-only) implementing a discount engine.
• It exposes HTTP endpoints via FastAPI for calculating discounts and validating voucher codes.
• It uses SQLite (SQLAlchemy) for persistence, Pydantic for schema validation, a custom TTL cache, and pytest for testing.
File structure
• Top-level: config and orchestration files (Dockerfile, Makefile), source under app/, tests under tests/.
• app/main.py – entry point, defines FastAPI app, startup/shutdown lifecycles (runs DB migrations/seeding).
• app/api/ – API layer (routes + Pydantic schemas).
• app/services/ – business logic (the DiscountService).
• app/db/ – data layer (SQLAlchemy Base, models, seeding logic).
• app/core/ – cross-cutting concerns (config, errors, TTL cache).
• app/fake_data.py – seed-data definitions.
• tests/ – integration/unit tests using in-memory SQLite.
README for additional insights
• Describes the lifecycle: startup seeds DB, error handling, caching strategy.
• Lists the discount-calculation sequence and caching details.
Main components
a. Client (UI or any HTTP consumer)
b. FastAPI Application
• Router/Controller (routes.py)
• Schemas (Pydantic models for request/response)
• Lifecycle manager (startup seeding)
c. Service Layer
• DiscountService with methods calculate_cart_discounts and validate_discount_code
d. Core Utilities
• TTL Cache (in-memory with 5-minute TTL)
• Configuration loader (Settings)
• Error translator (maps domain errors to HTTP responses)
e. Data Layer
• SQLAlchemy Engine & Session
• Models (BrandDiscount, CategoryDiscount, BankOffer, Voucher)
• Seed module (loads fake_data.py at startup)
f. SQLite Database (file or in-memory)
g. External build/deploy artifacts
• Docker container
• Makefile targets
Relationships and interactions
• Client sends HTTP request → FastAPI route handler
• Handler invokes DiscountService
• DiscountService queries TTL Cache for discount maps, voucher, bank offers
• On cache miss, it fetches from DB via SQLAlchemy Session, populates the cache
• Service applies sequential discount logic: brand → category → voucher → bank offer
• Service returns a DiscountedPrice DTO
• FastAPI handler wraps in HTTP 200 or maps service errors to HTTP 400
• On startup, FastAPI app uses a lifespan context to:
• Create tables (via seed.create_tables)
• Seed initial data
Architectural patterns and design principles
• Layered architecture: API → Service → Data
• Single Responsibility: separate modules for schema, routing, business logic, data models, seeding, caching
• Dependency injection via FastAPI (session, settings)
• TTL caching to reduce DB hits
• Centralized error handling
Elements
• Clear labels (e.g., “FastAPI Router”, “DiscountService”, “TTL Cache”, “SQLite DB”)
• Rectangles for services/modules
• Parallelogram or cloud for external client
• Optional: annotate the Docker container boundary and configuration source (env vars → Settings)
Final recommendations
• Be very detailed: break down the service into controllers, service methods, cache operations, and data access.
• Startup sequence and normal request sequence separately or in a combined flow.
• Keep component labels consistent with package/module names in the file tree.