diff --git a/attached_assets/image_1757927707750.png b/attached_assets/image_1757927707750.png new file mode 100644 index 0000000..1d08f11 Binary files /dev/null and b/attached_assets/image_1757927707750.png differ diff --git a/attached_assets/image_1757927754570.png b/attached_assets/image_1757927754570.png new file mode 100644 index 0000000..1d08f11 Binary files /dev/null and b/attached_assets/image_1757927754570.png differ diff --git a/server/routes.ts b/server/routes.ts index ea59632..ee5cdcf 100644 --- a/server/routes.ts +++ b/server/routes.ts @@ -3525,23 +3525,41 @@ export async function registerRoutes(app: Express): Promise { }); app.delete('/api/publicities/:id', isAuthenticated, async (req: any, res) => { + const publicityId = req.params.id; + console.log(`🗑️ [API] DELETE request received for publicity ID: ${publicityId}`); + console.log(`🗑️ [API] User info:`, { + hasUser: !!req.user, + userId: req.user?.id || req.user?.claims?.sub, + method: req.method, + url: req.url, + headers: { 'content-type': req.headers['content-type'] } + }); + try { const user = await storage.getUserWithGroups(req.user.claims ? req.user.claims.sub : req.user.id); if (!user) { + console.log(`❌ [API] User not found for publicity deletion: ${publicityId}`); return res.status(404).json({ message: "User not found" }); } + console.log(`🗑️ [API] User found:`, { id: user.id, role: user.role, name: user.name }); + // Check permissions (admin only for deletion) if (user.role !== 'admin') { + console.log(`❌ [API] Insufficient permissions for publicity deletion: ${publicityId}, user role: ${user.role}`); return res.status(403).json({ message: "Insufficient permissions" }); } - const id = parseInt(req.params.id); + const id = parseInt(publicityId); + console.log(`🗑️ [API] Admin ${user.name} (${user.id}) attempting to delete publicity ${id}`); + await storage.deletePublicity(id); + + console.log(`✅ [API] Successfully deleted publicity ${id} by admin ${user.name}`); res.json({ message: "Publicity deleted successfully" }); } catch (error) { - console.error("Error deleting publicity:", error); - res.status(500).json({ message: "Failed to delete publicity" }); + console.error(`❌ [API] Error deleting publicity ${publicityId}:`, error); + res.status(500).json({ message: "Failed to delete publicity", error: error.message }); } }); diff --git a/server/storage.ts b/server/storage.ts index 8ae08b9..6cace28 100644 --- a/server/storage.ts +++ b/server/storage.ts @@ -1479,8 +1479,26 @@ export class DatabaseStorage implements IStorage { } async deletePublicity(id: number): Promise { - await db.delete(publicityParticipations).where(eq(publicityParticipations.publicityId, id)); - await db.delete(publicities).where(eq(publicities.id, id)); + console.log(`🗑️ [DELETION] Starting deletion of publicity ID: ${id}`); + + try { + // First, delete all participations (defensive approach for production DB constraints) + const deletedParticipations = await db.delete(publicityParticipations).where(eq(publicityParticipations.publicityId, id)).returning(); + console.log(`🗑️ [DELETION] Deleted ${deletedParticipations.length} participations for publicity ${id}`); + + // Then delete the publicity itself + const deletedPublicity = await db.delete(publicities).where(eq(publicities.id, id)).returning(); + console.log(`🗑️ [DELETION] Deleted publicity ${id}, found: ${deletedPublicity.length > 0 ? 'YES' : 'NO'}`); + + if (deletedPublicity.length === 0) { + throw new Error(`Publicity with ID ${id} not found`); + } + + console.log(`✅ [DELETION] Successfully deleted publicity ID: ${id}`); + } catch (error) { + console.error(`❌ [DELETION] Failed to delete publicity ID: ${id}`, error); + throw error; + } } async getPublicityParticipations(publicityId: number): Promise {