Skip to content
This repository has been archived by the owner on Aug 11, 2023. It is now read-only.

Commit

Permalink
Merge pull request #24 from np-inprove/Sherlena/Insitution_DB_fix
Browse files Browse the repository at this point in the history
feat: Groups CRUD
  • Loading branch information
C4RR0T02 authored Jul 10, 2023
2 parents 3c832c0 + 0276f9a commit 20f649f
Show file tree
Hide file tree
Showing 30 changed files with 1,780 additions and 98 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
.vscode
config.yaml
*.db
mockery.*
mockery
2 changes: 1 addition & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func main() {
logger.String("area", "group"),
)

groupRepo := group.NewEntRepository(client)
groupRepo := group.NewEntRepository(appLogger, client)
groupService := group.NewUseCase(groupRepo)
groupHandler := group.NewHTTPHandler(groupService, cfg, tokenAuth)

Expand Down
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ github.com/gookit/validate v1.4.6 h1:Ix8NRy2+6z4YGHWXgZL9+emy9wRI2GWyhW2smPcIlSU
github.com/gookit/validate v1.4.6/go.mod h1:1rjeYaYlMK/8od4oge5C+Gt/3DnHkXymLPda7+3urC8=
github.com/hashicorp/hcl/v2 v2.17.0 h1:z1XvSUyXd1HP10U4lrLg5e0JMVz6CPaJvAgxM0KNZVY=
github.com/hashicorp/hcl/v2 v2.17.0/go.mod h1:gJyW2PTShkJqQBKpAmPO3yxMxIuoXkOF2TpqXzrQyx4=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/knadh/koanf/maps v0.1.1 h1:G5TjmUh2D7G2YWf5SQQqSiHRJEjaicvU0KpypqB3NIs=
github.com/knadh/koanf/maps v0.1.1/go.mod h1:npD/QZY3V6ghQDdcQzl1W4ICNVTkohC8E73eI2xW4yI=
github.com/knadh/koanf/parsers/yaml v0.1.0 h1:ZZ8/iGfRLvKSaMEECEBPM1HQslrZADk8fP1XFUxVI5w=
Expand Down Expand Up @@ -113,6 +114,7 @@ github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s=
github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8=
github.com/muesli/termenv v0.15.2 h1:GohcuySI0QmI3wN8Ok9PtKGkgkFIk7y6Vpb5PvrY+Wo=
github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1noLXviQ8=
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand All @@ -126,6 +128,8 @@ github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTE
github.com/segmentio/asm v1.2.0 h1:9BQrFxC+YOHJlTlHGkTrFWf59nbL3XnCoFLTwDCI7ys=
github.com/segmentio/asm v1.2.0/go.mod h1:BqMnlJP91P8d+4ibuonYZw9mfnzI9HfxselHZr5aAcs=
github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ=
github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/stretchr/objx v0.1.0/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 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
Expand Down Expand Up @@ -204,6 +208,7 @@ golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGm
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/tools v0.8.1-0.20230428195545-5283a0178901 h1:0wxTF6pSjIIhNt7mo9GvjDfzyCOiWhmICgtO/Ah948s=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
Expand Down
6 changes: 6 additions & 0 deletions internal/auth/httphandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,12 @@ func (h httpHandler) WhoAmI(w http.ResponseWriter, r *http.Request) {
PointsAwardedResetTime: user.PointsAwardedResetTime,
GodMode: user.GodMode,
Role: user.Role,
Institution: payload.Institution{
ID: user.Edges.Institution.ID,
Name: user.Edges.Institution.Name,
ShortName: user.Edges.Institution.ShortName,
Description: user.Edges.Institution.Description,
},
})
}

Expand Down
20 changes: 10 additions & 10 deletions internal/auth/mocks/mock_reader.go

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

20 changes: 10 additions & 10 deletions internal/auth/mocks/mock_repository.go

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

2 changes: 1 addition & 1 deletion internal/auth/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

