Initial commit: Port Nimara CRM (Layers 0-4)
Full CRM rebuild with Next.js 15, TypeScript, Tailwind, Drizzle ORM, PostgreSQL, Redis, BullMQ, MinIO, and Socket.io. Includes 461 source files covering clients, berths, interests/pipeline, documents/EOI, expenses/invoices, email, notifications, dashboard, admin, and client portal. CI/CD via Gitea Actions with Docker builds. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
89
src/lib/socket/events.ts
Normal file
89
src/lib/socket/events.ts
Normal file
@@ -0,0 +1,89 @@
|
||||
// Server → Client events
|
||||
export interface ServerToClientEvents {
|
||||
// Berth events
|
||||
'berth:statusChanged': (payload: { berthId: string; oldStatus?: string; newStatus: string; triggeredBy: string; trigger?: string }) => void;
|
||||
'berth:updated': (payload: { berthId: string; changedFields: string[] }) => void;
|
||||
'berth:waitingListChanged': (payload: { berthId: string; action: string; entry: unknown }) => void;
|
||||
'berth:maintenanceAdded': (payload: { berthId: string; logEntry: unknown }) => void;
|
||||
|
||||
// Client events
|
||||
'client:created': (payload: { clientId: string; clientName: string; source: string }) => void;
|
||||
'client:updated': (payload: { clientId: string; changedFields: string[] }) => void;
|
||||
'client:archived': (payload: { clientId: string }) => void;
|
||||
'client:restored': (payload: { clientId: string }) => void;
|
||||
'client:merged': (payload: { survivingId: string; mergedId: string }) => void;
|
||||
'client:noteAdded': (payload: { clientId: string; noteId: string; authorName: string; preview: string }) => void;
|
||||
'client:duplicateDetected': (payload: { clientAId: string; clientBId: string; score: number; reason: string }) => void;
|
||||
|
||||
// Interest events
|
||||
'interest:created': (payload: { interestId: string; clientId: string; berthId: string | null; source: string }) => void;
|
||||
'interest:updated': (payload: { interestId: string; changedFields: string[] }) => void;
|
||||
'interest:stageChanged': (payload: { interestId: string; oldStage: string; newStage: string; clientName: string; berthNumber: string }) => void;
|
||||
'interest:berthLinked': (payload: { interestId: string; berthId: string }) => void;
|
||||
'interest:berthUnlinked': (payload: { interestId: string; berthId: string }) => void;
|
||||
'interest:archived': (payload: { interestId: string }) => void;
|
||||
'interest:noteAdded': (payload: { interestId: string; noteId: string; authorName: string; preview: string }) => void;
|
||||
'interest:recommendationsGenerated': (payload: { interestId: string; count: number; topBerthId: string }) => void;
|
||||
'interest:recommendationAdded': (payload: { interestId: string; berthId: string; source: string; matchScore: number }) => void;
|
||||
'interest:leadCategoryChanged': (payload: { interestId: string; oldCategory: string; newCategory: string; auto: boolean }) => void;
|
||||
|
||||
// Document events
|
||||
'document:created': (payload: { documentId: string; type?: string; interestId?: string }) => void;
|
||||
'document:updated': (payload: { documentId: string; changedFields?: string[] }) => void;
|
||||
'document:deleted': (payload: { documentId: string }) => void;
|
||||
'document:sent': (payload: { documentId: string; type?: string; signerCount?: number; documensoId?: string }) => void;
|
||||
'document:signed': (payload: { documentId: string; signerName: string; signerRole: string; remainingSigners: number }) => void;
|
||||
'document:signer:signed': (payload: { documentId: string; signerName?: string; signerEmail?: string; signerRole?: string; order?: number }) => void;
|
||||
'document:completed': (payload: { documentId: string; type?: string; interestId?: string; clientName?: string }) => void;
|
||||
'document:expired': (payload: { documentId: string }) => void;
|
||||
'document:reminderSent': (payload: { documentId: string; recipientEmail: string }) => void;
|
||||
|
||||
// Document template events
|
||||
'documentTemplate:created': (payload: { templateId: string; name?: string; type?: string }) => void;
|
||||
'documentTemplate:updated': (payload: { templateId: string; changedFields?: string[] }) => void;
|
||||
'documentTemplate:deleted': (payload: { templateId: string }) => void;
|
||||
|
||||
// Financial events
|
||||
'expense:created': (payload: { expenseId: string; amount: number; currency: string; category: string }) => void;
|
||||
'expense:updated': (payload: { expenseId: string; changedFields: string[] }) => void;
|
||||
'expense:archived': (payload: { expenseId: string }) => void;
|
||||
'invoice:created': (payload: { invoiceId: string; invoiceNumber: string; total: number; clientName: string }) => void;
|
||||
'invoice:updated': (payload: { invoiceId: string; changedFields: string[] }) => void;
|
||||
'invoice:sent': (payload: { invoiceId: string; invoiceNumber: string; recipientEmail: string }) => void;
|
||||
'invoice:paid': (payload: { invoiceId: string; invoiceNumber: string; amount: number }) => void;
|
||||
'invoice:overdue': (payload: { invoiceId: string; invoiceNumber: string; daysPastDue: number }) => void;
|
||||
|
||||
// Reminder & Calendar events
|
||||
'reminder:created': (payload: { reminderId: string; title: string; assignedTo: string; dueAt: string }) => void;
|
||||
'reminder:updated': (payload: { reminderId: string; changedFields: string[] }) => void;
|
||||
'reminder:completed': (payload: { reminderId: string; title: string; completedBy: string }) => void;
|
||||
'reminder:overdue': (payload: { reminderId: string; title: string; dueAt: string }) => void;
|
||||
'reminder:snoozed': (payload: { reminderId: string; snoozedUntil: string }) => void;
|
||||
'calendar:synced': (payload: { eventCount: number; lastSyncAt: string }) => void;
|
||||
'calendar:disconnected': (payload: { reason: string }) => void;
|
||||
|
||||
// Notification events
|
||||
'notification:new': (payload: { notificationId: string; type: string; title: string; description: string; link: string }) => void;
|
||||
'notification:unreadCount': (payload: { count: number }) => void;
|
||||
|
||||
// Report events
|
||||
'report:queued': (payload: { reportId: string; reportType: string; name: string }) => void;
|
||||
'report:ready': (payload: { reportId: string; name: string }) => void;
|
||||
'report:failed': (payload: { reportId: string; name: string; error: string }) => void;
|
||||
|
||||
// System events
|
||||
'system:alert': (payload: { alertType: string; message: string; severity: string }) => void;
|
||||
'system:jobFailed': (payload: { queueName: string; jobId: string; error: string }) => void;
|
||||
'registration:new': (payload: { clientId: string; interestId: string; clientName: string; berthNumber: string }) => void;
|
||||
|
||||
// File events
|
||||
'file:uploaded': (payload: { fileId: string; filename: string; clientId?: string; category?: string }) => void;
|
||||
'file:updated': (payload: { fileId: string; changedFields?: string[] }) => void;
|
||||
'file:deleted': (payload: { fileId: string; filename?: string }) => void;
|
||||
}
|
||||
|
||||
// Client → Server events (minimal — most actions go through REST API)
|
||||
export interface ClientToServerEvents {
|
||||
'join:entity': (payload: { type: 'berth' | 'client' | 'interest'; id: string }) => void;
|
||||
'leave:entity': (payload: { type: 'berth' | 'client' | 'interest'; id: string }) => void;
|
||||
}
|
||||
103
src/lib/socket/server.ts
Normal file
103
src/lib/socket/server.ts
Normal file
@@ -0,0 +1,103 @@
|
||||
import { Server } from 'socket.io';
|
||||
import { createAdapter } from '@socket.io/redis-adapter';
|
||||
import type { Server as HTTPServer } from 'node:http';
|
||||
|
||||
import { redis } from '@/lib/redis';
|
||||
import { auth } from '@/lib/auth';
|
||||
import { logger } from '@/lib/logger';
|
||||
import type { ServerToClientEvents, ClientToServerEvents } from './events';
|
||||
|
||||
let io: Server<ClientToServerEvents, ServerToClientEvents> | null = null;
|
||||
|
||||
export function initSocketServer(httpServer: HTTPServer): Server<ClientToServerEvents, ServerToClientEvents> {
|
||||
const pubClient = redis.duplicate();
|
||||
const subClient = redis.duplicate();
|
||||
|
||||
io = new Server<ClientToServerEvents, ServerToClientEvents>(httpServer, {
|
||||
path: '/socket.io/',
|
||||
adapter: createAdapter(pubClient, subClient),
|
||||
cors: {
|
||||
origin: process.env.APP_URL,
|
||||
credentials: true,
|
||||
},
|
||||
connectionStateRecovery: { maxDisconnectionDuration: 2 * 60 * 1000 },
|
||||
maxHttpBufferSize: 1e6, // 1MB message limit
|
||||
});
|
||||
|
||||
// Auth middleware — validate session cookie via Better Auth
|
||||
io.use(async (socket, next) => {
|
||||
try {
|
||||
const cookie = socket.handshake.headers.cookie;
|
||||
if (!cookie) return next(new Error('Authentication required'));
|
||||
|
||||
// Parse session from cookie
|
||||
const session = await auth.api.getSession({
|
||||
headers: new Headers({ cookie }),
|
||||
});
|
||||
if (!session?.user) return next(new Error('Invalid session'));
|
||||
|
||||
// Enforce max 10 connections per user
|
||||
const userSockets = await io!.in(`user:${session.user.id}`).fetchSockets();
|
||||
if (userSockets.length >= 10) {
|
||||
return next(new Error('Maximum connections reached'));
|
||||
}
|
||||
|
||||
socket.data = {
|
||||
userId: session.user.id,
|
||||
portId: socket.handshake.auth.portId as string | undefined,
|
||||
};
|
||||
next();
|
||||
} catch {
|
||||
next(new Error('Authentication failed'));
|
||||
}
|
||||
});
|
||||
|
||||
// Connection handler
|
||||
io.on('connection', (socket) => {
|
||||
const { userId, portId } = socket.data as { userId: string; portId: string | undefined };
|
||||
logger.debug({ userId, portId }, 'Socket connected');
|
||||
|
||||
// Auto-join personal and port rooms
|
||||
socket.join(`user:${userId}`);
|
||||
if (portId) socket.join(`port:${portId}`);
|
||||
|
||||
// Entity-level room management
|
||||
socket.on('join:entity', ({ type, id }) => {
|
||||
socket.join(`${type}:${id}`);
|
||||
});
|
||||
socket.on('leave:entity', ({ type, id }) => {
|
||||
socket.leave(`${type}:${id}`);
|
||||
});
|
||||
|
||||
// Idle timeout (30 seconds — for development only, would be longer in prod)
|
||||
let idleTimer = setTimeout(() => socket.disconnect(), 30_000);
|
||||
socket.onAny(() => {
|
||||
clearTimeout(idleTimer);
|
||||
idleTimer = setTimeout(() => socket.disconnect(), 30_000);
|
||||
});
|
||||
|
||||
socket.on('disconnect', () => {
|
||||
clearTimeout(idleTimer);
|
||||
logger.debug({ userId }, 'Socket disconnected');
|
||||
});
|
||||
});
|
||||
|
||||
return io;
|
||||
}
|
||||
|
||||
export function getIO(): Server<ClientToServerEvents, ServerToClientEvents> {
|
||||
if (!io) throw new Error('Socket.io not initialized');
|
||||
return io;
|
||||
}
|
||||
|
||||
/**
|
||||
* Emit an event to a specific room. Used by service layer after mutations.
|
||||
*/
|
||||
export function emitToRoom<E extends keyof ServerToClientEvents>(
|
||||
room: string,
|
||||
event: E,
|
||||
...args: Parameters<ServerToClientEvents[E]>
|
||||
): void {
|
||||
if (!io) return;
|
||||
io.to(room).emit(event, ...args);
|
||||
}
|
||||
Reference in New Issue
Block a user