Files
computer/test-email.mjs
zeekay d2d5607201 feat: Production-ready platform with Stripe, Email, Analytics, Rate Limiting
Complete Production Features (1-7):
====================================

1. STRIPE PAYMENT INTEGRATION ✅
   - Full checkout flow with Stripe Elements
   - Payment intent creation for one-time purchases
   - Subscription creation for monthly GPU leasing
   - Success page with order confirmation
   - Serverless functions: create-payment-intent, create-subscription
   - PCI-compliant card input with glass-morphism styling
   - Error handling and loading states
   - Test mode ready with Stripe test keys

2. EMAIL NOTIFICATION SYSTEM ✅
   - SendGrid integration with professional templates
   - Quote emails with validity period
   - Order confirmations with invoice details
   - Subscription activation emails
   - RFQ confirmations (customer + admin)
   - Cluster request notifications
   - HTML + plain text versions
   - Responsive email design with Hanzo branding
   - API endpoints: send-quote, send-order, send-subscription, send-rfq

3. PDF INVOICE GENERATION ✅
   - @react-pdf/renderer integration
   - Professional invoice templates with Hanzo branding
   - Automatic generation on order payment
   - Download from customer dashboard
   - Supabase storage integration
   - Admin invoice management
   - Regenerate capability
   - Email invoice attachments ready

4. USAGE ANALYTICS DASHBOARD ✅
   - Comprehensive customer analytics at /analytics
   - Interactive charts: Line, Bar, Pie, Area charts
   - Metrics: compute hours, costs, GPU utilization
   - Date range selector (7/30/90 days, all time)
   - CSV export functionality
   - Usage tracking with Recharts
   - Real-time data from Supabase
   - Sample data generator for demo

5. API RATE LIMITING ✅
   - Upstash Redis-based rate limiting
   - Role-based limits: anonymous (10/min), auth (100/min), admin (1000/min)
   - Endpoint-specific limits for sensitive operations
   - 429 Too Many Requests responses
   - Rate limit headers (X-RateLimit-*)
   - Frontend retry with exponential backoff
   - Admin dashboard for monitoring
   - Clear rate limits endpoint for admins
   - Database logging and violation tracking

6. STRIPE WEBHOOK HANDLERS ✅
   - Webhook endpoint: /api/webhooks/stripe
   - Event handlers: payment success/failure, subscriptions, invoices, refunds
   - Signature verification for security
   - Idempotency handling (prevent duplicates)
   - Automatic order creation on payment
   - Subscription lifecycle management
   - Email triggers on events
   - Admin retry capability
   - Comprehensive event logging
   - Database schema for webhook tracking

7. ADMIN ANALYTICS & CHARTS ✅
   - Enhanced Admin Dashboard with Analytics tab
   - Revenue charts: time series, by GPU type, cumulative
   - Conversion funnel: RFQ → Quote → Order
   - Customer growth metrics
   - Operational metrics: response times, quote performance
   - Top customers table
   - Interactive Recharts visualizations
   - Date range filtering
   - Auto-refresh every 5 minutes
   - CSV export for reports
   - Key metrics cards with % change indicators

Technical Implementation:
========================

New Dependencies:
- @stripe/stripe-js, @stripe/react-stripe-js, stripe
- @sendgrid/mail
- @react-pdf/renderer
- recharts, date-fns
- @upstash/ratelimit, @upstash/redis
- micro (webhook body parsing)

New Files Created (50+):
- API routes: /api/webhooks/, /api/admin/, /api/create-*, /api/send-*
- Libraries: stripe.ts, email.ts, invoices.ts, analytics.ts, rateLimit.ts
- Components: Invoice, WebhookEventLog, RateLimitDashboard, DashboardInvoices
- Pages: Analytics, CheckoutSuccess, admin/RateLimitDashboard
- Documentation: EMAIL_NOTIFICATIONS.md, STRIPE_WEBHOOKS.md, RATE_LIMITING.md
- Database: Supabase migrations for webhooks, analytics, rate limiting

Environment Variables Required:
- STRIPE_SECRET_KEY, STRIPE_WEBHOOK_SECRET
- VITE_STRIPE_PUBLISHABLE_KEY
- SENDGRID_API_KEY, EMAIL_FROM, ADMIN_EMAIL
- UPSTASH_REDIS_REST_URL, UPSTASH_REDIS_REST_TOKEN
- ADMIN_API_KEY

Build Status:
=============
✓ Production build: 807KB gzipped
✓ 3480 modules transformed
✓ Build time: 5.20s
✓ All TypeScript validated
✓ Zero compilation errors

Ready for Launch:
=================
✅ Complete payment processing
✅ Professional email communications
✅ Automated invoicing
✅ Customer analytics
✅ API protection
✅ Webhook automation
✅ Business intelligence dashboard

The platform is now enterprise-ready for production deployment!
2025-10-31 13:55:31 -07:00

126 lines
5.0 KiB
JavaScript

