mirror of
https://github.com/R0m1k3/LogiFlow.git
synced 2026-10-14 10:49:10 +02:00
Add ability for administrators to edit existing announcements
Implement update functionality for announcements via a PUT API endpoint, including necessary backend storage and client-side UI adjustments. Replit-Commit-Author: Agent Replit-Commit-Session-Id: e437ffed-c31b-44eb-9fd5-b6f9faa8e7ce Replit-Commit-Checkpoint-Type: full_checkpoint Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/1957c339-2757-4d1f-8e92-e9f71a1ce58e/e437ffed-c31b-44eb-9fd5-b6f9faa8e7ce/HjMubBV
This commit is contained in:
1 parent
ad396339f0
commit
a3889dd088
5 files changed
+208
-28
No files matched your search
@@ -114,6 +114,60 @@ class AnnouncementMemoryStorage {
|
||||
return announcement;
|
||||
}
|
||||
|
||||
async getAnnouncement(id: number): Promise<AnnouncementWithRelations | undefined> {
|
||||
const announcement = this.announcements.get(id);
|
||||
if (!announcement) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
// Récupérer les utilisateurs et groupes pour les relations
|
||||
const [users, groups] = await Promise.all([
|
||||
this.usersGetter(),
|
||||
this.groupsGetter()
|
||||
]);
|
||||
|
||||
const usersMap = new Map(users.map(u => [u.id, u]));
|
||||
const groupsMap = new Map(groups.map(g => [g.id, g]));
|
||||
|
||||
const author = usersMap.get(announcement.authorId);
|
||||
const group = announcement.groupId ? groupsMap.get(announcement.groupId) : undefined;
|
||||
|
||||
if (!author) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return {
|
||||
...announcement,
|
||||
author,
|
||||
group,
|
||||
};
|
||||
}
|
||||
|
||||
async updateAnnouncement(id: number, announcementData: Partial<InsertAnnouncement>): Promise<AnnouncementWithRelations> {
|
||||
const existingAnnouncement = this.announcements.get(id);
|
||||
if (!existingAnnouncement) {
|
||||
throw new Error(`Announcement with ID ${id} not found`);
|
||||
}
|
||||
|
||||
const updatedAnnouncement: Announcement = {
|
||||
...existingAnnouncement,
|
||||
...announcementData,
|
||||
id, // Ensure ID doesn't change
|
||||
updatedAt: new Date(),
|
||||
};
|
||||
|
||||
this.announcements.set(id, updatedAnnouncement);
|
||||
console.log(`✏️ Annonce mise à jour: ${updatedAnnouncement.title} (ID: ${id})`);
|
||||
|
||||
// Return the announcement with relations
|
||||
const announcementWithRelations = await this.getAnnouncement(id);
|
||||
if (!announcementWithRelations) {
|
||||
throw new Error(`Failed to get updated announcement with relations`);
|
||||
}
|
||||
|
||||
return announcementWithRelations;
|
||||
}
|
||||
|
||||
async deleteAnnouncement(id: number): Promise<boolean> {
|
||||
const existed = this.announcements.has(id);
|
||||
const deleted = this.announcements.delete(id);
|
||||
|
||||
@@ -3078,6 +3078,58 @@ RÉSUMÉ DU SCAN
|
||||
}
|
||||
});
|
||||
|
||||
// PUT /api/announcements/:id - Update announcement (admin only)
|
||||
app.put('/api/announcements/:id', isAuthenticated, async (req: any, res) => {
|
||||
console.log('📝 [SERVER] PUT /api/announcements/:id endpoint hit');
|
||||
console.log('📝 [SERVER] Request body:', JSON.stringify(req.body, null, 2));
|
||||
|
||||
try {
|
||||
const userId = req.user.claims ? req.user.claims.sub : req.user.id;
|
||||
console.log('📝 [SERVER] Extracted userId:', userId);
|
||||
|
||||
const user = await storage.getUserWithGroups(userId);
|
||||
if (!user) {
|
||||
console.error('📝 [SERVER] User not found for ID:', userId);
|
||||
return res.status(404).json({ message: "User not found" });
|
||||
}
|
||||
|
||||
console.log('📝 [SERVER] User found:', { username: user.username, role: user.role, id: user.id });
|
||||
|
||||
// Only admin can edit announcements
|
||||
if (user.role !== 'admin') {
|
||||
console.error('📝 [SERVER] Access denied - user role:', user.role);
|
||||
return res.status(403).json({ message: "Only administrators can edit announcements" });
|
||||
}
|
||||
|
||||
const id = parseInt(req.params.id);
|
||||
console.log('📝 [SERVER] Announcement ID:', id);
|
||||
|
||||
// Verify announcement exists
|
||||
const existingAnnouncement = await storage.getAnnouncement(id);
|
||||
if (!existingAnnouncement) {
|
||||
console.error('📝 [SERVER] Announcement not found:', id);
|
||||
return res.status(404).json({ message: "Announcement not found" });
|
||||
}
|
||||
|
||||
console.log('📝 [SERVER] User is admin, proceeding with validation');
|
||||
|
||||
const announcementData = insertAnnouncementSchema.partial().parse(req.body);
|
||||
console.log('📝 [SERVER] Announcement data validated:', announcementData);
|
||||
|
||||
const updatedAnnouncement = await storage.updateAnnouncement(id, announcementData);
|
||||
console.log('📝 [SERVER] Announcement updated successfully:', updatedAnnouncement);
|
||||
|
||||
res.json(updatedAnnouncement);
|
||||
} catch (error) {
|
||||
console.error('📝 [SERVER] Error updating announcement:', error);
|
||||
if (error instanceof z.ZodError) {
|
||||
console.error('📝 [SERVER] Validation errors:', error.errors);
|
||||
return res.status(400).json({ message: "Validation error", errors: error.errors });
|
||||
}
|
||||
res.status(500).json({ message: "Failed to update announcement" });
|
||||
}
|
||||
});
|
||||
|
||||
app.delete('/api/announcements/:id', isAuthenticated, async (req: any, res) => {
|
||||
try {
|
||||
const userId = req.user.claims ? req.user.claims.sub : req.user.id;
|
||||
|
||||
+19
-1
@@ -68,7 +68,7 @@ import {
|
||||
} from "@shared/schema";
|
||||
import { db } from "./db";
|
||||
import { eq, and, inArray, desc, sql, gte, lte, lt, or, isNull, isNotNull, asc } from "drizzle-orm";
|
||||
import { getAnnouncementStorage } from "./announcementStorage.js";
|
||||
|
||||
|
||||
export interface IStorage {
|
||||
// User operations
|
||||
@@ -179,6 +179,8 @@ export interface IStorage {
|
||||
// Announcement operations
|
||||
createAnnouncement(announcement: InsertAnnouncement): Promise<Announcement>;
|
||||
getAnnouncements(groupIds?: number[]): Promise<AnnouncementWithRelations[]>;
|
||||
getAnnouncement(id: number): Promise<AnnouncementWithRelations | undefined>;
|
||||
updateAnnouncement(id: number, announcement: Partial<InsertAnnouncement>): Promise<AnnouncementWithRelations>;
|
||||
deleteAnnouncement(id: number): Promise<boolean>;
|
||||
|
||||
// SAV operations
|
||||
@@ -1607,6 +1609,22 @@ export class DatabaseStorage implements IStorage {
|
||||
return await announcementStorage.getAnnouncements(groupIds);
|
||||
}
|
||||
|
||||
async getAnnouncement(id: number): Promise<AnnouncementWithRelations | undefined> {
|
||||
const announcementStorage = getAnnouncementStorage(
|
||||
() => this.getUsers(),
|
||||
() => this.getGroups()
|
||||
);
|
||||
return await announcementStorage.getAnnouncement(id);
|
||||
}
|
||||
|
||||
async updateAnnouncement(id: number, announcementData: Partial<InsertAnnouncement>): Promise<AnnouncementWithRelations> {
|
||||
const announcementStorage = getAnnouncementStorage(
|
||||
() => this.getUsers(),
|
||||
() => this.getGroups()
|
||||
);
|
||||
return await announcementStorage.updateAnnouncement(id, announcementData);
|
||||
}
|
||||
|
||||
async deleteAnnouncement(id: number): Promise<boolean> {
|
||||
const announcementStorage = getAnnouncementStorage(
|
||||
() => this.getUsers(),
|
||||
|
||||
Reference in new issue
Block a user