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

required for webapp issue #211

Merged
merged 2 commits into from
Sep 27, 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
6 changes: 6 additions & 0 deletions dao/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,12 @@ func InitialDatabase() {
)
UpdateCollection = Database.Collection("updates").CreateIndex("name", true)
ReceiptFileCollection = Database.Collection(models.ReceiptFileCollection).CreateIndex("deposit_id", false)
Database.Database.CreateView(
context.Background(),
models.TakingDepositView,
models.TakingCollection,
models.TakingPipelineDeposit().Pipe,
)
}

func FixDatabase() {
Expand Down
20 changes: 13 additions & 7 deletions dao/taking.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/Viva-con-Agua/vcago/vmod"
"github.com/Viva-con-Agua/vcapool"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo/options"
)

var (
Expand Down Expand Up @@ -109,6 +108,10 @@ func TakingUpdate(ctx context.Context, i *models.TakingUpdate, token *vcapool.Ac
return
}

type Count struct {
ListSize int64 `bson:"list_size" json:"list_size"`
}

func TakingGet(ctx context.Context, query *models.TakingQuery, token *vcapool.AccessToken) (result []models.Taking, listSize int64, err error) {
if err = models.TakingPermission(token); err != nil {
return
Expand All @@ -124,15 +127,18 @@ func TakingGet(ctx context.Context, query *models.TakingQuery, token *vcapool.Ac
); err != nil {
return
}
opts := options.Count().SetHint("_id_")
if query.FullCount != "true" {
opts.SetSkip(query.Skip).SetLimit(query.Limit)
count := new([]Count)
if err = TakingCollection.Aggregate(
context.Background(),
models.TakingCountPipeline(filter).Pipe,
count,
); err != nil {
return
}
if cursor, cErr := UserViewCollection.Collection.CountDocuments(ctx, filter, opts); cErr != nil {
print(cErr)
if len(*count) == 0 {
listSize = 0
} else {
listSize = cursor
listSize = (*count)[0].ListSize
}
return
}
Expand Down
21 changes: 20 additions & 1 deletion dao/updates.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,15 @@ func UpdateDatabase() {
if !CheckUpdated(ctx, "event_applications") {
UpdateEventApplications(ctx)
InsertUpdate(ctx, "event_applications")
}
}
if !CheckUpdated(ctx, "last_login_date_1") {
UpdateSetLastLoginDate(ctx)
InsertUpdate(ctx, "last_login_date_1")
}
if !CheckUpdated(ctx, "update_deposit_units_1") {
UpdateDepositUnitNorms(ctx)
InsertUpdate(ctx, "update_deposit_units_1")
}
}

func UpdateCrewMaibox(ctx context.Context) {
Expand Down Expand Up @@ -255,3 +259,18 @@ func UpdateSetLastLoginDate(ctx context.Context) {
log.Print(err)
}
}

func UpdateDepositUnitNorms(ctx context.Context) {
filterDonation := vmdb.NewFilter()
filterDonation.EqualStringList("value", []string{"unknown", "can", "box", "gl", "other"})
filterEco := vmdb.NewFilter()
filterEco.EqualStringList("value", []string{"merch", "other_ec"})
updateDonation := bson.D{{Key: "$set", Value: bson.D{{Key: "norms", Value: "donation"}}}}
updateEco := bson.D{{Key: "$set", Value: bson.D{{Key: "norms", Value: "economic"}}}}
if err := SourceCollection.UpdateMany(ctx, filterDonation.Bson(), updateDonation); err != nil {
log.Print(err)
}
if err := SourceCollection.UpdateMany(ctx, filterEco.Bson(), updateEco); err != nil {
log.Print(err)
}
}
18 changes: 9 additions & 9 deletions models/deposit.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@ type (
Money vmod.Money `json:"money" bson:"money"`
}
DepositUnit struct {
ID string `json:"id" bson:"_id"`
TakingID string `json:"taking_id" bson:"taking_id"`
Taking TakingDatabase `json:"taking" bson:"taking"`
Money vmod.Money `json:"money" bson:"money"`
DepositID string `json:"deposit_id" bson:"deposit_id"`
Status string `json:"status" bson:"status"`
Modified vmod.Modified `json:"modified" bson:"modified"`
ID string `json:"id" bson:"_id"`
TakingID string `json:"taking_id" bson:"taking_id"`
Taking Taking `json:"taking" bson:"taking"`
Money vmod.Money `json:"money" bson:"money"`
DepositID string `json:"deposit_id" bson:"deposit_id"`
Status string `json:"status" bson:"status"`
Modified vmod.Modified `json:"modified" bson:"modified"`
}
DepositUnitTaking struct {
ID string `json:"id" bson:"_id"`
TakingID string `json:"taking_id" bson:"taking_id"`
Taking TakingDatabase `json:"taking" bson:"taking"`
Taking Taking `json:"taking" bson:"taking"`
Money vmod.Money `json:"money" bson:"money"`
DepositID string `json:"deposit_id" bson:"deposit_id"`
Deposit DepositDatabase `json:"deposit" bson:"deposit"`
Expand Down Expand Up @@ -128,7 +128,7 @@ func (i *DepositParam) DepositSyncPermission(token *vcapool.AccessToken) (err er
func DepositPipeline() *vmdb.Pipeline {
pipe := vmdb.NewPipeline()
pipe.LookupUnwind(DepositUnitCollection, "_id", "deposit_id", "deposit_units")
pipe.LookupUnwind(TakingCollection, "deposit_units.taking_id", "_id", "deposit_units.taking")
pipe.LookupUnwind(TakingDepositView, "deposit_units.taking_id", "_id", "deposit_units.taking")
pipe.Append(bson.D{
{Key: "$group", Value: bson.D{
{Key: "_id", Value: "$_id"}, {Key: "deposit_units", Value: bson.D{
Expand Down
22 changes: 22 additions & 0 deletions models/taking.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ type (
)

var TakingCollection = "takings"
var TakingDepositView = "taking_deposit_view"

func TakingPermission(token *vcapool.AccessToken) (err error) {
if !(token.Roles.Validate("admin;employee") || token.PoolRoles.Validate("finance")) {
Expand Down Expand Up @@ -137,12 +138,33 @@ func TakingPipeline() *vmdb.Pipeline {
return pipe
}

func TakingCountPipeline(filter bson.D) *vmdb.Pipeline {
pipe := TakingPipeline()
pipe.Match(filter)
pipe.Append(bson.D{
{Key: "$group", Value: bson.D{
{Key: "_id", Value: nil}, {Key: "list_size", Value: bson.D{
{Key: "$sum", Value: 1},
}},
}},
})
pipe.Append(bson.D{{Key: "$project", Value: bson.D{{Key: "_id", Value: 0}}}})
return pipe
}

func TakingPipelineTicker() *vmdb.Pipeline {
pipe := vmdb.NewPipeline()
pipe.LookupUnwind(EventCollection, "_id", "taking_id", "event")
return pipe
}

func TakingPipelineDeposit() *vmdb.Pipeline {
pipe := vmdb.NewPipeline()
pipe.LookupUnwind(EventCollection, "_id", "taking_id", "event")
pipe.Lookup(SourceCollection, "_id", "taking_id", "sources")
return pipe
}

func (i *TakingCreate) TakingDatabase() *TakingDatabase {
currency := "EUR"
if len(i.NewSource) > 0 {
Expand Down
2 changes: 1 addition & 1 deletion models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ func NewUserUpdate(user *vmod.User) *UserUpdate {

func UserPipeline(user bool) (pipe *vmdb.Pipeline) {
pipe = vmdb.NewPipeline()
if user == true {
if user {
pipe.LookupUnwind(AddressesCollection, "_id", "user_id", "address")
} else {
pipe.LookupUnwind(AddressesCollection, "_id", "user_id", "address_data")
Expand Down
Loading