From 5f10073f8db1d90e05e7865b6be2b4274ef7b7bb Mon Sep 17 00:00:00 2001 From: hanif salafi Date: Wed, 24 Sep 2025 05:21:47 +0700 Subject: [PATCH] feat: update chat schedule --- .../chat/mapper/chat_schedule.mapper.go | 18 +++++---- .../chat/request/chat_schedule.request.go | 2 +- .../chat/service/chat_schedule.service.go | 39 +++++++++++++++---- 3 files changed, 44 insertions(+), 15 deletions(-) diff --git a/app/module/chat/mapper/chat_schedule.mapper.go b/app/module/chat/mapper/chat_schedule.mapper.go index 8fca5ba..9252f62 100644 --- a/app/module/chat/mapper/chat_schedule.mapper.go +++ b/app/module/chat/mapper/chat_schedule.mapper.go @@ -111,13 +111,17 @@ func (m *ChatScheduleMapper) ToFileResponse(file *entity.ChatScheduleFiles) *res // ToEntity - Convert request to entity func (m *ChatScheduleMapper) ToEntity(req request.ChatScheduleCreateRequest) *entity.ChatSchedules { schedule := &entity.ChatSchedules{ - ChatSessionID: req.ChatSessionID, - Title: req.Title, - Description: req.Description, - Summary: req.Summary, - ScheduledAt: req.ScheduledAt, - Duration: req.Duration, - Status: "scheduled", + Title: req.Title, + Description: req.Description, + Summary: req.Summary, + ScheduledAt: req.ScheduledAt, + Duration: req.Duration, + Status: "scheduled", + } + + // Handle ChatSessionID pointer + if req.ChatSessionID != nil { + schedule.ChatSessionID = *req.ChatSessionID } // Files will be attached separately using file IDs diff --git a/app/module/chat/request/chat_schedule.request.go b/app/module/chat/request/chat_schedule.request.go index afc869c..9db948c 100644 --- a/app/module/chat/request/chat_schedule.request.go +++ b/app/module/chat/request/chat_schedule.request.go @@ -7,7 +7,7 @@ import ( // ChatScheduleCreateRequest - Request for creating chat schedule type ChatScheduleCreateRequest struct { - ChatSessionID uint `json:"chat_session_id" validate:"required"` + ChatSessionID *uint `json:"chat_session_id" validate:"omitempty"` // Optional - if empty, will create new chat session Title string `json:"title" validate:"required,min=3,max=255"` Description string `json:"description" validate:"max=1000"` Summary string `json:"summary" validate:"max=2000"` diff --git a/app/module/chat/service/chat_schedule.service.go b/app/module/chat/service/chat_schedule.service.go index a42f00a..b3e3d8a 100644 --- a/app/module/chat/service/chat_schedule.service.go +++ b/app/module/chat/service/chat_schedule.service.go @@ -17,6 +17,7 @@ import ( type chatScheduleService struct { chatScheduleRepository repository.ChatScheduleRepository chatRepository repository.ChatRepository + chatService ChatService chatScheduleMapper *mapper.ChatScheduleMapper Log zerolog.Logger UsersRepo usersRepository.UsersRepository @@ -39,12 +40,14 @@ type ChatScheduleService interface { func NewChatScheduleService( chatScheduleRepository repository.ChatScheduleRepository, chatRepository repository.ChatRepository, + chatService ChatService, log zerolog.Logger, usersRepo usersRepository.UsersRepository, ) ChatScheduleService { return &chatScheduleService{ chatScheduleRepository: chatScheduleRepository, chatRepository: chatRepository, + chatService: chatService, chatScheduleMapper: mapper.NewChatScheduleMapper(), Log: log, UsersRepo: usersRepo, @@ -64,18 +67,40 @@ func (_i *chatScheduleService) CreateChatSchedule(authToken string, req request. return nil, errors.New("scheduled time must be in the future") } - // Check if user is participant in the chat session - isParticipant, err := _i.chatRepository.CheckUserInChatSession(userID, req.ChatSessionID) - if err != nil { - return nil, err - } + var chatSessionID uint - if !isParticipant { - return nil, errors.New("user is not a participant in this chat session") + // If ChatSessionID is not provided, create a new chat session + if req.ChatSessionID == nil { + // Create a new personal chat session for the schedule + chatSessionReq := request.ChatSessionCreateRequest{ + Name: &req.Title, // Use schedule title as chat session name + Type: "group", // Default to group chat for schedules + UserIDs: []uint{}, // Empty for now, can be populated later + } + + chatSession, err := _i.chatService.CreateChatSession(authToken, chatSessionReq) + if err != nil { + return nil, errors.New("failed to create chat session: " + err.Error()) + } + + chatSessionID = chatSession.ID + } else { + chatSessionID = *req.ChatSessionID + + // Check if user is participant in the existing chat session + isParticipant, err := _i.chatRepository.CheckUserInChatSession(userID, chatSessionID) + if err != nil { + return nil, err + } + + if !isParticipant { + return nil, errors.New("user is not a participant in this chat session") + } } // Convert request to entity schedule := _i.chatScheduleMapper.ToEntity(req) + schedule.ChatSessionID = chatSessionID schedule.CreatedBy = userID // Create schedule