How tavt-dev/aivira-backend works
Purpose and Runtime
Aivira is a Java 21 / Spring Boot 4 REST backend for a single-vendor online bookstore. It exposes public storefront/catalog/blog APIs, authenticated customer commerce APIs, and admin management APIs. The process starts at src/main/java/com/tien/aivirabackend/AiviraBackendApplication.java; it is containerized by Dockerfile.
HTTP, Security, and API Boundary
Controllers form the external REST boundary, with request/response DTOs and a uniform API envelope. Security uses JWT access tokens, refresh-token sessions/cookies, custom JWT decoding, and hybrid role/direct-permission RBAC. Correlation IDs and centralized error conversion cross-cut all requests.
• src/main/java/com/tien/aivirabackend/config/SecurityConfig.java
• src/main/java/com/tien/aivirabackend/controller/AuthenticationController.java
• src/main/java/com/tien/aivirabackend/exception/GlobalExceptionHandler.java
Identity and Authorization
Authentication supports registration/email OTP verification, credentials, password reset, refresh/logout, session tracking, and Google OAuth state/ticket exchange. Authorization combines roles, a default permission matrix, and per-user grants. Token, OAuth, OTP, user, role, and permission state are persisted via JPA.
• src/main/java/com/tien/aivirabackend/service/auth/AuthenticationServiceImpl.java
• src/main/java/com/tien/aivirabackend/service/auth/GoogleOAuthServiceImpl.java
• src/main/java/com/tien/aivirabackend/service/rbac/AuthorizationServiceImpl.java
Catalog, Content, and Storefront
Catalog management models book-oriented Products, categories, variations/inventory, and Cloudinary-backed media. Public product search/detail and storefront-home aggregation are separated from admin mutations. The blog subsystem manages categories, rich-text posts/assets, related books, and HTML sanitization.
• src/main/java/com/tien/aivirabackend/service/catalog/ProductServiceImpl.java
• src/main/java/com/tien/aivirabackend/service/storefront/StorefrontServiceImpl.java
• src/main/java/com/tien/aivirabackend/service/blog/BlogPostServiceImpl.java
Commerce and Discount Flow
Customer flow is address → cart → non-mutating checkout preview → one-order checkout. Checkout validates cart, address, inventory, and discounts; promotions apply at item level before order-level coupons. Order, cart, address, inventory, coupon usage, and refund records are persistence-backed domain boundaries.
• src/main/java/com/tien/aivirabackend/service/commerce/CheckoutServiceImpl.java
• src/main/java/com/tien/aivirabackend/service/commerce/OrderServiceImpl.java
• src/main/java/com/tien/aivirabackend/service/discount/DiscountServiceImpl.java
Payments and Asynchronous Lifecycle
Payment creation groups order payments and delegates provider-specific work to COD, VNPay, or MoMo clients. Callback/IPN handling validates provider results and drives payment/order/coupon transitions; online coupon usage is reserved then finalized/released on outcome. A scheduler expires unresolved payments, while reconciliation and manual refunds are admin operations.
• src/main/java/com/tien/aivirabackend/service/payment/PaymentServiceImpl.java
• src/main/java/com/tien/aivirabackend/service/payment/PaymentExpiryScheduler.java
• src/main/java/com/tien/aivirabackend/service/payment/provider/VnpayPaymentProviderClient.java
Persistence, Integrations, and Operations
MySQL is accessed through Spring Data JPA; Flyway migrations establish and evolve the schema before JPA initialization. External integrations are Cloudinary storage, Brevo email/SMTP OTP delivery, Google OAuth, and VNPay/MoMo. Profiles and environment-driven configuration support local through production deployments; startup can optionally seed RBAC/admin and demo catalog data.
• src/main/resources/db/migration/V1__init_schema.sql
• src/main/java/com/tien/aivirabackend/config/FlywayJpaDependencyConfig.java
• src/main/java/com/tien/aivirabackend/config/ApplicationInitConfig.java
Main entry points: AuthenticationController.java REST controllers · PaymentServiceImpl.java Payments · CheckoutServiceImpl.java Checkout · AuthenticationServiceImpl.java Authentication
Components 21
REST & Security
- REST controllers · Storefront, customer, and admin APIs use DTOs and a common envelope.
src/main/java/com/tien/aivirabackend/controller/AuthenticationController.java - JWT security · JWT decoding, refresh sessions, and hybrid RBAC secure requests.
src/main/java/com/tien/aivirabackend/config/SecurityConfig.java - Error handling · Centralized API error conversion and correlation IDs.
src/main/java/com/tien/aivirabackend/exception/GlobalExceptionHandler.java
Domain Services
- Authentication · Registration, OTP, credentials, reset, refresh, logout, and sessions.
src/main/java/com/tien/aivirabackend/service/auth/AuthenticationServiceImpl.java - Google OAuth · OAuth state and ticket exchange.
src/main/java/com/tien/aivirabackend/service/auth/GoogleOAuthServiceImpl.java - Authorization · Roles, default permissions, and per-user grants.
src/main/java/com/tien/aivirabackend/service/rbac/AuthorizationServiceImpl.java - Catalog · Products, categories, variations, inventory, and media.
src/main/java/com/tien/aivirabackend/service/catalog/ProductServiceImpl.java - Storefront aggregation · Public home, search, and product-detail read flows.
src/main/java/com/tien/aivirabackend/service/storefront/StorefrontServiceImpl.java - Blog content · Posts, assets, related books, and HTML sanitization.
src/main/java/com/tien/aivirabackend/service/blog/BlogPostServiceImpl.java - Checkout · Validates cart, address, inventory, and discounts before creating one order.
src/main/java/com/tien/aivirabackend/service/commerce/CheckoutServiceImpl.java - Orders · Order lifecycle and refund records.
src/main/java/com/tien/aivirabackend/service/commerce/OrderServiceImpl.java - Discounts · Item promotions precede order coupons and coupon usage tracking.
src/main/java/com/tien/aivirabackend/service/discount/DiscountServiceImpl.java - Payments · Groups order payments and processes provider callbacks.
src/main/java/com/tien/aivirabackend/service/payment/PaymentServiceImpl.java - Payment expiry · Expires unresolved payments.
src/main/java/com/tien/aivirabackend/service/payment/PaymentExpiryScheduler.java
Data & Integrations
- JPA / MySQL · Persistent state for identity, catalog, commerce, payments, and content.
- Flyway migrations · Schema evolves before JPA initialization.
src/main/resources/db/migration/V1__init_schema.sql - Flyway-JPA ordering · Ensures migrations run before JPA.
src/main/java/com/tien/aivirabackend/config/FlywayJpaDependencyConfig.java - Payment providers · COD, VNPay, and MoMo payment processing.
src/main/java/com/tien/aivirabackend/service/payment/provider/VnpayPaymentProviderClient.java - External services · Cloudinary media, Brevo email/SMTP, and Google OAuth.
- Optional data seeding · Seeds RBAC/admin and demo catalog data when enabled.
src/main/java/com/tien/aivirabackend/config/ApplicationInitConfig.java
Other components
- Spring Boot app · Java 21 REST backend process.
src/main/java/com/tien/aivirabackend/AiviraBackendApplication.java
Connections 30
- Spring Boot app → JWT security: starts
- Spring Boot app → Flyway-JPA ordering: initializes
- Spring Boot app → Optional data seeding: optionally runs
- REST controllers → JWT security: secured by
- REST controllers → Error handling: errors handled by
- REST controllers → Authentication: identity requests
- REST controllers → Catalog: catalog management
- REST controllers → Storefront aggregation: public reads
- REST controllers → Checkout: checkout requests
- REST controllers → Payments: payment callbacks
- JWT security → Authentication: validates tokens
- JWT security → Authorization: checks access
- Authentication → Google OAuth: delegates OAuth
- Authentication → External services: sends OTP email
- Storefront aggregation → Catalog: aggregates catalog
- Checkout → Discounts: prices promotions
- Checkout → Orders: creates order
- Checkout → Catalog: validates inventory
- Orders → Payments: requires payment
- Payments → Payment providers: delegates provider work
- Payments → Discounts: reserves/finalizes coupons
- Payment expiry → Payments: expires pending payments
- Flyway-JPA ordering → Flyway migrations: runs first
- Flyway migrations → JPA / MySQL: creates schema
- Authentication → JPA / MySQL: persists identity
- Catalog → JPA / MySQL: persists catalog
- Blog content → JPA / MySQL: persists content
- Checkout → JPA / MySQL: reads cart and address
- Orders → JPA / MySQL: persists orders
- Payments → JPA / MySQL: persists payment state