File size: 21,710 Bytes
fc1eb7c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
import { eq, desc, and, gte, lte } from "drizzle-orm";
import { db } from "./db";
import {
  users, roles, campaigns, tasks, metrics, activities, equipment, maintenanceRecords,
  remarks, inspectionChecklists, dailyInspections, notifications,
  type User, type Role, type Campaign, type Task, type Metric, type Activity,
  type Equipment, type MaintenanceRecord, type Remark, type InspectionChecklist,
  type DailyInspection, type Notification,
  type InsertUser, type InsertRole, type InsertCampaign, type InsertTask,
  type InsertMetric, type InsertActivity, type InsertEquipment,
  type InsertMaintenanceRecord, type InsertRemark, type InsertInspectionChecklist,
  type InsertDailyInspection, type InsertNotification
} from "../shared/schema";

export interface IStorage {
  // User operations
  getUser(id: number): Promise<User | undefined>;
  getUserByEmail(email: string): Promise<User | undefined>;
  getAllUsers(): Promise<User[]>;
  createUser(user: InsertUser): Promise<User>;
  updateUser(id: number, user: Partial<InsertUser>): Promise<User | undefined>;
  deleteUser(id: number): Promise<boolean>;
  
  // Role operations
  getRole(id: number): Promise<Role | undefined>;
  getRoleByName(name: string): Promise<Role | undefined>;
  getAllRoles(): Promise<Role[]>;
  createRole(role: InsertRole): Promise<Role>;
  updateRole(id: number, role: Partial<InsertRole>): Promise<Role | undefined>;
  deleteRole(id: number): Promise<boolean>;
  
  // Campaign operations
  getCampaign(id: number): Promise<Campaign | undefined>;
  getAllCampaigns(): Promise<Campaign[]>;
  createCampaign(campaign: InsertCampaign): Promise<Campaign>;
  updateCampaign(id: number, campaign: Partial<InsertCampaign>): Promise<Campaign | undefined>;
  deleteCampaign(id: number): Promise<boolean>;
  
  // Task operations
  getTask(id: number): Promise<Task | undefined>;
  getAllTasks(): Promise<Task[]>;
  getTasksByUserId(userId: number): Promise<Task[]>;
  getTasksByCampaignId(campaignId: number): Promise<Task[]>;
  getTasksByEquipmentId(equipmentId: string): Promise<Task[]>;
  getOverdueTasks(): Promise<Task[]>;
  getUpcomingTasks(days: number): Promise<Task[]>;
  createTask(task: InsertTask): Promise<Task>;
  updateTask(id: number, task: Partial<InsertTask>): Promise<Task | undefined>;
  deleteTask(id: number): Promise<boolean>;
  markTaskOverdue(taskId: number): Promise<Task | undefined>;
  
  // Metric operations
  getMetric(id: number): Promise<Metric | undefined>;
  getMetricByUserId(userId: number): Promise<Metric | undefined>;
  getAllMetrics(): Promise<Metric[]>;
  createMetric(metric: InsertMetric): Promise<Metric>;
  updateMetric(id: number, metric: Partial<InsertMetric>): Promise<Metric | undefined>;
  
  // Activity operations
  getActivity(id: number): Promise<Activity | undefined>;
  getAllActivities(): Promise<Activity[]>;
  getActivitiesByUserId(userId: number): Promise<Activity[]>;
  createActivity(activity: InsertActivity): Promise<Activity>;

  // Equipment operations
  getEquipment(id: string): Promise<Equipment | undefined>;
  getAllEquipment(): Promise<Equipment[]>;
  createEquipment(equipment: InsertEquipment): Promise<Equipment>;
  updateEquipment(id: string, equipment: Partial<InsertEquipment>): Promise<Equipment | undefined>;
  deleteEquipment(id: string): Promise<boolean>;

  // Maintenance record operations
  getMaintenanceRecord(id: number): Promise<MaintenanceRecord | undefined>;
  getAllMaintenanceRecords(): Promise<MaintenanceRecord[]>;
  createMaintenanceRecord(record: InsertMaintenanceRecord): Promise<MaintenanceRecord>;
  updateMaintenanceRecord(id: number, record: Partial<InsertMaintenanceRecord>): Promise<MaintenanceRecord | undefined>;
  deleteMaintenanceRecord(id: number): Promise<boolean>;

