Skip to content

Commit

Permalink
Merge pull request #57 from nickshine/canceled-status-fix
Browse files Browse the repository at this point in the history
fix: allow for misspelled closure statuses
  • Loading branch information
nickshine authored Jan 28, 2021
2 parents 1a8e603 + ffee5e1 commit 6dd7257
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
9 changes: 5 additions & 4 deletions cmd/publisher/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,12 @@ func handler(ctx context.Context, e events.DynamoDBEvent) error {
rawTimeRange := image["RawTimeRange"].String()
timeRangeStatus := closures.TimeRangeStatus(image["TimeRangeStatus"].String())
closureStatus := closures.ClosureStatus(image["ClosureStatus"].String())
isCanceled := closures.IsCanceled(closureStatus)

switch record.EventName {
// An INSERT event means a new Closure has been added.
case string(events.DynamoDBOperationTypeInsert):
if closureStatus == closures.ClosureStatusCanceled {
if isCanceled {
log.Debugf("Closure Status of '%s' on 'INSERT', skipping publish", closureStatus)
return nil
}
Expand All @@ -75,10 +76,10 @@ func handler(ctx context.Context, e events.DynamoDBEvent) error {
oldTimeRangeStatus := closures.TimeRangeStatus(record.Change.OldImage["TimeRangeStatus"].String())
oldClosureStatus := closures.ClosureStatus(record.Change.OldImage["ClosureStatus"].String())

if closureStatus == oldClosureStatus && closureStatus == closures.ClosureStatusCanceled {
if closureStatus == oldClosureStatus && isCanceled {
log.Debugf("Closure is cancelled, skipping publish")
return nil
} else if timeRangeStatus != oldTimeRangeStatus && closureStatus != closures.ClosureStatusCanceled {
} else if timeRangeStatus != oldTimeRangeStatus && !isCanceled {
switch timeRangeStatus {
case closures.TimeRangeStatusActive:
messages = append(messages, fmt.Sprintf("Closure for %s - %s has started.\n%s",
Expand All @@ -87,7 +88,7 @@ func handler(ctx context.Context, e events.DynamoDBEvent) error {
messages = append(messages, fmt.Sprintf("Closure for %s - %s has ended.\n%s",
date, rawTimeRange, closures.SiteURL))
}
} else if rawTimeRange != oldRawTimeRange && closureStatus != closures.ClosureStatusCanceled {
} else if rawTimeRange != oldRawTimeRange && !isCanceled {
messages = append(messages, fmt.Sprintf("Time window for the %s - %s closure has changed to %s.\n%s",
date, oldRawTimeRange, rawTimeRange, closures.SiteURL))
} else if closureStatus != oldClosureStatus {
Expand Down
6 changes: 6 additions & 0 deletions pkg/closures/closures.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,9 @@ func scrapeClosuresSite() (*doc, error) {

return (*doc)(document), nil
}

// IsCanceled checks if a closure status is 'Status Canceled', forgiving for alternate spellings
// and capitilization differences.
func IsCanceled(status ClosureStatus) bool {
return strings.Contains(strings.ToLower(string(status)), "cancel")
}
37 changes: 37 additions & 0 deletions pkg/closures/closures_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,40 @@ func TestScrapeClosuresSite(t *testing.T) {
httpmock.Reset()
}
}

func TestIsCanceled(t *testing.T) {
tests := []struct {
name string
closure *Closure
expected bool
}{
{
"Scheduled",
&Closure{
ClosureStatus: ClosureStatusScheduled,
},
false,
},
{
"Canceled",
&Closure{
ClosureStatus: ClosureStatusCanceled,
},
true,
},
{
"Cancelled",
&Closure{
ClosureStatus: "Status Cancelled",
},
true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
actual := IsCanceled(tt.closure.ClosureStatus)
assert.Equal(t, actual, tt.expected)
})
}
}

0 comments on commit 6dd7257

Please sign in to comment.