From 84ffac1b585d572a65cd341bed8e76fc70a8ac68 Mon Sep 17 00:00:00 2001 From: ayusht2810 Date: Tue, 16 Jul 2024 17:42:05 +0530 Subject: [PATCH 1/4] [MM-595] Add new api to get calendar list --- calendar/api/api.go | 2 ++ calendar/api/events.go | 29 +++++++++++++++++++++++++++++ calendar/config/const.go | 2 ++ 3 files changed, 33 insertions(+) diff --git a/calendar/api/api.go b/calendar/api/api.go index 89e7b184..1765b18b 100644 --- a/calendar/api/api.go +++ b/calendar/api/api.go @@ -40,6 +40,8 @@ func Init(h *httputils.Handler, env engine.Env, notificationProcessor engine.Not dialogRouter.HandleFunc(config.PathUsers, api.autocompleteConnectedUsers) apiRoutes := h.Router.PathPrefix(config.InternalAPIPath).Subrouter() + calendarsRouter := apiRoutes.PathPrefix(config.PathCalendar).Subrouter() + calendarsRouter.HandleFunc(config.PathList, api.listCalendars).Methods(http.MethodGet) eventsRouter := apiRoutes.PathPrefix(config.PathEvents).Subrouter() eventsRouter.HandleFunc(config.PathCreate, api.createEvent).Methods("POST") apiRoutes.HandleFunc(config.PathConnectedUser, api.connectedUserHandler) diff --git a/calendar/api/events.go b/calendar/api/events.go index c7ffbe7e..fc28c5c1 100644 --- a/calendar/api/events.go +++ b/calendar/api/events.go @@ -22,6 +22,7 @@ import ( const ( createEventDateTimeFormat = "2006-01-02 15:04" createEventDateFormat = "2006-01-02" + HeaderMattermostUserID = "Mattermost-User-Id" ) type createEventPayload struct { @@ -272,3 +273,31 @@ func (api *api) createEvent(w http.ResponseWriter, r *http.Request) { httputils.WriteJSONResponse(w, `{"ok": true}`, http.StatusCreated) } + +func (api *api) listCalendars(w http.ResponseWriter, r *http.Request) { + mattermostUserID := r.Header.Get(HeaderMattermostUserID) + if mattermostUserID == "" { + httputils.WriteUnauthorizedError(w, fmt.Errorf("unauthorized")) + return + } + + user, errStore := api.Store.LoadUser(mattermostUserID) + if errStore != nil && !errors.Is(errStore, store.ErrNotFound) { + httputils.WriteInternalServerError(w, errStore) + return + } + if errors.Is(errStore, store.ErrNotFound) { + httputils.WriteUnauthorizedError(w, fmt.Errorf("unauthorized")) + return + } + + client := api.Remote.MakeClient(context.Background(), user.OAuth2Token) + + calendars, errMailbox := client.GetCalendars(user.Remote.ID) + if errMailbox != nil { + httputils.WriteInternalServerError(w, errMailbox) + return + } + + httputils.WriteJSONResponse(w, calendars, http.StatusCreated) +} diff --git a/calendar/config/const.go b/calendar/config/const.go index 61444dd5..ac2d439f 100644 --- a/calendar/config/const.go +++ b/calendar/config/const.go @@ -27,6 +27,8 @@ const ( InternalAPIPath = "/api/v1" PathEvents = "/events" + PathCalendar = "/calendar" + PathList = "/list" PathCreate = "/create" PathProvider = "/provider" PathConnectedUser = "/me" From 5de87287528273bd086ac412101aab35ac28a257 Mon Sep 17 00:00:00 2001 From: ayusht2810 Date: Wed, 17 Jul 2024 18:54:40 +0530 Subject: [PATCH 2/4] [MM-595] Update create event interface --- calendar/api/events.go | 3 ++- calendar/engine/calendar.go | 2 +- calendar/remote/client.go | 2 +- calendar/remote/mock_remote/mock_client.go | 4 ++-- msgraph/create_event.go | 2 +- 5 files changed, 7 insertions(+), 6 deletions(-) diff --git a/calendar/api/events.go b/calendar/api/events.go index fc28c5c1..1161339f 100644 --- a/calendar/api/events.go +++ b/calendar/api/events.go @@ -36,6 +36,7 @@ type createEventPayload struct { Subject string `json:"subject"` Location string `json:"location,omitempty"` ChannelID string `json:"channel_id"` + CalendarID string `json:"calendar_id"` } func (cep createEventPayload) ToRemoteEvent(loc *time.Location) (*remote.Event, error) { @@ -227,7 +228,7 @@ func (api *api) createEvent(w http.ResponseWriter, r *http.Request) { }) } - event, err := client.CreateEvent(user.Remote.ID, event) + event, err := client.CreateEvent("", user.Remote.ID, event) if err != nil { httputils.WriteInternalServerError(w, err) return diff --git a/calendar/engine/calendar.go b/calendar/engine/calendar.go index ba097633..b7ad96ef 100644 --- a/calendar/engine/calendar.go +++ b/calendar/engine/calendar.go @@ -91,7 +91,7 @@ func (m *mscalendar) CreateEvent(user *User, event *remote.Event, mattermostUser } } - return m.client.CreateEvent(user.Remote.ID, event) + return m.client.CreateEvent("", user.Remote.ID, event) } func (m *mscalendar) DeleteCalendar(user *User, calendarID string) error { diff --git a/calendar/remote/client.go b/calendar/remote/client.go index d2db0bb9..80c10142 100644 --- a/calendar/remote/client.go +++ b/calendar/remote/client.go @@ -30,7 +30,7 @@ type Calendars interface { } type Events interface { - CreateEvent(remoteUserID string, calendarEvent *Event) (*Event, error) + CreateEvent(calendarID, remoteUserID string, calendarEvent *Event) (*Event, error) AcceptEvent(remoteUserID, eventID string) error DeclineEvent(remoteUserID, eventID string) error TentativelyAcceptEvent(remoteUserID, eventID string) error diff --git a/calendar/remote/mock_remote/mock_client.go b/calendar/remote/mock_remote/mock_client.go index 892d979d..06fed12d 100644 --- a/calendar/remote/mock_remote/mock_client.go +++ b/calendar/remote/mock_remote/mock_client.go @@ -96,9 +96,9 @@ func (mr *MockClientMockRecorder) CreateCalendar(arg0, arg1 interface{}) *gomock } // CreateEvent mocks base method. -func (m *MockClient) CreateEvent(arg0 string, arg1 *remote.Event) (*remote.Event, error) { +func (m *MockClient) CreateEvent(arg0, arg1 string, arg2 *remote.Event) (*remote.Event, error) { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "CreateEvent", arg0, arg1) + ret := m.ctrl.Call(m, "CreateEvent", arg0, arg1, arg2) ret0, _ := ret[0].(*remote.Event) ret1, _ := ret[1].(error) return ret0, ret1 diff --git a/msgraph/create_event.go b/msgraph/create_event.go index f786830f..4e933dd1 100644 --- a/msgraph/create_event.go +++ b/msgraph/create_event.go @@ -12,7 +12,7 @@ import ( ) // CreateEvent creates a calendar event -func (c *client) CreateEvent(remoteUserID string, in *remote.Event) (*remote.Event, error) { +func (c *client) CreateEvent(calendarID, remoteUserID string, in *remote.Event) (*remote.Event, error) { var out = remote.Event{} err := c.rbuilder.Users().ID(remoteUserID).Events().Request().JSONRequest(c.ctx, http.MethodPost, "", &in, &out) if err != nil { From 7b13946ec02f329b6772b860c01b705bb452538d Mon Sep 17 00:00:00 2001 From: ayusht2810 Date: Mon, 22 Jul 2024 14:58:11 +0530 Subject: [PATCH 3/4] [MM-595] Fix status code and linting --- calendar/api/events.go | 6 +++--- calendar/config/const.go | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/calendar/api/events.go b/calendar/api/events.go index 1161339f..aafeeed8 100644 --- a/calendar/api/events.go +++ b/calendar/api/events.go @@ -22,7 +22,7 @@ import ( const ( createEventDateTimeFormat = "2006-01-02 15:04" createEventDateFormat = "2006-01-02" - HeaderMattermostUserID = "Mattermost-User-Id" + HeaderMattermostUserID = "Mattermost-User-Id" ) type createEventPayload struct { @@ -36,7 +36,7 @@ type createEventPayload struct { Subject string `json:"subject"` Location string `json:"location,omitempty"` ChannelID string `json:"channel_id"` - CalendarID string `json:"calendar_id"` + CalendarID string `json:"calendar_id"` } func (cep createEventPayload) ToRemoteEvent(loc *time.Location) (*remote.Event, error) { @@ -300,5 +300,5 @@ func (api *api) listCalendars(w http.ResponseWriter, r *http.Request) { return } - httputils.WriteJSONResponse(w, calendars, http.StatusCreated) + httputils.WriteJSONResponse(w, calendars, http.StatusOK) } diff --git a/calendar/config/const.go b/calendar/config/const.go index ac2d439f..6b483368 100644 --- a/calendar/config/const.go +++ b/calendar/config/const.go @@ -27,8 +27,8 @@ const ( InternalAPIPath = "/api/v1" PathEvents = "/events" - PathCalendar = "/calendar" - PathList = "/list" + PathCalendar = "/calendar" + PathList = "/list" PathCreate = "/create" PathProvider = "/provider" PathConnectedUser = "/me" From 5f461c96caaf8cbbe90c1d5ab11302add6d4a692 Mon Sep 17 00:00:00 2001 From: ayusht2810 Date: Tue, 23 Jul 2024 19:35:00 +0530 Subject: [PATCH 4/4] [MM-595] Add log to the API --- calendar/api/events.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/calendar/api/events.go b/calendar/api/events.go index aafeeed8..cea95058 100644 --- a/calendar/api/events.go +++ b/calendar/api/events.go @@ -284,6 +284,7 @@ func (api *api) listCalendars(w http.ResponseWriter, r *http.Request) { user, errStore := api.Store.LoadUser(mattermostUserID) if errStore != nil && !errors.Is(errStore, store.ErrNotFound) { + api.Logger.With(bot.LogContext{"err": errStore}).Errorf("error loading user from store") httputils.WriteInternalServerError(w, errStore) return } @@ -296,6 +297,7 @@ func (api *api) listCalendars(w http.ResponseWriter, r *http.Request) { calendars, errMailbox := client.GetCalendars(user.Remote.ID) if errMailbox != nil { + api.Logger.With(bot.LogContext{"err": errMailbox}).Errorf("error fetching calendar list") httputils.WriteInternalServerError(w, errMailbox) return }