diff --git a/pkg/controller/user_controller.go b/pkg/controller/user_controller.go index bff9d61..fd21afd 100644 --- a/pkg/controller/user_controller.go +++ b/pkg/controller/user_controller.go @@ -338,7 +338,13 @@ func (ctrl *UserController) SubscribeEvent(c *gin.Context) { return } - go ctrl.asynqService.EnqueueEventNotification(userID, *request.ID, request.EventTimeStart) + err = ctrl.asynqService.EnqueueEventNotification(c, userID, *request.ID, request.EventTimeStart) + if err != nil { + c.AbortWithStatusJSON(500, model.Response{ + Msg: err.Error(), + }) + return + } c.JSON(200, model.EventResponse{ Data: newEvent, @@ -416,7 +422,13 @@ func (ctrl *UserController) UnScribeEvent(c *gin.Context) { return } - go ctrl.asynqService.DeleteEventNotification(userID + eventID) + err = ctrl.asynqService.DeleteEventNotification(c, userID+eventID) + if err != nil { + c.AbortWithStatusJSON(500, model.Response{ + Msg: err.Error(), + }) + return + } c.JSON(200, model.Response{ Msg: "delete success", diff --git a/pkg/line_utils/flex.go b/pkg/line_utils/flex.go index 1c085b1..3f0e7b5 100644 --- a/pkg/line_utils/flex.go +++ b/pkg/line_utils/flex.go @@ -7,7 +7,7 @@ import ( "log" ) -func CreateFlexMessage(event *model.EventDetails) *linebot.FlexContainer { +func CreateFlexMessage(event *model.EventDetails) (*linebot.FlexContainer, error) { // Construct the Flex Message payload using event details contents := map[string]interface{}{ "type": "bubble", @@ -20,15 +20,16 @@ func CreateFlexMessage(event *model.EventDetails) *linebot.FlexContainer { contentsJSON, err := json.Marshal(contents) if err != nil { - log.Fatal(err) + return nil, err } flexContainer, err := linebot.UnmarshalFlexMessageJSON(contentsJSON) if err != nil { - log.Fatal(err) + log.Println(err) + return nil, err } - return &flexContainer + return &flexContainer, nil } func buildContents(event *model.EventDetails) []map[string]interface{} { diff --git a/pkg/model/event.go b/pkg/model/event.go index daf38f7..9de74a2 100644 --- a/pkg/model/event.go +++ b/pkg/model/event.go @@ -108,7 +108,7 @@ type EventService interface { } type AsynqNotificationService interface { - EnqueueEventNotification(userID, eventID, eventStartTime string) - DeleteEventNotification(TaskID string) + EnqueueEventNotification(ctx context.Context, userID, eventID, eventStartTime string) error + DeleteEventNotification(ctx context.Context, TaskID string) error // TODO: delete event notification by event id } diff --git a/pkg/service/notify.go b/pkg/service/notify.go index d066e47..611afe3 100644 --- a/pkg/service/notify.go +++ b/pkg/service/notify.go @@ -3,6 +3,7 @@ package service import ( "bikefest/pkg/bootstrap" "bikefest/pkg/model" + "context" "encoding/json" "log" "time" @@ -38,17 +39,18 @@ func newEventNotification(userId, eventId string) (*asynq.Task, error) { // DeleteEventNotification deletes the task from the queue. // the taskID is the userID + eventID -func (as *AsynqServiceImpl) DeleteEventNotification(taskID string) { +func (as *AsynqServiceImpl) DeleteEventNotification(ctx context.Context, taskID string) error { err := as.inspector.DeleteTask("default", taskID) if err != nil { - log.Fatal(err) + return err } + return nil } -func (as *AsynqServiceImpl) EnqueueEventNotification(userID, eventID, eventStartTime string) { +func (as *AsynqServiceImpl) EnqueueEventNotification(ctx context.Context, userID, eventID, eventStartTime string) error { t, err := newEventNotification(userID, eventID) if err != nil { - log.Fatal(err) + return err } location, _ := time.LoadLocation(as.env.Server.TimeZone) @@ -59,9 +61,10 @@ func (as *AsynqServiceImpl) EnqueueEventNotification(userID, eventID, eventStart info, err := as.client.Enqueue(t, asynq.ProcessAt(processTime), asynq.TaskID(userID+eventID)) if err != nil { - log.Fatal(err) + return err } log.Printf(" [*] Successfully enqueued task: %+v\nThe task should be executed at %s", info, processTime.String()) + return nil } func NewAsynqService(client *asynq.Client, inspector *asynq.Inspector, env *bootstrap.Env) model.AsynqNotificationService { diff --git a/pkg/service/user.go b/pkg/service/user.go index 729f476..385d2f4 100644 --- a/pkg/service/user.go +++ b/pkg/service/user.go @@ -2,6 +2,7 @@ package service import ( "context" + "errors" "time" tokensvc "bikefest/internal/token" diff --git a/pkg/worker/event.go b/pkg/worker/event.go index 68dbf83..e3e9de1 100644 --- a/pkg/worker/event.go +++ b/pkg/worker/event.go @@ -41,17 +41,24 @@ func (eth *EventTaskHandler) HandleEventTask(ctx context.Context, t *asynq.Task) eventDetails := model.EventDetails{} err = json.Unmarshal([]byte(*event.EventDetail), &eventDetails) if err != nil { - log.Fatal(err) + log.Println(err) + return err } - flexContainer := line_utils.CreateFlexMessage(&eventDetails) - flexMessage := linebot.NewFlexMessage(fmt.Sprintf("Event: %s, 即將開始", eventDetails.Name), *flexContainer) + var message linebot.SendingMessage + flexContainer, err := line_utils.CreateFlexMessage(&eventDetails) + if err != nil { + message = linebot.NewTextMessage(fmt.Sprintf("Event: %s, 即將開始", eventDetails.Name)) + } else { + message = linebot.NewFlexMessage(fmt.Sprintf("Event: %s, 即將開始", eventDetails.Name), *flexContainer) + } //message := linebot.NewTextMessage(fmt.Sprintf("Hello, Event %s is going to start within 30 minutes!!!", p.EventID)) - _, err = eth.bot.PushMessage(p.UserID, flexMessage).Do() + _, err = eth.bot.PushMessage(p.UserID, message).Do() if err != nil { - log.Fatal(err) + log.Println(err) + return err } return nil