2023-06-06 02:22:57 -04:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"git.agecem.com/agecem/bottin-agenda/data"
|
|
|
|
"git.agecem.com/agecem/bottin-agenda/models"
|
|
|
|
"git.agecem.com/agecem/bottin-agenda/responses"
|
2023-06-08 04:04:02 -04:00
|
|
|
bottindata "git.agecem.com/agecem/bottin/v5/data"
|
2023-06-06 02:22:57 -04:00
|
|
|
"github.com/labstack/echo/v4"
|
2023-06-08 04:04:02 -04:00
|
|
|
"github.com/spf13/viper"
|
2023-06-06 02:22:57 -04:00
|
|
|
)
|
|
|
|
|
2023-06-08 21:08:20 -04:00
|
|
|
// GetTransactions handles the listing of transactions
|
2023-06-08 00:53:05 -04:00
|
|
|
func GetTransactions(c echo.Context) error {
|
|
|
|
var statusCode int = http.StatusInternalServerError
|
|
|
|
var response responses.GetTransactionsResponse
|
|
|
|
|
|
|
|
client, err := data.NewDataClientFromViper()
|
|
|
|
if err != nil {
|
2023-06-10 00:07:51 -04:00
|
|
|
response.Message = fmt.Sprintf("Erreur pendant data.NewDataClientFromViper(): %s", err)
|
2023-06-08 00:53:05 -04:00
|
|
|
|
|
|
|
return c.JSON(statusCode, response)
|
|
|
|
}
|
|
|
|
defer client.DB.Close()
|
|
|
|
|
|
|
|
transactions, err := client.ListTransactions()
|
|
|
|
response.Data.Transactions = transactions
|
|
|
|
if err != nil {
|
2023-06-10 00:07:51 -04:00
|
|
|
response.Message = fmt.Sprintf("Erreur pendant client.ListTransactions(): %s", err)
|
2023-06-08 00:53:05 -04:00
|
|
|
|
|
|
|
return c.JSON(statusCode, response)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(transactions) == 0 {
|
2023-06-10 00:07:51 -04:00
|
|
|
response.Message = "Aucune transaction trouvée"
|
2023-06-08 00:53:05 -04:00
|
|
|
statusCode = http.StatusNotFound
|
|
|
|
|
|
|
|
return c.JSON(statusCode, response)
|
|
|
|
}
|
|
|
|
|
2023-06-10 00:07:51 -04:00
|
|
|
response.Message = "Liste complétée"
|
2023-06-08 00:53:05 -04:00
|
|
|
statusCode = http.StatusOK
|
|
|
|
|
|
|
|
return c.JSON(statusCode, response)
|
|
|
|
}
|
|
|
|
|
2023-06-08 21:08:20 -04:00
|
|
|
// PostTransactions handles the creation of transactions
|
2023-06-06 02:22:57 -04:00
|
|
|
func PostTransactions(c echo.Context) error {
|
|
|
|
var statusCode int = http.StatusInternalServerError
|
|
|
|
var response responses.PostTransactionsResponse
|
|
|
|
|
|
|
|
var transactions []models.Transaction
|
|
|
|
|
|
|
|
if err := c.Bind(&transactions); err != nil {
|
|
|
|
statusCode = http.StatusBadRequest
|
2023-06-10 00:07:51 -04:00
|
|
|
response.Message = fmt.Sprintf("Erreur pendant c.Bind(): %s", err)
|
2023-06-06 02:22:57 -04:00
|
|
|
|
|
|
|
return c.JSON(statusCode, response)
|
|
|
|
}
|
|
|
|
|
|
|
|
client, err := data.NewDataClientFromViper()
|
|
|
|
if err != nil {
|
2023-06-10 00:07:51 -04:00
|
|
|
response.Message = fmt.Sprintf("Erreur pendant data.NewDataClientFromViper(): %s", err)
|
2023-06-06 02:22:57 -04:00
|
|
|
|
|
|
|
return c.JSON(statusCode, response)
|
|
|
|
}
|
|
|
|
defer client.DB.Close()
|
|
|
|
|
|
|
|
if len(transactions) == 0 {
|
2023-06-10 00:07:51 -04:00
|
|
|
response.Message = fmt.Sprintf("Rien à faire")
|
2023-06-06 02:22:57 -04:00
|
|
|
statusCode = http.StatusOK
|
|
|
|
|
|
|
|
return c.JSON(statusCode, response)
|
|
|
|
}
|
|
|
|
|
2023-06-08 04:04:02 -04:00
|
|
|
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 == "" {
|
2023-06-10 00:07:51 -04:00
|
|
|
response.Message = fmt.Sprintf("Impossible d'insérer une transaction sans membre_id (numéro étudiant)")
|
2023-06-08 04:04:02 -04:00
|
|
|
statusCode = http.StatusBadRequest
|
|
|
|
|
|
|
|
return c.JSON(statusCode, response)
|
|
|
|
}
|
|
|
|
membre, err := bottinApiClient.GetMembre(transaction.MembreID)
|
|
|
|
if err != nil {
|
2023-06-10 00:07:51 -04:00
|
|
|
response.Message = fmt.Sprintf("Erreur pendant bottinApiClient.GetMembre(): %s", err)
|
2023-06-08 04:04:02 -04:00
|
|
|
|
|
|
|
return c.JSON(statusCode, response)
|
|
|
|
}
|
|
|
|
|
|
|
|
if membre.ID == "" {
|
2023-06-10 00:07:51 -04:00
|
|
|
response.Message = fmt.Sprintf("Aucun membre avec numéro étudiant '%s' dans le bottin de l'AGECEM", membre.ID)
|
2023-06-08 04:04:02 -04:00
|
|
|
statusCode = http.StatusNotFound
|
|
|
|
|
|
|
|
return c.JSON(statusCode, response)
|
|
|
|
}
|
|
|
|
|
|
|
|
// membre exists, can keep going
|
|
|
|
}
|
|
|
|
|
2023-06-06 02:22:57 -04:00
|
|
|
// Check for already-existing transactions
|
|
|
|
for _, transaction := range transactions {
|
|
|
|
transaction, err := client.GetTransaction(transaction.MembreID, transaction.IsPerpetual)
|
|
|
|
if err != nil {
|
|
|
|
if err.Error() != "sql: no rows in result set" {
|
2023-06-10 00:07:51 -04:00
|
|
|
response.Message = fmt.Sprintf("Erreur pendant client.GetTransaction(): %s", err)
|
2023-06-06 02:22:57 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if transaction.ID != "" {
|
2023-06-10 00:07:51 -04:00
|
|
|
agendaType := "non-perpétuel"
|
2023-06-06 02:22:57 -04:00
|
|
|
if transaction.IsPerpetual {
|
2023-06-10 00:07:51 -04:00
|
|
|
agendaType = "perpétuel"
|
2023-06-06 02:22:57 -04:00
|
|
|
}
|
|
|
|
|
2023-06-10 00:07:51 -04:00
|
|
|
response.Message = fmt.Sprintf("Membre %s a déjà son agenda %s", transaction.MembreID, agendaType)
|
2023-06-06 02:22:57 -04:00
|
|
|
|
|
|
|
statusCode = http.StatusBadRequest
|
|
|
|
|
|
|
|
return c.JSON(statusCode, response)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-08 01:31:34 -04:00
|
|
|
insertedTransactions, err := client.InsertTransactions(transactions)
|
|
|
|
response.Data.Transactions = insertedTransactions
|
2023-06-06 02:22:57 -04:00
|
|
|
if err != nil {
|
2023-06-10 00:07:51 -04:00
|
|
|
response.Message = fmt.Sprintf("Erreur pendant client.InsertTransactions(): %s", err)
|
2023-06-06 02:22:57 -04:00
|
|
|
|
|
|
|
return c.JSON(statusCode, response)
|
|
|
|
}
|
|
|
|
|
|
|
|
statusCode = http.StatusCreated
|
2023-06-10 00:07:51 -04:00
|
|
|
response.Message = "Insertion complétée"
|
2023-06-06 02:22:57 -04:00
|
|
|
|
|
|
|
return c.JSON(statusCode, response)
|
|
|
|
}
|