How nicolasm-83200/next-amazona-v2 works
Project Type and Purpose
• This is a full-stack e-commerce web application built with Next.js (using the new “app” directory), TypeScript, Tailwind CSS, and MongoDB via Mongoose.
• It provides both a customer-facing storefront (“app/(front)”) and an admin dashboard (“app/admin”), with authentication (NextAuth), order management, product management, and payment integrations (PayPal / Cloudinary).
High-Level Architecture Components
a. Client (Browser)
• React components served by Next.js, including customer pages (product listing, cart, checkout, profile) and admin pages (dashboard, orders, products, users).
b. Next.js Server
• Renders pages (Server Components / Client Components)
• Hosts API routes under app/api/… (e.g., /api/products, /api/orders, /api/auth, /api/admin/…)
• Applies middleware (middleware.ts) for auth checks or rewrites
c. Business Logic Layer
• lib/services/*.ts encapsulate domain operations (e.g., productService)
• lib/hooks provide client state management (e.g., useCartStore)
• NextAuth configuration (app/api/auth/[...nextauth])
d. Data Layer
• Mongoose models in lib/models (UserModel, ProductModel, OrderModel)
• dbConnect utility to establish MongoDB connection
e. External Services
• MongoDB Atlas (or self-hosted)
• PayPal REST API for payments (lib/paypal.ts, /api/orders/[id]/capture-paypal-order)
• Cloudinary for image uploads (app/api/cloudinary-sign)
f. Deployment Platform
• Vercel (leveraging next.config.mjs), environment variables
Relationships & Data Flow
1. User → Next.js Frontend
• Navigation triggers React components (pages under app/(front) or app/admin).
• Components call API via fetch or client hooks (e.g., /api/products).
2. Next.js API Routes → Business Logic
• API route handlers call service functions (lib/services)
• Services use Mongoose models and dbConnect to query/update MongoDB
3. Payment Flow
• Frontend requests PayPal order creation (/api/orders/[id]/create-paypal-order)
• PayPal SDK returns approval URL, user approves → capture route
4. Media Upload
• Frontend requests Cloudinary signature → uploads directly to Cloudinary
5. Auth Flow
• NextAuth “/api/auth/[...nextauth]” protects pages via middleware
• Profile API (/api/auth/profile) reads/writes user data
Architectural Patterns & Design Principles
• File-system routing (Next.js “app” directory) separates concerns by URL path.
• Server Components vs Client Components: use “use client” only where needed (e.g., CartDetails).
• Layered architecture: API routes → Services → Models → Database.
• Context / Global Providers: components/ClientProviders.tsx wraps React context (NextAuthProvider, Zustand store).
• Environment-agnostic config (next.config.mjs, tailwind.config.ts, tsconfig.json).