  // Remark operations
  getRemark(id: string): Promise<Remark | undefined>;
  getAllRemarks(): Promise<Remark[]>;
  createRemark(remark: InsertRemark): Promise<Remark>;
  updateRemark(id: string, remark: Partial<InsertRemark>): Promise<Remark | undefined>;
  deleteRemark(id: string): Promise<boolean>;

  // Inspection checklist operations
  getInspectionChecklist(id: number): Promise<InspectionChecklist | undefined>;
  getInspectionChecklistByEquipmentId(equipmentId: string): Promise<InspectionChecklist | undefined>;
  getAllInspectionChecklists(): Promise<InspectionChecklist[]>;
  createInspectionChecklist(checklist: InsertInspectionChecklist): Promise<InspectionChecklist>;
  updateInspectionChecklist(id: number, checklist: Partial<InsertInspectionChecklist>): Promise<InspectionChecklist | undefined>;
  deleteInspectionChecklist(id: number): Promise<boolean>;

  // Daily inspection operations
  getDailyInspection(id: number): Promise<DailyInspection | undefined>;
  getAllDailyInspections(): Promise<DailyInspection[]>;
  getDailyInspectionsByEquipmentId(equipmentId: string): Promise<DailyInspection[]>;
  getDailyInspectionsByDate(date: Date): Promise<DailyInspection[]>;
  createDailyInspection(inspection: InsertDailyInspection): Promise<DailyInspection>;
  updateDailyInspection(id: number, inspection: Partial<InsertDailyInspection>): Promise<DailyInspection | undefined>;
  deleteDailyInspection(id: number): Promise<boolean>;

  // Notification operations
  getNotification(id: number): Promise<Notification | undefined>;
  getAllNotifications(): Promise<Notification[]>;
  getNotificationsByUserId(userId: number): Promise<Notification[]>;
  getUnreadNotifications(userId: number): Promise<Notification[]>;
  getActiveNotifications(userId: number): Promise<Notification[]>;
  createNotification(notification: InsertNotification): Promise<Notification>;
  updateNotification(id: number, notification: Partial<InsertNotification>): Promise<Notification | undefined>;
  markNotificationAsRead(id: number): Promise<Notification | undefined>;
  archiveNotification(id: number): Promise<Notification | undefined>;
  deleteNotification(id: number): Promise<boolean>;
}

export class PostgreSQLStorage implements IStorage {
  constructor() {}

  // User operations
  async getUser(id: number): Promise<User | undefined> {
    const result = await db.select().from(users).where(eq(users.id, id));
    return result[0];
  }

  async getUserByEmail(email: string): Promise<User | undefined> {
    const result = await db.select().from(users).where(eq(users.email, email));
    return result[0];
  }

  async getAllUsers(): Promise<User[]> {
    return await db.select().from(users).orderBy(users.id);
  }

  async createUser(insertUser: InsertUser): Promise<User> {
    const result = await db.insert(users).values(insertUser).returning();
    return result[0];
  }

  async updateUser(id: number, updateData: Partial<InsertUser>): Promise<User | undefined> {
    const result = await db.update(users).set(updateData).where(eq(users.id, id)).returning();
    return result[0];
  }

  async deleteUser(id: number): Promise<boolean> {
    const result = await db.delete(users).where(eq(users.id, id)).returning();
    return result.length > 0;
  }

  // Equipment operations
  async getEquipment(id: string): Promise<Equipment | undefined> {
    const result = await db.select().from(equipment).where(eq(equipment.id, id));
    return result[0];
  }

  async getAllEquipment(): Promise<Equipment[]> {
    return await db.select().from(equipment);
  }

  async createEquipment(equipmentData: InsertEquipment): Promise<Equipment> {
    const result = await db.insert(equipment).values(equipmentData).returning();
    return result[0];
  }

  async updateEquipment(id: string, updateData: Partial<InsertEquipment>): Promise<Equipment | undefined> {
    const result = await db.update(equipment).set(updateData).where(eq(equipment.id, id)).returning();
    return result[0];
  }

  async deleteEquipment(id: string): Promise<boolean> {
    const result = await db.delete(equipment).where(eq(equipment.id, id)).returning();
    return result.length > 0;
  }

  // Maintenance record operations
  async getMaintenanceRecord(id: number): Promise<MaintenanceRecord | undefined> {
    const result = await db.select().from(maintenanceRecords).where(eq(maintenanceRecords.id, id));
    return result[0];
  }

