Skip to content
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
32 changes: 27 additions & 5 deletions backend/api/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,24 +142,46 @@ func (s *Server) GetTransactions(c echo.Context, params autogen.GetTransactionsP
return nil
}

var state string
if params.State != nil {
state = string(*params.State)
var TransactionState string
if params.TransactionState != nil {
TransactionState = string(*params.TransactionState)
}

var hide_canceled bool
if params.HideCanceled != nil {
hide_canceled = bool(*params.HideCanceled)
} else {
hide_canceled = true
}

var name string
if params.Name != nil {
name = string(*params.Name)
}

var StartTime int
if params.StartTime != nil {
StartTime = int(*params.StartTime)
}

var EndTime int
if params.EndTime != nil {
EndTime = int(*params.EndTime)
}

var ItemId string
if params.ItemId != nil {
ItemId = string(*params.ItemId)
}

var hide_remotes bool
if params.HideRemote != nil {
hide_remotes = bool(*params.HideRemote)
} else {
hide_remotes = true
}

count, err := s.DBackend.CountAllTransactions(c.Request().Context(), state, name, hide_remotes)
count, err := s.DBackend.CountAllTransactions(c.Request().Context(), TransactionState, hide_canceled, name, hide_remotes, StartTime, EndTime, ItemId)
if err != nil {
logrus.Error(err)
return Error500(c)
Expand All @@ -168,7 +190,7 @@ func (s *Server) GetTransactions(c echo.Context, params autogen.GetTransactionsP
// Make sure the last page is not empty
dbpage, page, limit, maxPage := autogen.Pager(params.Page, params.Limit, &count)

data, err := s.DBackend.GetAllTransactions(c.Request().Context(), dbpage, limit, state, name, hide_remotes)
data, err := s.DBackend.GetAllTransactions(c.Request().Context(), dbpage, limit, TransactionState, hide_canceled, name, hide_remotes, StartTime, EndTime, ItemId)
if err != nil {
logrus.Error(err)
return Error500(c)
Expand Down
189 changes: 115 additions & 74 deletions backend/autogen/bar.gen.go
Comment thread
aripot007 marked this conversation as resolved.

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions backend/internal/db/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ type DBackend interface {
GetTransactions(ctx context.Context, account string, page uint64, size uint64, state string) ([]*models.Transaction, error)
CountTransactions(ctx context.Context, account string, state string) (uint64, error)

GetAllTransactions(ctx context.Context, page uint64, size uint64, state string, name string, hide_remotes bool) ([]*models.Transaction, error)
CountAllTransactions(ctx context.Context, state string, name string, hide_remotes bool) (uint64, error)
GetAllTransactions(ctx context.Context, page uint64, size uint64, TransactionState string, hide_canceled bool, name string, hide_remotes bool, StartTime int, EndTime int, ItemId string) ([]*models.Transaction, error)
CountAllTransactions(ctx context.Context, TransactionState string, hide_canceled bool, name string, hide_remotes bool, StartTime int, EndTime int, ItemId string) (uint64, error)
GetAllActiveTransactionsItems(ctx context.Context, name string) ([]autogen.TransactionItem, error)

// Restock's CRUD
Expand Down
1 change: 1 addition & 0 deletions backend/internal/db/mongo/transaction_crud.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,4 @@ func (b *Backend) CountDeletedTransactions(ctx context.Context) (uint64, error)

return uint64(count), nil
}

150 changes: 64 additions & 86 deletions backend/internal/db/mongo/transaction_misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"bar/internal/models"
"context"

"github.com/google/uuid"

"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo/options"
)
Expand Down Expand Up @@ -56,65 +58,53 @@ func (b *Backend) CountTransactions(ctx context.Context, accountID string, state
return uint64(count), nil
}

func (b *Backend) GetAllTransactions(ctx context.Context, page uint64, size uint64, state string, name string, hide_remotes bool) ([]*models.Transaction, error) {
func (b *Backend) GetAllTransactions(ctx context.Context, page uint64, size uint64, TransactionState string, hide_canceled bool, name string, hide_remotes bool, StartTime int, EndTime int, ItemID string) ([]*models.Transaction, error) {
ctx, cancel := b.TimeoutContext(ctx)
defer cancel()

filter := bson.M{}

if state != "" {
filter["state"] = state
}
if TransactionState != "" {
filter["state"] = TransactionState
}

var namesFilter []bson.M

if name != "" {
namesFilter = []bson.M{
{
"account_name": bson.M{
"$regex": name,
"$options": "i",
},
},
{
"account_nick_name": bson.M{
"$regex": name,
"$options": "i",
},
},
filter["account_name"] = bson.M{
"$regex": name,
"$options": "i",
}
}

if hide_remotes {
remotesFilter := []bson.M{
{
"is_remote": bson.M{
"$exists": false,
},
},
{
"is_remote": false,
},
{
"is_remote": nil,
},
filter["$or"] = []bson.M{
{"is_remote": bson.M{"$ne": true}},
{"is_remote": nil},
}
if namesFilter != nil {
filter["$and"] = []bson.M{
{
"$or": namesFilter,
},
{
"$or": remotesFilter,
},
}
} else {
filter["$or"] = remotesFilter
}

if StartTime > 0 || EndTime > 0 {
timeFilter := bson.M{}
if StartTime > 0 {
timeFilter["$gte"] = StartTime
}
if EndTime > 0 {
timeFilter["$lte"] = EndTime
}
filter["created_at"] = timeFilter
}

} else if namesFilter != nil {
filter["$or"] = namesFilter
var itemsElemMatch bson.M = bson.M{}
if ItemID != "" {
itemsElemMatch["item_id"] = uuid.MustParse(ItemID)
}
if hide_canceled {
itemsElemMatch["state"] = bson.M{"$ne": autogen.TransactionItemCanceled}
}
if len(itemsElemMatch) > 0 {
filter["items"] = bson.M{"$elemMatch": itemsElemMatch}
}


// Get "size" transactions from "page" using aggregation
var transactions []*models.Transaction
Expand All @@ -131,64 +121,52 @@ func (b *Backend) GetAllTransactions(ctx context.Context, page uint64, size uint
return transactions, nil
}

func (b *Backend) CountAllTransactions(ctx context.Context, state string, name string, hide_remotes bool) (uint64, error) {

func (b *Backend) CountAllTransactions(ctx context.Context, TransactionState string, hide_canceled bool, name string, hide_remotes bool, StartTime int, EndTime int, ItemID string) (uint64, error) {
ctx, cancel := b.TimeoutContext(ctx)
defer cancel()

filter := bson.M{}

if state != "" {
filter["state"] = state
}
if TransactionState != "" {
filter["state"] = TransactionState
}

var namesFilter []bson.M

if name != "" {
namesFilter = []bson.M{
{
"account_name": bson.M{
"$regex": name,
"$options": "i",
},
},
{
"account_nick_name": bson.M{
"$regex": name,
"$options": "i",
},
},
filter["account_name"] = bson.M{
"$regex": name,
"$options": "i",
}
}

if hide_remotes {
remotesFilter := []bson.M{
{
"is_remote": bson.M{
"$exists": false,
},
},
{
"is_remote": false,
},
{
"is_remote": nil,
},
filter["$or"] = []bson.M{
{"is_remote": bson.M{"$ne": true}},
{"is_remote": nil},
}
if namesFilter != nil {
filter["$and"] = []bson.M{
{
"$or": namesFilter,
},
{
"$or": remotesFilter,
},
}
} else {
filter["$or"] = remotesFilter
}

if StartTime > 0 || EndTime > 0 {
timeFilter := bson.M{}
if StartTime > 0 {
timeFilter["$gte"] = StartTime
}
if EndTime > 0 {
timeFilter["$lte"] = EndTime
}
filter["created_at"] = timeFilter
}

} else if namesFilter != nil {
filter["$or"] = namesFilter
var itemsElemMatch bson.M = bson.M{}
if ItemID != "" {
itemsElemMatch["item_id"] = uuid.MustParse(ItemID)
}
if hide_canceled {
itemsElemMatch["state"] = bson.M{"$ne": autogen.TransactionItemCanceled}
}
if len(itemsElemMatch) > 0 {
filter["items"] = bson.M{"$elemMatch": itemsElemMatch}
}

count, err := b.db.Collection(TransactionsCollection).CountDocuments(ctx, filter)
Expand Down
28 changes: 26 additions & 2 deletions bar.openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1145,13 +1145,19 @@ paths:
schema:
type: integer
format: uint64
- name: state
- name: transaction_state
in: query
description: Filter by state
description: Filter transaction by state
required: false
schema:
type: string
$ref: "#/components/schemas/TransactionState"
- name: hide_canceled
in: query
description: Hide canceled items
required: false
schema:
type: boolean
- name: hide_remote
in: query
description: Hide remote transactions
Expand All @@ -1164,6 +1170,24 @@ paths:
required: false
schema:
type: string
- name: start_time
in: query
description: Filter by start_time
required: false
schema:
type: integer
- name: end_time
in: query
description: Filter by end_time
required: false
schema:
type: integer
- name: item_id
in: query
description: Filter by item
required: false
schema:
type: string
responses:
"200":
description: ""
Expand Down
2 changes: 1 addition & 1 deletion frontend/openapitools.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
"$schema": "./node_modules/@openapitools/openapi-generator-cli/config.schema.json",
"spaces": 2,
"generator-cli": {
"version": "6.6.0"
"version": "7.16.0"
}
}
Loading