#!/usr/bin/env node
/**
* Test script for email functionality
* Run with: node test-email.mjs
*/
import sgMail from '@sendgrid/mail';
// Test configuration
const SENDGRID_API_KEY = process.env.VITE_SENDGRID_API_KEY || 'SG.test-key';
const FROM_EMAIL = '[email protected]';
const TEST_EMAIL = '[email protected]'; // Replace with your test email
// Initialize SendGrid
sgMail.setApiKey(SENDGRID_API_KEY);
// Test email data
const testQuoteData = {
quoteNumber: 'TEST-001',
customerName: 'John Doe',
customerEmail: TEST_EMAIL,
gpuModel: 'NVIDIA H100',
quantity: 8,
duration: 'Monthly lease',
pricePerHour: 2.50,
totalPrice: 3600.00,
validUntil: new Date(Date.now() + 30 * 24 * 60 * 60 * 1000).toLocaleDateString(),
message: 'This is a test quote. Special pricing applied for your use case.'
};
// Simple HTML template for testing
const getTestEmailHtml = (data) => `
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Test Email - Hanzo Computer</title>
</head>
<body style="margin: 0; padding: 20px; font-family: Arial, sans-serif; background-color: #f5f5f5;">
<div style="max-width: 600px; margin: 0 auto; background: white; padding: 30px; border-radius: 10px; box-shadow: 0 2px 10px rgba(0,0,0,0.1);">
<h1 style="color: #FF6B35; border-bottom: 2px solid #FF6B35; padding-bottom: 10px;">Hanzo Computer</h1>
<h2>Your GPU Quote is Ready</h2>
<p>Hi ${data.customerName},</p>
<p>Thank you for your interest in Hanzo Computer GPU services. Here's your quote:</p>
<table style="width: 100%; border-collapse: collapse; margin: 20px 0;">
<tr style="background-color: #f9f9f9;">
<td style="padding: 10px; border: 1px solid #ddd;"><strong>Quote Number:</strong></td>
<td style="padding: 10px; border: 1px solid #ddd;">#${data.quoteNumber}</td>
</tr>
<tr>
<td style="padding: 10px; border: 1px solid #ddd;"><strong>GPU Model:</strong></td>
<td style="padding: 10px; border: 1px solid #ddd;">${data.gpuModel}</td>
</tr>
<tr style="background-color: #f9f9f9;">
<td style="padding: 10px; border: 1px solid #ddd;"><strong>Quantity:</strong></td>
<td style="padding: 10px; border: 1px solid #ddd;">${data.quantity} GPUs</td>
</tr>
<tr>
<td style="padding: 10px; border: 1px solid #ddd;"><strong>Duration:</strong></td>
<td style="padding: 10px; border: 1px solid #ddd;">${data.duration}</td>
</tr>
<tr style="background-color: #f9f9f9;">
<td style="padding: 10px; border: 1px solid #ddd;"><strong>Price per Hour:</strong></td>
<td style="padding: 10px; border: 1px solid #ddd;">$${data.pricePerHour.toFixed(2)}</td>
</tr>
<tr>
<td style="padding: 10px; border: 1px solid #ddd;"><strong>Total Price:</strong></td>
<td style="padding: 10px; border: 1px solid #ddd; font-size: 18px; color: #FF6B35;"><strong>$${data.totalPrice.toFixed(2)}</strong></td>
</tr>
</table>
${data.message ? `<p style="background-color: #fff3cd; border-left: 4px solid #ffc107; padding: 10px; margin: 20px 0;">${data.message}</p>` : ''}
<p>This quote is valid until <strong>${data.validUntil}</strong></p>
<div style="text-align: center; margin: 30px 0;">
<a href="https://hanzo.computer/dashboard" style="display: inline-block; padding: 12px 30px; background: linear-gradient(135deg, #FF6B35, #F7931E); color: white; text-decoration: none; border-radius: 5px; font-weight: bold;">View Quote in Dashboard</a>
</div>
<hr style="border: none; border-top: 1px solid #ddd; margin: 30px 0;">
<p style="color: #666; font-size: 12px; text-align: center;">
© ${new Date().getFullYear()} Hanzo AI Inc. All rights reserved.<br>
Techstars '17 | Building the future of AI infrastructure
</p>
</div>
</body>
</html>
`;
// Test function
async function testSendEmail() {
const msg = {
to: TEST_EMAIL,
from: FROM_EMAIL,
subject: `Test Quote #${testQuoteData.quoteNumber} - Hanzo Computer`,
text: `Hi ${testQuoteData.customerName}, Your GPU quote #${testQuoteData.quoteNumber} is ready. Total: $${testQuoteData.totalPrice}. View at https://hanzo.computer/dashboard`,
html: getTestEmailHtml(testQuoteData)
};
try {
console.log('Sending test email to:', TEST_EMAIL);
console.log('Using API key:', SENDGRID_API_KEY.substring(0, 10) + '...');
await sgMail.send(msg);
console.log('✅ Test email sent successfully!');
console.log('Check your inbox at:', TEST_EMAIL);
} catch (error) {
console.error('❌ Failed to send test email:');
if (error.response) {
console.error('Status:', error.response.status);
console.error('Body:', error.response.body);
} else {
console.error(error);
}
console.log('\n📋 Make sure to:');
console.log('1. Set VITE_SENDGRID_API_KEY environment variable');
console.log('2. Replace TEST_EMAIL with your actual email address');
console.log('3. Verify SendGrid account is active');
}
}
// Run test
testSendEmail();