// Integration: javascript_database and javascript_log_in_with_replit blueprints import { users, guards, certifications, sites, shifts, shiftAssignments, notifications, type User, type UpsertUser, type Guard, type InsertGuard, type Certification, type InsertCertification, type Site, type InsertSite, type Shift, type InsertShift, type ShiftAssignment, type InsertShiftAssignment, type Notification, type InsertNotification, } from "@shared/schema"; import { db } from "./db"; import { eq, and, gte, lte, desc } from "drizzle-orm"; export interface IStorage { // User operations (Replit Auth required) getUser(id: string): Promise; upsertUser(user: UpsertUser): Promise; // Guard operations getAllGuards(): Promise; getGuard(id: string): Promise; createGuard(guard: InsertGuard): Promise; updateGuard(id: string, guard: Partial): Promise; // Certification operations getCertificationsByGuard(guardId: string): Promise; createCertification(cert: InsertCertification): Promise; updateCertificationStatus(id: string, status: "valid" | "expiring_soon" | "expired"): Promise; // Site operations getAllSites(): Promise; getSite(id: string): Promise; createSite(site: InsertSite): Promise; updateSite(id: string, site: Partial): Promise; // Shift operations getAllShifts(): Promise; getShift(id: string): Promise; getActiveShifts(): Promise; createShift(shift: InsertShift): Promise; updateShiftStatus(id: string, status: "planned" | "active" | "completed" | "cancelled"): Promise; // Shift Assignment operations getShiftAssignments(shiftId: string): Promise; createShiftAssignment(assignment: InsertShiftAssignment): Promise; // Notification operations getNotificationsByUser(userId: string): Promise; createNotification(notification: InsertNotification): Promise; markNotificationAsRead(id: string): Promise; } export class DatabaseStorage implements IStorage { // User operations (Replit Auth required) async getUser(id: string): Promise { const [user] = await db.select().from(users).where(eq(users.id, id)); return user; } async upsertUser(userData: UpsertUser): Promise { const [user] = await db .insert(users) .values(userData) .onConflictDoUpdate({ target: users.id, set: { ...userData, updatedAt: new Date(), }, }) .returning(); return user; } // Guard operations async getAllGuards(): Promise { return await db.select().from(guards); } async getGuard(id: string): Promise { const [guard] = await db.select().from(guards).where(eq(guards.id, id)); return guard; } async createGuard(guard: InsertGuard): Promise { const [newGuard] = await db.insert(guards).values(guard).returning(); return newGuard; } async updateGuard(id: string, guardData: Partial): Promise { const [updated] = await db .update(guards) .set({ ...guardData, updatedAt: new Date() }) .where(eq(guards.id, id)) .returning(); return updated; } // Certification operations async getCertificationsByGuard(guardId: string): Promise { return await db .select() .from(certifications) .where(eq(certifications.guardId, guardId)) .orderBy(desc(certifications.expiryDate)); } async createCertification(cert: InsertCertification): Promise { const [newCert] = await db.insert(certifications).values(cert).returning(); return newCert; } async updateCertificationStatus( id: string, status: "valid" | "expiring_soon" | "expired" ): Promise { await db .update(certifications) .set({ status }) .where(eq(certifications.id, id)); } // Site operations async getAllSites(): Promise { return await db.select().from(sites); } async getSite(id: string): Promise { const [site] = await db.select().from(sites).where(eq(sites.id, id)); return site; } async createSite(site: InsertSite): Promise { const [newSite] = await db.insert(sites).values(site).returning(); return newSite; } async updateSite(id: string, siteData: Partial): Promise { const [updated] = await db .update(sites) .set({ ...siteData, updatedAt: new Date() }) .where(eq(sites.id, id)) .returning(); return updated; } // Shift operations async getAllShifts(): Promise { return await db.select().from(shifts).orderBy(desc(shifts.startTime)); } async getShift(id: string): Promise { const [shift] = await db.select().from(shifts).where(eq(shifts.id, id)); return shift; } async getActiveShifts(): Promise { return await db .select() .from(shifts) .where(eq(shifts.status, "active")) .orderBy(desc(shifts.startTime)); } async createShift(shift: InsertShift): Promise { const [newShift] = await db.insert(shifts).values(shift).returning(); return newShift; } async updateShiftStatus( id: string, status: "planned" | "active" | "completed" | "cancelled" ): Promise { await db .update(shifts) .set({ status, updatedAt: new Date() }) .where(eq(shifts.id, id)); } // Shift Assignment operations async getShiftAssignments(shiftId: string): Promise { return await db .select() .from(shiftAssignments) .where(eq(shiftAssignments.shiftId, shiftId)); } async createShiftAssignment(assignment: InsertShiftAssignment): Promise { const [newAssignment] = await db .insert(shiftAssignments) .values(assignment) .returning(); return newAssignment; } // Notification operations async getNotificationsByUser(userId: string): Promise { return await db .select() .from(notifications) .where(eq(notifications.userId, userId)) .orderBy(desc(notifications.createdAt)); } async createNotification(notification: InsertNotification): Promise { const [newNotification] = await db .insert(notifications) .values(notification) .returning(); return newNotification; } async markNotificationAsRead(id: string): Promise { await db .update(notifications) .set({ isRead: true }) .where(eq(notifications.id, id)); } } export const storage = new DatabaseStorage();