  async getAllMaintenanceRecords(): Promise<MaintenanceRecord[]> {
    return await db.select().from(maintenanceRecords).orderBy(maintenanceRecords.scheduledDate);
  }

  async createMaintenanceRecord(recordData: InsertMaintenanceRecord): Promise<MaintenanceRecord> {
    const result = await db.insert(maintenanceRecords).values(recordData).returning();
    return result[0];
  }

  async updateMaintenanceRecord(id: number, updateData: Partial<InsertMaintenanceRecord>): Promise<MaintenanceRecord | undefined> {
    const result = await db.update(maintenanceRecords).set(updateData).where(eq(maintenanceRecords.id, id)).returning();
    return result[0];
  }

  async deleteMaintenanceRecord(id: number): Promise<boolean> {
    const result = await db.delete(maintenanceRecords).where(eq(maintenanceRecords.id, id)).returning();
    return result.length > 0;
  }

  // Remark operations
  async getRemark(id: string): Promise<Remark | undefined> {
    const result = await db.select().from(remarks).where(eq(remarks.id, parseInt(id)));
    return result[0];
  }

  async getAllRemarks(): Promise<Remark[]> {
    return await db.select().from(remarks).orderBy(desc(remarks.createdAt));
  }

  async createRemark(remarkData: InsertRemark): Promise<Remark> {
    const result = await db.insert(remarks).values(remarkData).returning();
    return result[0];
  }

  async updateRemark(id: string, updateData: Partial<InsertRemark>): Promise<Remark | undefined> {
    const result = await db.update(remarks).set(updateData).where(eq(remarks.id, parseInt(id))).returning();
    return result[0];
  }

  async deleteRemark(id: string): Promise<boolean> {
    try {
      const existingRemark = await db.select().from(remarks).where(eq(remarks.id, parseInt(id)));
      if (existingRemark.length === 0) {
        return false;
      }
      await db.delete(remarks).where(eq(remarks.id, parseInt(id)));
      return true;
    } catch (error) {
      console.error('Error deleting remark:', error);
      return false;
    }
  }

  // Inspection checklist operations
  async getInspectionChecklist(id: number): Promise<InspectionChecklist | undefined> {
    const result = await db.select().from(inspectionChecklists).where(eq(inspectionChecklists.id, id));
    return result[0];
  }

  async getInspectionChecklistByEquipmentId(equipmentId: string): Promise<InspectionChecklist | undefined> {
    const result = await db.select().from(inspectionChecklists).where(eq(inspectionChecklists.equipmentId, equipmentId));
    return result[0];
  }

  async getAllInspectionChecklists(): Promise<InspectionChecklist[]> {
    return await db.select().from(inspectionChecklists);
  }

  async createInspectionChecklist(checklistData: InsertInspectionChecklist): Promise<InspectionChecklist> {
    const result = await db.insert(inspectionChecklists).values(checklistData).returning();
    return result[0];
  }

  async updateInspectionChecklist(id: number, updateData: Partial<InsertInspectionChecklist>): Promise<InspectionChecklist | undefined> {
    const result = await db.update(inspectionChecklists).set(updateData).where(eq(inspectionChecklists.id, id)).returning();
    return result[0];
  }

  async deleteInspectionChecklist(id: number): Promise<boolean> {
    const result = await db.delete(inspectionChecklists).where(eq(inspectionChecklists.id, id)).returning();
    return result.length > 0;
  }

  // Daily inspection operations
  async getDailyInspection(id: number): Promise<DailyInspection | undefined> {
    const result = await db.select().from(dailyInspections).where(eq(dailyInspections.id, id));
    return result[0];
  }

  async getAllDailyInspections(): Promise<DailyInspection[]> {
    return await db.select().from(dailyInspections).orderBy(desc(dailyInspections.inspectionDate));
  }

  async getDailyInspectionsByEquipmentId(equipmentId: string): Promise<DailyInspection[]> {
    return await db.select().from(dailyInspections)
      .where(eq(dailyInspections.equipmentId, equipmentId))
      .orderBy(desc(dailyInspections.inspectionDate));
  }

  async getDailyInspectionsByDate(date: Date): Promise<DailyInspection[]> {
    const startOfDay = new Date(date);
    startOfDay.setHours(0, 0, 0, 0);
    const endOfDay = new Date(date);
    endOfDay.setHours(23, 59, 59, 999);
    
    return await db.select().from(dailyInspections)
      .where(and(
        gte(dailyInspections.inspectionDate, startOfDay),
        lte(dailyInspections.inspectionDate, endOfDay)
      ))
      .orderBy(dailyInspections.inspectionDate);
  }

