# Wholesale Customer Approval Workflow - Visual Diagram

```
┌─────────────────────────────────────────────────────────────────────────────┐
│                         CUSTOMER REGISTRATION FLOW                           │
└─────────────────────────────────────────────────────────────────────────────┘

                              ┌──────────────┐
                              │   Customer   │
                              │  Visits Site │
                              └──────┬───────┘
                                     │
                                     ▼
                          ┌──────────────────────┐
                          │  /register Page      │
                          │  Choose Account Type │
                          └──────────┬───────────┘
                                     │
                    ┌────────────────┴────────────────┐
                    │                                 │
                    ▼                                 ▼
        ┌───────────────────────┐       ┌───────────────────────┐
        │  WHOLESALE PARTNER    │       │  BUSINESS CUSTOMER    │
        │  (ISP/Telecom)        │       │  (Company/Individual) │
        └───────────┬───────────┘       └───────────┬───────────┘
                    │                               │
                    ▼                               ▼
        ┌───────────────────────┐       ┌───────────────────────┐
        │  Fill Registration    │       │  Fill Registration    │
        │  + Upload POTRAZ      │       │  (No POTRAZ needed)   │
        │  License (Required)   │       │                       │
        └───────────┬───────────┘       └───────────┬───────────┘
                    │                               │
                    ▼                               ▼
        ┌───────────────────────┐       ┌───────────────────────┐
        │  Submit Registration  │       │  Submit Registration  │
        └───────────┬───────────┘       └───────────┬───────────┘
                    │                               │
                    ▼                               ▼
        ┌───────────────────────┐       ┌───────────────────────┐
        │  Backend Processing   │       │  Backend Processing   │
        │  - Create customer    │       │  - Create customer    │
        │  - approval_status:   │       │  - approval_status:   │
        │    'pending'          │       │    'approved'         │
        │  - Store POTRAZ file  │       │  - No file needed     │
        │  - Create in Odoo     │       │  - Create in Odoo     │
        │    (active=false)     │       │    (active=true)      │
        │  - Send admin email   │       │  - Send welcome email │
        └───────────┬───────────┘       └───────────┬───────────┘
                    │                               │
                    ▼                               ▼
        ┌───────────────────────┐       ┌───────────────────────┐
        │  Show Success Screen  │       │  Redirect to Login    │
        │  "Pending Approval"   │       │  "Can Login Now"      │
        └───────────┬───────────┘       └───────────┬───────────┘
                    │                               │
                    │                               ▼
                    │                   ┌───────────────────────┐
                    │                   │  Customer Logs In     │
                    │                   │  ✅ SUCCESS           │
                    │                   │  Access Dashboard     │
                    │                   └───────────────────────┘
                    │
                    ▼
        ┌───────────────────────┐
        │  Customer Tries Login │
        │  ❌ BLOCKED            │
        │  "Pending Approval"   │
        └───────────┬───────────┘
                    │
                    │
        ┌───────────┴───────────────────────────────────────────┐
        │                                                         │
        │              ADMIN APPROVAL PROCESS                     │
        │                                                         │
        │  1. Admin receives email with POTRAZ license           │
        │  2. Admin reviews in Odoo                              │
        │  3. Admin verifies:                                    │
        │     - POTRAZ license validity                          │
        │     - Company credentials                              │
        │     - TIN/VAT numbers                                  │
        │                                                         │
        │  4. Admin Decision:                                    │
        │                                                         │
        │     ┌─────────────┐              ┌─────────────┐      │
        │     │   APPROVE   │              │   REJECT    │      │
        │     └──────┬──────┘              └──────┬──────┘      │
        │            │                            │              │
        │            ▼                            ▼              │
        │  ┌──────────────────┐        ┌──────────────────┐    │
        │  │ Set active=true  │        │ Add rejection    │    │
        │  │ in Odoo          │        │ note in comment  │    │
        │  └──────┬───────────┘        └──────┬───────────┘    │
        │         │                            │                │
        │         │                            │                │
        └─────────┼────────────────────────────┼────────────────┘
                  │                            │
                  ▼                            ▼
      ┌───────────────────────┐   ┌───────────────────────┐
      │  TODO: Webhook Sync   │   │  TODO: Webhook Sync   │
      │  Update portal:       │   │  Update portal:       │
      │  approval_status:     │   │  approval_status:     │
      │  'approved'           │   │  'rejected'           │
      └───────────┬───────────┘   └───────────┬───────────┘
                  │                            │
                  ▼                            ▼
      ┌───────────────────────┐   ┌───────────────────────┐
      │  TODO: Send Email     │   │  TODO: Send Email     │
      │  "Account Approved"   │   │  "Account Rejected"   │
      └───────────┬───────────┘   └───────────────────────┘
                  │
                  ▼
      ┌───────────────────────┐
      │  Customer Logs In     │
      │  ✅ SUCCESS           │
      │  Access Dashboard     │
      └───────────────────────┘


┌─────────────────────────────────────────────────────────────────────────────┐
│                         DATABASE STATE DIAGRAM                               │
└─────────────────────────────────────────────────────────────────────────────┘

WHOLESALE CUSTOMER STATES:
┌──────────────────────────────────────────────────────────────────────────┐
│                                                                          │
│  ┌─────────────┐    Admin      ┌─────────────┐    Admin     ┌────────┐ │
│  │   PENDING   │  ─────────>   │  APPROVED   │  <────────   │REJECTED│ │
│  │             │   Approves     │             │   Re-reviews │        │ │
│  │ Can't Login │                │ Can Login   │              │Can't   │ │
│  │ active=false│                │ active=true │              │Login   │ │
│  └─────────────┘                └─────────────┘              └────────┘ │
│                                                                          │
└──────────────────────────────────────────────────────────────────────────┘

BUSINESS CUSTOMER STATES:
┌──────────────────────────────────────────────────────────────────────────┐
│                                                                          │
│  ┌─────────────┐                                                        │
│  │  APPROVED   │  (Auto-approved on registration)                       │
│  │             │                                                         │
│  │ Can Login   │                                                         │
│  │ active=true │                                                         │
│  └─────────────┘                                                        │
│                                                                          │
└──────────────────────────────────────────────────────────────────────────┘


┌─────────────────────────────────────────────────────────────────────────────┐
│                         FILE UPLOAD FLOW                                     │
└─────────────────────────────────────────────────────────────────────────────┘

Frontend:
┌──────────────────────────────────────────────────────────────────────────┐
│  1. User clicks "Choose File"                                            │
│  2. Selects PDF/JPG/PNG file                                             │
│  3. Frontend validates:                                                  │
│     - File type (PDF, JPG, PNG only)                                     │
│     - File size (max 5MB)                                                │
│  4. Shows preview with file name and size                                │
│  5. User can remove and re-upload                                        │
│  6. On submit, creates FormData with file                                │
└──────────────────────────────────────────────────────────────────────────┘
                                    │
                                    ▼
Backend:
┌──────────────────────────────────────────────────────────────────────────┐
│  1. Receives multipart/form-data request                                 │
│  2. Validates file again:                                                │
│     - Type: mimes:pdf,jpg,jpeg,png                                       │
│     - Size: max:5120 (5MB)                                               │
│  3. Stores file:                                                         │
│     - Path: storage/app/potraz_licenses/                                 │
│     - Name: potraz_{timestamp}_{original_name}                           │
│  4. Saves path to database:                                              │
│     - potraz_license_path field                                          │
│  5. Attaches to admin email                                              │
└──────────────────────────────────────────────────────────────────────────┘


┌─────────────────────────────────────────────────────────────────────────────┐
│                         EMAIL NOTIFICATION FLOW                              │
└─────────────────────────────────────────────────────────────────────────────┘

Wholesale Registration:
┌──────────────────────────────────────────────────────────────────────────┐
│  Customer Submits                                                        │
│         │                                                                │
│         ▼                                                                │
│  ┌─────────────────────────────────────────────────────────────┐        │
│  │  Email to: COMPANY_MAIL (tawona@quatrohaus.com)            │        │
│  │  Subject: "New Wholesale Partner Registration"             │        │
│  │  Content:                                                   │        │
│  │    - Company details (name, TIN, VAT, industry)            │        │
│  │    - Contact person (name, email, phone, job title)        │        │
│  │    - Portal customer ID                                     │        │
│  │    - Odoo partner ID                                        │        │
│  │    - Link to Odoo record                                    │        │
│  │  Attachment: POTRAZ license file                            │        │
│  └─────────────────────────────────────────────────────────────┘        │
│         │                                                                │
│         ▼                                                                │
│  Admin Reviews and Approves/Rejects                                     │
└──────────────────────────────────────────────────────────────────────────┘

TODO - Customer Notifications:
┌──────────────────────────────────────────────────────────────────────────┐
│  On Approval:                                                            │
│    Email to: customer@email.com                                          │
│    Subject: "Your AFINET Account Has Been Approved"                     │
│    Content: Login instructions, welcome message                         │
│                                                                          │
│  On Rejection:                                                           │
│    Email to: customer@email.com                                          │
│    Subject: "AFINET Account Registration Update"                        │
│    Content: Rejection reason, support contact                           │
└──────────────────────────────────────────────────────────────────────────┘


┌─────────────────────────────────────────────────────────────────────────────┐
│                         SECURITY LAYERS                                      │
└─────────────────────────────────────────────────────────────────────────────┘

Layer 1: Frontend Validation
┌──────────────────────────────────────────────────────────────────────────┐
│  - File type check (PDF, JPG, PNG)                                      │
│  - File size check (max 5MB)                                            │
│  - Form validation (required fields)                                    │
│  - Password strength                                                     │
└──────────────────────────────────────────────────────────────────────────┘

Layer 2: Backend Validation
┌──────────────────────────────────────────────────────────────────────────┐
│  - Laravel validation rules                                              │
│  - File MIME type verification                                           │
│  - File size limit enforcement                                           │
│  - Email uniqueness check                                                │
└──────────────────────────────────────────────────────────────────────────┘

Layer 3: Authentication
┌──────────────────────────────────────────────────────────────────────────┐
│  - Login blocked for unapproved customers                                │
│  - No token issued for pending/rejected                                  │
│  - Sanctum token authentication                                          │
└──────────────────────────────────────────────────────────────────────────┘

Layer 4: Authorization
┌──────────────────────────────────────────────────────────────────────────┐
│  - CheckApprovalStatus middleware                                        │
│  - Blocks access to protected routes                                     │
│  - Checks approval_status on every request                               │
└──────────────────────────────────────────────────────────────────────────┘

Layer 5: Odoo Integration
┌──────────────────────────────────────────────────────────────────────────┐
│  - Unapproved customers: active=false (archived)                         │
│  - Cannot create orders/invoices in Odoo                                 │
│  - Double verification layer                                             │
└──────────────────────────────────────────────────────────────────────────┘

Layer 6: File Storage
┌──────────────────────────────────────────────────────────────────────────┐
│  - Files stored outside public directory                                 │
│  - Path: storage/app/potraz_licenses/                                    │
│  - Not directly accessible via URL                                       │
│  - Sanitized file names                                                  │
└──────────────────────────────────────────────────────────────────────────┘
```

---

## Quick Reference

### Customer Types:
- **Wholesale**: ISPs, telecom partners → Requires approval
- **Business**: Companies, individuals → Auto-approved

### Approval Statuses:
- **pending**: Awaiting admin review (wholesale only)
- **approved**: Can access portal
- **rejected**: Cannot access portal

### File Requirements:
- **Types**: PDF, JPG, JPEG, PNG
- **Size**: Max 5MB
- **Required**: Wholesale only

### Key Endpoints:
- `POST /api/auth/register` - Registration with file upload
- `POST /api/auth/login` - Login with approval check
- `GET /api/dashboard` - Protected route (requires approval)

### Key Middleware:
- `auth:sanctum` - Authentication check
- `check.approval` - Approval status check

### Email Recipients:
- **Admin**: `COMPANY_MAIL` (tawona@quatrohaus.com)
- **Customer**: TODO (approval/rejection notifications)

---

**Legend:**
- ✅ = Success / Allowed
- ❌ = Blocked / Denied
- → = Flow direction
- TODO = Future enhancement
