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
25 changes: 18 additions & 7 deletions router/transaction.go
Hueter57 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Hueter57 marked this conversation as resolved.
Show resolved Hide resolved
ID uuid.UUID `json:"id"`
Amount int `json:"amount"`
Target string `json:"target"`
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
34 changes: 17 additions & 17 deletions router/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
89 changes: 53 additions & 36 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 @@ -136,24 +144,52 @@
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
}

Check warning on line 152 in service/webhook.go

View check run for this annotation

Codecov / codecov/patch

service/webhook.go#L148-L152

Added lines #L148 - L152 were not covered by tests
message += fmt.Sprintf(
"## :scroll:[入出金記録](%s/transactions/%s)が新規作成されました\n",
"https://jomon.trap.jp",
resApp.ID)
resApps[0].ID)
Hueter57 marked this conversation as resolved.
Show resolved Hide resolved
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 181 in service/webhook.go

View check run for this annotation

Codecov / codecov/patch

service/webhook.go#L156-L181

Added lines #L156 - L181 were not covered by tests
}
} else if c.Request().Method == http.MethodPut {
var resApp TransactionPutRequestApplication
Hueter57 marked this conversation as resolved.
Show resolved Hide resolved
err := json.Unmarshal(resBody, &resApp)
if err != nil {
return
}

Check warning on line 188 in service/webhook.go

View check run for this annotation

Codecov / codecov/patch

service/webhook.go#L184-L188

Added lines #L184 - L188 were not covered by tests
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",
Expand All @@ -165,36 +201,17 @@
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

Check warning on line 209 in service/webhook.go

View check run for this annotation

Codecov / codecov/patch

service/webhook.go#L204-L209

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

Check warning on line 211 in service/webhook.go

View check run for this annotation

Codecov / codecov/patch

service/webhook.go#L211

Added line #L211 was not covered by tests
}
}
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)
}

Expand Down
Loading