mirror of
https://github.com/R0m1k3/Regisflow.git
synced 2026-10-23 23:28:18 +02:00
Enable administrators to manage users and stores more effectively
Implements CRUD operations for users and stores with UI updates and server route handling. 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
This commit is contained in:
1 parent
462ebeb816
commit
a5336a5919
3 files changed
+762
-243
No files matched your search
File diff suppressed because it is too large.
Load diff
@@ -217,6 +217,27 @@ export async function registerRoutes(app: Express): Promise<Server> {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
app.delete('/api/admin/users/:id', requireRole(['admin']), async (req, res) => {
|
||||||
|
try {
|
||||||
|
const userId = parseInt(req.params.id);
|
||||||
|
|
||||||
|
// Don't allow deletion of the last administrator
|
||||||
|
const users = await storage.getAllUsers();
|
||||||
|
const adminUsers = users.filter(u => u.role === 'administrator');
|
||||||
|
const userToDelete = users.find(u => u.id === userId);
|
||||||
|
|
||||||
|
if (userToDelete?.role === 'administrator' && adminUsers.length <= 1) {
|
||||||
|
return res.status(400).json({ error: "Cannot delete the last administrator" });
|
||||||
|
}
|
||||||
|
|
||||||
|
await storage.deleteUser(userId);
|
||||||
|
res.json({ message: "User deleted successfully" });
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Delete user error:', error);
|
||||||
|
res.status(500).json({ error: "Failed to delete user" });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
// Admin routes - Stores
|
// Admin routes - Stores
|
||||||
app.get('/api/admin/stores', requireRole(['admin']), async (req, res) => {
|
app.get('/api/admin/stores', requireRole(['admin']), async (req, res) => {
|
||||||
try {
|
try {
|
||||||
@@ -260,6 +281,29 @@ export async function registerRoutes(app: Express): Promise<Server> {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
app.delete('/api/admin/stores/:id', requireRole(['admin']), async (req, res) => {
|
||||||
|
try {
|
||||||
|
const storeId = parseInt(req.params.id);
|
||||||
|
|
||||||
|
// Check if there are users assigned to this store
|
||||||
|
const users = await storage.getAllUsers();
|
||||||
|
const usersInStore = users.filter(u => u.storeId === storeId);
|
||||||
|
|
||||||
|
if (usersInStore.length > 0) {
|
||||||
|
return res.status(400).json({
|
||||||
|
error: "Cannot delete store with assigned users",
|
||||||
|
details: `${usersInStore.length} user(s) are assigned to this store`
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
await storage.deleteStore(storeId);
|
||||||
|
res.json({ message: "Store deleted successfully" });
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Delete store error:', error);
|
||||||
|
res.status(500).json({ error: "Failed to delete store" });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
const httpServer = createServer(app);
|
const httpServer = createServer(app);
|
||||||
return httpServer;
|
return httpServer;
|
||||||
}
|
}
|
||||||
@@ -19,6 +19,7 @@ export interface IStorage {
|
|||||||
getUserByUsername(username: string): Promise<User | undefined>;
|
getUserByUsername(username: string): Promise<User | undefined>;
|
||||||
createUser(user: InsertUser): Promise<User>;
|
createUser(user: InsertUser): Promise<User>;
|
||||||
updateUser(id: number, user: Partial<InsertUser>): Promise<User | undefined>;
|
updateUser(id: number, user: Partial<InsertUser>): Promise<User | undefined>;
|
||||||
|
deleteUser(id: number): Promise<void>;
|
||||||
getAllUsers(): Promise<User[]>;
|
getAllUsers(): Promise<User[]>;
|
||||||
|
|
||||||
// Store operations
|
// Store operations
|
||||||
@@ -26,6 +27,7 @@ export interface IStorage {
|
|||||||
createStore(store: InsertStore): Promise<Store>;
|
createStore(store: InsertStore): Promise<Store>;
|
||||||
getAllStores(): Promise<Store[]>;
|
getAllStores(): Promise<Store[]>;
|
||||||
updateStore(id: number, store: Partial<InsertStore>): Promise<Store | undefined>;
|
updateStore(id: number, store: Partial<InsertStore>): Promise<Store | undefined>;
|
||||||
|
deleteStore(id: number): Promise<void>;
|
||||||
|
|
||||||
// Sale operations
|
// Sale operations
|
||||||
getSale(id: number): Promise<Sale | undefined>;
|
getSale(id: number): Promise<Sale | undefined>;
|
||||||
@@ -82,6 +84,10 @@ export class DatabaseStorage implements IStorage {
|
|||||||
return await db.select().from(users).orderBy(desc(users.createdAt));
|
return await db.select().from(users).orderBy(desc(users.createdAt));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async deleteUser(id: number): Promise<void> {
|
||||||
|
await db.delete(users).where(eq(users.id, id));
|
||||||
|
}
|
||||||
|
|
||||||
// Store operations
|
// Store operations
|
||||||
async getStore(id: number): Promise<Store | undefined> {
|
async getStore(id: number): Promise<Store | undefined> {
|
||||||
const [store] = await db.select().from(stores).where(eq(stores.id, id));
|
const [store] = await db.select().from(stores).where(eq(stores.id, id));
|
||||||
@@ -109,6 +115,10 @@ export class DatabaseStorage implements IStorage {
|
|||||||
return store;
|
return store;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async deleteStore(id: number): Promise<void> {
|
||||||
|
await db.delete(stores).where(eq(stores.id, id));
|
||||||
|
}
|
||||||
|
|
||||||
// Sale operations
|
// Sale operations
|
||||||
async getSale(id: number): Promise<Sale | undefined> {
|
async getSale(id: number): Promise<Sale | undefined> {
|
||||||
const [sale] = await db.select().from(sales).where(eq(sales.id, id));
|
const [sale] = await db.select().from(sales).where(eq(sales.id, id));
|
||||||
|
|||||||
Reference in new issue
Block a user