diff --git a/router/router.go b/router/router.go index 21984a4b..ca3a347f 100644 --- a/router/router.go +++ b/router/router.go @@ -49,19 +49,22 @@ func NewServer(h Handlers) *echo.Echo { apiRequests := api.Group("/requests", h.CheckLoginMiddleware) { apiRequests.GET("", h.GetRequests) - apiRequests.POST("", h.PostRequest, middleware.BodyDump(service.WebhookEventHandler)) + apiRequests.POST( + "", + h.PostRequest, + middleware.BodyDump(service.WebhookRequestsEventHandler)) apiRequestIDs := apiRequests.Group("/:requestID", retrieveRequestCreator) { apiRequestIDs.GET("", h.GetRequest) apiRequestIDs.PUT( "", h.PutRequest, - middleware.BodyDump(service.WebhookEventHandler), + middleware.BodyDump(service.WebhookRequestsEventHandler), h.CheckRequestCreatorMiddleware) apiRequestIDs.POST( "/comments", h.PostComment, - middleware.BodyDump(service.WebhookEventHandler)) + middleware.BodyDump(service.WebhookRequestsEventHandler)) apiRequestIDs.PUT("/status", h.PutStatus, h.CheckAdminOrRequestCreatorMiddleware) } } @@ -72,13 +75,13 @@ func NewServer(h Handlers) *echo.Echo { apiTransactions.POST( "", h.PostTransaction, - middleware.BodyDump(service.WebhookEventHandler), + middleware.BodyDump(service.WebhookTransactionsEventHandler), h.CheckAdminMiddleware) apiTransactions.GET("/:transactionID", h.GetTransaction) apiTransactions.PUT( "/:transactionID", h.PutTransaction, - middleware.BodyDump(service.WebhookEventHandler), + middleware.BodyDump(service.WebhookTransactionsEventHandler), h.CheckAdminMiddleware) } diff --git a/service/webhook.go b/service/webhook.go index c2059158..1f1a3cdd 100644 --- a/service/webhook.go +++ b/service/webhook.go @@ -36,7 +36,15 @@ type CommentApplication struct { Comment string `json:"comment"` } -type TransactionRequestApplication struct { +type TransactionPostRequestApplication struct { + ID uuid.UUID `json:"id"` + Amount int `json:"amount"` + Target string `json:"target"` + Tags []*Tag `json:"tags"` + Group *Group `json:"group"` +} + +type TransactionPutRequestApplication struct { ID uuid.UUID `json:"id"` Amount int `json:"amount"` Target string `json:"target"` @@ -72,117 +80,133 @@ type Webhook struct { ID string } -func WebhookEventHandler(c echo.Context, reqBody, resBody []byte) { +func WebhookRequestsEventHandler(c echo.Context, reqBody, resBody []byte) { webhookSecret := os.Getenv("WEBHOOK_SECRET") webhookChannelId := os.Getenv("WEBHOOK_CHANNEL_ID") webhookId := os.Getenv("WEBHOOK_ID") var message string - if strings.Contains(c.Request().URL.Path, "/api/requests") { - if strings.Contains(c.Request().URL.Path, "/comments") { - resApp := new(CommentApplication) - err := json.Unmarshal(resBody, resApp) - if err != nil { - return - } - splitedPath := strings.Split(c.Request().URL.Path, "/") + if strings.Contains(c.Request().URL.Path, "/comments") { + resApp := new(CommentApplication) + err := json.Unmarshal(resBody, resApp) + if err != nil { + return + } + splitedPath := strings.Split(c.Request().URL.Path, "/") - message += fmt.Sprintf( - "## :comment:[申請](%s/requests/%s)", - "https://jomon.trap.jp", - splitedPath[3]) - message += "に対する" - message += fmt.Sprintf( - "[コメント](%s/requests/%s/comments/%s)", - "https://jomon.trap.jp", - splitedPath[3], - resApp.ID) - message += "が作成されました\n\n" - message += resApp.Comment + "\n" - } else { - resApp := new(RequestApplication) - err := json.Unmarshal(resBody, resApp) - if err != nil { - return - } - if c.Request().Method == http.MethodPost { - message += "## :receipt:申請が作成されました\n" - } else if c.Request().Method == http.MethodPut { - message += "## :receipt:申請が更新されました\n" - } + message += fmt.Sprintf( + "## :comment:[申請](%s/requests/%s)", + "https://jomon.trap.jp", + splitedPath[3]) + message += "に対する" + message += fmt.Sprintf( + "[コメント](%s/requests/%s/comments/%s)", + "https://jomon.trap.jp", + splitedPath[3], + resApp.ID) + message += "が作成されました\n\n" + message += resApp.Comment + "\n" + } else { + resApp := new(RequestApplication) + err := json.Unmarshal(resBody, resApp) + if err != nil { + return + } + if c.Request().Method == http.MethodPost { + message += "## :receipt:申請が作成されました\n" + } else if c.Request().Method == http.MethodPut { + message += "## :receipt:申請が更新されました\n" + } - message += fmt.Sprintf( - "### [%s](%s/applications/%s)\n", - resApp.Title, - "https://jomon.trap.jp", - resApp.ID) - - amount := lo.Reduce(resApp.Targets, func(amo int, target *Target, _ int) int { - return amo + target.Amount - }, 0) - message += fmt.Sprintf("- 支払金額: %d円\n", amount) - - if resApp.Group != nil { - message += fmt.Sprintf("- 請求先グループ: %s\n", resApp.Group.Name) - } - - if len(resApp.Tags) != 0 { - tags := lo.Map(resApp.Tags, func(tag *Tag, _ int) string { - return tag.Name - }) - message += fmt.Sprintf("- タグ: %s", strings.Join(tags, ", ")) - } - message += "\n\n" - message += resApp.Content + "\n" + message += fmt.Sprintf( + "### [%s](%s/applications/%s)\n", + resApp.Title, + "https://jomon.trap.jp", + resApp.ID) + + amount := lo.Reduce(resApp.Targets, func(amo int, target *Target, _ int) int { + return amo + target.Amount + }, 0) + message += fmt.Sprintf("- 支払金額: %d円\n", amount) + + if resApp.Group != nil { + message += fmt.Sprintf("- 請求先グループ: %s\n", resApp.Group.Name) } - } else if strings.Contains(c.Request().URL.Path, "/api/transactions") { - var resApps []TransactionRequestApplication + + if len(resApp.Tags) != 0 { + tags := lo.Map(resApp.Tags, func(tag *Tag, _ int) string { + return tag.Name + }) + message += fmt.Sprintf("- タグ: %s", strings.Join(tags, ", ")) + } + message += "\n\n" + message += resApp.Content + "\n" + } + _ = RequestWebhook(message, webhookSecret, webhookChannelId, webhookId, 1) +} + +func WebhookTransactionsEventHandler(c echo.Context, reqBody, resBody []byte) { + webhookSecret := os.Getenv("WEBHOOK_SECRET") + webhookChannelId := os.Getenv("WEBHOOK_CHANNEL_ID") + webhookId := os.Getenv("WEBHOOK_ID") + var message string + + if c.Request().Method == http.MethodPost { + var resApps []TransactionPostRequestApplication err := json.Unmarshal(resBody, &resApps) - resApp := resApps[0] - if err != nil { + if err != nil || len(resApps) < 1 { return } - if c.Request().Method == http.MethodPost { + message += fmt.Sprintf( + "## :scroll:[入出金記録](%s/transactions/%s)が新規作成されました\n", + "https://jomon.trap.jp", + resApps[0].ID) + targets := lo.Map( + resApps, func(resApp TransactionPostRequestApplication, _ int) string { + return resApp.Target + }) + if resApps[0].Amount < 0 { message += fmt.Sprintf( - "## :scroll:[入出金記録](%s/transactions/%s)が新規作成されました\n", - "https://jomon.trap.jp", - resApp.ID) - } else if c.Request().Method == http.MethodPut { + "- %sへの支払い\n - 支払い金額: 計%d円(一人当たりへの支払い金額: %d円)\n", + strings.Join(targets, " "), + -len(resApps)*resApps[0].Amount, + -resApps[0].Amount) + } else { message += fmt.Sprintf( - "## :scroll:[入出金記録](%s/transactions/%s)が修正されました\n", - "https://jomon.trap.jp", - resApp.ID) + "- %sからの振込\n - 受け取り金額: 計%d円(一人当たりからの受け取り金額: %d円)\n", + strings.Join(targets, " "), + len(resApps)*resApps[0].Amount, + resApps[0].Amount) + } + if resApps[0].Group != nil { + message += fmt.Sprintf("- 関連するグループ: %s\n", resApps[0].Group.Name) + } + if len(resApps[0].Tags) != 0 { + tags := lo.Map(resApps[0].Tags, func(tag *Tag, _ int) string { + return tag.Name + }) + message += fmt.Sprintf("- タグ: %s", strings.Join(tags, ", ")) } - if len(resApps) == 1 { - if resApp.Amount < 0 { - message += fmt.Sprintf( - "- `%s`への支払い\n - 支払い金額: %d円\n", - resApp.Target, - -resApp.Amount) - } else { - message += fmt.Sprintf( - "- `%s`からの振込\n - 受け取り金額: %d円\n", - resApp.Target, - resApp.Amount) - } + } else if c.Request().Method == http.MethodPut { + var resApp TransactionPutRequestApplication + err := json.Unmarshal(resBody, &resApp) + if err != nil { + return + } + message += fmt.Sprintf( + "## :scroll:[入出金記録](%s/transactions/%s)が修正されました\n", + "https://jomon.trap.jp", + resApp.ID) + if resApp.Amount < 0 { + message += fmt.Sprintf( + "- `%s`への支払い\n - 支払い金額: %d円\n", + resApp.Target, + -resApp.Amount) } else { - targets := lo.Map( - resApps, func(resApp TransactionRequestApplication, _ int) string { - return resApp.Target - }) - if resApp.Amount < 0 { - message += fmt.Sprintf( - "- %sへの支払い\n - 支払い金額: 計%d円(一人当たりへの支払い金額: %d円)\n", - strings.Join(targets, " "), - -len(resApps)*resApp.Amount, - -resApp.Amount) - } else { - message += fmt.Sprintf( - "- %sからの振込\n - 受け取り金額: 計%d円(一人当たりからの受け取り金額: %d円)\n", - strings.Join(targets, " "), - len(resApps)*resApp.Amount, - resApp.Amount) - } + message += fmt.Sprintf( + "- `%s`からの振込\n - 受け取り金額: %d円\n", + resApp.Target, + resApp.Amount) } if resApp.Group != nil { message += fmt.Sprintf("- 関連するグループ: %s\n", resApp.Group.Name) @@ -194,7 +218,6 @@ func WebhookEventHandler(c echo.Context, reqBody, resBody []byte) { message += fmt.Sprintf("- タグ: %s", strings.Join(tags, ", ")) } } - _ = RequestWebhook(message, webhookSecret, webhookChannelId, webhookId, 1) }