Skip to content

Commit

Permalink
separate WebhookEventHundler into POST ,PUT
Browse files Browse the repository at this point in the history
  • Loading branch information
Hueter57 committed Dec 6, 2024
1 parent b58ee1d commit 7591bdf
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 70 deletions.
4 changes: 2 additions & 2 deletions router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,13 @@ func NewServer(h Handlers) *echo.Echo {
apiTransactions.POST(
"",
h.PostTransaction,
middleware.BodyDump(service.WebhookEventHandler),
middleware.BodyDump(service.WebhookPostEventHandler),

Check warning on line 75 in router/router.go

View check run for this annotation

Codecov / codecov/patch

router/router.go#L75

Added line #L75 was not covered by tests
h.CheckAdminMiddleware)
apiTransactions.GET("/:transactionID", h.GetTransaction)
apiTransactions.PUT(
"/:transactionID",
h.PutTransaction,
middleware.BodyDump(service.WebhookEventHandler),
middleware.BodyDump(service.WebhookPutEventHandler),

Check warning on line 81 in router/router.go

View check run for this annotation

Codecov / codecov/patch

router/router.go#L81

Added line #L81 was not covered by tests
h.CheckAdminMiddleware)
}

Expand Down
149 changes: 81 additions & 68 deletions service/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Check warning on line 147 in service/webhook.go

View check run for this annotation

Codecov / codecov/patch

service/webhook.go#L147

Added line #L147 was not covered by tests
}

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, ", "))

Check warning on line 188 in service/webhook.go

View check run for this annotation

Codecov / codecov/patch

service/webhook.go#L150-L188

Added lines #L150 - L188 were not covered by tests
}

_ = 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, ", "))

Check warning on line 226 in service/webhook.go

View check run for this annotation

Codecov / codecov/patch

service/webhook.go#L194-L226

Added lines #L194 - L226 were not covered by tests
}

_ = RequestWebhook(message, webhookSecret, webhookChannelId, webhookId, 1)

Check warning on line 229 in service/webhook.go

View check run for this annotation

Codecov / codecov/patch

service/webhook.go#L229

Added line #L229 was not covered by tests
}
func RequestWebhook(message, secret, channelID, webhookID string, embed int) error {
u, err := url.Parse("https://q.trap.jp/api/v3/webhooks")
if err != nil {
Expand Down

0 comments on commit 7591bdf

Please sign in to comment.