  async createDailyInspection(inspectionData: InsertDailyInspection): Promise<DailyInspection> {
    const result = await db.insert(dailyInspections).values(inspectionData).returning();
    return result[0];
  }

  async updateDailyInspection(id: number, updateData: Partial<InsertDailyInspection>): Promise<DailyInspection | undefined> {
    const result = await db.update(dailyInspections).set(updateData).where(eq(dailyInspections.id, id)).returning();
    return result[0];
  }

  async deleteDailyInspection(id: number): Promise<boolean> {
    const result = await db.delete(dailyInspections).where(eq(dailyInspections.id, id)).returning();
    return result.length > 0;
  }

  // Role operations
  async getRole(id: number): Promise<Role | undefined> {
    const result = await db.select().from(roles).where(eq(roles.id, id));
    return result[0];
  }

  async getRoleByName(name: string): Promise<Role | undefined> {
    const result = await db.select().from(roles).where(eq(roles.name, name));
    return result[0];
  }

  async getAllRoles(): Promise<Role[]> {
    return await db.select().from(roles);
  }

  async createRole(roleData: InsertRole): Promise<Role> {
    const result = await db.insert(roles).values(roleData).returning();
    return result[0];
  }

  async updateRole(id: number, updateData: Partial<InsertRole>): Promise<Role | undefined> {
    const result = await db.update(roles).set(updateData).where(eq(roles.id, id)).returning();
    return result[0];
  }

  async deleteRole(id: number): Promise<boolean> {
    const result = await db.delete(roles).where(eq(roles.id, id)).returning();
    return result.length > 0;
  }

  // Campaign operations
  async getCampaign(id: number): Promise<Campaign | undefined> {
    const result = await db.select().from(campaigns).where(eq(campaigns.id, id));
    return result[0];
  }

  async getAllCampaigns(): Promise<Campaign[]> {
    return await db.select().from(campaigns);
  }

  async createCampaign(campaignData: InsertCampaign): Promise<Campaign> {
    const result = await db.insert(campaigns).values(campaignData).returning();
    return result[0];
  }

  async updateCampaign(id: number, updateData: Partial<InsertCampaign>): Promise<Campaign | undefined> {
    const result = await db.update(campaigns).set(updateData).where(eq(campaigns.id, id)).returning();
    return result[0];
  }

  async deleteCampaign(id: number): Promise<boolean> {
    const result = await db.delete(campaigns).where(eq(campaigns.id, id)).returning();
    return result.length > 0;
  }

  // Task operations
  async getTask(id: number): Promise<Task | undefined> {
    const result = await db.select().from(tasks).where(eq(tasks.id, id));
    return result[0];
  }

  async getAllTasks(): Promise<Task[]> {
    return await db.select().from(tasks).orderBy(desc(tasks.createdAt));
  }

  async getTasksByUserId(userId: number): Promise<Task[]> {
    return await db.select().from(tasks)
      .where(eq(tasks.userId, userId))
      .orderBy(desc(tasks.createdAt));
  }

  async getTasksByCampaignId(campaignId: number): Promise<Task[]> {
    return await db.select().from(tasks)
      .where(eq(tasks.campaignId, campaignId))
      .orderBy(desc(tasks.createdAt));
  }

  async createTask(taskData: InsertTask): Promise<Task> {
    const result = await db.insert(tasks).values(taskData).returning();
    return result[0];
  }

  async updateTask(id: number, updateData: Partial<InsertTask>): Promise<Task | undefined> {
    const result = await db.update(tasks).set(updateData).where(eq(tasks.id, id)).returning();
    return result[0];
  }

  async deleteTask(id: number): Promise<boolean> {
    try {
      const existingTask = await db.select().from(tasks).where(eq(tasks.id, id));
      if (existingTask.length === 0) {
        return false;
      }
      await db.delete(tasks).where(eq(tasks.id, id));
      return true;
    } catch (error) {
      console.error('Error deleting task:', error);
      return false;
    }
  }

  async getTasksByEquipmentId(equipmentId: string): Promise<Task[]> {
    return await db.select().from(tasks)
      .where(eq(tasks.equipmentId, equipmentId))
      .orderBy(desc(tasks.createdAt));
  }

