diff --git a/handlers/transaction.go b/handlers/transaction.go index 9b313d6..ccc77b6 100644 --- a/handlers/transaction.go +++ b/handlers/transaction.go @@ -7,7 +7,9 @@ import ( "git.agecem.com/agecem/bottin-agenda/data" "git.agecem.com/agecem/bottin-agenda/models" "git.agecem.com/agecem/bottin-agenda/responses" + bottindata "git.agecem.com/agecem/bottin/v5/data" "github.com/labstack/echo/v4" + "github.com/spf13/viper" ) // GetTransactions lists transactions on GET /v2/transactions http/1.1 @@ -73,6 +75,44 @@ func PostTransactions(c echo.Context) error { return c.JSON(statusCode, response) } + bottinApiKey := viper.GetString("bottin.api.key") + bottinApiHost := viper.GetString("bottin.api.host") + bottinApiProtocol := viper.GetString("bottin.api.protocol") + bottinApiPort := viper.GetInt("bottin.api.port") + + // Using bottin's API client + bottinApiClient := bottindata.NewApiClient( + bottinApiKey, + bottinApiHost, + bottinApiProtocol, + bottinApiPort, + ) + + // Check if membre_id exists according to bottin + for _, transaction := range transactions { + if transaction.MembreID == "" { + response.Message = fmt.Sprintf("Cannot insert transaction without a membre_id") + statusCode = http.StatusBadRequest + + return c.JSON(statusCode, response) + } + membre, err := bottinApiClient.GetMembre(transaction.MembreID) + if err != nil { + response.Message = fmt.Sprintf("Error during bottinApiClient.GetMembre(): %s", err) + + return c.JSON(statusCode, response) + } + + if membre.ID == "" { + response.Message = fmt.Sprintf("Cannot insert transaction for non-existent membre %s", membre.ID) + statusCode = http.StatusNotFound + + return c.JSON(statusCode, response) + } + + // membre exists, can keep going + } + // Check for already-existing transactions for _, transaction := range transactions { transaction, err := client.GetTransaction(transaction.MembreID, transaction.IsPerpetual)