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

fix(incoherence) : fixed incoherence and added new entrypoint #80

Merged
merged 13 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions backend/api/items.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,3 +299,65 @@ func (s *Server) GetAllItems(c echo.Context, params autogen.GetAllItemsParams) e

return nil
}

// (GET /items/incoherent)
func (s *Server) GetAllIncoherentItems(c echo.Context, params autogen.GetAllIncoherentItemsParams) error {
// Get account from cookie
account, err := MustGetAdmin(c)
if err != nil {
return nil
}

state := ""
categoryId := ""
name := ""
if params.State != nil {
state = string(*params.State)
}
if params.CategoryId != nil {
categoryId = params.CategoryId.String()
}
if params.Name != nil {
name = string(*params.Name)
}

count, err := s.DBackend.CountIncoherentItems(c.Request().Context(), categoryId, state, name)
if err != nil {
logrus.Error(err)
return Error500(c)
}

// Make sure the last page is not empty
dbpage, page, limit, maxPage := autogen.Pager(params.Page, params.Limit, &count)

data, err := s.DBackend.GetIncoherentItems(c.Request().Context(), dbpage, limit, categoryId, state, name)
if err != nil {
logrus.Error(err)
return Error500(c)
}



var items []autogen.Item

for _, item := range data {
rp := item.RealPrice(account.PriceRole)
item.DisplayPrice = &rp

if account.HasPrivileges() {
rp := item.RealPrices()
item.DisplayPrices = &rp
}

items = append(items, item.Item)
}

autogen.GetAllIncoherentItems200JSONResponse{
Items: items,
Page: page,
Limit: limit,
MaxPage: maxPage,
}.VisitGetAllIncoherentItemsResponse(c.Response())

return nil
}
395 changes: 267 additions & 128 deletions backend/autogen/bar.gen.go

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions backend/internal/db/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,9 @@ type DBackend interface {
GetRefills(ctx context.Context, account string, page uint64, size uint64, startAt, endAt uint64) ([]*models.Refill, error)
CountRefills(ctx context.Context, account string, startAt, endAt uint64) (uint64, error)
GetItems(ctx context.Context, categoryID string, page, size uint64, state string, name string, fournisseur string) ([]*models.Item, error)
GetIncoherentItems(ctx context.Context, page, size uint64, categoryID string, state string, name string) ([]*models.Item, error)
CountItems(ctx context.Context, categoryID string, state string, name string, fournisseur string) (uint64, error)
CountIncoherentItems(ctx context.Context, categoryID string, state string, name string) (uint64, error)

GetAllRefills(ctx context.Context, page uint64, size uint64, startAt, endAt uint64) ([]*models.Refill, error)
CountAllRefills(ctx context.Context, startAt, endAt uint64) (uint64, error)
Expand Down
179 changes: 179 additions & 0 deletions backend/internal/db/mongo/item_misc.go
aripot007 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,99 @@ func (b *Backend) GetItems(ctx context.Context, categoryID string, page, size ui
return items, nil
}

func (b *Backend) GetIncoherentItems(ctx context.Context, page, size uint64, categoryID string, state string, name string) ([]*models.Item, error) {
ctx, cancel := b.TimeoutContext(ctx)
defer cancel()

var items []*models.Item

filter := bson.M{
"$and": []bson.M{
{
"deleted_at": nil,
},
{
"$or": []bson.M{
{
"$and": []bson.M{
{
"amount_left": bson.M{
"$gt": 0,
},
},
{
"state": bson.M{
"$ne": "buyable",
},
},
},
},
{"prices.ceten": 0},
{"prices.coutant": 0},
{"prices.externe": 0},
{"prices.menu": 0},
{"prices.privilegies": 0},
{"prices.staff_bar": 0},
},
},
},
}
if state != "" {
filter["state"] = state
if state == string(autogen.ItemBuyable) {
// Get seconds since day start
t := time.Since(time.Now().Truncate(24 * time.Hour)).Seconds()
// available_from <= t <= available_until or (available_from == nil && available_until == nil)
filter["$and"] = []bson.M{
{
"$or": []bson.M{
{
"available_from": bson.M{
"$lte": t,
},
},
{
"available_from": nil,
},
},
},
{
"$or": []bson.M{
{
"available_until": bson.M{
"$gte": t,
},
},
{
"available_until": nil,
},
},
},
}
}
}
if categoryID != "" {
filter["category_id"] = uuid.MustParse(categoryID)
}
if name != "" {
filter["name"] = bson.M{
"$regex": name,
"$options": "i",
}
}

cursor, err := b.db.Collection(ItemsCollection).Find(ctx, filter, options.Find().SetSkip(int64(page*size)).SetLimit(int64(size)))
if err != nil {
return nil, err
}

if err := cursor.All(ctx, &items); err != nil {
return nil, err
}

return items, nil
}

func (b *Backend) CountItems(ctx context.Context, categoryID string, state string, name string, fournisseur string) (uint64, error) {
ctx, cancel := b.TimeoutContext(ctx)
defer cancel()
Expand Down Expand Up @@ -156,3 +249,89 @@ func (b *Backend) CountItems(ctx context.Context, categoryID string, state strin

return uint64(count), nil
}

func (b *Backend) CountIncoherentItems(ctx context.Context, categoryID string, state string, name string) (uint64, error) {
ctx, cancel := b.TimeoutContext(ctx)
defer cancel()

filter := bson.M{
"$and": []bson.M{
{
"deleted_at": nil,
},
{
"$or": []bson.M{
{
"$and": []bson.M{
{
"amount_left": bson.M{
"$gt": 0,
},
},
{
"state": bson.M{
"$ne": "buyable",
},
},
},
},
{"prices.ceten": 0},
{"prices.coutant": 0},
{"prices.externe": 0},
{"prices.menu": 0},
{"prices.privilegies": 0},
{"prices.staff_bar": 0},
},
},
},
}

if state != "" {
filter["state"] = state
if state == string(autogen.ItemBuyable) {
t := time.Since(time.Now().Truncate(24 * time.Hour)).Seconds()
filter["$and"] = []bson.M{
{
"$or": []bson.M{
{
"available_from": bson.M{
"$lte": t,
},
},
{
"available_from": nil,
},
},
},
{
"$or": []bson.M{
{
"available_until": bson.M{
"$gte": t,
},
},
{
"available_until": nil,
},
},
},
}
}
}
if categoryID != "" {
filter["category_id"] = uuid.MustParse(categoryID)
}
if name != "" {
filter["name"] = bson.M{
"$regex": name,
"$options": "i",
}
}

count, err := b.db.Collection(ItemsCollection).CountDocuments(ctx, filter)
if err != nil {
return 0, err
}

return uint64(count), nil
}
80 changes: 80 additions & 0 deletions bar.openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2442,6 +2442,86 @@ paths:
- admin_auth: []
tags:
- items
/items/incoherent:
get:
description: (admin) Get all incoherent items with filters and pagination
operationId: getAllIncoherentItems
parameters:
- name: page
in: query
description: Page number
required: false
schema:
type: integer
format: uint64
- name: limit
in: query
description: Number of items per page
required: false
schema:
type: integer
format: uint64
- name: state
in: query
description: Filter by state
required: false
schema:
type: string
$ref: "#/components/schemas/ItemState"
- name: category_id
in: query
description: Filter by category
required: false
schema:
$ref: "#/components/schemas/UUID"
- name: name
in: query
description: Filter by name
required: false
schema:
type: string
responses:
"200":
description: ""
content:
application/json:
schema:
type: object
properties:
items:
type: array
items:
$ref: "#/components/schemas/Item"
page:
type: integer
format: uint64
limit:
type: integer
format: uint64
max_page:
type: integer
format: uint64
required:
- items
- page
- limit
- max_page
"403":
description: "Forbidden"
content:
application/json:
schema:
$ref: "#/components/schemas/HTTPError"
"500":
description: "Internal server error"
content:
application/json:
schema:
$ref: "#/components/schemas/HTTPError"
security:
- admin_auth: []
tags:
- items
/categories/{category_id}/items:
get:
description: Get all items of a category
Expand Down
Loading
Loading