Skip to content

Commit

Permalink
fix: listing active state query
Browse files Browse the repository at this point in the history
  • Loading branch information
Ajay Kelkar committed Dec 19, 2022
1 parent 9fc2243 commit eddece5
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions persistence/sql/persister_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,10 @@ func (p *Persister) ListSessions(ctx context.Context, active *bool, paginatorOpt
if err := p.Transaction(ctx, func(ctx context.Context, c *pop.Connection) error {
q := c.Where("nid = ?", nid)
if active != nil {
q = q.Where("active = ?", *active)

if *active {
q.Where("expires_at >= ?", time.Now())
q.Where("active = ? AND expires_at >= ?", *active, time.Now().UTC())
} else {
q.Where("expires_at < ?", time.Now())
q.Where("active = ? OR expires_at < ?", *active, time.Now().UTC())
}
}

Expand Down Expand Up @@ -113,6 +111,7 @@ func (p *Persister) ListSessions(ctx context.Context, active *bool, paginatorOpt
return err
}

sess.Active = sess.IsActive()
sess.Identity = i
}
}
Expand All @@ -135,22 +134,15 @@ func (p *Persister) ListSessionsByIdentity(ctx context.Context, iID uuid.UUID, a
nid := p.NetworkID(ctx)

if err := p.Transaction(ctx, func(ctx context.Context, c *pop.Connection) error {
i, err := p.GetIdentity(ctx, iID)
if err != nil {
return sqlcon.HandleError(err)
}

q := c.Where("identity_id = ? AND nid = ?", iID, nid)
if except != uuid.Nil {
q = q.Where("id != ?", except)
}
if active != nil {
q = q.Where("active = ?", *active)

if *active {
q.Where("expires_at >= ?", time.Now())
q.Where("active = ? AND expires_at >= ?", *active, time.Now().UTC())
} else {
q.Where("expires_at < ?", time.Now())
q.Where("active = ? OR expires_at < ?", *active, time.Now().UTC())
}
}
if len(expandables) > 0 {
Expand All @@ -170,7 +162,13 @@ func (p *Persister) ListSessionsByIdentity(ctx context.Context, iID uuid.UUID, a
}

if expandables.Has(session.ExpandSessionIdentity) {
i, err := p.GetIdentity(ctx, iID)
if err != nil {
return sqlcon.HandleError(err)
}

for _, s := range s {
s.Active = s.IsActive()
s.Identity = i
}
}
Expand Down

0 comments on commit eddece5

Please sign in to comment.