Spaces:
Sleeping
Sleeping
| import { getDb } from "./db"; | |
| import { eq } from "drizzle-orm"; | |
| import { users } from "../drizzle/schema"; | |
| export interface Notification { | |
| userId: number; | |
| title: string; | |
| message: string; | |
| type: "success" | "error" | "info" | "warning"; | |
| projectId?: number; | |
| timestamp: Date; | |
| } | |
| /** | |
| * Notification System for HackingFactory | |
| * Supports in-app notifications and push notifications | |
| */ | |
| export class NotificationManager { | |
| private static instance: NotificationManager; | |
| private notifications: Notification[] = []; | |
| private constructor() {} | |
| public static getInstance(): NotificationManager { | |
| if (!NotificationManager.instance) { | |
| NotificationManager.instance = new NotificationManager(); | |
| } | |
| return NotificationManager.instance; | |
| } | |
| /** | |
| * Send notification to user | |
| */ | |
| async sendNotification(notification: Notification) { | |
| this.notifications.push(notification); | |
| // In production, this would send to WebSocket or push service | |
| console.log(`[Notification] ${notification.type.toUpperCase()}: ${notification.title}`); | |
| console.log(`Message: ${notification.message}`); | |
| // Trigger push notification if available | |
| await this.triggerPushNotification(notification); | |
| } | |
| /** | |
| * Get notifications for user | |
| */ | |
| getUserNotifications(userId: number, limit: number = 10): Notification[] { | |
| return this.notifications | |
| .filter(n => n.userId === userId) | |
| .sort((a, b) => b.timestamp.getTime() - a.timestamp.getTime()) | |
| .slice(0, limit); | |
| } | |
| /** | |
| * Notify on project completion | |
| */ | |
| async notifyProjectComplete(userId: number, projectName: string, score: number, projectId: number) { | |
| await this.sendNotification({ | |
| userId, | |
| title: "Project Completed! 🎉", | |
| message: `"${projectName}" has been completed with a score of ${score}/100`, | |
| type: score >= 90 ? "success" : "info", | |
| projectId, | |
| timestamp: new Date(), | |
| }); | |
| } | |
| /** | |
| * Notify on iteration complete | |
| */ | |
| async notifyIterationComplete(userId: number, projectName: string, iteration: number, score: number, projectId: number) { | |
| await this.sendNotification({ | |
| userId, | |
| title: `Iteration ${iteration} Complete`, | |
| message: `Iteration ${iteration} of "${projectName}" scored ${score}/100`, | |
| type: score >= 90 ? "success" : "info", | |
| projectId, | |
| timestamp: new Date(), | |
| }); | |
| } | |
| /** | |
| * Notify on error | |
| */ | |
| async notifyError(userId: number, projectName: string, error: string, projectId: number) { | |
| await this.sendNotification({ | |
| userId, | |
| title: "Error Occurred", | |
| message: `Error in "${projectName}": ${error}`, | |
| type: "error", | |
| projectId, | |
| timestamp: new Date(), | |
| }); | |
| } | |
| /** | |
| * Trigger push notification (placeholder) | |
| */ | |
| private async triggerPushNotification(notification: Notification) { | |
| // In production, integrate with Firebase Cloud Messaging or similar | |
| // For now, this is a placeholder | |
| if (process.env.ENABLE_PUSH_NOTIFICATIONS === "true") { | |
| console.log(`[Push] Sending to user ${notification.userId}`); | |
| } | |
| } | |
| } | |