|
|
import { pgTable, text, serial, integer, boolean, timestamp, unique, date } from "drizzle-orm/pg-core"; |
|
|
import { createInsertSchema } from "drizzle-zod"; |
|
|
import { z } from "zod"; |
|
|
|
|
|
export const users = pgTable("users", { |
|
|
id: serial("id").primaryKey(), |
|
|
name: text("name").notNull(), |
|
|
email: text("email").notNull().unique(), |
|
|
password: text("password").notNull(), |
|
|
role: text("role").notNull().default("viewer"), |
|
|
department: text("department"), |
|
|
position: text("position"), |
|
|
phone: text("phone"), |
|
|
avatar: text("avatar"), |
|
|
isActive: boolean("is_active").notNull().default(true), |
|
|
createdAt: timestamp("created_at").notNull().defaultNow(), |
|
|
}); |
|
|
|
|
|
export const roles = pgTable("roles", { |
|
|
id: serial("id").primaryKey(), |
|
|
name: text("name").notNull().unique(), |
|
|
description: text("description").notNull(), |
|
|
permissions: text("permissions").array().notNull(), |
|
|
}); |
|
|
|
|
|
export const campaigns = pgTable("campaigns", { |
|
|
id: serial("id").primaryKey(), |
|
|
name: text("name").notNull(), |
|
|
progress: integer("progress").notNull().default(0), |
|
|
startDate: timestamp("start_date").notNull(), |
|
|
endDate: timestamp("end_date"), |
|
|
status: text("status").notNull().default("active"), |
|
|
}); |
|
|
|
|
|
export const tasks = pgTable("tasks", { |
|
|
id: serial("id").primaryKey(), |
|
|
title: text("title").notNull(), |
|
|
description: text("description"), |
|
|
userId: integer("user_id").notNull(), |
|
|
campaignId: integer("campaign_id"), |
|
|
status: text("status").notNull().default("pending"), |
|
|
priority: text("priority").notNull().default("medium"), |
|
|
dueDate: timestamp("due_date"), |
|
|
reminderDate: timestamp("reminder_date"), |
|
|
completedAt: timestamp("completed_at"), |
|
|
equipmentId: text("equipment_id"), |
|
|
maintenanceType: text("maintenance_type"), |
|
|
estimatedHours: integer("estimated_hours"), |
|
|
actualHours: integer("actual_hours"), |
|
|
createdBy: text("created_by").notNull(), |
|
|
lastModifiedBy: text("last_modified_by"), |
|
|
completedBy: text("completed_by"), |
|
|
createdAt: timestamp("created_at").notNull().defaultNow(), |
|
|
updatedAt: timestamp("updated_at").notNull().defaultNow(), |
|
|
}); |
|
|
|
|
|
export const metrics = pgTable("metrics", { |
|
|
id: serial("id").primaryKey(), |
|
|
userId: integer("user_id").notNull(), |
|
|
tasksCompleted: integer("tasks_completed").notNull().default(0), |
|
|
tasksTotal: integer("tasks_total").notNull().default(0), |
|
|
onTimeRate: integer("on_time_rate").notNull().default(0), |
|
|
productivityScore: integer("productivity_score").notNull().default(0), |
|
|
}); |
|
|
|
|
|
export const activities = pgTable("activities", { |
|
|
id: serial("id").primaryKey(), |
|
|
userId: integer("user_id").notNull(), |
|
|
action: text("action").notNull(), |
|
|
timestamp: timestamp("timestamp").notNull().defaultNow(), |
|
|
resourceType: text("resource_type"), |
|
|
resourceId: integer("resource_id"), |
|
|
}); |
|
|
|
|
|
|
|
|
export const equipment = pgTable("equipment", { |
|
|
id: text("id").primaryKey(), |
|
|
name: text("name").notNull(), |
|
|
type: text("type").notNull(), |
|
|
description: text("description"), |
|
|
status: text("status").notNull().default("active"), |
|
|
lastMaintenance: text("last_maintenance").notNull(), |
|
|
nextMaintenance: text("next_maintenance").notNull(), |
|
|
responsible: text("responsible").notNull(), |
|
|
maintenancePeriods: text("maintenance_periods").array().notNull().default([]), |
|
|
department: text("department").notNull(), |
|
|
}); |
|
|
|
|
|
|
|
|
export const maintenanceRecords = pgTable("maintenance_records", { |
|
|
id: serial("id").primaryKey(), |
|
|
equipmentId: text("equipment_id").notNull(), |
|
|
equipmentName: text("equipment_name").notNull(), |
|
|
maintenanceType: text("maintenance_type").notNull(), |
|
|
scheduledDate: timestamp("scheduled_date").notNull(), |
|
|
completedDate: timestamp("completed_date"), |
|
|
responsible: text("responsible").notNull(), |
|
|
status: text("status").notNull().default("scheduled"), |
|
|
priority: text("priority").notNull().default("medium"), |
|
|
notes: text("notes"), |
|
|
duration: text("duration"), |
|
|
createdAt: timestamp("created_at").notNull().defaultNow(), |
|
|
updatedAt: timestamp("updated_at").notNull().defaultNow(), |
|
|
}); |
|
|
|
|
|
|
|
|
export const remarks = pgTable("remarks", { |
|
|
id: serial("id").primaryKey(), |
|
|
title: text("title").notNull(), |
|
|
description: text("description").notNull(), |
|
|
equipmentName: text("equipment_name").notNull(), |
|
|
equipmentId: text("equipment_id").notNull(), |
|
|
type: text("type").notNull(), |
|
|
priority: text("priority").notNull().default("medium"), |
|
|
status: text("status").notNull().default("open"), |
|
|
reportedBy: text("reported_by").notNull(), |
|
|
assignedTo: text("assigned_to").notNull(), |
|
|
lastModifiedBy: text("last_modified_by"), |
|
|
resolvedBy: text("resolved_by"), |
|
|
createdAt: timestamp("created_at").notNull().defaultNow(), |
|
|
updatedAt: timestamp("updated_at").notNull().defaultNow(), |
|
|
resolvedAt: timestamp("resolved_at"), |
|
|
notes: text("notes").array().notNull().default([]), |
|
|
}); |
|
|
|
|
|
|
|
|
export const inspectionChecklists = pgTable("inspection_checklists", { |
|
|
id: serial("id").primaryKey(), |
|
|
equipmentId: text("equipment_id").notNull(), |
|
|
equipmentName: text("equipment_name").notNull(), |
|
|
checkItems: text("check_items").array().notNull(), |
|
|
createdBy: text("created_by").notNull(), |
|
|
createdAt: timestamp("created_at").notNull().defaultNow(), |
|
|
updatedAt: timestamp("updated_at").notNull().defaultNow(), |
|
|
}); |
|
|
|
|
|
|
|
|
export const dailyInspections = pgTable("daily_inspections", { |
|
|
id: serial("id").primaryKey(), |
|
|
equipmentId: text("equipment_id").notNull(), |
|
|
equipmentName: text("equipment_name").notNull(), |
|
|
inspectionDate: timestamp("inspection_date").notNull(), |
|
|
checkResults: text("check_results").array().notNull(), |
|
|
comments: text("comments").array().notNull().default([]), |
|
|
inspectedBy: text("inspected_by").notNull(), |
|
|
status: text("status").notNull().default("completed"), |
|
|
createdAt: timestamp("created_at").notNull().defaultNow(), |
|
|
updatedAt: timestamp("updated_at").notNull().defaultNow(), |
|
|
}); |
|
|
|
|
|
|
|
|
export const notifications = pgTable("notifications", { |
|
|
id: serial("id").primaryKey(), |
|
|
userId: integer("user_id").notNull(), |
|
|
title: text("title").notNull(), |
|
|
message: text("message").notNull(), |
|
|
type: text("type").notNull(), |
|
|
taskId: integer("task_id"), |
|
|
equipmentId: text("equipment_id"), |
|
|
priority: text("priority").notNull().default("medium"), |
|
|
isRead: boolean("is_read").notNull().default(false), |
|
|
isArchived: boolean("is_archived").notNull().default(false), |
|
|
scheduledFor: timestamp("scheduled_for"), |
|
|
createdAt: timestamp("created_at").notNull().defaultNow(), |
|
|
readAt: timestamp("read_at"), |
|
|
}); |
|
|
|
|
|
|
|
|
export const insertUserSchema = createInsertSchema(users).omit({ id: true }); |
|
|
export const insertRoleSchema = createInsertSchema(roles).omit({ id: true }); |
|
|
export const insertCampaignSchema = createInsertSchema(campaigns).omit({ id: true }); |
|
|
export const insertTaskSchema = createInsertSchema(tasks).omit({ id: true, createdAt: true, updatedAt: true }); |
|
|
export const insertMetricSchema = createInsertSchema(metrics).omit({ id: true }); |
|
|
export const insertActivitySchema = createInsertSchema(activities).omit({ id: true }); |
|
|
export const insertEquipmentSchema = createInsertSchema(equipment); |
|
|
export const insertMaintenanceRecordSchema = createInsertSchema(maintenanceRecords).omit({ id: true }); |
|
|
export const insertRemarkSchema = createInsertSchema(remarks).omit({ id: true }); |
|
|
export const insertInspectionChecklistSchema = createInsertSchema(inspectionChecklists).omit({ id: true }); |
|
|
export const insertDailyInspectionSchema = createInsertSchema(dailyInspections).omit({ id: true }); |
|
|
export const insertNotificationSchema = createInsertSchema(notifications).omit({ id: true }); |
|
|
|
|
|
|
|
|
export const loginSchema = z.object({ |
|
|
email: z.string().email(), |
|
|
password: z.string().min(6), |
|
|
}); |
|
|
|
|
|
|
|
|
export const registerSchema = z.object({ |
|
|
name: z.string().min(2, "Имя должно содержать минимум 2 символа"), |
|
|
email: z.string().email("Введите корректный email"), |
|
|
password: z.string().min(6, "Пароль должен содержать минимум 6 символов"), |
|
|
confirmPassword: z.string(), |
|
|
position: z.string().optional(), |
|
|
}).refine((data) => data.password === data.confirmPassword, { |
|
|
message: "Пароли не совпадают", |
|
|
path: ["confirmPassword"], |
|
|
}); |
|
|
|
|
|
|
|
|
export type User = typeof users.$inferSelect; |
|
|
export type InsertUser = z.infer<typeof insertUserSchema>; |
|
|
export type Role = typeof roles.$inferSelect; |
|
|
export type InsertRole = z.infer<typeof insertRoleSchema>; |
|
|
export type Campaign = typeof campaigns.$inferSelect; |
|
|
export type InsertCampaign = z.infer<typeof insertCampaignSchema>; |
|
|
export type Task = typeof tasks.$inferSelect; |
|
|
export type InsertTask = z.infer<typeof insertTaskSchema>; |
|
|
export type Metric = typeof metrics.$inferSelect; |
|
|
export type InsertMetric = z.infer<typeof insertMetricSchema>; |
|
|
export type Activity = typeof activities.$inferSelect; |
|
|
export type InsertActivity = z.infer<typeof insertActivitySchema>; |
|
|
export type Equipment = typeof equipment.$inferSelect; |
|
|
export type InsertEquipment = z.infer<typeof insertEquipmentSchema>; |
|
|
export type MaintenanceRecord = typeof maintenanceRecords.$inferSelect; |
|
|
export type InsertMaintenanceRecord = z.infer<typeof insertMaintenanceRecordSchema>; |
|
|
export type Remark = typeof remarks.$inferSelect; |
|
|
export type InsertRemark = z.infer<typeof insertRemarkSchema>; |
|
|
export type InspectionChecklist = typeof inspectionChecklists.$inferSelect; |
|
|
export type InsertInspectionChecklist = z.infer<typeof insertInspectionChecklistSchema>; |
|
|
export type DailyInspection = typeof dailyInspections.$inferSelect; |
|
|
export type InsertDailyInspection = z.infer<typeof insertDailyInspectionSchema>; |
|
|
export type Notification = typeof notifications.$inferSelect; |
|
|
export type InsertNotification = z.infer<typeof insertNotificationSchema>; |
|
|
export type Login = z.infer<typeof loginSchema>; |
|
|
export type Register = z.infer<typeof registerSchema>; |
|
|
|