From 7a5f71dca19eb9670dc750ca867ca5bb1ba79288 Mon Sep 17 00:00:00 2001 From: Hueter Date: Fri, 6 Dec 2024 23:24:25 +0900 Subject: [PATCH 1/8] separete Transaction struct --- router/transaction.go | 25 ++++++++++++++++++------- router/transaction_test.go | 34 +++++++++++++++++----------------- 2 files changed, 35 insertions(+), 24 deletions(-) diff --git a/router/transaction.go b/router/transaction.go index 8e6e26b5..eab09d8d 100644 --- a/router/transaction.go +++ b/router/transaction.go @@ -14,7 +14,18 @@ import ( "go.uber.org/zap" ) -type Transaction struct { +type TransactionNewCreate struct { + ID uuid.UUID `json:"id"` + Amount int `json:"amount"` + Target string `json:"target"` + Request *uuid.UUID `json:"request"` + Tags []*TagOverview `json:"tags"` + Group *GroupOverview `json:"group"` + CreatedAt time.Time `json:"created_at"` + UpdatedAt time.Time `json:"updated_at"` +} + +type TransactionCorrestion struct { ID uuid.UUID `json:"id"` Amount int `json:"amount"` Target string `json:"target"` @@ -137,7 +148,7 @@ func (h Handlers) GetTransactions(c echo.Context) error { return echo.NewHTTPError(http.StatusInternalServerError, err) } - res := lo.Map(txs, func(tx *model.TransactionResponse, _ int) *Transaction { + res := lo.Map(txs, func(tx *model.TransactionResponse, _ int) *TransactionNewCreate { tags := lo.Map(tx.Tags, func(tag *model.Tag, _ int) *TagOverview { return &TagOverview{ ID: tag.ID, @@ -158,7 +169,7 @@ func (h Handlers) GetTransactions(c echo.Context) error { UpdatedAt: tx.Group.UpdatedAt, } } - return &Transaction{ + return &TransactionNewCreate{ ID: tx.ID, Amount: tx.Amount, Target: tx.Target, @@ -180,7 +191,7 @@ func (h Handlers) PostTransaction(c echo.Context) error { return echo.NewHTTPError(http.StatusBadRequest, err) } - transactions := []*Transaction{} + transactions := []*TransactionNewCreate{} ctx := c.Request().Context() for _, target := range tx.Targets { if target == nil { @@ -215,7 +226,7 @@ func (h Handlers) PostTransaction(c echo.Context) error { UpdatedAt: created.Group.UpdatedAt, } } - res := Transaction{ + res := TransactionNewCreate{ ID: created.ID, Amount: created.Amount, Target: created.Target, @@ -265,7 +276,7 @@ func (h Handlers) GetTransaction(c echo.Context) error { UpdatedAt: tx.Group.UpdatedAt, } } - res := Transaction{ + res := TransactionCorrestion{ ID: tx.ID, Amount: tx.Amount, Target: tx.Target, @@ -323,7 +334,7 @@ func (h Handlers) PutTransaction(c echo.Context) error { UpdatedAt: updated.Group.UpdatedAt, } } - res := Transaction{ + res := TransactionCorrestion{ ID: updated.ID, Amount: updated.Amount, Target: updated.Target, diff --git a/router/transaction_test.go b/router/transaction_test.go index 5897ee09..87dbfe3c 100644 --- a/router/transaction_test.go +++ b/router/transaction_test.go @@ -100,7 +100,7 @@ func TestHandlers_GetTransactions(t *testing.T) { }). Return(txs, nil) - res := lo.Map(txs, func(tx *model.TransactionResponse, _ int) *Transaction { + res := lo.Map(txs, func(tx *model.TransactionResponse, _ int) *TransactionNewCreate { tag := lo.Map(tx.Tags, func(modelTag *model.Tag, _ int) *TagOverview { return &TagOverview{ ID: modelTag.ID, @@ -118,7 +118,7 @@ func TestHandlers_GetTransactions(t *testing.T) { CreatedAt: tx.Group.CreatedAt, UpdatedAt: tx.Group.UpdatedAt, } - return &Transaction{ + return &TransactionNewCreate{ ID: tx.ID, Amount: tx.Amount, Target: tx.Target, @@ -213,7 +213,7 @@ func TestHandlers_GetTransactions(t *testing.T) { }). Return(txs, nil) - res := lo.Map(txs, func(tx *model.TransactionResponse, _ int) *Transaction { + res := lo.Map(txs, func(tx *model.TransactionResponse, _ int) *TransactionNewCreate { tag := lo.Map(tx.Tags, func(modelTag *model.Tag, _ int) *TagOverview { return &TagOverview{ ID: modelTag.ID, @@ -231,7 +231,7 @@ func TestHandlers_GetTransactions(t *testing.T) { CreatedAt: tx.Group.CreatedAt, UpdatedAt: tx.Group.UpdatedAt, } - return &Transaction{ + return &TransactionNewCreate{ ID: tx.ID, Amount: tx.Amount, Target: tx.Target, @@ -327,7 +327,7 @@ func TestHandlers_GetTransactions(t *testing.T) { }). Return(txs, nil) - res := lo.Map(txs, func(tx *model.TransactionResponse, _ int) *Transaction { + res := lo.Map(txs, func(tx *model.TransactionResponse, _ int) *TransactionNewCreate { tag := lo.Map(tx.Tags, func(modelTag *model.Tag, _ int) *TagOverview { return &TagOverview{ ID: modelTag.ID, @@ -345,7 +345,7 @@ func TestHandlers_GetTransactions(t *testing.T) { CreatedAt: tx.Group.CreatedAt, UpdatedAt: tx.Group.UpdatedAt, } - return &Transaction{ + return &TransactionNewCreate{ ID: tx.ID, Amount: tx.Amount, Target: tx.Target, @@ -446,7 +446,7 @@ func TestHandlers_GetTransactions(t *testing.T) { }). Return(txs, nil) - res := lo.Map(txs, func(tx *model.TransactionResponse, _ int) *Transaction { + res := lo.Map(txs, func(tx *model.TransactionResponse, _ int) *TransactionNewCreate { tag := lo.Map(tx.Tags, func(modelTag *model.Tag, _ int) *TagOverview { return &TagOverview{ ID: modelTag.ID, @@ -464,7 +464,7 @@ func TestHandlers_GetTransactions(t *testing.T) { CreatedAt: tx.Group.CreatedAt, UpdatedAt: tx.Group.UpdatedAt, } - return &Transaction{ + return &TransactionNewCreate{ ID: tx.ID, Amount: tx.Amount, Target: tx.Target, @@ -549,7 +549,7 @@ func TestHandlers_GetTransactions(t *testing.T) { }). Return(txs, nil) - res := lo.Map(txs, func(tx *model.TransactionResponse, _ int) *Transaction { + res := lo.Map(txs, func(tx *model.TransactionResponse, _ int) *TransactionNewCreate { tag := lo.Map(tx.Tags, func(modelTag *model.Tag, _ int) *TagOverview { return &TagOverview{ ID: modelTag.ID, @@ -567,7 +567,7 @@ func TestHandlers_GetTransactions(t *testing.T) { CreatedAt: tx.Group.CreatedAt, UpdatedAt: tx.Group.UpdatedAt, } - return &Transaction{ + return &TransactionNewCreate{ ID: tx.ID, Amount: tx.Amount, Target: tx.Target, @@ -655,7 +655,7 @@ func TestHandlers_PostTransaction(t *testing.T) { CreateTransaction(c.Request().Context(), tx1.Amount, tx1.Target, tags, &group, nil). Return(tx1, nil) - res := lo.Map(txs, func(tx *model.TransactionResponse, _ int) *Transaction { + res := lo.Map(txs, func(tx *model.TransactionResponse, _ int) *TransactionNewCreate { tag := lo.Map(tx.Tags, func(modelTag *model.Tag, _ int) *TagOverview { return &TagOverview{ ID: modelTag.ID, @@ -673,7 +673,7 @@ func TestHandlers_PostTransaction(t *testing.T) { CreatedAt: tx.Group.CreatedAt, UpdatedAt: tx.Group.UpdatedAt, } - return &Transaction{ + return &TransactionNewCreate{ ID: tx.ID, Amount: tx.Amount, Target: tx.Target, @@ -788,7 +788,7 @@ func TestHandlers_PostTransaction(t *testing.T) { CreatedAt: tx.Group.CreatedAt, UpdatedAt: tx.Group.UpdatedAt, } - res := []*Transaction{ + res := []*TransactionNewCreate{ { ID: tx.ID, Amount: tx.Amount, @@ -861,7 +861,7 @@ func TestHandlers_GetTransaction(t *testing.T) { GetTransaction(c.Request().Context(), tx.ID). Return(tx, nil) - var resOverview Transaction + var resOverview TransactionCorrestion to := lo.Map(tx.Tags, func(modelTag *model.Tag, _ int) *TagOverview { return &TagOverview{ ID: modelTag.ID, @@ -879,7 +879,7 @@ func TestHandlers_GetTransaction(t *testing.T) { CreatedAt: tx.Group.CreatedAt, UpdatedAt: tx.Group.UpdatedAt, } - resOverview = Transaction{ + resOverview = TransactionCorrestion{ ID: tx.ID, Amount: tx.Amount, Target: tx.Target, @@ -990,7 +990,7 @@ func TestHandlers_PutTransaction(t *testing.T) { updatedTags, nil, nil). Return(updated, nil) - var resOverview Transaction + var resOverview TransactionCorrestion to := lo.Map(updated.Tags, func(modelTag *model.Tag, _ int) *TagOverview { return &TagOverview{ ID: modelTag.ID, @@ -1007,7 +1007,7 @@ func TestHandlers_PutTransaction(t *testing.T) { CreatedAt: updated.Group.CreatedAt, UpdatedAt: updated.Group.UpdatedAt, } - resOverview = Transaction{ + resOverview = TransactionCorrestion{ ID: tx.ID, Amount: updated.Amount, Target: updated.Target, From f47dec7c0caa5d0ff55bf4b9e73a025531e3969f Mon Sep 17 00:00:00 2001 From: Hueter Date: Sat, 7 Dec 2024 00:21:29 +0900 Subject: [PATCH 2/8] separete transaction in webhook --- service/webhook.go | 89 +++++++++++++++++++++++++++------------------- 1 file changed, 53 insertions(+), 36 deletions(-) diff --git a/service/webhook.go b/service/webhook.go index c2059158..759bc50e 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"` @@ -136,24 +144,52 @@ func WebhookEventHandler(c echo.Context, reqBody, resBody []byte) { message += resApp.Content + "\n" } } else if strings.Contains(c.Request().URL.Path, "/api/transactions") { - var resApps []TransactionRequestApplication - err := json.Unmarshal(resBody, &resApps) - resApp := resApps[0] - if err != nil { - return - } if c.Request().Method == http.MethodPost { + var resApps []TransactionPostRequestApplication + err := json.Unmarshal(resBody, &resApps) + if err != nil { + return + } message += fmt.Sprintf( "## :scroll:[入出金記録](%s/transactions/%s)が新規作成されました\n", "https://jomon.trap.jp", - resApp.ID) + resApps[0].ID) + targets := lo.Map( + resApps, func(resApp TransactionPostRequestApplication, _ int) string { + return resApp.Target + }) + if resApps[0].Amount < 0 { + message += fmt.Sprintf( + "- %sへの支払い\n - 支払い金額: 計%d円(一人当たりへの支払い金額: %d円)\n", + strings.Join(targets, " "), + -len(resApps)*resApps[0].Amount, + -resApps[0].Amount) + } else { + message += fmt.Sprintf( + "- %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, ", ")) + } } 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 len(resApps) == 1 { if resApp.Amount < 0 { message += fmt.Sprintf( "- `%s`への支払い\n - 支払い金額: %d円\n", @@ -165,36 +201,17 @@ func WebhookEventHandler(c echo.Context, reqBody, resBody []byte) { resApp.Target, resApp.Amount) } - } else { - targets := lo.Map( - resApps, func(resApp TransactionRequestApplication, _ int) string { - return resApp.Target + 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 }) - 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", strings.Join(tags, ", ")) } } - 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, ", ")) - } } - _ = RequestWebhook(message, webhookSecret, webhookChannelId, webhookId, 1) } From b58ee1d5fc23c4c997deb60788d41820a5d94f95 Mon Sep 17 00:00:00 2001 From: Hueter Date: Sat, 7 Dec 2024 00:30:56 +0900 Subject: [PATCH 3/8] fix typo --- router/transaction.go | 6 +++--- router/transaction_test.go | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/router/transaction.go b/router/transaction.go index eab09d8d..f52db46b 100644 --- a/router/transaction.go +++ b/router/transaction.go @@ -25,7 +25,7 @@ type TransactionNewCreate struct { UpdatedAt time.Time `json:"updated_at"` } -type TransactionCorrestion struct { +type TransactionCorrection struct { ID uuid.UUID `json:"id"` Amount int `json:"amount"` Target string `json:"target"` @@ -276,7 +276,7 @@ func (h Handlers) GetTransaction(c echo.Context) error { UpdatedAt: tx.Group.UpdatedAt, } } - res := TransactionCorrestion{ + res := TransactionCorrection{ ID: tx.ID, Amount: tx.Amount, Target: tx.Target, @@ -334,7 +334,7 @@ func (h Handlers) PutTransaction(c echo.Context) error { UpdatedAt: updated.Group.UpdatedAt, } } - res := TransactionCorrestion{ + res := TransactionCorrection{ ID: updated.ID, Amount: updated.Amount, Target: updated.Target, diff --git a/router/transaction_test.go b/router/transaction_test.go index 87dbfe3c..a416f812 100644 --- a/router/transaction_test.go +++ b/router/transaction_test.go @@ -861,7 +861,7 @@ func TestHandlers_GetTransaction(t *testing.T) { GetTransaction(c.Request().Context(), tx.ID). Return(tx, nil) - var resOverview TransactionCorrestion + var resOverview TransactionCorrection to := lo.Map(tx.Tags, func(modelTag *model.Tag, _ int) *TagOverview { return &TagOverview{ ID: modelTag.ID, @@ -879,7 +879,7 @@ func TestHandlers_GetTransaction(t *testing.T) { CreatedAt: tx.Group.CreatedAt, UpdatedAt: tx.Group.UpdatedAt, } - resOverview = TransactionCorrestion{ + resOverview = TransactionCorrection{ ID: tx.ID, Amount: tx.Amount, Target: tx.Target, @@ -990,7 +990,7 @@ func TestHandlers_PutTransaction(t *testing.T) { updatedTags, nil, nil). Return(updated, nil) - var resOverview TransactionCorrestion + var resOverview TransactionCorrection to := lo.Map(updated.Tags, func(modelTag *model.Tag, _ int) *TagOverview { return &TagOverview{ ID: modelTag.ID, @@ -1007,7 +1007,7 @@ func TestHandlers_PutTransaction(t *testing.T) { CreatedAt: updated.Group.CreatedAt, UpdatedAt: updated.Group.UpdatedAt, } - resOverview = TransactionCorrestion{ + resOverview = TransactionCorrection{ ID: tx.ID, Amount: updated.Amount, Target: updated.Target, From 7591bdfde58e9f412cf947c9b863353feb7f5fa6 Mon Sep 17 00:00:00 2001 From: Hueter Date: Sat, 7 Dec 2024 00:48:00 +0900 Subject: [PATCH 4/8] separate WebhookEventHundler into POST ,PUT --- router/router.go | 4 +- service/webhook.go | 149 ++++++++++++++++++++++++--------------------- 2 files changed, 83 insertions(+), 70 deletions(-) diff --git a/router/router.go b/router/router.go index 21984a4b..3ed89928 100644 --- a/router/router.go +++ b/router/router.go @@ -72,13 +72,13 @@ func NewServer(h Handlers) *echo.Echo { apiTransactions.POST( "", h.PostTransaction, - middleware.BodyDump(service.WebhookEventHandler), + middleware.BodyDump(service.WebhookPostEventHandler), h.CheckAdminMiddleware) apiTransactions.GET("/:transactionID", h.GetTransaction) apiTransactions.PUT( "/:transactionID", h.PutTransaction, - middleware.BodyDump(service.WebhookEventHandler), + middleware.BodyDump(service.WebhookPutEventHandler), h.CheckAdminMiddleware) } diff --git a/service/webhook.go b/service/webhook.go index 759bc50e..dbe958a4 100644 --- a/service/webhook.go +++ b/service/webhook.go @@ -143,78 +143,91 @@ func WebhookEventHandler(c echo.Context, reqBody, resBody []byte) { message += "\n\n" message += resApp.Content + "\n" } - } else if strings.Contains(c.Request().URL.Path, "/api/transactions") { - if c.Request().Method == http.MethodPost { - var resApps []TransactionPostRequestApplication - err := json.Unmarshal(resBody, &resApps) - if err != nil { - return - } - 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( - "- %sへの支払い\n - 支払い金額: 計%d円(一人当たりへの支払い金額: %d円)\n", - strings.Join(targets, " "), - -len(resApps)*resApps[0].Amount, - -resApps[0].Amount) - } else { - message += fmt.Sprintf( - "- %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, ", ")) - } - } 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 { - message += fmt.Sprintf( - "- `%s`からの振込\n - 受け取り金額: %d円\n", - resApp.Target, - resApp.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, ", ")) - } - } } _ = RequestWebhook(message, webhookSecret, webhookChannelId, webhookId, 1) } +func WebhookPostEventHandler(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 + var resApps []TransactionPostRequestApplication + err := json.Unmarshal(resBody, &resApps) + if err != nil || len(resApps) < 1 { + return + } + 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( + "- %sへの支払い\n - 支払い金額: 計%d円(一人当たりへの支払い金額: %d円)\n", + strings.Join(targets, " "), + -len(resApps)*resApps[0].Amount, + -resApps[0].Amount) + } else { + message += fmt.Sprintf( + "- %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, ", ")) + } + + _ = RequestWebhook(message, webhookSecret, webhookChannelId, webhookId, 1) +} + +func WebhookPutEventHandler(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 + 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 { + message += fmt.Sprintf( + "- `%s`からの振込\n - 受け取り金額: %d円\n", + resApp.Target, + resApp.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, ", ")) + } + + _ = RequestWebhook(message, webhookSecret, webhookChannelId, webhookId, 1) +} func RequestWebhook(message, secret, channelID, webhookID string, embed int) error { u, err := url.Parse("https://q.trap.jp/api/v3/webhooks") if err != nil { From 0a435812adda508de3fdcb9e4f36c866853fea4c Mon Sep 17 00:00:00 2001 From: Hueter Date: Mon, 9 Dec 2024 19:52:46 +0900 Subject: [PATCH 5/8] create new WebhookEventHundler about Requests and Transactions --- router/router.go | 4 +- service/webhook.go | 249 ++++++++++++++++++++++----------------------- 2 files changed, 126 insertions(+), 127 deletions(-) diff --git a/router/router.go b/router/router.go index 3ed89928..21984a4b 100644 --- a/router/router.go +++ b/router/router.go @@ -72,13 +72,13 @@ func NewServer(h Handlers) *echo.Echo { apiTransactions.POST( "", h.PostTransaction, - middleware.BodyDump(service.WebhookPostEventHandler), + middleware.BodyDump(service.WebhookEventHandler), h.CheckAdminMiddleware) apiTransactions.GET("/:transactionID", h.GetTransaction) apiTransactions.PUT( "/:transactionID", h.PutTransaction, - middleware.BodyDump(service.WebhookPutEventHandler), + middleware.BodyDump(service.WebhookEventHandler), h.CheckAdminMiddleware) } diff --git a/service/webhook.go b/service/webhook.go index dbe958a4..66c812f9 100644 --- a/service/webhook.go +++ b/service/webhook.go @@ -87,147 +87,146 @@ func WebhookEventHandler(c echo.Context, reqBody, resBody []byte) { 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, "/") - - 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 += WebhookRequestsEventHandler(c, resBody) + } else if strings.Contains(c.Request().URL.Path, "/api/transactions") { + message += WebhookTransactionsEventHandler(c, resBody) } _ = RequestWebhook(message, webhookSecret, webhookChannelId, webhookId, 1) } -func WebhookPostEventHandler(c echo.Context, reqBody, resBody []byte) { - webhookSecret := os.Getenv("WEBHOOK_SECRET") - webhookChannelId := os.Getenv("WEBHOOK_CHANNEL_ID") - webhookId := os.Getenv("WEBHOOK_ID") +func WebhookRequestsEventHandler(c echo.Context, resBody []byte) string { var message string - var resApps []TransactionPostRequestApplication - err := json.Unmarshal(resBody, &resApps) - if err != nil || len(resApps) < 1 { - return - } - 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 { + 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( - "- %sへの支払い\n - 支払い金額: 計%d円(一人当たりへの支払い金額: %d円)\n", - strings.Join(targets, " "), - -len(resApps)*resApps[0].Amount, - -resApps[0].Amount) + "## :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からの振込\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, ", ")) - } + "### [%s](%s/applications/%s)\n", + resApp.Title, + "https://jomon.trap.jp", + resApp.ID) - _ = RequestWebhook(message, webhookSecret, webhookChannelId, webhookId, 1) + 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" + } + return message } -func WebhookPutEventHandler(c echo.Context, reqBody, resBody []byte) { - webhookSecret := os.Getenv("WEBHOOK_SECRET") - webhookChannelId := os.Getenv("WEBHOOK_CHANNEL_ID") - webhookId := os.Getenv("WEBHOOK_ID") +func WebhookTransactionsEventHandler(c echo.Context, resBody []byte) string { var message string - 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 { + if c.Request().Method == http.MethodPost { + var resApps []TransactionPostRequestApplication + err := json.Unmarshal(resBody, &resApps) + if err != nil || len(resApps) < 1 { + return "" + } message += fmt.Sprintf( - "- `%s`への支払い\n - 支払い金額: %d円\n", - resApp.Target, - -resApp.Amount) - } else { + "## :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( + "- %sへの支払い\n - 支払い金額: 計%d円(一人当たりへの支払い金額: %d円)\n", + strings.Join(targets, " "), + -len(resApps)*resApps[0].Amount, + -resApps[0].Amount) + } else { + message += fmt.Sprintf( + "- %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, ", ")) + } + } else if c.Request().Method == http.MethodPut { + var resApp TransactionPutRequestApplication + err := json.Unmarshal(resBody, &resApp) + if err != nil { + return "" + } message += fmt.Sprintf( - "- `%s`からの振込\n - 受け取り金額: %d円\n", - resApp.Target, - resApp.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, ", ")) + "## :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 { + message += fmt.Sprintf( + "- `%s`からの振込\n - 受け取り金額: %d円\n", + resApp.Target, + resApp.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, ", ")) + } } - - _ = RequestWebhook(message, webhookSecret, webhookChannelId, webhookId, 1) + return message } + func RequestWebhook(message, secret, channelID, webhookID string, embed int) error { u, err := url.Parse("https://q.trap.jp/api/v3/webhooks") if err != nil { From 0f91889c05848e6a90950b79c89dd01bfc710971 Mon Sep 17 00:00:00 2001 From: Hueter Date: Fri, 13 Dec 2024 18:02:56 +0900 Subject: [PATCH 6/8] fix message in webhook --- service/webhook.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/service/webhook.go b/service/webhook.go index 66c812f9..7c73821c 100644 --- a/service/webhook.go +++ b/service/webhook.go @@ -87,9 +87,12 @@ func WebhookEventHandler(c echo.Context, reqBody, resBody []byte) { var message string if strings.Contains(c.Request().URL.Path, "/api/requests") { - message += WebhookRequestsEventHandler(c, resBody) + message = WebhookRequestsEventHandler(c, resBody) } else if strings.Contains(c.Request().URL.Path, "/api/transactions") { - message += WebhookTransactionsEventHandler(c, resBody) + message = WebhookTransactionsEventHandler(c, resBody) + } + if len(message) == 0 { + return } _ = RequestWebhook(message, webhookSecret, webhookChannelId, webhookId, 1) } From 20d0dceb8ea5cad2034a03db6bc21d7961cecb2a Mon Sep 17 00:00:00 2001 From: Hueter Date: Thu, 26 Dec 2024 17:24:33 +0900 Subject: [PATCH 7/8] revert router/transaction --- router/transaction.go | 26 +++++++------------------- router/transaction_test.go | 30 +++++++++++++++--------------- 2 files changed, 22 insertions(+), 34 deletions(-) diff --git a/router/transaction.go b/router/transaction.go index ae3cb9ca..ec374eb4 100644 --- a/router/transaction.go +++ b/router/transaction.go @@ -14,19 +14,7 @@ import ( "go.uber.org/zap" ) -type TransactionNewCreate struct { - ID uuid.UUID `json:"id"` - Title string `json:"title"` - Amount int `json:"amount"` - Target string `json:"target"` - Request *uuid.UUID `json:"request"` - Tags []*TagOverview `json:"tags"` - Group *GroupOverview `json:"group"` - CreatedAt time.Time `json:"created_at"` - UpdatedAt time.Time `json:"updated_at"` -} - -type TransactionCorrection struct { +type Transaction struct { ID uuid.UUID `json:"id"` Title string `json:"title"` Amount int `json:"amount"` @@ -152,7 +140,7 @@ func (h Handlers) GetTransactions(c echo.Context) error { return echo.NewHTTPError(http.StatusInternalServerError, err) } - res := lo.Map(txs, func(tx *model.TransactionResponse, _ int) *TransactionNewCreate { + res := lo.Map(txs, func(tx *model.TransactionResponse, _ int) *Transaction { tags := lo.Map(tx.Tags, func(tag *model.Tag, _ int) *TagOverview { return &TagOverview{ ID: tag.ID, @@ -173,7 +161,7 @@ func (h Handlers) GetTransactions(c echo.Context) error { UpdatedAt: tx.Group.UpdatedAt, } } - return &TransactionNewCreate{ + return &Transaction{ ID: tx.ID, Title: tx.Title, Amount: tx.Amount, @@ -197,7 +185,7 @@ func (h Handlers) PostTransaction(c echo.Context) error { return echo.NewHTTPError(http.StatusBadRequest, err) } - transactions := []*TransactionNewCreate{} + transactions := []*Transaction{} ctx := c.Request().Context() for _, target := range tx.Targets { if target == nil { @@ -232,7 +220,7 @@ func (h Handlers) PostTransaction(c echo.Context) error { UpdatedAt: created.Group.UpdatedAt, } } - res := TransactionNewCreate{ + res := Transaction{ ID: created.ID, Title: created.Title, Amount: created.Amount, @@ -283,7 +271,7 @@ func (h Handlers) GetTransaction(c echo.Context) error { UpdatedAt: tx.Group.UpdatedAt, } } - res := TransactionCorrection{ + res := Transaction{ ID: tx.ID, Title: tx.Title, Amount: tx.Amount, @@ -343,7 +331,7 @@ func (h Handlers) PutTransaction(c echo.Context) error { UpdatedAt: updated.Group.UpdatedAt, } } - res := TransactionCorrection{ + res := Transaction{ ID: updated.ID, Title: updated.Title, Amount: updated.Amount, diff --git a/router/transaction_test.go b/router/transaction_test.go index 492a95f5..b92775c0 100644 --- a/router/transaction_test.go +++ b/router/transaction_test.go @@ -102,7 +102,7 @@ func TestHandlers_GetTransactions(t *testing.T) { }). Return(txs, nil) - res := lo.Map(txs, func(tx *model.TransactionResponse, _ int) *TransactionNewCreate { + res := lo.Map(txs, func(tx *model.TransactionResponse, _ int) *Transaction { tag := lo.Map(tx.Tags, func(modelTag *model.Tag, _ int) *TagOverview { return &TagOverview{ ID: modelTag.ID, @@ -120,7 +120,7 @@ func TestHandlers_GetTransactions(t *testing.T) { CreatedAt: tx.Group.CreatedAt, UpdatedAt: tx.Group.UpdatedAt, } - return &TransactionNewCreate{ + return &Transaction{ ID: tx.ID, Title: tx.Title, Amount: tx.Amount, @@ -218,7 +218,7 @@ func TestHandlers_GetTransactions(t *testing.T) { }). Return(txs, nil) - res := lo.Map(txs, func(tx *model.TransactionResponse, _ int) *TransactionNewCreate { + res := lo.Map(txs, func(tx *model.TransactionResponse, _ int) *Transaction { tag := lo.Map(tx.Tags, func(modelTag *model.Tag, _ int) *TagOverview { return &TagOverview{ ID: modelTag.ID, @@ -236,7 +236,7 @@ func TestHandlers_GetTransactions(t *testing.T) { CreatedAt: tx.Group.CreatedAt, UpdatedAt: tx.Group.UpdatedAt, } - return &TransactionNewCreate{ + return &Transaction{ ID: tx.ID, Title: tx.Title, Amount: tx.Amount, @@ -335,7 +335,7 @@ func TestHandlers_GetTransactions(t *testing.T) { }). Return(txs, nil) - res := lo.Map(txs, func(tx *model.TransactionResponse, _ int) *TransactionNewCreate { + res := lo.Map(txs, func(tx *model.TransactionResponse, _ int) *Transaction { tag := lo.Map(tx.Tags, func(modelTag *model.Tag, _ int) *TagOverview { return &TagOverview{ ID: modelTag.ID, @@ -353,7 +353,7 @@ func TestHandlers_GetTransactions(t *testing.T) { CreatedAt: tx.Group.CreatedAt, UpdatedAt: tx.Group.UpdatedAt, } - return &TransactionNewCreate{ + return &Transaction{ ID: tx.ID, Title: tx.Title, Amount: tx.Amount, @@ -457,7 +457,7 @@ func TestHandlers_GetTransactions(t *testing.T) { }). Return(txs, nil) - res := lo.Map(txs, func(tx *model.TransactionResponse, _ int) *TransactionNewCreate { + res := lo.Map(txs, func(tx *model.TransactionResponse, _ int) *Transaction { tag := lo.Map(tx.Tags, func(modelTag *model.Tag, _ int) *TagOverview { return &TagOverview{ ID: modelTag.ID, @@ -475,7 +475,7 @@ func TestHandlers_GetTransactions(t *testing.T) { CreatedAt: tx.Group.CreatedAt, UpdatedAt: tx.Group.UpdatedAt, } - return &TransactionNewCreate{ + return &Transaction{ ID: tx.ID, Title: tx.Title, Amount: tx.Amount, @@ -562,7 +562,7 @@ func TestHandlers_GetTransactions(t *testing.T) { }). Return(txs, nil) - res := lo.Map(txs, func(tx *model.TransactionResponse, _ int) *TransactionNewCreate { + res := lo.Map(txs, func(tx *model.TransactionResponse, _ int) *Transaction { tag := lo.Map(tx.Tags, func(modelTag *model.Tag, _ int) *TagOverview { return &TagOverview{ ID: modelTag.ID, @@ -580,7 +580,7 @@ func TestHandlers_GetTransactions(t *testing.T) { CreatedAt: tx.Group.CreatedAt, UpdatedAt: tx.Group.UpdatedAt, } - return &TransactionNewCreate{ + return &Transaction{ ID: tx.ID, Title: tx.Title, Amount: tx.Amount, @@ -673,7 +673,7 @@ func TestHandlers_PostTransaction(t *testing.T) { tx1.Title, tx1.Amount, tx1.Target, tags, &group, nil). Return(tx1, nil) - res := lo.Map(txs, func(tx *model.TransactionResponse, _ int) *TransactionNewCreate { + res := lo.Map(txs, func(tx *model.TransactionResponse, _ int) *Transaction { tag := lo.Map(tx.Tags, func(modelTag *model.Tag, _ int) *TagOverview { return &TagOverview{ ID: modelTag.ID, @@ -691,7 +691,7 @@ func TestHandlers_PostTransaction(t *testing.T) { CreatedAt: tx.Group.CreatedAt, UpdatedAt: tx.Group.UpdatedAt, } - return &TransactionNewCreate{ + return &Transaction{ ID: tx.ID, Title: tx.Title, Amount: tx.Amount, @@ -810,7 +810,7 @@ func TestHandlers_PostTransaction(t *testing.T) { CreatedAt: tx.Group.CreatedAt, UpdatedAt: tx.Group.UpdatedAt, } - res := []*TransactionNewCreate{ + res := []*Transaction{ { ID: tx.ID, Title: tx.Title, @@ -905,7 +905,7 @@ func TestHandlers_GetTransaction(t *testing.T) { CreatedAt: tx.Group.CreatedAt, UpdatedAt: tx.Group.UpdatedAt, } - resOverview := TransactionCorrection{ + resOverview := Transaction{ ID: tx.ID, Title: tx.Title, Amount: tx.Amount, @@ -1034,7 +1034,7 @@ func TestHandlers_PutTransaction(t *testing.T) { CreatedAt: updated.Group.CreatedAt, UpdatedAt: updated.Group.UpdatedAt, } - resOverview := TransactionCorrection{ + resOverview := Transaction{ ID: tx.ID, Title: updated.Title, Amount: updated.Amount, From a040a41bdec90aa14a42eca2481a9ac64d74fa22 Mon Sep 17 00:00:00 2001 From: Hueter Date: Thu, 26 Dec 2024 17:48:19 +0900 Subject: [PATCH 8/8] delete WebhookEventHundler and change to Requests, Transactions EventHandler --- router/router.go | 13 ++++++++----- service/webhook.go | 33 ++++++++++++--------------------- 2 files changed, 20 insertions(+), 26 deletions(-) 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 7c73821c..1f1a3cdd 100644 --- a/service/webhook.go +++ b/service/webhook.go @@ -80,30 +80,17 @@ 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") { - message = WebhookRequestsEventHandler(c, resBody) - } else if strings.Contains(c.Request().URL.Path, "/api/transactions") { - message = WebhookTransactionsEventHandler(c, resBody) - } - if len(message) == 0 { - return - } - _ = RequestWebhook(message, webhookSecret, webhookChannelId, webhookId, 1) -} - -func WebhookRequestsEventHandler(c echo.Context, resBody []byte) string { - var message string if strings.Contains(c.Request().URL.Path, "/comments") { resApp := new(CommentApplication) err := json.Unmarshal(resBody, resApp) if err != nil { - return "" + return } splitedPath := strings.Split(c.Request().URL.Path, "/") @@ -123,7 +110,7 @@ func WebhookRequestsEventHandler(c echo.Context, resBody []byte) string { resApp := new(RequestApplication) err := json.Unmarshal(resBody, resApp) if err != nil { - return "" + return } if c.Request().Method == http.MethodPost { message += "## :receipt:申請が作成されました\n" @@ -155,16 +142,20 @@ func WebhookRequestsEventHandler(c echo.Context, resBody []byte) string { message += "\n\n" message += resApp.Content + "\n" } - return message + _ = RequestWebhook(message, webhookSecret, webhookChannelId, webhookId, 1) } -func WebhookTransactionsEventHandler(c echo.Context, resBody []byte) string { +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) if err != nil || len(resApps) < 1 { - return "" + return } message += fmt.Sprintf( "## :scroll:[入出金記録](%s/transactions/%s)が新規作成されました\n", @@ -200,7 +191,7 @@ func WebhookTransactionsEventHandler(c echo.Context, resBody []byte) string { var resApp TransactionPutRequestApplication err := json.Unmarshal(resBody, &resApp) if err != nil { - return "" + return } message += fmt.Sprintf( "## :scroll:[入出金記録](%s/transactions/%s)が修正されました\n", @@ -227,7 +218,7 @@ func WebhookTransactionsEventHandler(c echo.Context, resBody []byte) string { message += fmt.Sprintf("- タグ: %s", strings.Join(tags, ", ")) } } - return message + _ = RequestWebhook(message, webhookSecret, webhookChannelId, webhookId, 1) } func RequestWebhook(message, secret, channelID, webhookID string, embed int) error {