From 0a5dcf6efd11423ce4c5d2c28f44244ff286eaab Mon Sep 17 00:00:00 2001 From: Shoaibdev7 Date: Tue, 17 Dec 2024 08:51:34 +0500 Subject: [PATCH] feat: add workspace chat listing endpoint --- db/chat.go | 14 ++++++++++++ db/interface.go | 1 + handlers/chat.go | 28 ++++++++++++++++++++++++ mocks/Database.go | 55 +++++++++++++++++++++++++++++++++++++++++++++++ routes/chat.go | 4 +++- 5 files changed, 101 insertions(+), 1 deletion(-) diff --git a/db/chat.go b/db/chat.go index 6812a141a..1659397bc 100644 --- a/db/chat.go +++ b/db/chat.go @@ -97,3 +97,17 @@ func (db database) GetChatMessagesForChatID(chatID string) ([]ChatMessage, error return chatMessages, nil } + +func (db database) GetChatsForWorkspace(workspaceID string) ([]Chat, error) { + var chats []Chat + + result := db.db.Where("workspace_id = ?", workspaceID). + Order("updated_at DESC"). + Find(&chats) + + if result.Error != nil { + return nil, fmt.Errorf("failed to fetch chats: %w", result.Error) + } + + return chats, nil +} diff --git a/db/interface.go b/db/interface.go index 8ae99fd09..571fab083 100644 --- a/db/interface.go +++ b/db/interface.go @@ -206,6 +206,7 @@ type Database interface { AddChatMessage(message *ChatMessage) (ChatMessage, error) UpdateChatMessage(message *ChatMessage) (ChatMessage, error) GetChatMessagesForChatID(chatID string) ([]ChatMessage, error) + GetChatsForWorkspace(workspaceID string) ([]Chat, error) GetCodeGraphByUUID(uuid string) (WorkspaceCodeGraph, error) GetCodeGraphsByWorkspaceUuid(workspace_uuid string) ([]WorkspaceCodeGraph, error) CreateOrEditCodeGraph(m WorkspaceCodeGraph) (WorkspaceCodeGraph, error) diff --git a/handlers/chat.go b/handlers/chat.go index a222bad22..b15d5c0d7 100644 --- a/handlers/chat.go +++ b/handlers/chat.go @@ -270,6 +270,34 @@ func (ch *ChatHandler) sendToStakwork(payload StakworkChatPayload) error { return nil } +func (ch *ChatHandler) GetChat(w http.ResponseWriter, r *http.Request) { + workspaceID := r.URL.Query().Get("workspace_id") + if workspaceID == "" { + w.WriteHeader(http.StatusBadRequest) + json.NewEncoder(w).Encode(ChatResponse{ + Success: false, + Message: "workspace_id query parameter is required", + }) + return + } + + chats, err := ch.db.GetChatsForWorkspace(workspaceID) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + json.NewEncoder(w).Encode(ChatResponse{ + Success: false, + Message: fmt.Sprintf("Failed to fetch chats: %v", err), + }) + return + } + + w.WriteHeader(http.StatusOK) + json.NewEncoder(w).Encode(ChatResponse{ + Success: true, + Data: chats, + }) +} + func (ch *ChatHandler) GetChatHistory(w http.ResponseWriter, r *http.Request) { chatID := chi.URLParam(r, "uuid") if chatID == "" { diff --git a/mocks/Database.go b/mocks/Database.go index d8d2a4b4b..81cf482a4 100644 --- a/mocks/Database.go +++ b/mocks/Database.go @@ -10337,3 +10337,58 @@ func NewDatabase(t interface { return mock } + +// GetChatsForWorkspace provides a mock function with given fields: workspaceID +func (_m *Database) GetChatsForWorkspace(workspaceID string) ([]db.Chat, error) { + ret := _m.Called(workspaceID) + + if len(ret) == 0 { + panic("no return value specified for GetChatsForWorkspace") + } + + var r0 []db.Chat + var r1 error + if rf, ok := ret.Get(0).(func(string) ([]db.Chat, error)); ok { + return rf(workspaceID) + } + if rf, ok := ret.Get(0).(func(string) []db.Chat); ok { + r0 = rf(workspaceID) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]db.Chat) + } + } + + if rf, ok := ret.Get(1).(func(string) error); ok { + r1 = rf(workspaceID) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +type Database_GetChatsForWorkspace_Call struct { + *mock.Call +} + +func (_e *Database_Expecter) GetChatsForWorkspace(workspaceID interface{}) *Database_GetChatsForWorkspace_Call { + return &Database_GetChatsForWorkspace_Call{Call: _e.mock.On("GetChatsForWorkspace", workspaceID)} +} + +func (_c *Database_GetChatsForWorkspace_Call) Run(run func(workspaceID string)) *Database_GetChatsForWorkspace_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(string)) + }) + return _c +} + +func (_c *Database_GetChatsForWorkspace_Call) Return(_a0 []db.Chat, _a1 error) *Database_GetChatsForWorkspace_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *Database_GetChatsForWorkspace_Call) RunAndReturn(run func(string) ([]db.Chat, error)) *Database_GetChatsForWorkspace_Call { + _c.Call.Return(run) + return _c +} \ No newline at end of file diff --git a/routes/chat.go b/routes/chat.go index 095069d0e..ab8365706 100644 --- a/routes/chat.go +++ b/routes/chat.go @@ -1,11 +1,12 @@ package routes import ( + "net/http" + "github.com/go-chi/chi" "github.com/stakwork/sphinx-tribes/auth" "github.com/stakwork/sphinx-tribes/db" "github.com/stakwork/sphinx-tribes/handlers" - "net/http" ) func ChatRoutes() chi.Router { @@ -16,6 +17,7 @@ func ChatRoutes() chi.Router { r.Group(func(r chi.Router) { r.Use(auth.PubKeyContext) + r.Get("/", chatHandler.GetChat) r.Post("/", chatHandler.CreateChat) r.Post("/send", chatHandler.SendMessage) r.Get("/history/{uuid}", chatHandler.GetChatHistory)