-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrollback.go
More file actions
74 lines (60 loc) · 2.28 KB
/
Copy pathrollback.go
File metadata and controls
74 lines (60 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package interbank
import (
"net/http"
"github.com/gofiber/fiber/v2"
"github.com/google/uuid"
"github.com/jnaraujo/tec502-inter-bank/bank/internal/models"
"github.com/jnaraujo/tec502-inter-bank/bank/internal/storage"
"github.com/jnaraujo/tec502-inter-bank/bank/internal/validate"
)
type rollbackBodySchema struct {
TxId uuid.UUID `json:"tx_id" validate:"required"`
Step string `json:"step" validate:"required,oneof=credit debit"`
}
func RollbackRoute(c *fiber.Ctx) error {
var body rollbackBodySchema
if errs := validate.ParseAndValidate(c.Body(), &body); len(errs) > 0 {
return c.Status(http.StatusBadRequest).JSON(&fiber.Map{"errors": errs})
}
transaction := storage.Transactions.FindTransactionById(body.TxId)
if transaction == nil {
return c.Status(http.StatusBadRequest).JSON(&fiber.Map{
"message": "Transação não encontrada",
})
}
if len(transaction.Operations) != 1 {
return c.Status(http.StatusBadRequest).JSON(&fiber.Map{
"message": "A transação deve ter apenas uma operação",
})
}
operation := transaction.Operations[0]
fromAcc := storage.Accounts.FindAccountByIBK(operation.From)
toAcc := storage.Accounts.FindAccountByIBK(operation.To)
if body.Step == "debit" && fromAcc == nil {
return c.Status(http.StatusBadRequest).JSON(&fiber.Map{
"message": "Conta de origem não encontrada",
})
}
if body.Step == "credit" && toAcc == nil {
return c.Status(http.StatusBadRequest).JSON(&fiber.Map{
"message": "Conta de destino não encontrada",
})
}
if operation.Status == models.OperationStatusSuccess {
if body.Step == "debit" {
storage.Accounts.AddToAccountBalance(fromAcc.Id, operation.Amount)
} else if body.Step == "credit" {
storage.Accounts.SubFromAccountBalance(toAcc.Id, operation.Amount)
}
}
if operation.Status == models.OperationStatusPending {
if body.Step == "debit" {
storage.Accounts.SubFromBlockedAccountBalance(fromAcc.Id, operation.Amount)
} else if body.Step == "credit" {
storage.Accounts.SubFromPendingAccountBalance(toAcc.Id, operation.Amount)
}
}
storage.Transactions.UpdateOperationStatus(*transaction, operation, models.OperationStatusFailed)
transaction = storage.Transactions.UpdateTransactionStatus(*transaction, models.TransactionStatusFailed)
return c.Status(http.StatusOK).JSON(transaction)
}