Skip to content

Commit

Permalink
Merge pull request #217 from Viva-con-Agua/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
deinelieblings authored Nov 22, 2024
2 parents 3ef159b + 0acd065 commit a8af3a2
Show file tree
Hide file tree
Showing 78 changed files with 2,299 additions and 889 deletions.
17 changes: 8 additions & 9 deletions dao/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,18 @@ import (
"pool-backend/models"

"github.com/Viva-con-Agua/vcago/vmdb"
"github.com/Viva-con-Agua/vcapool"
"go.mongodb.org/mongo-driver/bson"
)

func AddressInsert(ctx context.Context, i *models.AddressCreate, token *vcapool.AccessToken) (result *models.Address, err error) {
func AddressInsert(ctx context.Context, i *models.AddressCreate, token *models.AccessToken) (result *models.Address, err error) {
result = i.Address(token.ID)
if err = AddressesCollection.InsertOne(ctx, result); err != nil {
return
}
return
}

func UsersAddressInsert(ctx context.Context, i *models.UsersAddressCreate, token *vcapool.AccessToken) (result *models.Address, err error) {
func UsersAddressInsert(ctx context.Context, i *models.UsersAddressCreate, token *models.AccessToken) (result *models.Address, err error) {
if err = models.AddressPermission(token); err != nil {
return
}
Expand All @@ -28,7 +27,7 @@ func UsersAddressInsert(ctx context.Context, i *models.UsersAddressCreate, token
return
}

func AddressGet(ctx context.Context, i *models.AddressQuery, token *vcapool.AccessToken) (result *[]models.Address, err error) {
func AddressGet(ctx context.Context, i *models.AddressQuery, token *models.AccessToken) (result *[]models.Address, err error) {
filter := i.PermittedFilter(token)
result = new([]models.Address)
if err = AddressesCollection.Find(ctx, filter, result); err != nil {
Expand All @@ -37,15 +36,15 @@ func AddressGet(ctx context.Context, i *models.AddressQuery, token *vcapool.Acce
return
}

func AddressGetByID(ctx context.Context, i *models.AddressParam, token *vcapool.AccessToken) (result *models.Address, err error) {
func AddressGetByID(ctx context.Context, i *models.AddressParam, token *models.AccessToken) (result *models.Address, err error) {
filter := i.PermittedFilter(token)
if err = AddressesCollection.FindOne(ctx, filter, &result); err != nil {
return
}
return
}

func UsersAddressUpdate(ctx context.Context, i *models.AddressUpdate, token *vcapool.AccessToken) (result *models.Address, err error) {
func UsersAddressUpdate(ctx context.Context, i *models.AddressUpdate, token *models.AccessToken) (result *models.Address, err error) {
if err = models.AddressPermission(token); err != nil {

return
Expand All @@ -56,15 +55,15 @@ func UsersAddressUpdate(ctx context.Context, i *models.AddressUpdate, token *vca
return
}

func AddressUpdate(ctx context.Context, i *models.AddressUpdate, token *vcapool.AccessToken) (result *models.Address, err error) {
func AddressUpdate(ctx context.Context, i *models.AddressUpdate, token *models.AccessToken) (result *models.Address, err error) {
filter := i.PermittedFilter(token)
if err = AddressesCollection.UpdateOne(ctx, filter, vmdb.UpdateSet(i), &result); err != nil {
return
}
return
}

func AddressDelete(ctx context.Context, i *models.AddressParam, token *vcapool.AccessToken) (result *models.NVM, err error) {
func AddressDelete(ctx context.Context, i *models.AddressParam, token *models.AccessToken) (result *models.NVM, err error) {
filter := i.PermittedFilter(token)
if err = AddressesCollection.DeleteOne(ctx, filter); err != nil {
return
Expand All @@ -75,7 +74,7 @@ func AddressDelete(ctx context.Context, i *models.AddressParam, token *vcapool.A
return
}

func UsersAddressDelete(ctx context.Context, i *models.AddressParam, token *vcapool.AccessToken) (result *models.NVM, err error) {
func UsersAddressDelete(ctx context.Context, i *models.AddressParam, token *models.AccessToken) (result *models.NVM, err error) {
if err = models.AddressPermission(token); err != nil {
return
}
Expand Down
7 changes: 3 additions & 4 deletions dao/artist.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ import (
"pool-backend/models"

"github.com/Viva-con-Agua/vcago/vmdb"
"github.com/Viva-con-Agua/vcapool"
)

func ArtistInsert(ctx context.Context, i *models.ArtistCreate, token *vcapool.AccessToken) (result *models.Artist, err error) {
func ArtistInsert(ctx context.Context, i *models.ArtistCreate, token *models.AccessToken) (result *models.Artist, err error) {
if err = models.ArtistPermission(token); err != nil {
return
}
Expand Down Expand Up @@ -36,7 +35,7 @@ func ArtistGetByID(ctx context.Context, i *models.ArtistParam) (result *models.A
return
}

func ArtistUpdate(ctx context.Context, i *models.ArtistUpdate, token *vcapool.AccessToken) (result *models.Artist, err error) {
func ArtistUpdate(ctx context.Context, i *models.ArtistUpdate, token *models.AccessToken) (result *models.Artist, err error) {
if err = models.ArtistPermission(token); err != nil {
return
}
Expand All @@ -47,7 +46,7 @@ func ArtistUpdate(ctx context.Context, i *models.ArtistUpdate, token *vcapool.Ac
return
}

func ArtistDelete(ctx context.Context, i *models.ArtistParam, token *vcapool.AccessToken) (err error) {
func ArtistDelete(ctx context.Context, i *models.ArtistParam, token *models.AccessToken) (err error) {
if err = models.ArtistDeletePermission(token); err != nil {
return
}
Expand Down
53 changes: 48 additions & 5 deletions dao/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,14 @@ var (
AvatarCollection *vmdb.Collection

// PoolRoleCollection represents the database collection of the PoolRole Collection.
PoolRoleCollection *vmdb.Collection
PoolRoleHistoryCollection *vmdb.Collection
PoolRoleCollection *vmdb.Collection
PoolRoleHistoryCollection *vmdb.Collection
EventStateHistoryCollection *vmdb.Collection

MailboxCollection *vmdb.Collection
MessageCollection *vmdb.Collection

OrganisationCollection *vmdb.Collection
ArtistCollection *vmdb.Collection
ParticipationCollection *vmdb.Collection
OrganizerCollection *vmdb.Collection
Expand All @@ -63,20 +65,27 @@ var (

ReasonForPaymentCollection *vmdb.Collection
UserViewCollection *vmdb.Collection
EventViewCollection *vmdb.Collection
PublicEventViewCollection *vmdb.Collection

NewsletterCollection *vmdb.Collection

DepositUnitTakingPipe = vmdb.NewPipeline()
ParticipationEventPipe = vmdb.NewPipeline()
ActitityUserPipe = vmdb.NewPipeline()
UserPipe = vmdb.NewPipeline()
EventPipe = vmdb.NewPipeline()
PublicEventPipe = vmdb.NewPipeline()
UpdateCollection *vmdb.Collection
Updates *vmdb.CollectionUpdate
ReceiptFileCollection *vmdb.Collection

TestLogin bool
)

func InitialDatabase() {
Database = vmdb.NewDatabase("pool-backend").Connect()
Database.Database.Collection(models.CrewCollection).Indexes().DropAll(context.Background())

// UserCollection represents the database collection of the User model.
UserCollection = Database.Collection(models.UserCollection).CreateIndex("email", true)
Expand All @@ -94,7 +103,7 @@ func InitialDatabase() {
AddressesCollection = Database.Collection(models.AddressesCollection).CreateIndex("user_id", true)

// CrewsCollection represents the database collection of the Crew model.
CrewsCollection = Database.Collection(models.CrewCollection).CreateIndex("name", true)
CrewsCollection = Database.Collection(models.CrewCollection).CreateIndex("name", true).CreateIndex("organisation_id", false)

// ProfileCollection represents the database collection of the Profile model.
ProfileCollection = Database.Collection(models.ProfileCollection).CreateIndex("user_id", true)
Expand All @@ -105,11 +114,13 @@ func InitialDatabase() {
// PoolRoleCollection represents the database collection of the PoolRole Collection.
PoolRoleCollection = Database.Collection(models.PoolRoleCollection).CreateIndex("user_id", false).CreateMultiIndex(bson.D{{Key: "name", Value: 1}, {Key: "user_id", Value: 1}}, true)
PoolRoleHistoryCollection = Database.Collection(models.PoolRoleHistoryCollection).CreateIndex("user_id", false).CreateIndex("crew_id", false)
EventStateHistoryCollection = Database.Collection(models.EventStateHistoryCollection).CreateIndex("user_id", false).CreateIndex("crew_id", false).CreateIndex("event_id", false).CreateIndex("new_state", false).CreateIndex("date", false)

//
MailboxCollection = Database.Collection(models.MailboxCollection)

MessageCollection = Database.Collection(models.MessageCollection).CreateIndex("user_id", false).CreateIndex("mailbox_id", false)
OrganisationCollection = Database.Collection(models.OrganisationCollection).CreateIndex("name", true).CreateIndex("abbreviation", true)
ArtistCollection = Database.Collection(models.ArtistCollection).CreateIndex("name", true)
ParticipationCollection = Database.Collection(models.ParticipationCollection).CreateIndex("user_id", false).CreateMultiIndex(
bson.D{
Expand All @@ -119,9 +130,9 @@ func InitialDatabase() {
OrganizerCollection = Database.Collection(models.OrganizerCollection).CreateIndex("name", true)
EventCollection = Database.Collection(models.EventCollection)
SourceCollection = Database.Collection(models.SourceCollection)
TakingCollection = Database.Collection(models.TakingCollection)
TakingCollection = Database.Collection(models.TakingCollection).CreateIndex("crew_id", false)
DepositCollection = Database.Collection(models.DepositCollection)
DepositUnitCollection = Database.Collection(models.DepositUnitCollection).CreateMultiIndex(bson.D{{Key: "taking_id", Value: 1}, {Key: "deposit_id", Value: 1}}, true)
DepositUnitCollection = Database.Collection(models.DepositUnitCollection).CreateMultiIndex(bson.D{{Key: "taking_id", Value: 1}, {Key: "deposit_id", Value: 1}}, true).CreateIndex("taking_id", false).CreateIndex("deposit_id", false)

FSChunkCollection = Database.Collection(models.FSChunkCollection)
FSFilesCollection = Database.Collection(models.FSFilesCollection)
Expand All @@ -148,6 +159,30 @@ func InitialDatabase() {
)
UserViewCollection = Database.Collection(models.UserView)

PublicEventPipe.Lookup(models.ParticipationCollection, "_id", "event_id", "participations")
PublicEventPipe.LookupUnwind(models.OrganizerCollection, "organizer_id", "_id", "organizer")
PublicEventPipe.LookupList(models.ArtistCollection, "artist_ids", "_id", "artists")
PublicEventPipe.LookupUnwind(models.CrewCollection, "crew_id", "_id", "crew")
Database.Database.CreateView(
context.Background(),
models.PublicEventView,
models.EventCollection,
models.EventPipelinePublic().Pipe,
)
PublicEventViewCollection = Database.Collection(models.PublicEventView)

EventPipe.Lookup(models.ParticipationCollection, "_id", "event_id", "participations")
EventPipe.LookupUnwind(models.OrganizerCollection, "organizer_id", "_id", "organizer")
EventPipe.LookupList(models.ArtistCollection, "artist_ids", "_id", "artists")
EventPipe.LookupUnwind(models.CrewCollection, "crew_id", "_id", "crew")
Database.Database.CreateView(
context.Background(),
models.EventView,
models.EventCollection,
models.EventPipeline(&models.AccessToken{ID: ""}),
)
EventViewCollection = Database.Collection(models.EventView)

DepositUnitTakingPipe.LookupUnwind(models.DepositCollection, "deposit_id", "_id", "deposit")
Database.Database.CreateView(
context.Background(),
Expand All @@ -171,6 +206,14 @@ func InitialDatabase() {
ActitityUserPipe.Pipe,
)
UpdateCollection = Database.Collection("updates").CreateIndex("name", true)
Updates = vmdb.NewCollectionUpdate(UpdateCollection)
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
28 changes: 15 additions & 13 deletions dao/crew.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import (

"github.com/Viva-con-Agua/vcago"
"github.com/Viva-con-Agua/vcago/vmdb"
"github.com/Viva-con-Agua/vcapool"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo/options"
)

func CrewInsert(ctx context.Context, i *models.CrewCreate, token *vcapool.AccessToken) (result *models.Crew, err error) {
func CrewInsert(ctx context.Context, i *models.CrewCreate, token *models.AccessToken) (result *models.Crew, err error) {

if err = models.CrewPermission(token); err != nil {
return
Expand All @@ -32,19 +32,22 @@ func CrewInsert(ctx context.Context, i *models.CrewCreate, token *vcapool.Access
return
}

func CrewGet(ctx context.Context, i *models.CrewQuery, token *vcapool.AccessToken) (result *[]models.Crew, err error) {
func CrewGet(ctx context.Context, i *models.CrewQuery, token *models.AccessToken) (result *[]models.Crew, err error) {
if err = models.CrewPermission(token); err != nil {
return
}
filter := i.Filter()
result = new([]models.Crew)
if err = CrewsCollection.Find(ctx, filter, result); err != nil {
opt := options.Find().SetSort(bson.D{{Key: "name", Value: 1}})
opt.Collation = &options.Collation{Locale: "en", Strength: 2}

if err = CrewsCollection.Aggregate(ctx, models.CrewPipeline().Match(filter).Pipe, result); err != nil {
return
}
return
}

func CrewGetByID(ctx context.Context, i *models.CrewParam, token *vcapool.AccessToken) (result *models.Crew, err error) {
func CrewGetByID(ctx context.Context, i *models.CrewParam, token *models.AccessToken) (result *models.Crew, err error) {
filter := i.PermittedFilter(token)
if err = CrewsCollection.FindOne(ctx, filter, &result); err != nil {
return
Expand All @@ -61,15 +64,15 @@ func CrewPublicGet(ctx context.Context, i *models.CrewQuery) (result *[]models.C
return
}

func CrewGetAsMember(ctx context.Context, i *models.CrewQuery, token *vcapool.AccessToken) (result *models.Crew, err error) {
func CrewGetAsMember(ctx context.Context, i *models.CrewQuery, token *models.AccessToken) (result *models.Crew, err error) {
filter := i.PermittedFilter(token)
if err = CrewsCollection.FindOne(ctx, filter, &result); err != nil {
return
}
return
}

func CrewUpdate(ctx context.Context, i *models.CrewUpdate, token *vcapool.AccessToken) (result *models.Crew, err error) {
func CrewUpdate(ctx context.Context, i *models.CrewUpdate, token *models.AccessToken) (result *models.Crew, err error) {
if err = models.CrewUpdatePermission(token); err != nil {
return
}
Expand All @@ -88,7 +91,7 @@ func CrewUpdate(ctx context.Context, i *models.CrewUpdate, token *vcapool.Access
if crew.AspSelection == "selected" && match < len(strings) && strings[match] == i.AspSelection {
RoleHistoryDelete(ctx, &models.RoleHistoryRequest{CrewID: i.ID, Confirmed: false}, token)
}
if !token.Roles.Validate("employee;admin") {
if !token.Roles.Validate("admin;employee;pool_employee") {
if err = CrewsCollection.UpdateOne(ctx, filter, vmdb.UpdateSet(i.ToCrewUpdateASP()), &result); err != nil {
return
}
Expand All @@ -97,18 +100,17 @@ func CrewUpdate(ctx context.Context, i *models.CrewUpdate, token *vcapool.Access
return
}
}
if crew.Email != i.Email || crew.Name != i.Name {
if crew.Email != i.Email || crew.Name != i.Name || crew.OrganisationID != i.OrganisationID {
filter := bson.D{{Key: "crew_id", Value: i.ID}}
update := bson.D{{Key: "email", Value: i.Email}, {Key: "name", Value: i.Name}}
update := bson.D{{Key: "email", Value: i.Email}, {Key: "name", Value: i.Name}, {Key: "organisation_id", Value: i.OrganisationID}}
if err = UserCrewCollection.UpdateMany(ctx, filter, vmdb.UpdateSet(update)); err != nil {
return
}

}
return
}

func CrewUpdateAspSelection(ctx context.Context, i *models.CrewParam, value string, token *vcapool.AccessToken) (result *models.Crew, err error) {
func CrewUpdateAspSelection(ctx context.Context, i *models.CrewParam, value string, token *models.AccessToken) (result *models.Crew, err error) {
if err = models.CrewUpdatePermission(token); err != nil {
return
}
Expand All @@ -124,7 +126,7 @@ func CrewUpdateAspSelection(ctx context.Context, i *models.CrewParam, value stri
return
}

func CrewDelete(ctx context.Context, i *models.CrewParam, token *vcapool.AccessToken) (err error) {
func CrewDelete(ctx context.Context, i *models.CrewParam, token *models.AccessToken) (err error) {
if err = models.CrewPermission(token); err != nil {
return
}
Expand Down
5 changes: 3 additions & 2 deletions dao/defaults.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package dao

import (
"pool-backend/models"

"github.com/Viva-con-Agua/vcago"
"github.com/Viva-con-Agua/vcapool"
)

type Check struct {
Expand All @@ -17,7 +18,7 @@ func (i *Check) Return() error {
return i.Error
}

func (i *Check) ASP(token *vcapool.AccessToken) *Check {
func (i *Check) ASP(token *models.AccessToken) *Check {
if !token.PoolRoles.Validate("finance;network;education;") {
i.Error = vcago.NewPermissionDenied("permission_denied", nil)
}
Expand Down
Loading

0 comments on commit a8af3a2

Please sign in to comment.