diff --git a/internal/db/migrate.go b/internal/db/migrate.go index 1314f0f..f1fe5a0 100644 --- a/internal/db/migrate.go +++ b/internal/db/migrate.go @@ -21,6 +21,7 @@ func Migrate(db *sql.DB) error { createInvites, createBuyerGroups, createFolderAccess, + createFileComments, } for i, m := range migrations { @@ -219,6 +220,16 @@ CREATE TABLE IF NOT EXISTS folder_access ( PRIMARY KEY (folder_id, buyer_group) );` +const createFileComments = ` +CREATE TABLE IF NOT EXISTS file_comments ( + id TEXT PRIMARY KEY, + file_id TEXT NOT NULL, + deal_id TEXT NOT NULL, + user_id TEXT NOT NULL, + content TEXT NOT NULL, + created_at DATETIME DEFAULT CURRENT_TIMESTAMP +);` + // Additive migrations - each statement is run individually, errors ignored (for already-existing columns) var additiveMigrationStmts = []string{ // Section 1: org_type diff --git a/internal/handler/files.go b/internal/handler/files.go index 85c85f9..93a4434 100644 --- a/internal/handler/files.go +++ b/internal/handler/files.go @@ -158,6 +158,67 @@ func (h *Handler) logActivity(dealID, userID, orgID, actType, resourceType, reso id, orgID, dealID, userID, actType, resourceType, resourceName, resourceID) } +func (h *Handler) handleFileComments(w http.ResponseWriter, r *http.Request) { + profile := getProfile(r.Context()) + // Parse: /deals/files/comments/{fileID} + fileID := strings.TrimPrefix(r.URL.Path, "/deals/files/comments/") + + // Get deal_id for this file + var dealID string + h.db.QueryRow("SELECT deal_id FROM files WHERE id = ?", fileID).Scan(&dealID) + + if r.Method == http.MethodPost { + content := strings.TrimSpace(r.FormValue("content")) + if content != "" { + id := generateID("cmt") + h.db.Exec("INSERT INTO file_comments (id, file_id, deal_id, user_id, content) VALUES (?, ?, ?, ?, ?)", + id, fileID, dealID, profile.ID, content) + } + } + + // Get comments + rows, err := h.db.Query(` + SELECT c.id, c.content, c.created_at, COALESCE(p.full_name, 'Unknown') + FROM file_comments c LEFT JOIN profiles p ON c.user_id = p.id + WHERE c.file_id = ? ORDER BY c.created_at ASC`, fileID) + if err != nil { + http.Error(w, "Error loading comments", 500) + return + } + defer rows.Close() + + w.Header().Set("Content-Type", "text/html") + + // Build HTML response + html := `
%s
+No comments yet.
` + } + html += `Click the comment icon to load comments.
+