Skip to content

Commit

Permalink
Merge pull request #136 from Viva-con-Agua/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
TobiKaestle authored Aug 4, 2023
2 parents 6053dbc + 2f3fc08 commit a77c7c6
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 89 deletions.
176 changes: 90 additions & 86 deletions dao/deposit.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,90 @@ func DepositUpdate(ctx context.Context, i *models.DepositUpdate, token *vcapool.
}

if i.Status == "confirmed" {
for _, unit := range i.DepositUnit {
go func() {

for _, unit := range i.DepositUnit {
event := new(models.EventUpdate)
if err = EventCollection.FindOne(
ctx,
bson.D{{Key: "taking_id", Value: unit.TakingID}},
event,
); err != nil {
if !vmdb.ErrNoDocuments(err) {
return
}
err = nil
}
if event.ID != "" {
event.EventState.State = "closed"
e := new(models.Event)
if err = EventCollection.UpdateOneAggregate(
ctx,
event.Match(),
vmdb.UpdateSet(event),
e,
models.EventPipeline(token).Match(event.Match()).Pipe,
); err != nil {
return
}

// Update CRM event
if err = IDjango.Post(e, "/v1/pool/event/update/"); err != nil {
log.Print(err)
}

// Add takings to CRM
var taking *models.Taking
if taking, err = TakingGetByID(ctx, &models.TakingParam{ID: unit.TakingID}, token); err != nil {
log.Print(err)
}

taking.EditorID = token.ID
if err = IDjango.Post(taking, "/v1/pool/taking/create/"); err != nil {
log.Print(err)
}

// Add participations to event
participations := new([]models.Participation)

if err = ParticipationCollection.Aggregate(
ctx,
models.ParticipationPipeline().Match(bson.D{{Key: "event_id", Value: e.ID}}).Pipe,
participations,
); err != nil {
return
}

if err = IDjango.Post(participations, "/v1/pool/participations/create/"); err != nil {
log.Print(err)
err = nil
}

}
}
}()

}

return
}

func DepositSync(ctx context.Context, i *models.DepositParam, token *vcapool.AccessToken) (result *models.Deposit, err error) {

filter := bson.D{{Key: "_id", Value: i.ID}}
if err = DepositCollection.AggregateOne(
ctx,
models.DepositPipeline().Match(filter).Pipe,
&result,
); err != nil {
return
}
if result.Status != "confirmed" {
return nil, vcago.NewBadRequest("deposit", "deposit_confirmed_failure", nil)
}

go func() {
for _, unit := range result.DepositUnit {
event := new(models.EventUpdate)
if err = EventCollection.FindOne(
ctx,
Expand All @@ -137,22 +220,21 @@ func DepositUpdate(ctx context.Context, i *models.DepositUpdate, token *vcapool.
return
}

// Update CRM event
if err = IDjango.Post(e, "/v1/pool/event/update/"); err != nil {
log.Print(err)
}

// Add takings to CRM
var taking *models.Taking
if taking, err = TakingGetByID(ctx, &models.TakingParam{ID: unit.TakingID}, token); err != nil {
log.Print(err)
}

taking.EditorID = token.ID
if err = IDjango.Post(taking, "/v1/pool/taking/create/"); err != nil {
log.Print(err)
}

// Update CRM event
if err = IDjango.Post(e, "/v1/pool/event/update/"); err != nil {
log.Print(err)
}

// Add participations to event
participations := new([]models.Participation)

Expand All @@ -171,85 +253,7 @@ func DepositUpdate(ctx context.Context, i *models.DepositUpdate, token *vcapool.

}
}

}

return
}

func DepositSync(ctx context.Context, i *models.DepositParam, token *vcapool.AccessToken) (result *models.Deposit, err error) {

filter := bson.D{{Key: "_id", Value: i.ID}}
if err = DepositCollection.AggregateOne(
ctx,
models.DepositPipeline().Match(filter).Pipe,
&result,
); err != nil {
return
}
if result.Status != "confirmed" {
return nil, vcago.NewBadRequest("deposit", "deposit_confirmed_failure", nil)
}

for _, unit := range result.DepositUnit {
event := new(models.EventUpdate)
if err = EventCollection.FindOne(
ctx,
bson.D{{Key: "taking_id", Value: unit.TakingID}},
event,
); err != nil {
if !vmdb.ErrNoDocuments(err) {
return
}
err = nil
}
if event.ID != "" {
event.EventState.State = "closed"
e := new(models.Event)
if err = EventCollection.UpdateOneAggregate(
ctx,
event.Match(),
vmdb.UpdateSet(event),
e,
models.EventPipeline(token).Match(event.Match()).Pipe,
); err != nil {
return
}

// Add takings to CRM
var taking *models.Taking
if taking, err = TakingGetByID(ctx, &models.TakingParam{ID: unit.TakingID}, token); err != nil {
log.Print(err)
}

taking.EditorID = token.ID
if err = IDjango.Post(taking, "/v1/pool/taking/create/"); err != nil {
log.Print(err)
}

// Update CRM event
if err = IDjango.Post(e, "/v1/pool/event/update/"); err != nil {
log.Print(err)
}

// Add participations to event
participations := new([]models.Participation)

if err = ParticipationCollection.Aggregate(
ctx,
models.ParticipationPipeline().Match(bson.D{{Key: "event_id", Value: e.ID}}).Pipe,
participations,
); err != nil {
return
}

if err = IDjango.Post(participations, "/v1/pool/participations/create/"); err != nil {
log.Print(err)
err = nil
}

}
}
}()

return
}
Expand Down
9 changes: 7 additions & 2 deletions dao/newsletter.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,16 @@ func NewsletterImport(ctx context.Context, i *models.NewsletterImport) (result *
return
}

func NewsletterSync(ctx context.Context, i []models.Newsletter, token *vcapool.AccessToken) (result *[]models.Newsletter, err error) {
func NewsletterSync(ctx context.Context, i *models.User, token *vcapool.AccessToken) (result *[]models.Newsletter, err error) {
export := &models.NewsletterExport{
UserID: i.ID,
Newsletter: i.Newsletter,
}
go func() {
if err = IDjango.Post(i, "/v1/pool/newsletters/"); err != nil {
if err = IDjango.Post(export, "/v1/pool/newsletters/"); err != nil {
log.Print(err)
}
}()
result = &i.Newsletter
return
}
2 changes: 1 addition & 1 deletion handlers/token/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (i *ProfileHandler) UserSync(cc echo.Context) (err error) {
if _, err = dao.ProfileSync(c.Ctx(), result.Profile, token); err != nil {
return
}
if _, err = dao.NewsletterSync(c.Ctx(), result.Newsletter, token); err != nil {
if _, err = dao.NewsletterSync(c.Ctx(), result, token); err != nil {
return
}
return c.SuccessResponse(http.StatusOK, "successfully_synced", "active", nil)
Expand Down
4 changes: 4 additions & 0 deletions models/newsletter.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ type (
UserID string `json:"user_id" bson:"user_id"`
Modified vmod.Modified `json:"modified" bson:"modified"`
}
NewsletterExport struct {
UserID string `json:"user_id"`
Newsletter []Newsletter `json:"newsletter"`
}
NewsletterParam struct {
ID string `param:"id"`
}
Expand Down

0 comments on commit a77c7c6

Please sign in to comment.