Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correction webhook eventhandler #815

Merged
merged 9 commits into from
Dec 27, 2024
13 changes: 8 additions & 5 deletions router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,22 @@
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))

Check warning on line 55 in router/router.go

View check run for this annotation

Codecov / codecov/patch

router/router.go#L52-L55

Added lines #L52 - L55 were not covered by tests
apiRequestIDs := apiRequests.Group("/:requestID", retrieveRequestCreator)
{
apiRequestIDs.GET("", h.GetRequest)
apiRequestIDs.PUT(
"",
h.PutRequest,
middleware.BodyDump(service.WebhookEventHandler),
middleware.BodyDump(service.WebhookRequestsEventHandler),

Check warning on line 62 in router/router.go

View check run for this annotation

Codecov / codecov/patch

router/router.go#L62

Added line #L62 was not covered by tests
h.CheckRequestCreatorMiddleware)
apiRequestIDs.POST(
"/comments",
h.PostComment,
middleware.BodyDump(service.WebhookEventHandler))
middleware.BodyDump(service.WebhookRequestsEventHandler))

Check warning on line 67 in router/router.go

View check run for this annotation

Codecov / codecov/patch

router/router.go#L67

Added line #L67 was not covered by tests
apiRequestIDs.PUT("/status", h.PutStatus, h.CheckAdminOrRequestCreatorMiddleware)
}
}
Expand All @@ -72,13 +75,13 @@
apiTransactions.POST(
"",
h.PostTransaction,
middleware.BodyDump(service.WebhookEventHandler),
middleware.BodyDump(service.WebhookTransactionsEventHandler),

Check warning on line 78 in router/router.go

View check run for this annotation

Codecov / codecov/patch

router/router.go#L78

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

Check warning on line 84 in router/router.go

View check run for this annotation

Codecov / codecov/patch

router/router.go#L84

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

Expand Down
219 changes: 121 additions & 98 deletions service/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,15 @@
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"`
Expand Down Expand Up @@ -72,117 +80,133 @@
ID string
}

func WebhookEventHandler(c echo.Context, reqBody, resBody []byte) {
func WebhookRequestsEventHandler(c echo.Context, reqBody, resBody []byte) {

Check warning on line 83 in service/webhook.go

View check run for this annotation

Codecov / codecov/patch

service/webhook.go#L83

Added line #L83 was not covered by tests
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, "/")

Check warning on line 95 in service/webhook.go

View check run for this annotation

Codecov / codecov/patch

service/webhook.go#L89-L95

Added lines #L89 - L95 were not covered by tests

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"
}

Check warning on line 119 in service/webhook.go

View check run for this annotation

Codecov / codecov/patch

service/webhook.go#L97-L119

Added lines #L97 - L119 were not covered by tests

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)

Check warning on line 133 in service/webhook.go

View check run for this annotation

Codecov / codecov/patch

service/webhook.go#L121-L133

Added lines #L121 - L133 were not covered by tests
}
} 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, ", "))

Check warning on line 140 in service/webhook.go

View check run for this annotation

Codecov / codecov/patch

service/webhook.go#L136-L140

Added lines #L136 - L140 were not covered by tests
}
message += "\n\n"
message += resApp.Content + "\n"

Check warning on line 143 in service/webhook.go

View check run for this annotation

Codecov / codecov/patch

service/webhook.go#L142-L143

Added lines #L142 - L143 were not covered by tests
}
_ = RequestWebhook(message, webhookSecret, webhookChannelId, webhookId, 1)

Check warning on line 145 in service/webhook.go

View check run for this annotation

Codecov / codecov/patch

service/webhook.go#L145

Added line #L145 was not covered by tests
}

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

Check warning on line 155 in service/webhook.go

View check run for this annotation

Codecov / codecov/patch

service/webhook.go#L148-L155

Added lines #L148 - L155 were not covered by tests
err := json.Unmarshal(resBody, &resApps)
resApp := resApps[0]
if err != nil {
if err != nil || len(resApps) < 1 {

Check warning on line 157 in service/webhook.go

View check run for this annotation

Codecov / codecov/patch

service/webhook.go#L157

Added line #L157 was not covered by tests
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 {

Check warning on line 168 in service/webhook.go

View check run for this annotation

Codecov / codecov/patch

service/webhook.go#L160-L168

Added lines #L160 - L168 were not covered by tests
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 {

Check warning on line 174 in service/webhook.go

View check run for this annotation

Codecov / codecov/patch

service/webhook.go#L170-L174

Added lines #L170 - L174 were not covered by tests
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, ", "))

Check warning on line 188 in service/webhook.go

View check run for this annotation

Codecov / codecov/patch

service/webhook.go#L176-L188

Added lines #L176 - L188 were not covered by tests
}
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)

Check warning on line 204 in service/webhook.go

View check run for this annotation

Codecov / codecov/patch

service/webhook.go#L190-L204

Added lines #L190 - L204 were not covered by tests
} 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)

Check warning on line 209 in service/webhook.go

View check run for this annotation

Codecov / codecov/patch

service/webhook.go#L206-L209

Added lines #L206 - L209 were not covered by tests
}
if resApp.Group != nil {
message += fmt.Sprintf("- 関連するグループ: %s\n", resApp.Group.Name)
Expand All @@ -194,7 +218,6 @@
message += fmt.Sprintf("- タグ: %s", strings.Join(tags, ", "))
}
}

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

Expand Down
Loading