Skip to content

Commit

Permalink
fix: processor crashes when location dont exist yet (#168)
Browse files Browse the repository at this point in the history
  • Loading branch information
42atomys committed Jul 5, 2022
1 parent bf47948 commit e3a5e48
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
5 changes: 4 additions & 1 deletion internal/api/api.resolvers.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,10 @@ func (r *mutationResolver) InviteOnDiscord(ctx context.Context) (bool, error) {
return false, err
}

acc := r.client.Account.Query().Where(account.UserID(cu.ID), account.Provider(string(typesgen.ProviderDiscord))).OnlyX(ctx)
acc, err := r.client.Account.Query().Where(account.UserID(cu.ID), account.Provider(string(typesgen.ProviderDiscord))).Only(ctx)
if err != nil {
return false, err
}

err = s.GuildMemberAdd(acc.AccessToken, viper.GetString("discord.guildID"), acc.ProviderAccountID, "", []string{}, false, false)
if err != nil {
Expand Down
22 changes: 18 additions & 4 deletions internal/webhooks/location.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/rs/zerolog/log"

modelsutils "atomys.codes/stud42/internal/models"
"atomys.codes/stud42/internal/models/generated"
"atomys.codes/stud42/internal/models/generated/campus"
"atomys.codes/stud42/internal/models/generated/location"
"atomys.codes/stud42/internal/models/generated/user"
Expand Down Expand Up @@ -65,22 +66,35 @@ func (p *locationProcessor) Close(loc *duoapi.Location[duoapi.LocationUser], met
SetIdentifier(loc.Host).
Where(location.DuoID(loc.ID)).
Exec(p.ctx)
if err != nil {

if err != nil && !generated.IsNotFound(err) {
return err
}

// Unlink the user from the location in database if the location is closed
// and the user is not assigned to another location anymore (i.e. the user
// is not assigned to any other location)
return p.db.User.UpdateOne(p.db.User.Query().Where(user.DuoID(loc.User.ID)).FirstX(p.ctx)).SetCurrentLocation(nil).Exec(p.ctx)
return p.unlinkLocation(loc)
}
func (p *locationProcessor) Destroy(loc *duoapi.Location[duoapi.LocationUser], metadata *duoapi.WebhookMetadata) error {
// Delete the location in database
_, err := p.db.Location.Delete().Where(location.DuoID(loc.ID)).Exec(p.ctx)
if err != nil {
if err != nil && !generated.IsNotFound(err) {
return err
}

// Unlink the user from the location in database if the location is destroyed
return p.db.User.UpdateOne(p.db.User.Query().Where(user.DuoID(loc.User.ID)).FirstX(p.ctx)).SetCurrentLocation(nil).Exec(p.ctx)
return p.unlinkLocation(loc)
}

func (p *locationProcessor) unlinkLocation(duoLoc *duoapi.Location[duoapi.LocationUser]) error {
user, err := p.db.User.Query().Where(user.DuoID(duoLoc.User.ID)).First(p.ctx)
if err != nil {
if generated.IsNotFound(err) {
return nil
}
return err
}

return p.db.User.UpdateOne(user).ClearCurrentLocation().Exec(p.ctx)
}
3 changes: 1 addition & 2 deletions internal/webhooks/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ func New() *processor {
}

func (p *processor) Serve(amqpUrl, channel string) error {

conn, err := amqp.Dial(amqpUrl)
if err != nil {
return err
Expand Down Expand Up @@ -68,7 +67,6 @@ func (p *processor) Serve(amqpUrl, channel string) error {

log.Info().Msg("Consumer ready. Waiting for messages...")
for d := range msgs {
log.Debug().Msg("Received a message")
err := p.handler(d.Body)
if err != nil {
sentry.CaptureException(err)
Expand All @@ -83,6 +81,7 @@ func (p *processor) Serve(amqpUrl, channel string) error {
sentry.CaptureException(err)
}
}

return nil
}

Expand Down

0 comments on commit e3a5e48

Please sign in to comment.