diff --git a/dao/deposit.go b/dao/deposit.go index b0b2b06..e59f12c 100644 --- a/dao/deposit.go +++ b/dao/deposit.go @@ -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, @@ -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) @@ -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 } diff --git a/dao/newsletter.go b/dao/newsletter.go index a701984..f8f9ed5 100644 --- a/dao/newsletter.go +++ b/dao/newsletter.go @@ -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 } diff --git a/handlers/token/profile.go b/handlers/token/profile.go index 2880631..605e447 100644 --- a/handlers/token/profile.go +++ b/handlers/token/profile.go @@ -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) diff --git a/models/newsletter.go b/models/newsletter.go index a376f2c..fea21f6 100644 --- a/models/newsletter.go +++ b/models/newsletter.go @@ -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"` }