VigilanzaTurni/server/storage.ts
marco370 0203c9694d Add vehicle management and improve user authentication and management
Introduce a new section for vehicle management, enhance user authentication with bcrypt, and implement CRUD operations for users.

Replit-Commit-Author: Agent
Replit-Commit-Session-Id: 42d8028a-fa71-4ec2-938c-e43eedf7df01
Replit-Commit-Checkpoint-Type: intermediate_checkpoint
Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/6d543d2c-20b9-4ea6-93fe-70fe9b1d9f80/42d8028a-fa71-4ec2-938c-e43eedf7df01/GNrPM6a
2025-10-16 17:41:22 +00:00

553 lines
17 KiB
TypeScript

// Integration: javascript_database and javascript_log_in_with_replit blueprints
import {
users,
guards,
certifications,
vehicles,
sites,
shifts,
shiftAssignments,
notifications,
guardConstraints,
sitePreferences,
trainingCourses,
holidays,
holidayAssignments,
absences,
absenceAffectedShifts,
type User,
type UpsertUser,
type Guard,
type InsertGuard,
type Certification,
type InsertCertification,
type Vehicle,
type InsertVehicle,
type Site,
type InsertSite,
type Shift,
type InsertShift,
type ShiftAssignment,
type InsertShiftAssignment,
type Notification,
type InsertNotification,
type GuardConstraints,
type InsertGuardConstraints,
type SitePreference,
type InsertSitePreference,
type TrainingCourse,
type InsertTrainingCourse,
type Holiday,
type InsertHoliday,
type HolidayAssignment,
type InsertHolidayAssignment,
type Absence,
type InsertAbsence,
type AbsenceAffectedShift,
type InsertAbsenceAffectedShift,
} 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<User | undefined>;
upsertUser(user: UpsertUser): Promise<User>;
getAllUsers(): Promise<User[]>;
updateUserRole(id: string, role: "admin" | "coordinator" | "guard" | "client"): Promise<User | undefined>;
// Guard operations
getAllGuards(): Promise<Guard[]>;
getGuard(id: string): Promise<Guard | undefined>;
createGuard(guard: InsertGuard): Promise<Guard>;
updateGuard(id: string, guard: Partial<InsertGuard>): Promise<Guard | undefined>;
// Certification operations
getCertificationsByGuard(guardId: string): Promise<Certification[]>;
createCertification(cert: InsertCertification): Promise<Certification>;
updateCertificationStatus(id: string, status: "valid" | "expiring_soon" | "expired"): Promise<void>;
// Site operations
getAllSites(): Promise<Site[]>;
getSite(id: string): Promise<Site | undefined>;
createSite(site: InsertSite): Promise<Site>;
updateSite(id: string, site: Partial<InsertSite>): Promise<Site | undefined>;
// Shift operations
getAllShifts(): Promise<Shift[]>;
getShift(id: string): Promise<Shift | undefined>;
getActiveShifts(): Promise<Shift[]>;
createShift(shift: InsertShift): Promise<Shift>;
updateShiftStatus(id: string, status: "planned" | "active" | "completed" | "cancelled"): Promise<void>;
// Shift Assignment operations
getShiftAssignments(shiftId: string): Promise<ShiftAssignment[]>;
createShiftAssignment(assignment: InsertShiftAssignment): Promise<ShiftAssignment>;
// Notification operations
getNotificationsByUser(userId: string): Promise<Notification[]>;
createNotification(notification: InsertNotification): Promise<Notification>;
markNotificationAsRead(id: string): Promise<void>;
// Guard Constraints operations
getGuardConstraints(guardId: string): Promise<GuardConstraints | undefined>;
upsertGuardConstraints(constraints: InsertGuardConstraints): Promise<GuardConstraints>;
// Site Preferences operations
getSitePreferences(siteId: string): Promise<SitePreference[]>;
createSitePreference(pref: InsertSitePreference): Promise<SitePreference>;
deleteSitePreference(id: string): Promise<void>;
// Training Courses operations
getTrainingCoursesByGuard(guardId: string): Promise<TrainingCourse[]>;
getAllTrainingCourses(): Promise<TrainingCourse[]>;
createTrainingCourse(course: InsertTrainingCourse): Promise<TrainingCourse>;
updateTrainingCourse(id: string, course: Partial<InsertTrainingCourse>): Promise<TrainingCourse | undefined>;
deleteTrainingCourse(id: string): Promise<void>;
// Holidays operations
getAllHolidays(year?: number): Promise<Holiday[]>;
createHoliday(holiday: InsertHoliday): Promise<Holiday>;
deleteHoliday(id: string): Promise<void>;
// Holiday Assignments operations
getHolidayAssignments(holidayId: string): Promise<HolidayAssignment[]>;
createHolidayAssignment(assignment: InsertHolidayAssignment): Promise<HolidayAssignment>;
deleteHolidayAssignment(id: string): Promise<void>;
// Absences operations
getAllAbsences(): Promise<Absence[]>;
getAbsencesByGuard(guardId: string): Promise<Absence[]>;
createAbsence(absence: InsertAbsence): Promise<Absence>;
updateAbsence(id: string, absence: Partial<InsertAbsence>): Promise<Absence | undefined>;
deleteAbsence(id: string): Promise<void>;
// Absence Affected Shifts operations
getAffectedShiftsByAbsence(absenceId: string): Promise<AbsenceAffectedShift[]>;
createAbsenceAffectedShift(affected: InsertAbsenceAffectedShift): Promise<AbsenceAffectedShift>;
deleteAbsenceAffectedShift(id: string): Promise<void>;
}
export class DatabaseStorage implements IStorage {
// User operations (Replit Auth required)
async getUser(id: string): Promise<User | undefined> {
const [user] = await db.select().from(users).where(eq(users.id, id));
return user;
}
async upsertUser(userData: UpsertUser): Promise<User> {
// Check if user already exists by email (unique constraint)
const existingUser = await db
.select()
.from(users)
.where(eq(users.email, userData.email || ''))
.limit(1);
if (existingUser.length > 0) {
// Update existing user
const [updated] = await db
.update(users)
.set({
...userData,
updatedAt: new Date(),
})
.where(eq(users.email, userData.email || ''))
.returning();
return updated;
} else {
// Insert new user
const [user] = await db
.insert(users)
.values(userData)
.returning();
return user;
}
}
async getAllUsers(): Promise<User[]> {
return await db.select().from(users).orderBy(desc(users.createdAt));
}
async updateUserRole(id: string, role: "admin" | "coordinator" | "guard" | "client"): Promise<User | undefined> {
const [updated] = await db
.update(users)
.set({ role, updatedAt: new Date() })
.where(eq(users.id, id))
.returning();
return updated;
}
async deleteUser(id: string): Promise<User | undefined> {
const [deleted] = await db.delete(users).where(eq(users.id, id)).returning();
return deleted;
}
// Guard operations
async getAllGuards(): Promise<Guard[]> {
return await db.select().from(guards);
}
async getGuard(id: string): Promise<Guard | undefined> {
const [guard] = await db.select().from(guards).where(eq(guards.id, id));
return guard;
}
async createGuard(guard: InsertGuard): Promise<Guard> {
const [newGuard] = await db.insert(guards).values(guard).returning();
return newGuard;
}
async updateGuard(id: string, guardData: Partial<InsertGuard>): Promise<Guard | undefined> {
const [updated] = await db
.update(guards)
.set({ ...guardData, updatedAt: new Date() })
.where(eq(guards.id, id))
.returning();
return updated;
}
async deleteGuard(id: string): Promise<Guard | undefined> {
const [deleted] = await db.delete(guards).where(eq(guards.id, id)).returning();
return deleted;
}
// Vehicle operations
async getAllVehicles(): Promise<Vehicle[]> {
return await db.select().from(vehicles).orderBy(desc(vehicles.createdAt));
}
async getVehicle(id: string): Promise<Vehicle | undefined> {
const [vehicle] = await db.select().from(vehicles).where(eq(vehicles.id, id));
return vehicle;
}
async createVehicle(vehicle: InsertVehicle): Promise<Vehicle> {
const [newVehicle] = await db.insert(vehicles).values(vehicle).returning();
return newVehicle;
}
async updateVehicle(id: string, vehicleData: Partial<InsertVehicle>): Promise<Vehicle | undefined> {
const [updated] = await db
.update(vehicles)
.set({ ...vehicleData, updatedAt: new Date() })
.where(eq(vehicles.id, id))
.returning();
return updated;
}
async deleteVehicle(id: string): Promise<Vehicle | undefined> {
const [deleted] = await db.delete(vehicles).where(eq(vehicles.id, id)).returning();
return deleted;
}
// Certification operations
async getCertificationsByGuard(guardId: string): Promise<Certification[]> {
return await db
.select()
.from(certifications)
.where(eq(certifications.guardId, guardId))
.orderBy(desc(certifications.expiryDate));
}
async createCertification(cert: InsertCertification): Promise<Certification> {
const [newCert] = await db.insert(certifications).values(cert).returning();
return newCert;
}
async updateCertificationStatus(
id: string,
status: "valid" | "expiring_soon" | "expired"
): Promise<void> {
await db
.update(certifications)
.set({ status })
.where(eq(certifications.id, id));
}
// Site operations
async getAllSites(): Promise<Site[]> {
return await db.select().from(sites);
}
async getSite(id: string): Promise<Site | undefined> {
const [site] = await db.select().from(sites).where(eq(sites.id, id));
return site;
}
async createSite(site: InsertSite): Promise<Site> {
const [newSite] = await db.insert(sites).values(site).returning();
return newSite;
}
async updateSite(id: string, siteData: Partial<InsertSite>): Promise<Site | undefined> {
const [updated] = await db
.update(sites)
.set({ ...siteData, updatedAt: new Date() })
.where(eq(sites.id, id))
.returning();
return updated;
}
async deleteSite(id: string): Promise<Site | undefined> {
const [deleted] = await db.delete(sites).where(eq(sites.id, id)).returning();
return deleted;
}
// Shift operations
async getAllShifts(): Promise<Shift[]> {
return await db.select().from(shifts).orderBy(desc(shifts.startTime));
}
async getShift(id: string): Promise<Shift | undefined> {
const [shift] = await db.select().from(shifts).where(eq(shifts.id, id));
return shift;
}
async getActiveShifts(): Promise<Shift[]> {
return await db
.select()
.from(shifts)
.where(eq(shifts.status, "active"))
.orderBy(desc(shifts.startTime));
}
async createShift(shift: InsertShift): Promise<Shift> {
const [newShift] = await db.insert(shifts).values(shift).returning();
return newShift;
}
async updateShiftStatus(
id: string,
status: "planned" | "active" | "completed" | "cancelled"
): Promise<void> {
await db
.update(shifts)
.set({ status, updatedAt: new Date() })
.where(eq(shifts.id, id));
}
async updateShift(id: string, shiftData: Partial<InsertShift>): Promise<Shift | undefined> {
const [updated] = await db
.update(shifts)
.set({ ...shiftData, updatedAt: new Date() })
.where(eq(shifts.id, id))
.returning();
return updated;
}
async deleteShift(id: string): Promise<Shift | undefined> {
const [deleted] = await db.delete(shifts).where(eq(shifts.id, id)).returning();
return deleted;
}
// Shift Assignment operations
async getShiftAssignments(shiftId: string): Promise<ShiftAssignment[]> {
return await db
.select()
.from(shiftAssignments)
.where(eq(shiftAssignments.shiftId, shiftId));
}
async createShiftAssignment(assignment: InsertShiftAssignment): Promise<ShiftAssignment> {
const [newAssignment] = await db
.insert(shiftAssignments)
.values(assignment)
.returning();
return newAssignment;
}
async deleteShiftAssignment(id: string): Promise<void> {
await db
.delete(shiftAssignments)
.where(eq(shiftAssignments.id, id));
}
// Notification operations
async getNotificationsByUser(userId: string): Promise<Notification[]> {
return await db
.select()
.from(notifications)
.where(eq(notifications.userId, userId))
.orderBy(desc(notifications.createdAt));
}
async createNotification(notification: InsertNotification): Promise<Notification> {
const [newNotification] = await db
.insert(notifications)
.values(notification)
.returning();
return newNotification;
}
async markNotificationAsRead(id: string): Promise<void> {
await db
.update(notifications)
.set({ isRead: true })
.where(eq(notifications.id, id));
}
// Guard Constraints operations
async getGuardConstraints(guardId: string): Promise<GuardConstraints | undefined> {
const [constraints] = await db
.select()
.from(guardConstraints)
.where(eq(guardConstraints.guardId, guardId));
return constraints;
}
async upsertGuardConstraints(constraintsData: InsertGuardConstraints): Promise<GuardConstraints> {
const existing = await this.getGuardConstraints(constraintsData.guardId);
if (existing) {
const [updated] = await db
.update(guardConstraints)
.set({ ...constraintsData, updatedAt: new Date() })
.where(eq(guardConstraints.guardId, constraintsData.guardId))
.returning();
return updated;
} else {
const [created] = await db
.insert(guardConstraints)
.values(constraintsData)
.returning();
return created;
}
}
// Site Preferences operations
async getSitePreferences(siteId: string): Promise<SitePreference[]> {
return await db
.select()
.from(sitePreferences)
.where(eq(sitePreferences.siteId, siteId));
}
async createSitePreference(pref: InsertSitePreference): Promise<SitePreference> {
const [newPref] = await db.insert(sitePreferences).values(pref).returning();
return newPref;
}
async deleteSitePreference(id: string): Promise<void> {
await db.delete(sitePreferences).where(eq(sitePreferences.id, id));
}
// Training Courses operations
async getTrainingCoursesByGuard(guardId: string): Promise<TrainingCourse[]> {
return await db
.select()
.from(trainingCourses)
.where(eq(trainingCourses.guardId, guardId))
.orderBy(desc(trainingCourses.scheduledDate));
}
async getAllTrainingCourses(): Promise<TrainingCourse[]> {
return await db.select().from(trainingCourses).orderBy(desc(trainingCourses.scheduledDate));
}
async createTrainingCourse(course: InsertTrainingCourse): Promise<TrainingCourse> {
const [newCourse] = await db.insert(trainingCourses).values(course).returning();
return newCourse;
}
async updateTrainingCourse(id: string, courseData: Partial<InsertTrainingCourse>): Promise<TrainingCourse | undefined> {
const [updated] = await db
.update(trainingCourses)
.set(courseData)
.where(eq(trainingCourses.id, id))
.returning();
return updated;
}
async deleteTrainingCourse(id: string): Promise<void> {
await db.delete(trainingCourses).where(eq(trainingCourses.id, id));
}
// Holidays operations
async getAllHolidays(year?: number): Promise<Holiday[]> {
if (year) {
return await db
.select()
.from(holidays)
.where(eq(holidays.year, year))
.orderBy(holidays.date);
}
return await db.select().from(holidays).orderBy(holidays.date);
}
async createHoliday(holiday: InsertHoliday): Promise<Holiday> {
const [newHoliday] = await db.insert(holidays).values(holiday).returning();
return newHoliday;
}
async deleteHoliday(id: string): Promise<void> {
await db.delete(holidays).where(eq(holidays.id, id));
}
// Holiday Assignments operations
async getHolidayAssignments(holidayId: string): Promise<HolidayAssignment[]> {
return await db
.select()
.from(holidayAssignments)
.where(eq(holidayAssignments.holidayId, holidayId));
}
async createHolidayAssignment(assignment: InsertHolidayAssignment): Promise<HolidayAssignment> {
const [newAssignment] = await db.insert(holidayAssignments).values(assignment).returning();
return newAssignment;
}
async deleteHolidayAssignment(id: string): Promise<void> {
await db.delete(holidayAssignments).where(eq(holidayAssignments.id, id));
}
// Absences operations
async getAllAbsences(): Promise<Absence[]> {
return await db.select().from(absences).orderBy(desc(absences.startDate));
}
async getAbsencesByGuard(guardId: string): Promise<Absence[]> {
return await db
.select()
.from(absences)
.where(eq(absences.guardId, guardId))
.orderBy(desc(absences.startDate));
}
async createAbsence(absence: InsertAbsence): Promise<Absence> {
const [newAbsence] = await db.insert(absences).values(absence).returning();
return newAbsence;
}
async updateAbsence(id: string, absenceData: Partial<InsertAbsence>): Promise<Absence | undefined> {
const [updated] = await db
.update(absences)
.set(absenceData)
.where(eq(absences.id, id))
.returning();
return updated;
}
async deleteAbsence(id: string): Promise<void> {
await db.delete(absences).where(eq(absences.id, id));
}
// Absence Affected Shifts operations
async getAffectedShiftsByAbsence(absenceId: string): Promise<AbsenceAffectedShift[]> {
return await db
.select()
.from(absenceAffectedShifts)
.where(eq(absenceAffectedShifts.absenceId, absenceId));
}
async createAbsenceAffectedShift(affected: InsertAbsenceAffectedShift): Promise<AbsenceAffectedShift> {
const [newAffected] = await db.insert(absenceAffectedShifts).values(affected).returning();
return newAffected;
}
async deleteAbsenceAffectedShift(id: string): Promise<void> {
await db.delete(absenceAffectedShifts).where(eq(absenceAffectedShifts.id, id));
}
}
export const storage = new DatabaseStorage();