feat: add advanced analytics and territory management system

- Add comprehensive analytics components with export functionality
- Implement territory management with manager performance tracking
- Add seatmap components for venue layout management
- Create customer management features with modal interface
- Add advanced hooks for dashboard flags and territory data
- Implement seat selection and venue management utilities
- Add type definitions for ticketing and seatmap systems

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-08-26 09:25:10 -06:00
parent d5c3953888
commit aa81eb5adb
438 changed files with 90509 additions and 2787 deletions

View File

@@ -0,0 +1,289 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const globals_1 = require("@jest/globals");
/**
* Integration tests for hardened Stripe Connect functionality
*
* These tests demonstrate the key hardening features:
* - Idempotency protection against duplicate webhooks
* - Transactional inventory management preventing overselling
* - Platform fee configuration
* - Refund safety with organization validation
*
* Note: These are example tests showing the patterns.
* In a real environment, you'd use Firebase Test SDK and mock Stripe.
*/
(0, globals_1.describe)('Stripe Connect Hardening Integration Tests', () => {
(0, globals_1.beforeAll)(async () => {
// Initialize test Firebase project
// Initialize test Stripe environment
console.log('Setting up integration test environment...');
});
(0, globals_1.afterAll)(async () => {
// Clean up test data
console.log('Cleaning up test environment...');
});
(0, globals_1.describe)('Idempotency Protection', () => {
(0, globals_1.test)('should handle duplicate webhook delivery gracefully', async () => {
/**
* Test Scenario:
* 1. Create a checkout session
* 2. Simulate successful payment webhook
* 3. Send the same webhook again (simulate Stripe retry)
* 4. Verify only one set of tickets was created
*/
const sessionId = 'cs_test_idempotency_123';
const orgId = 'org_test_123';
const eventId = 'event_test_123';
const ticketTypeId = 'tt_test_123';
const quantity = 2;
// First webhook delivery
const firstWebhookPayload = {
id: 'evt_test_1',
type: 'checkout.session.completed',
account: 'acct_test_123',
data: {
object: {
id: sessionId,
metadata: {
orgId,
eventId,
ticketTypeId,
quantity: quantity.toString(),
type: 'ticket_purchase'
},
customer_details: {
email: 'test@example.com',
name: 'Test User'
},
amount_total: 10000,
currency: 'usd',
payment_intent: 'pi_test_123'
}
}
};
// TODO: Send first webhook and verify tickets created
// const firstResponse = await sendWebhook(firstWebhookPayload);
// expect(firstResponse.status).toBe(200);
// TODO: Verify tickets were created
// const tickets = await getTicketsBySession(sessionId);
// expect(tickets).toHaveLength(quantity);
// Second webhook delivery (duplicate)
const secondWebhookPayload = { ...firstWebhookPayload, id: 'evt_test_2' };
// TODO: Send duplicate webhook
// const secondResponse = await sendWebhook(secondWebhookPayload);
// expect(secondResponse.status).toBe(200);
// TODO: Verify no additional tickets were created
// const ticketsAfterDuplicate = await getTicketsBySession(sessionId);
// expect(ticketsAfterDuplicate).toHaveLength(quantity); // Same count
// TODO: Verify processedSessions document shows idempotency skip
// const processedSession = await getProcessedSession(sessionId);
// expect(processedSession.status).toBe('completed');
(0, globals_1.expect)(true).toBe(true); // Placeholder for actual test implementation
});
});
(0, globals_1.describe)('Inventory Concurrency Control', () => {
(0, globals_1.test)('should prevent overselling with concurrent purchases', async () => {
/**
* Test Scenario:
* 1. Create ticket type with limited inventory (e.g., 3 tickets)
* 2. Simulate 3 concurrent purchases of 2 tickets each
* 3. Verify only the first purchase succeeds, others fail gracefully
* 4. Verify inventory is accurate (3 - 2 = 1 remaining)
*/
const ticketTypeId = 'tt_limited_inventory';
const initialInventory = 3;
const purchaseQuantity = 2;
// TODO: Setup ticket type with limited inventory
// await createTicketType({
// id: ticketTypeId,
// eventId: 'event_concurrency_test',
// inventory: initialInventory,
// sold: 0,
// price: 5000
// });
// Simulate 3 concurrent webhook deliveries
const concurrentWebhooks = Array.from({ length: 3 }, (_, i) => ({
id: `evt_concurrent_${i}`,
type: 'checkout.session.completed',
account: 'acct_test_123',
data: {
object: {
id: `cs_concurrent_${i}`,
metadata: {
orgId: 'org_test_123',
eventId: 'event_concurrency_test',
ticketTypeId,
quantity: purchaseQuantity.toString(),
type: 'ticket_purchase'
},
customer_details: {
email: `test${i}@example.com`,
name: `Test User ${i}`
},
amount_total: 10000,
currency: 'usd',
payment_intent: `pi_concurrent_${i}`
}
}
}));
// TODO: Send all webhooks concurrently
// const responses = await Promise.all(
// concurrentWebhooks.map(webhook => sendWebhook(webhook))
// );
// TODO: Verify only one purchase succeeded
// const successfulPurchases = responses.filter(r => r.status === 200);
// expect(successfulPurchases).toHaveLength(1);
// TODO: Verify final inventory is correct
// const finalTicketType = await getTicketType(ticketTypeId);
// expect(finalTicketType.inventory).toBe(initialInventory - purchaseQuantity);
// expect(finalTicketType.sold).toBe(purchaseQuantity);
(0, globals_1.expect)(true).toBe(true); // Placeholder for actual test implementation
});
});
(0, globals_1.describe)('Platform Fee Configuration', () => {
(0, globals_1.test)('should calculate fees using environment configuration', async () => {
/**
* Test Scenario:
* 1. Set custom platform fee configuration
* 2. Create checkout session
* 3. Verify correct platform fee calculation
*/
// TODO: Set environment variables
process.env.PLATFORM_FEE_BPS = '250'; // 2.5%
process.env.PLATFORM_FEE_FIXED = '25'; // $0.25
const checkoutRequest = {
orgId: 'org_test_123',
eventId: 'event_test_123',
ticketTypeId: 'tt_test_123',
quantity: 2,
customerEmail: 'test@example.com'
};
// TODO: Create checkout session
// const response = await createCheckoutSession(checkoutRequest);
// expect(response.status).toBe(200);
// TODO: Verify platform fee calculation
// Expected for $50 ticket x 2 = $100:
// Platform fee = (10000 * 250 / 10000) + 25 = 250 + 25 = 275 cents ($2.75)
// const expectedPlatformFee = 275;
// expect(response.data.platformFee).toBe(expectedPlatformFee);
(0, globals_1.expect)(true).toBe(true); // Placeholder for actual test implementation
});
});
(0, globals_1.describe)('Refund Safety', () => {
(0, globals_1.test)('should validate organization ownership before processing refund', async () => {
/**
* Test Scenario:
* 1. Create order for organization A
* 2. Attempt refund from organization B
* 3. Verify refund is rejected
* 4. Attempt refund from organization A
* 5. Verify refund succeeds
*/
const orderSessionId = 'cs_refund_test_123';
const correctOrgId = 'org_correct_123';
const wrongOrgId = 'org_wrong_123';
// TODO: Create order for correct organization
// await createOrder({
// sessionId: orderSessionId,
// orgId: correctOrgId,
// totalAmount: 10000,
// status: 'completed'
// });
// Attempt refund from wrong organization
const wrongOrgRefundRequest = {
orgId: wrongOrgId,
sessionId: orderSessionId
};
// TODO: Attempt refund with wrong org
// const wrongOrgResponse = await requestRefund(wrongOrgRefundRequest);
// expect(wrongOrgResponse.status).toBe(404);
// expect(wrongOrgResponse.data.error).toContain('Order not found for this organization');
// Attempt refund from correct organization
const correctOrgRefundRequest = {
orgId: correctOrgId,
sessionId: orderSessionId
};
// TODO: Attempt refund with correct org
// const correctOrgResponse = await requestRefund(correctOrgRefundRequest);
// expect(correctOrgResponse.status).toBe(200);
// expect(correctOrgResponse.data.refundId).toBeDefined();
(0, globals_1.expect)(true).toBe(true); // Placeholder for actual test implementation
});
});
(0, globals_1.describe)('Structured Logging', () => {
(0, globals_1.test)('should log all operations with consistent structure', async () => {
/**
* Test Scenario:
* 1. Perform various operations (checkout, webhook, refund)
* 2. Verify all logs follow structured format
* 3. Verify critical information is logged
*/
// TODO: Capture logs during operations
// const logCapture = startLogCapture();
// TODO: Perform operations
// await createCheckoutSession({ ... });
// await processWebhook({ ... });
// await requestRefund({ ... });
// TODO: Verify log structure
// const logs = logCapture.getLogs();
//
// logs.forEach(log => {
// expect(log).toMatchObject({
// timestamp: expect.any(String),
// level: expect.stringMatching(/^(info|warn|error)$/),
// message: expect.any(String),
// action: expect.any(String)
// });
// });
// TODO: Verify specific actions are logged
// const actions = logs.map(log => log.action);
// expect(actions).toContain('checkout_create_start');
// expect(actions).toContain('checkout_create_success');
// expect(actions).toContain('webhook_received');
// expect(actions).toContain('ticket_purchase_success');
(0, globals_1.expect)(true).toBe(true); // Placeholder for actual test implementation
});
});
});
/**
* Helper functions for integration tests
* These would be implemented with actual Firebase and Stripe test SDKs
*/
// async function sendWebhook(payload: any) {
// // Implementation would use test HTTP client
// return { status: 200, data: { received: true } };
// }
// async function getTicketsBySession(sessionId: string) {
// // Implementation would query Firestore test database
// return [];
// }
// async function getProcessedSession(sessionId: string) {
// // Implementation would query processedSessions collection
// return { sessionId, status: 'completed' };
// }
// async function createTicketType(ticketType: any) {
// // Implementation would create test ticket type in Firestore
// }
// async function getTicketType(ticketTypeId: string) {
// // Implementation would query Firestore for ticket type
// return { inventory: 0, sold: 0 };
// }
// async function createCheckoutSession(request: any) {
// // Implementation would call checkout creation function
// return { status: 200, data: { url: 'https://checkout.stripe.com/...', sessionId: 'cs_...' } };
// }
// async function createOrder(order: any) {
// // Implementation would create test order in Firestore
// }
// async function requestRefund(request: any) {
// // Implementation would call refund function
// return { status: 200, data: { refundId: 'ref_...' } };
// }
// function startLogCapture() {
// // Implementation would capture console.log calls
// return {
// getLogs: () => []
// };
// }
// # sourceMappingURL=stripeConnect.integration.test.js.map