Files
Regisflow/shared/schema.ts
T
michaelschal 5c01234fc4 Implement secure user authentication with role-based access control
Integrates user authentication using bcrypt, session management with PostgreSQL, and role-based access control for enhanced security and user management.

Replit-Commit-Author: Agent
Replit-Commit-Session-Id: 91318273-c764-4fd4-be04-bdc12c38af32
Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/0715008c-7244-40f4-befc-26c014852236/ab880d72-1a34-4d36-b5be-9f44df85ed1a.jpg
2025-07-09 18:44:20 +00:00

145 lines
4.3 KiB
TypeScript

import {
pgTable,
text,
varchar,
timestamp,
jsonb,
index,
serial,
integer,
boolean,
} from "drizzle-orm/pg-core";
import { relations } from "drizzle-orm";
import { createInsertSchema } from "drizzle-zod";
import { z } from "zod";
// Session storage table
export const sessions = pgTable(
"sessions",
{
sid: varchar("sid").primaryKey(),
sess: jsonb("sess").notNull(),
expire: timestamp("expire").notNull(),
},
(table) => [index("IDX_session_expire").on(table.expire)],
);
// Magasins table
export const stores = pgTable("stores", {
id: serial("id").primaryKey(),
name: varchar("name", { length: 255 }).notNull(),
address: text("address"),
phone: varchar("phone", { length: 20 }),
email: varchar("email", { length: 255 }),
isActive: boolean("is_active").default(true),
createdAt: timestamp("created_at").defaultNow(),
updatedAt: timestamp("updated_at").defaultNow(),
});
// Users table with authentication
export const users = pgTable("users", {
id: serial("id").primaryKey(),
username: varchar("username", { length: 50 }).unique().notNull(),
password: varchar("password", { length: 255 }).notNull(), // Will be hashed
email: varchar("email", { length: 255 }).unique(),
firstName: varchar("first_name", { length: 100 }),
lastName: varchar("last_name", { length: 100 }),
role: varchar("role", { length: 20 }).notNull().default("employee"), // admin, manager, employee
storeId: integer("store_id").references(() => stores.id),
isActive: boolean("is_active").default(true),
createdAt: timestamp("created_at").defaultNow(),
updatedAt: timestamp("updated_at").defaultNow(),
});
// Sales table
export const sales = pgTable("sales", {
id: serial("id").primaryKey(),
storeId: integer("store_id").references(() => stores.id).notNull(),
userId: integer("user_id").references(() => users.id).notNull(),
timestamp: timestamp("timestamp").defaultNow(),
// Vendeur
vendeur: varchar("vendeur", { length: 255 }).notNull(),
dateVente: varchar("date_vente", { length: 10 }).notNull(),
// Produit
typeArticle: varchar("type_article", { length: 255 }).notNull(),
categorie: varchar("categorie", { length: 2 }).notNull(),
quantite: integer("quantite").notNull(),
gencode: varchar("gencode", { length: 13 }).notNull(),
// Client
nom: varchar("nom", { length: 255 }).notNull(),
prenom: varchar("prenom", { length: 255 }).notNull(),
dateNaissance: varchar("date_naissance", { length: 10 }).notNull(),
lieuNaissance: varchar("lieu_naissance", { length: 255 }),
// Identité
typeIdentite: varchar("type_identite", { length: 50 }).notNull(),
numeroIdentite: varchar("numero_identite", { length: 100 }).notNull(),
autoriteDelivrance: varchar("autorite_delivrance", { length: 255 }).notNull(),
dateDelivrance: varchar("date_delivrance", { length: 10 }).notNull(),
// Photos
photoRecto: text("photo_recto"),
photoVerso: text("photo_verso"),
});
// Relations
export const storesRelations = relations(stores, ({ many }) => ({
users: many(users),
sales: many(sales),
}));
export const usersRelations = relations(users, ({ one, many }) => ({
store: one(stores, {
fields: [users.storeId],
references: [stores.id],
}),
sales: many(sales),
}));
export const salesRelations = relations(sales, ({ one }) => ({
store: one(stores, {
fields: [sales.storeId],
references: [stores.id],
}),
user: one(users, {
fields: [sales.userId],
references: [users.id],
}),
}));
// Insert schemas
export const insertUserSchema = createInsertSchema(users).omit({
id: true,
createdAt: true,
updatedAt: true,
});
export const insertStoreSchema = createInsertSchema(stores).omit({
id: true,
createdAt: true,
updatedAt: true,
});
export const insertSaleSchema = createInsertSchema(sales).omit({
id: true,
timestamp: true,
});
// Login schema
export const loginSchema = z.object({
username: z.string().min(1, "Nom d'utilisateur requis"),
password: z.string().min(1, "Mot de passe requis"),
});
// Types
export type User = typeof users.$inferSelect;
export type InsertUser = typeof users.$inferInsert;
export type Store = typeof stores.$inferSelect;
export type InsertStore = typeof stores.$inferInsert;
export type Sale = typeof sales.$inferSelect;
export type InsertSale = typeof sales.$inferInsert;
export type LoginData = z.infer<typeof loginSchema>;