File size: 3,106 Bytes
4c41b3d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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}`);
    }
  }
}