type Reader interface {
FindUserByEmail(ctx context.Context, email string) (*entity.User, error)
FindUserByEmailWithInstitution(ctx context.Context, email string) (*entity.User, error)
FindInstitutionInviteLinkWithInstitution(ctx context.Context, code string) (*entity.InstitutionInviteLink, error)
FindJWTRevocation(ctx context.Context, jti string) (*entity.JWTRevocation, error)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/auth/repository_ent.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ func NewEntRepository(ent *ent.Client) Repository {
return entRepository{ent}
}

func (e entRepository) FindUserByEmail(ctx context.Context, email string) (*entity.User, error) {
u, err := e.client.User.Query().Where(entuser.Email(email)).Only(ctx)
func (e entRepository) FindUserByEmailWithInstitution(ctx context.Context, email string) (*entity.User, error) {
u, err := e.client.User.Query().Where(entuser.Email(email)).WithInstitution().Only(ctx)
if err != nil {
return nil, fmt.Errorf("failed to find user with email: %w", err)
}
Expand Down
8 changes: 6 additions & 2 deletions internal/auth/usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,20 @@ func (u useCase) WhoAmI(ctx context.Context, token jwt.Token) (*entity.User, err
return nil, fmt.Errorf("failed to find jwt revocation: %w", err)
}

user, err := u.repo.FindUserByEmail(ctx, token.Subject())
user, err := u.repo.FindUserByEmailWithInstitution(ctx, token.Subject())
if err != nil {
return nil, fmt.Errorf("failed to find user: %w", err)
}

if user.Edges.Institution == nil {
return nil, fmt.Errorf("invariant")
}

return user, nil
}

func (u useCase) Login(ctx context.Context, email string, password string) (*entity.Session, error) {
user, err := u.repo.FindUserByEmail(ctx, email)
user, err := u.repo.FindUserByEmailWithInstitution(ctx, email)
if err != nil {
if apperror.IsNotFound(err) {
return nil, ErrUserNotFound
Expand Down
16 changes: 8 additions & 8 deletions internal/auth/usecase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func (suite *UseCaseTestSuite) TestWhoAmI() {
configure: func(repository *mocks.MockRepository) {
repository.On("FindJWTRevocation", mock.Anything, mock.Anything).
Return(&entity.JWTRevocation{}, fixture.RepoNotFoundErr)
repository.On("FindUserByEmail", mock.Anything, mock.Anything).
repository.On("FindUserByEmailWithInstitution", mock.Anything, mock.Anything).
Return(nil, fixture.RepoNotFoundErr)
},
want: nil,
Expand All @@ -136,10 +136,10 @@ func (suite *UseCaseTestSuite) TestWhoAmI() {
configure: func(repository *mocks.MockRepository) {
repository.On("FindJWTRevocation", mock.Anything, mock.Anything).
Return(&entity.JWTRevocation{}, fixture.RepoNotFoundErr)
repository.On("FindUserByEmail", mock.Anything, mock.Anything).
Return(&entity.User{}, nil)
repository.On("FindUserByEmailWithInstitution", mock.Anything, mock.Anything).
Return(fixture.UserJohn, nil)
},
want: &entity.User{},
want: fixture.UserJohn,
err: nil,
},
}
Expand Down Expand Up @@ -178,7 +178,7 @@ func (suite *UseCaseTestSuite) TestLogin() {
password: "example",
},
configure: func(repository *mocks.MockRepository) {
repository.On("FindUserByEmail", mock.Anything, mock.Anything).
repository.On("FindUserByEmailWithInstitution", mock.Anything, mock.Anything).
Return(nil, fixture.RepoNotFoundErr)
},
assert: func(ret *entity.Session) {
Expand All @@ -194,7 +194,7 @@ func (suite *UseCaseTestSuite) TestLogin() {
password: fixture.UserJohnPassword,
},
configure: func(repository *mocks.MockRepository) {
repository.On("FindUserByEmail", mock.Anything, mock.Anything).
repository.On("FindUserByEmailWithInstitution", mock.Anything, mock.Anything).
Return(nil, fixture.RepoInternalErr)
},
assert: func(ret *entity.Session) {
Expand All @@ -210,7 +210,7 @@ func (suite *UseCaseTestSuite) TestLogin() {
password: "wrong password",
},
configure: func(repository *mocks.MockRepository) {
repository.On("FindUserByEmail", mock.Anything, mock.Anything).
repository.On("FindUserByEmailWithInstitution", mock.Anything, mock.Anything).
Return(fixture.UserJohn, nil)
},
assert: func(ret *entity.Session) {
Expand All @@ -226,7 +226,7 @@ func (suite *UseCaseTestSuite) TestLogin() {
password: fixture.UserJohnPassword,
},
configure: func(repository *mocks.MockRepository) {
repository.On("FindUserByEmail", mock.Anything, mock.Anything).
repository.On("FindUserByEmailWithInstitution", mock.Anything, mock.Anything).
Return(fixture.UserJohn, nil)
},
assert: func(ret *entity.Session) {
Expand Down
2 changes: 2 additions & 0 deletions internal/ent/institution/institution.go

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

18 changes: 18 additions & 0 deletions internal/ent/institution_create.go

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

16 changes: 16 additions & 0 deletions internal/ent/institution_update.go

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

10 changes: 5 additions & 5 deletions internal/ent/migrate/schema.go

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

Loading

0 comments on commit 20f649f

Please sign in to comment.