Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix double device registration #32

Merged
merged 2 commits into from
Dec 9, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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