Skip to content

Commit aba6454

Browse files
committed
set ExpiresAt in confirmations that support expiration
This field will help the care partner mobile app. BACK-2807
1 parent 85c89a8 commit aba6454

File tree

2 files changed

+53
-4
lines changed

2 files changed

+53
-4
lines changed

models/confirmation.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ type (
2323
Created time.Time `json:"created" bson:"created"`
2424
Modified time.Time `json:"modified" bson:"modified"`
2525
Status Status `json:"status" bson:"status"`
26+
ExpiresAt *time.Time `json:"expiresAt,omitempty" bson:"expiresAt,omitempty"`
2627

2728
Restrictions *Restrictions `json:"restrictions" bson:"-"`
2829
TemplateName TemplateName `json:"-" bson:"templateName"`
@@ -110,6 +111,10 @@ func NewConfirmation(theType Type, templateName TemplateName, creatorId string)
110111
Created: time.Now(),
111112
}
112113

114+
if timeout, ok := Timeouts[theType]; ok {
115+
expiresAt := conf.Created.Add(timeout)
116+
conf.ExpiresAt = &expiresAt
117+
}
113118
return conf, nil
114119
}
115120
}
@@ -232,12 +237,10 @@ func (c *Confirmation) ValidateType(expectedType Type, validationErrors *[]error
232237
}
233238

234239
func (c *Confirmation) IsExpired() bool {
235-
timeout, ok := Timeouts[c.Type]
236-
if !ok {
240+
if c.ExpiresAt == nil || c.ExpiresAt.IsZero() {
237241
return false
238242
}
239-
240-
return time.Now().After(c.Created.Add(timeout))
243+
return time.Now().After(*c.ExpiresAt)
241244
}
242245

243246
func (c *Confirmation) ResetKey() error {

models/confirmation_test.go

+46
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/json"
66
"fmt"
77
"testing"
8+
"time"
89
)
910

1011
const (
@@ -67,6 +68,10 @@ func Test_NewConfirmation(t *testing.T) {
6768
t.Fail()
6869
}
6970

71+
if confirmation.ExpiresAt.IsZero() {
72+
t.Errorf("expected expiresAt to be non-Zero")
73+
}
74+
7075
confirmation.UpdateStatus(StatusCompleted)
7176

7277
if confirmation.Status != StatusCompleted {
@@ -229,6 +234,47 @@ func TestConfirmationContextCustomUnmarshaler(s *testing.T) {
229234
})
230235
}
231236

237+
func TestConfirmationCalculatesExpiresAt(t *testing.T) {
238+
for cType := range Timeouts {
239+
invite, err := NewConfirmation(cType, TemplateNamePasswordReset, USERID)
240+
if err != nil {
241+
t.Fatalf("expected nil, got %+v", err)
242+
}
243+
if invite.ExpiresAt == nil || invite.ExpiresAt.IsZero() {
244+
t.Errorf("expected non-Zero ExiresAt")
245+
}
246+
}
247+
}
248+
249+
func TestConfirmationIsExpired(t *testing.T) {
250+
for cType := range Timeouts {
251+
invite, err := NewConfirmation(cType, TemplateNameCareteamInvite, USERID)
252+
if err != nil {
253+
t.Fatalf("expected nil error, got %s", err)
254+
}
255+
if invite.IsExpired() {
256+
t.Errorf("expected false, got true")
257+
}
258+
*invite.ExpiresAt = time.Unix(0, 0)
259+
if !invite.IsExpired() {
260+
t.Errorf("expected true, got false")
261+
}
262+
}
263+
264+
// These types don't have timeouts, so don't get an expires at. They're
265+
// never expired.
266+
for _, cType := range []Type{TypeClinicianInvite, TypeNoAccount} {
267+
nonExpiringInviting, err := NewConfirmation(cType, TemplateNameCareteamInvite, USERID)
268+
if err != nil {
269+
t.Fatalf("expected nil error, got %s", err)
270+
}
271+
if nonExpiringInviting.IsExpired() {
272+
t.Errorf("expected invitation type %q to never expire", cType)
273+
}
274+
}
275+
276+
}
277+
232278
// buff is a helper for generating a JSON []byte representation.
233279
func buff(format string, args ...interface{}) *bytes.Buffer {
234280
return bytes.NewBufferString(fmt.Sprintf(format, args...))

0 commit comments

Comments
 (0)