File size: 4,230 Bytes
34367da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import { prisma } from '../../database/prisma.js';
import { PalEventInput } from '@widget-tdc/mcp-types';

export class PalRepository {
  // User Profile operations
  async getUserProfile(userId: string, orgId: string): Promise<any | null> {
    const profile = await prisma.palUserProfile.findUnique({
      where: {
        userId_orgId: { userId, orgId },
      },
    });

    if (!profile) return null;

    return {
      id: profile.id,
      user_id: profile.userId,
      org_id: profile.orgId,
      preference_tone: profile.preferenceTone,
      created_at: profile.createdAt,
      updated_at: profile.updatedAt,
    };
  }

  async createUserProfile(userId: string, orgId: string, preferenceTone: string = 'neutral'): Promise<number> {
    const profile = await prisma.palUserProfile.create({
      data: {
        userId,
        orgId,
        preferenceTone,
      },
    });

    return profile.id;
  }

  async updateUserProfile(userId: string, orgId: string, preferenceTone: string): Promise<void> {
    await prisma.palUserProfile.update({
      where: {
        userId_orgId: { userId, orgId },
      },
      data: {
        preferenceTone,
      },
    });
  }

  // Focus Windows operations
  async getFocusWindows(userId: string, orgId: string): Promise<any[]> {
    const windows = await prisma.palFocusWindow.findMany({
      where: { userId, orgId },
      orderBy: [
        { weekday: 'asc' },
        { startHour: 'asc' },
      ],
    });

    return windows.map(w => ({
      id: w.id,
      user_id: w.userId,
      org_id: w.orgId,
      weekday: w.weekday,
      start_hour: w.startHour,
      end_hour: w.endHour,
      created_at: w.createdAt,
    }));
  }

  async addFocusWindow(userId: string, orgId: string, weekday: number, startHour: number, endHour: number): Promise<number> {
    const window = await prisma.palFocusWindow.create({
      data: {
        userId,
        orgId,
        weekday,
        startHour,
        endHour,
      },
    });

    return window.id;
  }

  // Event operations
  async recordEvent(event: PalEventInput): Promise<number> {
    const palEvent = await prisma.palEvent.create({
      data: {
        userId: event.userId,
        orgId: event.orgId,
        eventType: event.eventType,
        payload: event.payload || {},
        detectedStressLevel: event.detectedStressLevel != null ? Number(event.detectedStressLevel) : null,
      },
    });

    return palEvent.id;
  }

  async getRecentEvents(userId: string, orgId: string, limit: number = 20): Promise<any[]> {
    const events = await prisma.palEvent.findMany({
      where: { userId, orgId },
      orderBy: { createdAt: 'desc' },
      take: limit,
    });

    return events.map(e => ({
      id: e.id,
      user_id: e.userId,
      org_id: e.orgId,
      event_type: e.eventType,
      payload: e.payload,
      detected_stress_level: e.detectedStressLevel,
      created_at: e.createdAt,
    }));
  }

  async getEventsByType(userId: string, orgId: string, eventType: string, limit: number = 10): Promise<any[]> {
    const events = await prisma.palEvent.findMany({
      where: { userId, orgId, eventType },
      orderBy: { createdAt: 'desc' },
      take: limit,
    });

    return events.map(e => ({
      id: e.id,
      user_id: e.userId,
      org_id: e.orgId,
      event_type: e.eventType,
      payload: e.payload,
      detected_stress_level: e.detectedStressLevel,
      created_at: e.createdAt,
    }));
  }

  async getStressLevelDistribution(userId: string, orgId: string, hoursBack: number = 24): Promise<any[]> {
    const since = new Date(Date.now() - hoursBack * 60 * 60 * 1000);

    const events = await prisma.palEvent.groupBy({
      by: ['detectedStressLevel'],
      where: {
        userId,
        orgId,
        detectedStressLevel: { not: null },
        createdAt: { gte: since },
      },
      _count: {
        detectedStressLevel: true,
      },
    });

    return events.map(e => ({
      detected_stress_level: e.detectedStressLevel,
      count: e._count.detectedStressLevel,
    }));
  }
}