  async getOverdueTasks(): Promise<Task[]> {
    const now = new Date();
    return await db.select().from(tasks)
      .where(and(
        eq(tasks.status, 'pending'),
        lte(tasks.dueDate, now)
      ))
      .orderBy(tasks.dueDate);
  }

  async getUpcomingTasks(days: number): Promise<Task[]> {
    const now = new Date();
    const futureDate = new Date();
    futureDate.setDate(now.getDate() + days);
    
    return await db.select().from(tasks)
      .where(and(
        eq(tasks.status, 'pending'),
        gte(tasks.dueDate, now),
        lte(tasks.dueDate, futureDate)
      ))
      .orderBy(tasks.dueDate);
  }

  async markTaskOverdue(taskId: number): Promise<Task | undefined> {
    const result = await db.update(tasks)
      .set({ status: 'overdue' })
      .where(eq(tasks.id, taskId))
      .returning();
    return result[0];
  }

  // Metric operations
  async getMetric(id: number): Promise<Metric | undefined> {
    const result = await db.select().from(metrics).where(eq(metrics.id, id));
    return result[0];
  }

  async getMetricByUserId(userId: number): Promise<Metric | undefined> {
    const result = await db.select().from(metrics).where(eq(metrics.userId, userId));
    return result[0];
  }

  async getAllMetrics(): Promise<Metric[]> {
    return await db.select().from(metrics);
  }

  async createMetric(metricData: InsertMetric): Promise<Metric> {
    const result = await db.insert(metrics).values(metricData).returning();
    return result[0];
  }

  async updateMetric(id: number, updateData: Partial<InsertMetric>): Promise<Metric | undefined> {
    const result = await db.update(metrics).set(updateData).where(eq(metrics.id, id)).returning();
    return result[0];
  }

  // Activity operations
  async getActivity(id: number): Promise<Activity | undefined> {
    const result = await db.select().from(activities).where(eq(activities.id, id));
    return result[0];
  }

  async getAllActivities(): Promise<Activity[]> {
    return await db.select().from(activities).orderBy(desc(activities.timestamp));
  }

  async getActivitiesByUserId(userId: number): Promise<Activity[]> {
    return await db.select().from(activities)
      .where(eq(activities.userId, userId))
      .orderBy(desc(activities.timestamp));
  }

  async createActivity(activityData: InsertActivity): Promise<Activity> {
    const result = await db.insert(activities).values(activityData).returning();
    return result[0];
  }

  // Notification operations
  async getNotification(id: number): Promise<Notification | undefined> {
    const result = await db.select().from(notifications).where(eq(notifications.id, id));
    return result[0];
  }

  async getAllNotifications(): Promise<Notification[]> {
    return await db.select().from(notifications).orderBy(desc(notifications.createdAt));
  }

  async getNotificationsByUserId(userId: number): Promise<Notification[]> {
    return await db.select().from(notifications)
      .where(eq(notifications.userId, userId))
      .orderBy(desc(notifications.createdAt));
  }

  async getUnreadNotifications(userId: number): Promise<Notification[]> {
    return await db.select().from(notifications)
      .where(and(
        eq(notifications.userId, userId),
        eq(notifications.isRead, false)
      ))
      .orderBy(desc(notifications.createdAt));
  }

  async getActiveNotifications(userId: number): Promise<Notification[]> {
    return await db.select().from(notifications)
      .where(and(
        eq(notifications.userId, userId),
        eq(notifications.isArchived, false)
      ))
      .orderBy(desc(notifications.createdAt));
  }

  async createNotification(notificationData: InsertNotification): Promise<Notification> {
    const result = await db.insert(notifications).values(notificationData).returning();
    return result[0];
  }

  async updateNotification(id: number, updateData: Partial<InsertNotification>): Promise<Notification | undefined> {
    const result = await db.update(notifications).set(updateData).where(eq(notifications.id, id)).returning();
    return result[0];
  }

  async markNotificationAsRead(id: number): Promise<Notification | undefined> {
    const result = await db.update(notifications)
      .set({ isRead: true })
      .where(eq(notifications.id, id))
      .returning();
    return result[0];
  }

  async archiveNotification(id: number): Promise<Notification | undefined> {
    const result = await db.update(notifications)
      .set({ isArchived: true })
      .where(eq(notifications.id, id))
      .returning();
    return result[0];
  }

  async deleteNotification(id: number): Promise<boolean> {
    const result = await db.delete(notifications).where(eq(notifications.id, id)).returning();
    return result.length > 0;
  }
}

export const storage = new PostgreSQLStorage();