Skip to content

Commit

Permalink
refactor(backend): change name of struct
Browse files Browse the repository at this point in the history
  • Loading branch information
IgnisDa committed Oct 16, 2023
1 parent 8bad8a9 commit e4b79c3
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 47 deletions.
19 changes: 10 additions & 9 deletions backend/cmd/user/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package user
import (
"errors"
"fmt"
"github.com/gofrs/uuid"
"time"

"github.com/gofrs/uuid"
)

// ImportEmail The import format for a user's email
type ImportEmail struct {
// ImportOrExportEmail The import/export format for a user's email
type ImportOrExportEmail struct {
// Address Valid email address
Address string `json:"address" yaml:"address"`
// IsPrimary indicates if this is the primary email of the users. In the Emails array there has to be exactly one primary email.
Expand All @@ -18,10 +19,10 @@ type ImportEmail struct {
}

// Emails Array of email addresses
type Emails []ImportEmail
type Emails []ImportOrExportEmail

// ImportEntry represents a user to be imported to the Hanko database
type ImportEntry struct {
// ImportOrExportEntry represents a user to be imported/export to the Hanko database
type ImportOrExportEntry struct {
// UserID optional uuid.v4. If not provided a new one will be generated for the user
UserID string `json:"user_id" yaml:"user_id"`
// Emails List of emails
Expand All @@ -32,10 +33,10 @@ type ImportEntry struct {
UpdatedAt *time.Time `json:"updated_at" yaml:"updated_at"`
}

// ImportList a list of ImportEntries
type ImportList []ImportEntry
// ImportOrExportList a list of ImportEntries
type ImportOrExportList []ImportOrExportEntry

func (entry *ImportEntry) validate() error {
func (entry *ImportOrExportEntry) validate() error {
if len(entry.Emails) == 0 {
return errors.New(fmt.Sprintf("Entry with id: %v has got no Emails.", entry.UserID))
}
Expand Down
17 changes: 9 additions & 8 deletions backend/cmd/user/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package user

import (
"fmt"
"github.com/stretchr/testify/assert"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

const validUUID = "62418053-a2cd-47a8-9b61-4426380d263a"
Expand All @@ -27,7 +28,7 @@ func TestImportEntry_validate(t *testing.T) {
fields: fields{
UserID: "",
Emails: Emails{
ImportEmail{
ImportOrExportEmail{
Address: "primary@hanko.io",
IsPrimary: true,
IsVerified: false,
Expand All @@ -43,7 +44,7 @@ func TestImportEntry_validate(t *testing.T) {
fields: fields{
UserID: validUUID,
Emails: Emails{
ImportEmail{
ImportOrExportEmail{
Address: "primary@hanko.io",
IsPrimary: true,
IsVerified: false,
Expand All @@ -59,7 +60,7 @@ func TestImportEntry_validate(t *testing.T) {
fields: fields{
UserID: invalidUUID,
Emails: Emails{
ImportEmail{
ImportOrExportEmail{
Address: "primary@hanko.io",
IsPrimary: true,
IsVerified: false,
Expand All @@ -85,7 +86,7 @@ func TestImportEntry_validate(t *testing.T) {
fields: fields{
UserID: "",
Emails: Emails{
ImportEmail{
ImportOrExportEmail{
Address: "primary@hanko.io",
IsPrimary: false,
IsVerified: false,
Expand All @@ -101,12 +102,12 @@ func TestImportEntry_validate(t *testing.T) {
fields: fields{
UserID: "",
Emails: Emails{
ImportEmail{
ImportOrExportEmail{
Address: "primary@hanko.io",
IsPrimary: true,
IsVerified: false,
},
ImportEmail{
ImportOrExportEmail{
Address: "primary2@hanko.io",
IsPrimary: true,
IsVerified: false,
Expand All @@ -120,7 +121,7 @@ func TestImportEntry_validate(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
entry := &ImportEntry{
entry := &ImportOrExportEntry{
UserID: tt.fields.UserID,
Emails: tt.fields.Emails,
CreatedAt: tt.fields.CreatedAt,
Expand Down
13 changes: 7 additions & 6 deletions backend/cmd/user/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package user

import (
"encoding/json"
"github.com/brianvoe/gofakeit/v6"
"github.com/gofrs/uuid"
"github.com/spf13/cobra"
"log"
"os"
"time"

"github.com/brianvoe/gofakeit/v6"
"github.com/gofrs/uuid"
"github.com/spf13/cobra"
)

var outputFile string
Expand Down Expand Up @@ -36,18 +37,18 @@ func NewGenerateCommand() *cobra.Command {
}

func generate() error {
var entries []ImportEntry
var entries []ImportOrExportEntry
for i := 0; i < count; i++ {
now := time.Now().UTC()
id, _ := uuid.NewV4()
emails := []ImportEmail{
emails := []ImportOrExportEmail{
{
Address: gofakeit.Email(),
IsPrimary: true,
IsVerified: true,
},
}
entry := ImportEntry{
entry := ImportOrExportEntry{
UserID: id.String(),
Emails: emails,
CreatedAt: &now,
Expand Down
8 changes: 4 additions & 4 deletions backend/cmd/user/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func NewImportCommand() *cobra.Command {

// loadAndValidate reads json from an io.Reader so we read every entry separate and validate it. We go through the whole
// array to print out every validation error in the input data.
func loadAndValidate(input io.Reader) ([]ImportEntry, error) {
func loadAndValidate(input io.Reader) ([]ImportOrExportEntry, error) {
dec := json.NewDecoder(input)

// read the open bracket
Expand All @@ -113,14 +113,14 @@ func loadAndValidate(input io.Reader) ([]ImportEntry, error) {
return nil, err
}

users := []ImportEntry{}
users := []ImportOrExportEntry{}

numErrors := 0
index := 0
// while the array contains values
for dec.More() {
index = index + 1
var userEntry ImportEntry
var userEntry ImportOrExportEntry
// decode one ImportEntry
err := dec.Decode(&userEntry)
if err != nil {
Expand Down Expand Up @@ -153,7 +153,7 @@ func loadAndValidate(input io.Reader) ([]ImportEntry, error) {
}

// commits the list of ImportEntries to the database. Wrapped in a transaction so if something fails no new users are added.
func addToDatabase(entries []ImportEntry, persister persistence.Persister) error {
func addToDatabase(entries []ImportOrExportEntry, persister persistence.Persister) error {
tx := persister.GetConnection()
err := tx.Transaction(func(tx *pop.Connection) error {
for i, v := range entries {
Expand Down
37 changes: 19 additions & 18 deletions backend/cmd/user/import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ package user

import (
"fmt"
"github.com/gofrs/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"github.com/teamhanko/hanko/backend/persistence"
"github.com/teamhanko/hanko/backend/test"
"io"
"log"
"strings"
"testing"
"time"

"github.com/gofrs/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"github.com/teamhanko/hanko/backend/persistence"
"github.com/teamhanko/hanko/backend/test"
)

const validUUID2 = "799e95f0-4cc7-4bd7-9f01-5fdc4fa26ea3"
Expand All @@ -33,7 +34,7 @@ func (s *importSuite) Test_loadAndValidate() {
tests := []struct {
name string
args args
want []ImportEntry
want []ImportOrExportEntry
wantErr assert.ErrorAssertionFunc
}{
{
Expand All @@ -42,7 +43,7 @@ func (s *importSuite) Test_loadAndValidate() {
input: strings.NewReader("[]"),
},
wantErr: assert.NoError,
want: []ImportEntry{},
want: []ImportOrExportEntry{},
},
{
name: "empty file -> nil result",
Expand All @@ -58,11 +59,11 @@ func (s *importSuite) Test_loadAndValidate() {
input: strings.NewReader("[{\"user_id\":\"799e95f0-4cc7-4bd7-9f01-5fdc4fa26ea3\",\"emails\":[{\"address\":\"koreyrath@wolff.name\",\"is_primary\":true,\"is_verified\":true}],\"created_at\":\"2023-06-07T13:42:49.369489Z\",\"updated_at\":\"2023-06-07T13:42:49.369489Z\"}]\n"),
},
wantErr: assert.NoError,
want: []ImportEntry{
want: []ImportOrExportEntry{
{
UserID: validUUID2,
Emails: Emails{
ImportEmail{
ImportOrExportEmail{
Address: "koreyrath@wolff.name",
IsPrimary: true,
IsVerified: true,
Expand Down Expand Up @@ -107,7 +108,7 @@ func (s *importSuite) Test_addToDatabase() {
}

type args struct {
entries []ImportEntry
entries []ImportOrExportEntry
persister persistence.Persister
}
tests := []struct {
Expand All @@ -119,11 +120,11 @@ func (s *importSuite) Test_addToDatabase() {
{
name: "Positive",
args: args{
entries: []ImportEntry{
entries: []ImportOrExportEntry{
{
UserID: "",
Emails: Emails{
ImportEmail{
ImportOrExportEmail{
Address: "primary@hanko.io",
IsPrimary: true,
IsVerified: false,
Expand All @@ -141,11 +142,11 @@ func (s *importSuite) Test_addToDatabase() {
{
name: "Double uuid",
args: args{
entries: []ImportEntry{
entries: []ImportOrExportEntry{
{
UserID: validUUID,
Emails: Emails{
ImportEmail{
ImportOrExportEmail{
Address: "primary1@hanko.io",
IsPrimary: true,
IsVerified: false,
Expand All @@ -157,7 +158,7 @@ func (s *importSuite) Test_addToDatabase() {
{
UserID: validUUID,
Emails: Emails{
ImportEmail{
ImportOrExportEmail{
Address: "primary2@hanko.io",
IsPrimary: true,
IsVerified: false,
Expand All @@ -175,11 +176,11 @@ func (s *importSuite) Test_addToDatabase() {
{
name: "Double primary email",
args: args{
entries: []ImportEntry{
entries: []ImportOrExportEntry{
{
UserID: validUUID,
Emails: Emails{
ImportEmail{
ImportOrExportEmail{
Address: "primary@hanko.io",
IsPrimary: true,
IsVerified: false,
Expand All @@ -191,7 +192,7 @@ func (s *importSuite) Test_addToDatabase() {
{
UserID: validUUID,
Emails: Emails{
ImportEmail{
ImportOrExportEmail{
Address: "primary@hanko.io",
IsPrimary: true,
IsVerified: false,
Expand Down
5 changes: 3 additions & 2 deletions backend/json_schema/schema_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@ package main
import (
"encoding/json"
"fmt"
"os"

"github.com/invopop/jsonschema"
"github.com/teamhanko/hanko/backend/cmd/user"
"github.com/teamhanko/hanko/backend/config"
"os"
)

func main() {
if err := generateSchema("./config", "./json_schema/hanko.config.json", &config.Config{}); err != nil {
panic(err)
}
if err := generateSchema("./cmd/user", "./json_schema/hanko.user_import.json", &user.ImportList{}); err != nil {
if err := generateSchema("./cmd/user", "./json_schema/hanko.user_import.json", &user.ImportOrExportList{}); err != nil {
panic(err)
}

Expand Down
15 changes: 15 additions & 0 deletions backend/persistence/user_persister.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"database/sql"
"errors"
"fmt"

"github.com/gobuffalo/pop/v6"
"github.com/gofrs/uuid"
"github.com/teamhanko/hanko/backend/persistence/models"
Expand All @@ -15,6 +16,7 @@ type UserPersister interface {
Update(models.User) error
Delete(models.User) error
List(page int, perPage int, userId uuid.UUID, email string, sortDirection string) ([]models.User, error)
All() ([]models.User, error)
Count(userId uuid.UUID, email string) (int, error)
}

Expand All @@ -26,6 +28,19 @@ func NewUserPersister(db *pop.Connection) UserPersister {
return &userPersister{db: db}
}

func (p *userPersister) All() ([]models.User, error) {
users := []models.User{}
err := p.db.EagerPreload("Emails", "Emails.PrimaryEmail", "Emails.Identity", "WebauthnCredentials").All(&users)
if err != nil && errors.Is(err, sql.ErrNoRows) {
return users, nil
}
if err != nil {
return nil, fmt.Errorf("failed to fetch users: %w", err)
}

return users, nil
}

func (p *userPersister) Get(id uuid.UUID) (*models.User, error) {
user := models.User{}
err := p.db.EagerPreload("Emails", "Emails.PrimaryEmail", "Emails.Identity", "WebauthnCredentials").Find(&user, id)
Expand Down

0 comments on commit e4b79c3

Please sign in to comment.