Skip to content

Commit

Permalink
Merge pull request #32 from tapglue/fix-double-device
Browse files Browse the repository at this point in the history
Fix double device registration
  • Loading branch information
xla authored Dec 9, 2016
2 parents 861110a + 4727d47 commit 78b5404
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 84 deletions.
14 changes: 14 additions & 0 deletions core/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ func DeviceUpdate(devices device.Service) DeviceUpdateFunc {
token string,
language string,
) error {
// Get user devices.
ds, err := devices.Query(currentApp.Namespace(), device.QueryOptions{
Deleted: &defaultDeleted,
Platforms: []sns.Platform{
Expand All @@ -211,6 +212,19 @@ func DeviceUpdate(devices device.Service) DeviceUpdateFunc {
return err
}

// Check if there is a device with that token.
ts, err := devices.Query(currentApp.Namespace(), device.QueryOptions{
Deleted: &defaultDeleted,
Tokens: []string{
token,
},
})
if err != nil {
return err
}

ds = append(ds, ts...)

d := &device.Device{}

for _, dev := range ds {
Expand Down
1 change: 1 addition & 0 deletions service/device/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ type QueryOptions struct {
EndpointARNs []string
IDs []uint64
Platforms []sns.Platform
Tokens []string
UserIDs []uint64
}

Expand Down
103 changes: 19 additions & 84 deletions service/device/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,91 +88,26 @@ func testServiceQuery(t *testing.T, p prepareFunc) {
}
}

ds, err = service.Query(namespace, QueryOptions{
Deleted: &deleted,
})
if err != nil {
t.Fatal(err)
}

if have, want := len(ds), 5; have != want {
t.Errorf("have %v, want %v", have, want)
}

ds, err = service.Query(namespace, QueryOptions{
DeviceIDs: []string{
created.DeviceID,
},
})
if err != nil {
t.Fatal(err)
}

if have, want := len(ds), 1; have != want {
t.Errorf("have %v, want %v", have, want)
}

ds, err = service.Query(namespace, QueryOptions{
Disabled: &deleted,
})
if err != nil {
t.Fatal(err)
}

if have, want := len(ds), 7; have != want {
t.Errorf("have %v, want %v", have, want)
}

ds, err = service.Query(namespace, QueryOptions{
EndpointARNs: []string{
created.EndpointARN,
},
})
if err != nil {
t.Fatal(err)
}

if have, want := len(ds), 1; have != want {
t.Errorf("have %v, want %v", have, want)
}

ds, err = service.Query(namespace, QueryOptions{
IDs: []uint64{
created.ID,
},
})
if err != nil {
t.Fatal(err)
}

if have, want := len(ds), 1; have != want {
t.Errorf("have %v, want %v", have, want)
}

ds, err = service.Query(namespace, QueryOptions{
Platforms: []sns.Platform{
PlatformIOSSandbox,
},
})
if err != nil {
t.Fatal(err)
}

if have, want := len(ds), 13; have != want {
t.Errorf("have %v, want %v", have, want)
}

ds, err = service.Query(namespace, QueryOptions{
UserIDs: []uint64{
created.UserID,
},
})
if err != nil {
t.Fatal(err)
}
cases := map[*QueryOptions]int{
&QueryOptions{Deleted: &deleted}: 5,
&QueryOptions{DeviceIDs: []string{created.DeviceID}}: 1,
&QueryOptions{Disabled: &deleted}: 7,
&QueryOptions{EndpointARNs: []string{created.EndpointARN}}: 1,
&QueryOptions{IDs: []uint64{created.ID}}: 1,
&QueryOptions{Platforms: []sns.Platform{PlatformIOSSandbox}}: 13,
&QueryOptions{Tokens: []string{created.Token}}: 1,
&QueryOptions{UserIDs: []uint64{created.UserID}}: 1,
}

for opts, want := range cases {
list, err := service.Query(namespace, *opts)
if err != nil {
t.Fatal(err)
}

if have, want := len(ds), 1; have != want {
t.Errorf("have %v, want %v", have, want)
if have := len(list); have != want {
t.Errorf("have %v, want %v", have, want)
}
}
}

Expand Down
17 changes: 17 additions & 0 deletions service/device/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const (
pgClauseEndpointARNs = `endpoint_arn IN (?)`
pgClauseIDs = `id IN (?)`
pgClausePlatforms = `platform IN (?)`
pgClauseTokens = `token IN (?)`
pgClauseUserIDs = `user_id IN (?)`

pgOrderCreatedAt = `ORDER BY created_at DESC`
Expand Down Expand Up @@ -382,6 +383,22 @@ func convertOpts(opts QueryOptions) ([]string, []interface{}, error) {
params = append(params, ps...)
}

if len(opts.Tokens) > 0 {
ps := []interface{}{}

for _, p := range opts.Tokens {
ps = append(ps, p)
}

clause, _, err := sqlx.In(pgClauseTokens, ps)
if err != nil {
return nil, nil, err
}

clauses = append(clauses, clause)
params = append(params, ps...)
}

if len(opts.UserIDs) > 0 {
ps := []interface{}{}

Expand Down

0 comments on commit 78b5404

Please sign in to comment.