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

webhookのeventhandlerが怪しい #769

Open
H1rono opened this issue Jun 28, 2024 · 0 comments
Open

webhookのeventhandlerが怪しい #769

H1rono opened this issue Jun 28, 2024 · 0 comments

Comments

@H1rono
Copy link
Member

H1rono commented Jun 28, 2024

特にtransaction周り

handler登録:

Jomon/router/router.go

Lines 62 to 68 in 2083952

apiTransactions := api.Group("/transactions", h.CheckLoginMiddleware)
{
apiTransactions.GET("", h.GetTransactions)
apiTransactions.POST("", h.PostTransaction, middleware.BodyDump(service.WebhookEventHandler), h.CheckAdminMiddleware)
apiTransactions.GET("/:transactionID", h.GetTransaction)
apiTransactions.PUT("/:transactionID", h.PutTransaction, middleware.BodyDump(service.WebhookEventHandler), h.CheckAdminMiddleware)
}

handler実装:

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

Jomon/service/webhook.go

Lines 129 to 173 in 2083952

} 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 {
message += fmt.Sprintf("## :scroll:[入出金記録](%s/transactions/%s)が新規作成されました\n", "https://jomon.trap.jp", resApp.ID)
} else if c.Request().Method == http.MethodPut {
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", resApp.Target, -resApp.Amount)
} else {
message += fmt.Sprintf("- `%s`からの振込\n - 受け取り金額: %d円\n", resApp.Target, resApp.Amount)
}
} else {
targets := make([]string, len(resApps))
for i, resApp := range resApps {
targets[i] = fmt.Sprintf(`%s`, 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)
}
}
if resApp.Group != nil {
message += fmt.Sprintf("- 関連するグループ: %s\n", resApp.Group.Name)
}
if resApp.Tags != nil {
tags := make([]string, len(resApp.Tags))
for i, tag := range resApp.Tags {
tags[i] = fmt.Sprintf(`%s`, tag.Name)
}
if len(resApp.Tags) == 0 {
message += fmt.Sprintf("")
} else {
message += fmt.Sprintf("- タグ: %s", strings.Join(tags, " "))
}
}
}

POST /api/transactionsPUT /api/transactions/:transactionIDに対して同一の構造体でペイロードをbindしている

Jomon/service/webhook.go

Lines 38 to 44 in 2083952

type TransactionRequestApplication struct {
ID uuid.UUID `json:"id"`
Amount int `json:"amount"`
Target string `json:"target"`
Tags []*Tag `json:"tags"`
Group *Group `json:"group"`
}

が、この2つのエンドポイントではペイロードの構造が違う
POSTのペイロード
type TransactionOverview struct {
Amount int `json:"amount"`
Targets []*string `json:"targets"`
Tags []*uuid.UUID `json:"tags"`
Group *uuid.UUID `json:"group"`
Request *uuid.UUID `json:"request"`
}

PUTのペイロード
type TransactionOverviewWithOneTarget struct {
Amount int `json:"amount"`
Target string `json:"target"`
Tags []*uuid.UUID `json:"tags"`
Group *uuid.UUID `json:"group"`
Request *uuid.UUID `json:"request"`
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant