Skip to content

Commit

Permalink
Merge pull request #185 from Pigmice2733/tba-deleted
Browse files Browse the repository at this point in the history
Mark events/matches as deleted instead of deleting them
  • Loading branch information
fharding1 authored Mar 25, 2019
2 parents a6ae72a + 9611a80 commit cf14564
Show file tree
Hide file tree
Showing 12 changed files with 197 additions and 157 deletions.
4 changes: 4 additions & 0 deletions api-tests/matchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ module.exports = {
expect(received.fullDistrict).toBeUndefinedOr(String)
expect(received.week).toBeUndefinedOr(Number)
expect(received.webcasts).toBeA(Array)
expect(received.tbaDeleted).toBeA(Boolean)
expect(Object.keys(received)).toBeASubsetOf([
'key',
'realmId',
Expand All @@ -71,6 +72,7 @@ module.exports = {
'district',
'fullDistrict',
'webcasts',
'tbaDeleted',
])
} catch (error) {
return {
Expand All @@ -84,6 +86,7 @@ module.exports = {
try {
expect(received.key).toBeA(String)
expect(received.time).toBeADateString()
expect(received.tbaDeleted).toBeA(Boolean)
expect(received.redScore).toBeUndefinedOr(Number)
expect(received.blueScore).toBeUndefinedOr(Number)
expect(received.redAlliance).toEqual(expect.any(Array))
Expand All @@ -104,6 +107,7 @@ module.exports = {
'blueAlliance',
'redScore',
'blueScore',
'tbaDeleted',
])
} catch (error) {
return {
Expand Down
47 changes: 14 additions & 33 deletions api-tests/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions api-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
"test:watch": "jest --watch"
},
"devDependencies": {
"merge": ">=1.2.1",
"eslint": "5.6.0",
"eslint-plugin-caleb": "1.6.0",
"jest": "23.6.0",
"js-yaml": "3.12.0",
"js-yaml": "^3.13.0",
"merge": ">=1.2.1",
"node-fetch": "2.2.0",
"prettier": "1.14.3"
},
Expand Down
2 changes: 2 additions & 0 deletions api-tests/route-tests/events.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ test('events', async () => {
lat: 45.498555,
lon: -122.6385231,
webcasts: ['https://www.twitch.tv/firstwa_red'],
tbaDeleted: false,
}

resp = await fetch(api.address + `/events/${event.key}`, {
Expand Down Expand Up @@ -63,6 +64,7 @@ test('events', async () => {
lat: event.lat,
lon: event.lon,
webcasts: event.webcasts,
tbaDeleted: event.tbaDeleted,
})

// /events/{eventKey} endpoint
Expand Down
2 changes: 2 additions & 0 deletions api-tests/route-tests/matches.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ test('/events/{eventKey}/matches', async () => {
blueScore: 74,
redAlliance: ['frc1592', 'frc5722', 'frc1421'],
blueAlliance: ['frc6322', 'frc4024', 'frc5283'],
tbaDeleted: false,
}

resp = await fetch(api.address + '/events/1970flir/matches', {
Expand Down Expand Up @@ -135,6 +136,7 @@ test('/events/{eventKey}/matches', async () => {
blueScore: match.blueScore,
redAlliance: match.redAlliance,
blueAlliance: match.blueAlliance,
tbaDeleted: match.tbaDeleted,
})

// /events/{eventKey}/matches/{matchKey} endpoint
Expand Down
4 changes: 2 additions & 2 deletions api-tests/route-tests/realms.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ test('realms', async () => {
realm.id = d.id

// /realms/{id} update
realm.name += "foobar"
realm.name += 'foobar'
realmResp = await fetch(api.address + `/realms/${realm.id}`, {
method: 'POST',
body: JSON.stringify(realm),
Expand Down Expand Up @@ -102,7 +102,7 @@ test('realms', async () => {
})
expect(resp.status).toBe(200)
d = await resp.json()
expect(d).toHaveLength(2)
expect(d).toHaveLength(3)

// /realms/{id} endpoint
// /realms/{id} get super-admin
Expand Down
15 changes: 9 additions & 6 deletions internal/server/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,26 @@ import (
// eventsHandler returns a handler to get all events in a given year.
func (s *Server) eventsHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// Get new event data from TBA if event data is over 24 hours old
if err := s.updateEvents(r.Context()); err != nil {
ihttp.Error(w, http.StatusInternalServerError)
go s.Logger.WithError(err).Error("unable to update event data")
return
}

var events []store.Event
tbaDeleted := r.URL.Query().Get("tbaDeleted") == "true"

roles := ihttp.GetRoles(r)

userRealm, getRealmErr := ihttp.GetRealmID(r)

var events []store.Event
var err error
if roles.IsSuperAdmin {
events, err = s.Store.GetEvents(r.Context())
events, err = s.Store.GetEvents(r.Context(), tbaDeleted)
} else {
if getRealmErr != nil {
events, err = s.Store.GetEventsFromRealm(r.Context(), nil)
events, err = s.Store.GetEventsFromRealm(r.Context(), nil, tbaDeleted)
} else {
events, err = s.Store.GetEventsFromRealm(r.Context(), &userRealm)
events, err = s.Store.GetEventsFromRealm(r.Context(), &userRealm, tbaDeleted)
}
}

Expand Down Expand Up @@ -152,6 +151,10 @@ func (s *Server) updateEvents(ctx context.Context) error {
return errors.Wrap(err, "upserting events")
}

if err := s.Store.MarkEventsDeleted(ctx, events); err != nil {
return errors.Wrap(err, "marking missing events deleted")
}

s.eventsLastUpdate = &now
}

Expand Down
Loading

0 comments on commit cf14564

Please sign in to comment.