Skip to content

Commit

Permalink
Merge pull request #215 from Viva-con-Agua/feature/crew_event
Browse files Browse the repository at this point in the history
Feature/crew event
  • Loading branch information
TobiKaestle authored Nov 22, 2024
2 parents afd5344 + c5ab3a1 commit d69ae14
Show file tree
Hide file tree
Showing 13 changed files with 382 additions and 53 deletions.
6 changes: 4 additions & 2 deletions dao/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ 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
Expand Down Expand Up @@ -112,6 +113,7 @@ 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)
Expand Down
69 changes: 48 additions & 21 deletions dao/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (

"github.com/Viva-con-Agua/vcago"
"github.com/Viva-con-Agua/vcago/vmdb"
"github.com/Viva-con-Agua/vcago/vmod"
"github.com/labstack/gommon/log"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo/options"
)

func EventInsert(ctx context.Context, i *models.EventCreate, token *models.AccessToken) (result *models.Event, err error) {
Expand Down Expand Up @@ -44,6 +44,10 @@ func EventInsert(ctx context.Context, i *models.EventCreate, token *models.Acces
if err = EventCollection.AggregateOne(ctx, models.EventPipeline(token).Match(filter).Pipe, &result); err != nil {
return
}
history := result.NewEventStateHistory("", result.EventState.State, token)
if err = EventStateHistoryInsert(ctx, history, token); err != nil {
return
}
return
}

Expand All @@ -58,14 +62,13 @@ func EventGet(i *models.EventQuery, token *models.AccessToken) (result *[]models
if err = EventCollection.Aggregate(ctx, pipeline, result); err != nil {
return
}
opts := options.Count().SetHint("_id_")
if i.FullCount != "true" {
opts.SetSkip(i.Skip).SetLimit(i.Limit)
}
if cursor, cErr := EventViewCollection.Collection.CountDocuments(ctx, filter, opts); cErr != nil {
list_size = 0

count := vmod.Count{}
var cErr error
if cErr = EventCollection.AggregateOne(ctx, models.EventPipeline(token).Match(filter).Count().Pipe, &count); cErr != nil {
list_size = 1
} else {
list_size = cursor
list_size = int64(count.Total)
}
return
}
Expand Down Expand Up @@ -129,16 +132,14 @@ func EventGetPublic(i *models.EventQuery) (result *[]models.EventPublic, list_si
if err = EventCollection.Aggregate(ctx, pipeline, result); err != nil {
return
}
ctx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
opts := options.Count().SetHint("_id_")
if i.FullCount != "true" {
opts.SetSkip(i.Skip).SetLimit(i.Limit)
}
if cursor, cErr := PublicEventViewCollection.Collection.CountDocuments(ctx, filter, opts); cErr != nil {
list_size = 0

count := vmod.Count{}
var cErr error
if cErr = EventCollection.AggregateOne(ctx, models.EventPipelinePublic().Match(filter).Count().Pipe, &count); cErr != nil {
print(cErr)
list_size = 1
} else {
list_size = cursor
list_size = int64(count.Total)
}
return
}
Expand Down Expand Up @@ -196,6 +197,10 @@ func EventUpdate(ctx context.Context, i *models.EventUpdate, token *models.Acces
return
}
if event.EventState.State != result.EventState.State {
history := result.NewEventStateHistory(event.EventState.State, result.EventState.State, token)
if err = EventStateHistoryInsert(ctx, history, token); err != nil {
return
}
if result.EventState.State == "canceled" {
EventParticipantsNotification(ctx, result, "event_cancel")
updateTaking := bson.D{{Key: "state.no_income", Value: true}}
Expand All @@ -207,7 +212,7 @@ func EventUpdate(ctx context.Context, i *models.EventUpdate, token *models.Acces
if result.EventState.State == "published" ||
result.EventState.State == "canceled" ||
(result.EventState.State == "requested" && result.EventState.CrewConfirmation == "") {
EventStateNotification(ctx, result, "event_state")
EventStateNotification(ctx, result)
}
} else if event.StartAt != result.StartAt ||
event.EndAt != result.EndAt ||
Expand All @@ -216,7 +221,7 @@ func EventUpdate(ctx context.Context, i *models.EventUpdate, token *models.Acces
EventParticipantsNotification(ctx, result, "event_update")
}
if event.EventASPID != result.EventASPID && result.EventASPID != token.ID {
EventASPNotification(ctx, result, "event_asp")
EventASPNotification(ctx, result)
}
if event.EndAt != i.EndAt {
updateTaking := bson.D{{Key: "date_of_taking", Value: i.EndAt}}
Expand Down Expand Up @@ -394,7 +399,7 @@ func EventParticipantsNotification(ctx context.Context, i *models.Event, templat
return
}

func EventASPNotification(ctx context.Context, i *models.Event, template string) (err error) {
func EventASPNotification(ctx context.Context, i *models.Event) (err error) {

if i.EventASPID == "" {
return vcago.NewNotFound(models.EventCollection, i)
Expand All @@ -405,6 +410,7 @@ func EventASPNotification(ctx context.Context, i *models.Event, template string)
return
}

template := "event_asp"
mail := vcago.NewMailData(user.Email, "pool-backend", template, "pool", user.Country)
mail.AddUser(user.User())
mail.AddContent(i.ToContent())
Expand All @@ -417,7 +423,7 @@ func EventASPNotification(ctx context.Context, i *models.Event, template string)
return
}

func EventStateNotification(ctx context.Context, i *models.Event, template string) (err error) {
func EventStateNotification(ctx context.Context, i *models.Event) (err error) {

if i.EventASPID == "" {
return vcago.NewNotFound(models.EventCollection, i)
Expand All @@ -428,6 +434,7 @@ func EventStateNotification(ctx context.Context, i *models.Event, template strin
return
}

template := "event_state"
mail := vcago.NewMailData(eventAps.Email, "pool-backend", template, "pool", eventAps.Country)
mail.AddUser(eventAps.User())
mail.AddContent(i.ToContent())
Expand All @@ -438,3 +445,23 @@ func EventStateNotification(ctx context.Context, i *models.Event, template strin
//vcago.Nats.Publish("system.notification.job", notification)
return
}

func EventHistoryAdminNotification(ctx context.Context, data []models.EventStateHistoryNotification) (err error) {
mail := vcago.NewMailData("netzwerk@vivaconagua.org", "pool-backend", "events_published", "pool", "de")
mail.AddContent(models.EventHistoryAdminContent(data))
vcago.Nats.Publish("system.mail.job", mail)

mail2 := vcago.NewMailData("festival@vivaconagua.org", "pool-backend", "events_published", "pool", "de")
mail2.AddContent(models.EventHistoryAdminContent(data))
vcago.Nats.Publish("system.mail.job", mail2)
return
}

func EventHistoryCrewNotification(ctx context.Context, data_collection map[string][]models.EventStateHistoryNotification) (err error) {
for email, data := range data_collection {
mail := vcago.NewMailData(email, "pool-backend", "events_crew_published", "pool", "de")
mail.AddContent(models.EventHistoryAdminContent(data))
vcago.Nats.Publish("system.mail.job", mail)
}
return
}
36 changes: 36 additions & 0 deletions dao/event_history.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package dao

import (
"context"
"pool-backend/models"

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

func EventStateHistoryInsert(ctx context.Context, i *models.EventStateHistoryCreate, token *models.AccessToken) (err error) {
if err = EventStateHistoryCollection.InsertOne(ctx, i); err != nil {
return
}
return
}

func EventStateHistoryGet(ctx context.Context, i *models.EventStateHistoryQuery, token *models.AccessToken) (result *[]models.EventStateHistory, list_size int64, err error) {
result = new([]models.EventStateHistory)
pipeline := models.EventStatePipeline().Match(i.Filter())
if err = EventStateHistoryCollection.Aggregate(
ctx,
pipeline.Pipe,
result,
); err != nil {
return
}
count := vmod.Count{}
var cErr error
if cErr = EventStateHistoryCollection.AggregateOne(ctx, pipeline.Count().Pipe, &count); cErr != nil {
print(cErr)
list_size = 1
} else {
list_size = int64(count.Total)
}
return
}
3 changes: 2 additions & 1 deletion dao/organisation.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ func OrganisationInsert(ctx context.Context, i *models.OrganisationCreate, token
func OrganisationGet(ctx context.Context, i *models.OrganisationQuery) (result *[]models.Organisation, err error) {
filter := i.Filter()
result = new([]models.Organisation)
if err = OrganisationCollection.Find(ctx, filter, result); err != nil {

if err = OrganisationCollection.Aggregate(ctx, models.OrganisationPipeline().Match(filter).Pipe, result); err != nil {
return
}
return
Expand Down
11 changes: 9 additions & 2 deletions dao/participation.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,18 @@ import (

func ParticipationInsert(ctx context.Context, i *models.ParticipationCreate, token *models.AccessToken) (result *models.Participation, err error) {

database := i.ParticipationDatabase(token)
event := new(models.Event)
if err = EventCollection.FindOne(
ctx,
bson.D{{Key: "_id", Value: i.EventID}},
event,
); err != nil {
return
}
database := i.ParticipationDatabase(token, event)
if err = ParticipationCollection.InsertOne(ctx, database); err != nil {
return
}
event := new(models.Event)
if event, err = EventGetInternalByID(ctx, &models.EventParam{ID: i.EventID}); err != nil {
return
}
Expand Down
77 changes: 77 additions & 0 deletions dao/update_ticker.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@ func UpdateTicker() {
EventStateFinishTicker()
EventStateClosedTicker()
UserActiveStateTicker()
//SendWeeklyNotification()
SendWeeklyCrewNotification()
go func() {
for {
select {
case <-ticker.C:
//SendWeeklyNotification()
//SendWeeklyCrewNotification()
EventStateFinishTicker()
EventStateClosedTicker()
UserActiveStateTicker()
Expand Down Expand Up @@ -112,3 +116,76 @@ func UserActiveStateTicker() {
}

}

// SendWeeklyNotification Send a notification mail to festival@ and netzwerk@ for all published events last 7 days
func SendWeeklyNotification() {

h, _, _ := time.Now().Clock()
if time.Now().Weekday() != time.Monday {
return
} else if h < 9 || h >= 10 {
return
}

filter := models.EventPublishedLastWeek()

events := []models.EventStateHistory{}
pipeline := models.EventStatePipeline().Match(filter).Pipe
if err := EventStateHistoryCollection.Aggregate(context.Background(), pipeline, &events); err != nil {
log.Print(err)
}

eventNotifications := []models.EventStateHistoryNotification{}
for _, e := range events {
eventNotifications = append(eventNotifications, models.EventStateHistoryNotification{
EventID: e.EventID,
EventName: e.Event.Name,
EventCrew: e.Crew.Name,
EventStart: time.Unix(e.Event.StartAt, 0).Format("02.01.2006 15:04"),
EventArtist: models.ToArtistList(e.Event.Artists),
EventLocation: e.Event.GetLocation(),
PublishedDate: time.Unix(e.Date, 0).Format("02.01.2006"),
})
}

if err := EventHistoryAdminNotification(context.Background(), eventNotifications); err != nil {
log.Print(err)

}
}

// SendWeeklyCrewNotification Send a notification mail to each crew email address for all published events last 7 days
func SendWeeklyCrewNotification() {

h, _, _ := time.Now().Clock()
if time.Now().Weekday() != time.Monday {
return
} else if h < 9 || h >= 10 {
return
}

filter := models.EventPublishedLastWeek()
events := []models.EventStateHistory{}
pipeline := models.EventStatePipeline().Match(filter).Pipe
if err := EventStateHistoryCollection.Aggregate(context.Background(), pipeline, &events); err != nil {
log.Print(err)
}
m := make(map[string][]models.EventStateHistoryNotification)
for _, e := range events {
if e.CrewID == "" {
continue
}
m[e.Crew.Email] = append(m[e.Crew.Email], models.EventStateHistoryNotification{
EventID: e.EventID,
EventName: e.Event.Name,
EventCrew: e.Crew.Name,
EventStart: time.Unix(e.Event.StartAt, 0).Format("02.01.2006 15:04"),
EventArtist: models.ToArtistList(e.Event.Artists),
EventLocation: e.Event.GetLocation(),
PublishedDate: time.Unix(e.Date, 0).Format("02.01.2006"),
})
}
if err := EventHistoryCrewNotification(context.Background(), m); err != nil {
log.Print(err)
}
}
18 changes: 8 additions & 10 deletions dao/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (
"time"

"github.com/Viva-con-Agua/vcago/vmdb"
"github.com/Viva-con-Agua/vcago/vmod"

"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo/options"
)

func UserInsert(ctx context.Context, i *models.UserDatabase) (result *models.User, err error) {
Expand Down Expand Up @@ -48,16 +48,14 @@ func UsersGet(i *models.UserQuery, token *models.AccessToken) (result *[]models.
if err = UserCollection.Aggregate(ctx, pipeline, result); err != nil {
return
}
ctx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
opts := options.Count().SetHint("_id_")
if i.FullCount != "true" {
opts.SetSkip(i.Skip).SetLimit(i.Limit)
}
if cursor, cErr := UserViewCollection.Collection.CountDocuments(ctx, filter, opts); cErr != nil {
list_size = 0

count := vmod.Count{}
var cErr error
if cErr = UserViewCollection.AggregateOne(ctx, models.UserPermittedPipeline(token).Match(filter).Count().Pipe, &count); cErr != nil {
print(cErr)
list_size = 1
} else {
list_size = cursor
list_size = int64(count.Total)
}
return
}
Expand Down
12 changes: 12 additions & 0 deletions models/artist.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package models

import (
"strings"

"github.com/Viva-con-Agua/vcago"
"github.com/Viva-con-Agua/vcago/vmdb"
"github.com/Viva-con-Agua/vcago/vmod"
Expand Down Expand Up @@ -50,6 +52,16 @@ func ArtistDeletePermission(token *AccessToken) (err error) {
return
}

func ToArtistList(artists []Artist) string {
names := make([]string, len(artists))
for i, artist := range artists {
names[i] = artist.Name
}

result := strings.Join(names, ", ")
return result
}

func (i *ArtistCreate) Artist() *Artist {
return &Artist{
ID: uuid.NewString(),
Expand Down
Loading

0 comments on commit d69ae14

Please sign in to comment.