Skip to content

Commit

Permalink
Merge pull request #132 from CETEN-OpenBar/StateRestock
Browse files Browse the repository at this point in the history
Feature: Amélioration des réappros et création automatique avec les courses générées
  • Loading branch information
BaptTF authored Feb 6, 2025
2 parents 5e8a326 + 5a52917 commit 431bf0c
Show file tree
Hide file tree
Showing 17 changed files with 1,791 additions and 905 deletions.
146 changes: 46 additions & 100 deletions backend/api/restock.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bar/autogen"
"bar/internal/models"
"bar/internal/webhook"
"math"
"time"

"github.com/google/uuid"
Expand All @@ -24,7 +23,7 @@ func (s *Server) GetRestocks(c echo.Context, params autogen.GetRestocksParams) e
return nil
}

count, err := s.DBackend.CountAllRestocks(c.Request().Context())
count, err := s.DBackend.CountAllRestocks(c.Request().Context(), params.State)
if err != nil {
logrus.Error(err)
return Error500(c)
Expand All @@ -33,7 +32,7 @@ func (s *Server) GetRestocks(c echo.Context, params autogen.GetRestocksParams) e
// Make sure the last page is not empty
dbpage, page, limit, maxPage := autogen.Pager(params.Page, params.Limit, &count)

data, err := s.DBackend.GetAllRestocks(c.Request().Context(), dbpage, limit)
data, err := s.DBackend.GetAllRestocks(c.Request().Context(), dbpage, limit, params.State)
if err != nil {
logrus.Error(err)
return Error500(c)
Expand Down Expand Up @@ -73,9 +72,11 @@ func (s *Server) CreateRestock(c echo.Context) error {
TotalCostHt: body.TotalCostHt,
TotalCostTtc: body.TotalCostTtc,
Type: body.Type,
State: body.State,
CreatedAt: uint64(time.Now().Unix()),
CreatedBy: usr.Id,
CreatedByName: usr.Name(),
Items: []autogen.RestockItem{},
},
}

Expand All @@ -99,106 +100,49 @@ func (s *Server) CreateRestock(c echo.Context) error {
logrus.Error(err)
return nil, Error500(c)
}
category, err := s.DBackend.GetCategory(c.Request().Context(), item.CategoryId.String())
if err != nil {
if err == mongo.ErrNoDocuments {
// return nil, ErrorItemNotFound(c)
// Si l'item n'as pas de catégorie, on met une catégorie par défaut
category = &models.Category{
Category: autogen.Category{
Id: uuid.Nil,
Name: "Autres",
SpecialPrice: false,
},
}
} else {
logrus.Error(err)
return nil, Error500(c)
}
}
restockItem.ItemName = item.Name
restockItem.ItemPictureUri = item.PictureUri
item.State = autogen.ItemBuyable
item.AmountLeft += restockItem.AmountOfBundle * restockItem.AmountPerBundle
item.LastTva = &restockItem.Tva
if category.SpecialPrice == false {
item.Prices.Coutant = uint64(math.Ceil(float64(restockItem.BundleCostTtc) / (float64(restockItem.AmountPerBundle))))
if item.Prices.Coutant < 30 {
item.Prices.Externe = arrondiAuMutilple(item.Prices.Coutant, 5) + 20
item.Prices.Ceten = arrondiAuMutilple(item.Prices.Coutant, 5) + 10
item.Prices.StaffBar = arrondiAuMutilple(item.Prices.Coutant, 5) + 5
item.Prices.Privilegies = arrondiAuMutilple(item.Prices.Coutant, 5) + 5
item.Prices.Menu = arrondiAuMutilple(item.Prices.Coutant, 5) + 10
} else if item.Prices.Coutant >= 30 && item.Prices.Coutant < 130 {
item.Prices.Externe = arrondiAuMutilple(item.Prices.Coutant*3/2, 5)
item.Prices.Ceten = arrondiAuMutilple(item.Prices.Coutant*113/100, 5)
item.Prices.StaffBar = arrondiAuMutilple(item.Prices.Coutant*108/100, 5)
item.Prices.Privilegies = arrondiAuMutilple(item.Prices.Coutant*11/10, 5)
item.Prices.Menu = arrondiAuMutilple(item.Prices.Coutant*13/10, 5)
} else if item.Prices.Coutant >= 130 && item.Prices.Coutant < 300 {
item.Prices.Externe = arrondiAuMutilple(item.Prices.Coutant*14/10, 5)
item.Prices.Ceten = arrondiAuMutilple(item.Prices.Coutant*11/10, 5)
item.Prices.StaffBar = arrondiAuMutilple(item.Prices.Coutant*108/100, 5)
item.Prices.Privilegies = arrondiAuMutilple(item.Prices.Coutant*11/10, 5)
item.Prices.Menu = arrondiAuMutilple(item.Prices.Coutant*12/10, 5)
} else if item.Prices.Coutant >= 300 {
item.Prices.Externe = arrondiAuMutilple(item.Prices.Coutant*125/100, 5)
item.Prices.Ceten = arrondiAuMutilple(item.Prices.Coutant*11/10, 5)
item.Prices.StaffBar = arrondiAuMutilple(item.Prices.Coutant*105/100, 5)
item.Prices.Privilegies = arrondiAuMutilple(item.Prices.Coutant*11/10, 5)
item.Prices.Menu = arrondiAuMutilple(item.Prices.Coutant*1125/1000, 5)
if restock.State == autogen.RestockFinished {
category, err := s.DBackend.GetCategory(c.Request().Context(), item.CategoryId.String())
if err != nil {
if err == mongo.ErrNoDocuments {
// return nil, ErrorItemNotFound(c)
// Si l'item n'as pas de catégorie, on met une catégorie par défaut
category = &models.Category{
Category: autogen.Category{
Id: uuid.Nil,
Name: "Autres",
SpecialPrice: false,
},
}
} else {
logrus.Error(err)
return nil, Error500(c)
}
}
} else {
item.Prices.Coutant = uint64(math.Ceil(float64(restockItem.BundleCostTtc) / (float64(restockItem.AmountPerBundle))))
if item.Prices.Coutant < 30 {
item.Prices.Externe = arrondiAuMutilple(item.Prices.Coutant, 5) + 20
item.Prices.Ceten = arrondiAuMutilple(item.Prices.Coutant, 5) + 10
item.Prices.StaffBar = item.Prices.Ceten
item.Prices.Privilegies = item.Prices.Ceten
item.Prices.Menu = item.Prices.Ceten
} else if item.Prices.Coutant >= 30 && item.Prices.Coutant < 130 {
item.Prices.Externe = arrondiAuMutilple(item.Prices.Coutant*3/2, 5)
item.Prices.Ceten = arrondiAuMutilple(item.Prices.Coutant*113/100, 5)
item.Prices.StaffBar = item.Prices.Ceten
item.Prices.Privilegies = item.Prices.Ceten
item.Prices.Menu = item.Prices.Ceten
} else if item.Prices.Coutant >= 130 && item.Prices.Coutant < 300 {
item.Prices.Externe = arrondiAuMutilple(item.Prices.Coutant*14/10, 5)
item.Prices.Ceten = arrondiAuMutilple(item.Prices.Coutant*11/10, 5)
item.Prices.StaffBar = item.Prices.Ceten
item.Prices.Privilegies = item.Prices.Ceten
item.Prices.Menu = item.Prices.Ceten
} else if item.Prices.Coutant >= 300 {
item.Prices.Externe = arrondiAuMutilple(item.Prices.Coutant*125/100, 5)
item.Prices.Ceten = arrondiAuMutilple(item.Prices.Coutant*11/10, 5)
item.Prices.StaffBar = item.Prices.Ceten
item.Prices.Privilegies = item.Prices.Ceten
item.Prices.Menu = item.Prices.Ceten
item = UpdateItem(item, category, restockItem)
err = s.DBackend.UpdateItem(c.Request().Context(), item)
if err != nil {
logrus.Error(err)
return nil, Error500(c)
}
item.Prices.Coutant = item.Prices.Ceten
}
err = s.DBackend.UpdateItem(c.Request().Context(), item)
if err != nil {
logrus.Error(err)
return nil, Error500(c)
}

restock.Items = append(restock.Items, restockItem)
}

err = s.DBackend.CreateRestock(c.Request().Context(), &restock)
if restock.Type == autogen.RestockViennoiserie {
if err != nil {
logrus.Error(err)
return nil, Error500(c)
}
if restock.Type == autogen.RestockViennoiserie && restock.State == autogen.RestockFinished {
err := webhook.SendDiscordWebhook(restock)
if err != nil {
logrus.Errorf("Error sending webhook: %v\n", err)
}
}

if err != nil {
logrus.Error(err)
return nil, Error500(c)
}

return nil, nil
})
if err != nil {
Expand Down Expand Up @@ -232,22 +176,24 @@ func (s *Server) DeleteRestock(c echo.Context, restockId autogen.UUID) error {
return nil, Error500(c)
}

for _, item := range restock.Items {
i, err := s.DBackend.GetItem(c.Request().Context(), item.ItemId.String())
if err != nil {
if err == mongo.ErrNoDocuments {
return nil, ErrorItemNotFound(c)
if restock.State == autogen.RestockFinished {
for _, item := range restock.Items {
i, err := s.DBackend.GetItem(c.Request().Context(), item.ItemId.String())
if err != nil {
if err == mongo.ErrNoDocuments {
return nil, ErrorItemNotFound(c)
}
logrus.Error(err)
return nil, Error500(c)
}
logrus.Error(err)
return nil, Error500(c)
}

i.AmountLeft -= item.AmountOfBundle * item.AmountPerBundle
i.AmountLeft -= item.AmountOfBundle * item.AmountPerBundle

err = s.DBackend.UpdateItem(c.Request().Context(), i)
if err != nil {
logrus.Error(err)
return nil, Error500(c)
err = s.DBackend.UpdateItem(c.Request().Context(), i)
if err != nil {
logrus.Error(err)
return nil, Error500(c)
}
}
}

Expand Down
64 changes: 64 additions & 0 deletions backend/api/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bar/autogen"
"bar/internal/models"
"errors"
"math"

"github.com/labstack/echo/v4"
)
Expand Down Expand Up @@ -103,3 +104,66 @@ func MustGetAdmin(c echo.Context) (*models.Account, error) {

return account, nil
}

func UpdateItem(item *models.Item, category *models.Category, restockItem autogen.RestockItem) *models.Item {
item.State = autogen.ItemBuyable
item.AmountLeft += restockItem.AmountOfBundle * restockItem.AmountPerBundle
item.LastTva = &restockItem.Tva
if !category.SpecialPrice {
item.Prices.Coutant = uint64(math.Ceil(float64(restockItem.BundleCostTtc) / (float64(restockItem.AmountPerBundle))))
if item.Prices.Coutant < 30 {
item.Prices.Externe = arrondiAuMutilple(item.Prices.Coutant, 5) + 20
item.Prices.Ceten = arrondiAuMutilple(item.Prices.Coutant, 5) + 10
item.Prices.StaffBar = arrondiAuMutilple(item.Prices.Coutant, 5) + 5
item.Prices.Privilegies = arrondiAuMutilple(item.Prices.Coutant, 5) + 5
item.Prices.Menu = arrondiAuMutilple(item.Prices.Coutant, 5) + 10
} else if item.Prices.Coutant >= 30 && item.Prices.Coutant < 130 {
item.Prices.Externe = arrondiAuMutilple(item.Prices.Coutant*3/2, 5)
item.Prices.Ceten = arrondiAuMutilple(item.Prices.Coutant*113/100, 5)
item.Prices.StaffBar = arrondiAuMutilple(item.Prices.Coutant*108/100, 5)
item.Prices.Privilegies = arrondiAuMutilple(item.Prices.Coutant*11/10, 5)
item.Prices.Menu = arrondiAuMutilple(item.Prices.Coutant*13/10, 5)
} else if item.Prices.Coutant >= 130 && item.Prices.Coutant < 300 {
item.Prices.Externe = arrondiAuMutilple(item.Prices.Coutant*14/10, 5)
item.Prices.Ceten = arrondiAuMutilple(item.Prices.Coutant*11/10, 5)
item.Prices.StaffBar = arrondiAuMutilple(item.Prices.Coutant*108/100, 5)
item.Prices.Privilegies = arrondiAuMutilple(item.Prices.Coutant*11/10, 5)
item.Prices.Menu = arrondiAuMutilple(item.Prices.Coutant*12/10, 5)
} else if item.Prices.Coutant >= 300 {
item.Prices.Externe = arrondiAuMutilple(item.Prices.Coutant*125/100, 5)
item.Prices.Ceten = arrondiAuMutilple(item.Prices.Coutant*11/10, 5)
item.Prices.StaffBar = arrondiAuMutilple(item.Prices.Coutant*105/100, 5)
item.Prices.Privilegies = arrondiAuMutilple(item.Prices.Coutant*11/10, 5)
item.Prices.Menu = arrondiAuMutilple(item.Prices.Coutant*1125/1000, 5)
}
} else {
item.Prices.Coutant = uint64(math.Ceil(float64(restockItem.BundleCostTtc) / (float64(restockItem.AmountPerBundle))))
if item.Prices.Coutant < 30 {
item.Prices.Externe = arrondiAuMutilple(item.Prices.Coutant, 5) + 20
item.Prices.Ceten = arrondiAuMutilple(item.Prices.Coutant, 5) + 10
item.Prices.StaffBar = item.Prices.Ceten
item.Prices.Privilegies = item.Prices.Ceten
item.Prices.Menu = item.Prices.Ceten
} else if item.Prices.Coutant >= 30 && item.Prices.Coutant < 130 {
item.Prices.Externe = arrondiAuMutilple(item.Prices.Coutant*3/2, 5)
item.Prices.Ceten = arrondiAuMutilple(item.Prices.Coutant*113/100, 5)
item.Prices.StaffBar = item.Prices.Ceten
item.Prices.Privilegies = item.Prices.Ceten
item.Prices.Menu = item.Prices.Ceten
} else if item.Prices.Coutant >= 130 && item.Prices.Coutant < 300 {
item.Prices.Externe = arrondiAuMutilple(item.Prices.Coutant*14/10, 5)
item.Prices.Ceten = arrondiAuMutilple(item.Prices.Coutant*11/10, 5)
item.Prices.StaffBar = item.Prices.Ceten
item.Prices.Privilegies = item.Prices.Ceten
item.Prices.Menu = item.Prices.Ceten
} else if item.Prices.Coutant >= 300 {
item.Prices.Externe = arrondiAuMutilple(item.Prices.Coutant*125/100, 5)
item.Prices.Ceten = arrondiAuMutilple(item.Prices.Coutant*11/10, 5)
item.Prices.StaffBar = item.Prices.Ceten
item.Prices.Privilegies = item.Prices.Ceten
item.Prices.Menu = item.Prices.Ceten
}
item.Prices.Coutant = item.Prices.Ceten
}
return item
}
99 changes: 99 additions & 0 deletions backend/api/validerRestock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package api

import (
"bar/autogen"
"bar/internal/webhook"
"time"

"github.com/labstack/echo/v4"
"github.com/sirupsen/logrus"
)

// PATCH /restocks/{restock_id}
func (s *Server) UpdateRestock(c echo.Context, restockId autogen.UUID) error {
usr, err := MustGetAdmin(c)
if err != nil {
return nil
}

var body autogen.UpdateRestockJSONRequestBody
if err := c.Bind(&body); err != nil {
logrus.Error(err)
return Error400(c)
}

restock, err := s.DBackend.GetRestock(c.Request().Context(), restockId.String())
if err != nil {
logrus.Error(err)
return Error500(c)
}
if restock.State == autogen.RestockFinished {
return Error400(c)
}

restock.Items = []autogen.RestockItem{}
restock.TotalCostHt = body.TotalCostHt
restock.TotalCostTtc = body.TotalCostTtc
restock.Type = body.Type
restock.CreatedAt = uint64(time.Now().Unix())

oldState := restock.State
if restock.State != autogen.RestockFinished {
restock.State = body.State // Cannot go from finished to anything else
}

for _, newRestockItem := range body.Items {
restockItem := autogen.RestockItem{
AmountOfBundle: newRestockItem.AmountOfBundle,
AmountPerBundle: newRestockItem.AmountPerBundle,
BundleCostHt: newRestockItem.BundleCostHt,
BundleCostTtc: newRestockItem.BundleCostTtc,
ItemId: newRestockItem.ItemId,
Tva: newRestockItem.Tva,
ItemName: newRestockItem.ItemName,
}

item, err := s.DBackend.GetItem(c.Request().Context(), restockItem.ItemId.String())
if err != nil {
logrus.Error(err)
return Error500(c)
}
restockItem.ItemPictureUri = item.PictureUri

restock.Items = append(restock.Items, restockItem)

if oldState != autogen.RestockFinished && body.State == autogen.RestockFinished {

category, err := s.DBackend.GetCategory(c.Request().Context(), item.CategoryId.String())
if err != nil {
logrus.Error(err)
return Error500(c)
}
item = UpdateItem(item, category, restockItem)
err = s.DBackend.UpdateItem(c.Request().Context(), item)
if err != nil {
logrus.Error(err)
return Error500(c)
}
logrus.WithField("restock", restock.Id.String()).WithField("by", usr.Name()).Info("Items updated")
}
}
err = s.DBackend.UpdateRestock(c.Request().Context(), restock)
if err != nil {
logrus.Error(err)
return Error500(c)
}
if oldState != autogen.RestockFinished && body.State == autogen.RestockFinished && restock.Type == autogen.RestockViennoiserie {
err := webhook.SendDiscordWebhook(*restock)
if err != nil {
logrus.Errorf("Error sending webhook: %v\n", err)
}
}

if oldState == body.State {
return nil
}

logrus.WithField("restock", restock.Id.String()).WithField("by", usr.Name()).Info("Patch restock")
return nil
}
Loading

0 comments on commit 431bf0c

Please sign in to comment.