Skip to content

Commit

Permalink
Merge pull request #11 from DIMO-Network/as-3111-accounts-admin-search
Browse files Browse the repository at this point in the history
As 3111 accounts admin search
  • Loading branch information
elffjs authored Nov 7, 2024
2 parents 33a6dc0 + 9947334 commit afad688
Show file tree
Hide file tree
Showing 8 changed files with 652 additions and 13 deletions.
1 change: 1 addition & 0 deletions charts/accounts-api/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ cronJob:
env:
LOG_LEVEL: info
DB_PORT: 5432
GRPC_PORT: 8086
DB_NAME: accounts_api
PORT: 8080
JWT_KEY_SET_URL: https://auth.dev.dimo.zone/keys
Expand Down
19 changes: 19 additions & 0 deletions cmd/accounts-api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@ import (
"context"
"database/sql"
"errors"
"net"
"os"
"runtime/debug"

_ "github.com/DIMO-Network/accounts-api/docs"
"github.com/DIMO-Network/accounts-api/internal/config"
"github.com/DIMO-Network/accounts-api/internal/controller"
"github.com/DIMO-Network/accounts-api/internal/rpc"
"github.com/DIMO-Network/accounts-api/internal/services"
"github.com/DIMO-Network/accounts-api/internal/services/cio"
pb "github.com/DIMO-Network/accounts-api/pkg/grpc"
"github.com/DIMO-Network/shared"
"github.com/DIMO-Network/shared/db"
"github.com/ethereum/go-ethereum/common"
Expand All @@ -23,6 +26,7 @@ import (
"github.com/pressly/goose/v3"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/rs/zerolog"
"google.golang.org/grpc"
)

// @title DIMO Accounts API
Expand Down Expand Up @@ -152,6 +156,21 @@ func main() {

logger.Info().Msg("Server started on port " + settings.Port)

serv := grpc.NewServer()
pb.RegisterAccountsServer(serv, &rpc.Server{DBS: dbs})

lis, err := net.Listen("tcp", ":"+settings.GRPCPort)
if err != nil {
logger.Fatal().Err(err).Msgf("Failed to listen for gRPC clients on port %s.", settings.GRPCPort)
}

go func() {
err := serv.Serve(lis)
if err != nil {
logger.Fatal().Err(err).Send()
}
}()

// Start Server
if err := app.Listen(":" + settings.Port); err != nil {
logger.Fatal().Err(err).Send()
Expand Down
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ require (
github.com/volatiletech/null/v8 v8.1.2
github.com/volatiletech/sqlboiler/v4 v4.16.2
github.com/volatiletech/strmangle v0.0.8
google.golang.org/grpc v1.66.0
google.golang.org/protobuf v1.34.2
)

require (
Expand Down Expand Up @@ -114,14 +116,13 @@ require (
go.opentelemetry.io/otel/trace v1.26.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.27.0 // indirect
golang.org/x/net v0.28.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/text v0.18.0 // indirect
golang.org/x/time v0.5.0 // indirect
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect
golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240604185151-ef581f913117 // indirect
google.golang.org/grpc v1.66.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
13 changes: 2 additions & 11 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/DATA-DOG/go-sqlmock v1.4.1 h1:ThlnYciV1iM/V0OSF/dtkqWb6xo5qITT1TJBG1MRDJM=
github.com/DATA-DOG/go-sqlmock v1.4.1/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM=
github.com/DIMO-Network/shared v0.12.4 h1:gEQuJva1V0CNDXIX32n9d5daZydRUtJTTSdmy+v0YrA=
github.com/DIMO-Network/shared v0.12.4/go.mod h1:dtoa/pN19FFR/ghTsUWJynQClwvc+cIlf2mPDaUBDsE=
github.com/DIMO-Network/shared v0.12.5 h1:+e1JjbGcGwJBb26HE7BSgI+V2cYGkor7GFQb0o2FHjE=
github.com/DIMO-Network/shared v0.12.5/go.mod h1:dtoa/pN19FFR/ghTsUWJynQClwvc+cIlf2mPDaUBDsE=
github.com/DIMO-Network/yaml v0.1.0 h1:KQ3oKHUZETchR6Pxbmmol3e4ewrPv/q8cEwqxfwyZbU=
Expand Down Expand Up @@ -158,8 +156,6 @@ github.com/containerd/platforms v0.2.1/go.mod h1:XHCb+2/hzowdiut9rkudds9bE5yJ7np
github.com/coreos/go-semver v0.3.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
github.com/coreos/go-systemd/v22 v22.3.2/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/cpuguy83/dockercfg v0.3.1 h1:/FpZ+JaygUR/lZP2NlFI2DVfrOEMAIKP5wWEJdoYe9E=
github.com/cpuguy83/dockercfg v0.3.1/go.mod h1:sugsbF4//dDlL/i+S+rtpIWp+5h0BHJHfjj5/jFyUJc=
github.com/cpuguy83/dockercfg v0.3.2 h1:DlJTyZGBDlXqUZ2Dk2Q3xHs/FtnooJJVaad2S9GKorA=
github.com/cpuguy83/dockercfg v0.3.2/go.mod h1:sugsbF4//dDlL/i+S+rtpIWp+5h0BHJHfjj5/jFyUJc=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
Expand Down Expand Up @@ -534,8 +530,6 @@ github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5Fsn
github.com/prometheus/client_golang v1.4.0/go.mod h1:e9GMxYsXl05ICDXkRhurwBS4Q3OK1iX/F2sw+iXX5zU=
github.com/prometheus/client_golang v1.7.1/go.mod h1:PY5Wy2awLA44sXw4AOSfFBetzPP4j5+D6mVACh+pe2M=
github.com/prometheus/client_golang v1.11.1/go.mod h1:Z6t4BnS23TR94PD6BsDNk8yVqroYurpAkEiz0P2BEV0=
github.com/prometheus/client_golang v1.20.4 h1:Tgh3Yr67PaOv/uTqloMsCEdeuFTatm5zIq5+qNN23vI=
github.com/prometheus/client_golang v1.20.4/go.mod h1:PIEt8X02hGcP8JWbeHyeZ53Y/jReSnHgO035n//V5WE=
github.com/prometheus/client_golang v1.20.5 h1:cxppBPuYhUnsO6yo/aoRol4L7q7UFfdm+bR9r+8l63Y=
github.com/prometheus/client_golang v1.20.5/go.mod h1:PIEt8X02hGcP8JWbeHyeZ53Y/jReSnHgO035n//V5WE=
github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
Expand Down Expand Up @@ -610,6 +604,8 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
Expand All @@ -626,12 +622,8 @@ github.com/subosito/gotenv v1.3.0/go.mod h1:YzJjq/33h7nrwdY+iHMhEOEEbW0ovIz0tB6t
github.com/subosito/gotenv v1.4.1/go.mod h1:ayKnFf/c6rvx/2iiLrJUk1e6plDbT3edrFNGqEflhK0=
github.com/swaggo/files/v2 v2.0.0 h1:hmAt8Dkynw7Ssz46F6pn8ok6YmGZqHSVLZ+HQM7i0kw=
github.com/swaggo/files/v2 v2.0.0/go.mod h1:24kk2Y9NYEJ5lHuCra6iVwkMjIekMCaFq/0JQj66kyM=
github.com/swaggo/swag v1.16.3 h1:PnCYjPCah8FK4I26l2F/KQ4yz3sILcVUN3cTlBFA9Pg=
github.com/swaggo/swag v1.16.3/go.mod h1:DImHIuOFXKpMFAQjcC7FG4m3Dg4+QuUgUzJmKjI/gRk=
github.com/swaggo/swag v1.16.4 h1:clWJtd9LStiG3VeijiCfOVODP6VpHtKdQy9ELFG3s1A=
github.com/swaggo/swag v1.16.4/go.mod h1:VBsHJRsDvfYvqoiMKnsdwhNV9LEMHgEDZcyVYX0sxPg=
github.com/testcontainers/testcontainers-go v0.33.0 h1:zJS9PfXYT5O0ZFXM2xxXfk4J5UMw/kRiISng037Gxdw=
github.com/testcontainers/testcontainers-go v0.33.0/go.mod h1:W80YpTa8D5C3Yy16icheD01UTDu+LmXIA2Keo+jWtT8=
github.com/testcontainers/testcontainers-go v0.34.0 h1:5fbgF0vIN5u+nD3IWabQwRybuB4GY8G2HHgCkbMzMHo=
github.com/testcontainers/testcontainers-go v0.34.0/go.mod h1:6P/kMkQe8yqPHfPWNulFGdFHTD8HB2vLq/231xY2iPQ=
github.com/tklauser/go-sysconf v0.3.12/go.mod h1:Ho14jnntGE1fpdOqQEEaiKRpvIavV0hSfmBq8nJbHYI=
Expand All @@ -656,7 +648,6 @@ github.com/volatiletech/randomize v0.0.1/go.mod h1:GN3U0QYqfZ9FOJ67bzax1cqZ5q2xu
github.com/volatiletech/sqlboiler/v4 v4.16.2 h1:PcV2bxjE+S+GwPKCyX7/AjlY3aiTKsOEjciLhpWQImc=
github.com/volatiletech/sqlboiler/v4 v4.16.2/go.mod h1:B14BPBGTrJ2X6l7lwnvV/iXgYR48+ozGSlzHI3frl6U=
github.com/volatiletech/strmangle v0.0.1/go.mod h1:F6RA6IkB5vq0yTG4GQ0UsbbRcl3ni9P76i+JrTBKFFg=
github.com/volatiletech/strmangle v0.0.6 h1:AdOYE3B2ygRDq4rXDij/MMwq6KVK/pWAYxpC7CLrkKQ=
github.com/volatiletech/strmangle v0.0.6/go.mod h1:ycDvbDkjDvhC0NUU8w3fWwl5JEMTV56vTKXzR3GeR+0=
github.com/volatiletech/strmangle v0.0.8 h1:UZkTDFIjZcL1Lk4BXhGsxcyXxNcWuM5ZwdzZc0sJcWg=
github.com/volatiletech/strmangle v0.0.8/go.mod h1:ycDvbDkjDvhC0NUU8w3fWwl5JEMTV56vTKXzR3GeR+0=
Expand Down
80 changes: 80 additions & 0 deletions internal/rpc/accounts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package rpc

import (
"context"
"fmt"

"github.com/DIMO-Network/accounts-api/models"
pb "github.com/DIMO-Network/accounts-api/pkg/grpc"
"github.com/DIMO-Network/shared/db"
"github.com/volatiletech/sqlboiler/v4/queries/qm"
"google.golang.org/protobuf/types/known/timestamppb"
)

type Server struct {
pb.UnimplementedAccountsServer
DBS db.Store
}

var emailJoin = fmt.Sprintf("%s ON %s = %s", models.TableNames.Emails, models.EmailTableColumns.AccountID, models.AccountTableColumns.ID)
var walletJoin = fmt.Sprintf("%s ON %s = %s", models.TableNames.Wallets, models.WalletTableColumns.AccountID, models.AccountTableColumns.ID)

var emailHas = fmt.Sprintf("position(? in %s) > 0", models.EmailTableColumns.Address)
var walletHas = fmt.Sprintf("position(? in %s) > 0", models.WalletTableColumns.Address)

func (s *Server) ListAccounts(ctx context.Context, in *pb.ListAccountsRequest) (*pb.ListAccountsResponse, error) {
var mods = []qm.QueryMod{
qm.Load(models.AccountRels.Email),
qm.Load(models.AccountRels.Wallet),
qm.LeftOuterJoin(emailJoin), // TODO(elffjs): This seems a bit wasteful.
qm.LeftOuterJoin(walletJoin),
qm.OrderBy(models.AccountColumns.CreatedAt + " DESC"),
qm.Limit(100), // TODO(elffjs): Revisit.
}

if in.PartialEmailAddress != "" {
mods = append(mods, qm.Where(emailHas, in.PartialEmailAddress))
}
if len(in.PartialWalletAddress) != 0 {
mods = append(mods, qm.Where(walletHas, in.PartialEmailAddress))
}

accs, err := models.Accounts(mods...).All(ctx, s.DBS.DBS().Reader)
if err != nil {
return nil, err
}

out := &pb.ListAccountsResponse{
Accounts: make([]*pb.Account, len(accs)),
}

for i, a := range accs {
out.Accounts[i] = dbToRPC(a)
}

return out, nil
}

func dbToRPC(acc *models.Account) *pb.Account {
out := &pb.Account{
Id: acc.ID,
CreatedAt: timestamppb.New(acc.CreatedAt),
}

if acc.CountryCode.Valid {
out.CountryCode = acc.CountryCode.String
}

if acc.R.Email != nil {
out.Email = &pb.Email{
Address: acc.R.Email.Address,
}
}
if acc.R.Wallet != nil {
out.Wallet = &pb.Wallet{
Address: acc.R.Wallet.Address,
}
}

return out
}
Loading

0 comments on commit afad688

Please sign in to comment.