Skip to content

Commit

Permalink
remove organization data that is also provided by the sdk and vochain
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasmenendez committed Oct 2, 2024
1 parent bf16dd3 commit 0aca91a
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 94 deletions.
23 changes: 0 additions & 23 deletions api/organizations.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,12 @@ func (a *API) createOrganizationHandler(w http.ResponseWriter, r *http.Request)
// create the organization
if err := a.db.SetOrganization(&db.Organization{
Address: signer.AddressString(),
Name: orgInfo.Name,
Creator: user.Email,
CreatedAt: time.Now(),
Nonce: nonce,
Type: db.OrganizationType(orgInfo.Type),
Description: orgInfo.Description,
Size: orgInfo.Size,
Color: orgInfo.Color,
Logo: orgInfo.Logo,
Subdomain: orgInfo.Subdomain,
Timezone: orgInfo.Timezone,
Active: true,
Expand Down Expand Up @@ -177,14 +174,6 @@ func (a *API) updateOrganizationHandler(w http.ResponseWriter, r *http.Request)
}
// update just the fields that can be updated and are not empty
updateOrg := false
if newOrgInfo.Name != "" {
org.Name = newOrgInfo.Name
updateOrg = true
}
if newOrgInfo.Description != "" {
org.Description = newOrgInfo.Description
updateOrg = true
}
if newOrgInfo.Website != "" {
org.Website = newOrgInfo.Website
updateOrg = true
Expand All @@ -197,14 +186,6 @@ func (a *API) updateOrganizationHandler(w http.ResponseWriter, r *http.Request)
org.Color = newOrgInfo.Color
updateOrg = true
}
if newOrgInfo.Logo != "" {
org.Logo = newOrgInfo.Logo
updateOrg = true
}
if newOrgInfo.Header != "" {
org.Header = newOrgInfo.Header
updateOrg = true
}
if newOrgInfo.Subdomain != "" {
org.Subdomain = newOrgInfo.Subdomain
updateOrg = true
Expand All @@ -217,10 +198,6 @@ func (a *API) updateOrganizationHandler(w http.ResponseWriter, r *http.Request)
org.Timezone = newOrgInfo.Timezone
updateOrg = true
}
if newOrgInfo.Language != "" {
org.Language = newOrgInfo.Language
updateOrg = true
}
if newOrgInfo.Active != org.Active {
org.Active = newOrgInfo.Active
updateOrg = true
Expand Down
54 changes: 22 additions & 32 deletions api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,17 @@ import (

// Organization is the struct that represents an organization in the API
type OrganizationInfo struct {
Address string `json:"address"`
Name string `json:"name"`
Website string `json:"website"`
CreatedAt string `json:"createdAt"`
Type string `json:"type"`
Description string `json:"description"`
Size string `json:"size"`
Color string `json:"color"`
Logo string `json:"logo"`
Header string `json:"header"`
Subdomain string `json:"subdomain"`
Country string `json:"country"`
Timezone string `json:"timezone"`
Language string `json:"language"`
Active bool `json:"active"`
Parent *OrganizationInfo `json:"parent"`
Address string `json:"address"`
Website string `json:"website"`
CreatedAt string `json:"createdAt"`
Type string `json:"type"`
Size string `json:"size"`
Color string `json:"color"`
Subdomain string `json:"subdomain"`
Country string `json:"country"`
Timezone string `json:"timezone"`
Active bool `json:"active"`
Parent *OrganizationInfo `json:"parent"`
}

// OrganizationMembers is the struct that represents a list of members of
Expand Down Expand Up @@ -117,21 +112,16 @@ func organizationFromDB(dbOrg, parent *db.Organization) *OrganizationInfo {
parentOrg = organizationFromDB(parent, nil)
}
return &OrganizationInfo{
Address: dbOrg.Address,
Name: dbOrg.Name,
Website: dbOrg.Website,
CreatedAt: dbOrg.CreatedAt.Format(time.RFC3339),
Type: string(dbOrg.Type),
Description: dbOrg.Description,
Size: dbOrg.Size,
Color: dbOrg.Color,
Logo: dbOrg.Logo,
Header: dbOrg.Header,
Subdomain: dbOrg.Subdomain,
Country: dbOrg.Country,
Timezone: dbOrg.Timezone,
Language: dbOrg.Language,
Active: dbOrg.Active,
Parent: parentOrg,
Address: dbOrg.Address,
Website: dbOrg.Website,
CreatedAt: dbOrg.CreatedAt.Format(time.RFC3339),
Type: string(dbOrg.Type),
Size: dbOrg.Size,
Color: dbOrg.Color,
Subdomain: dbOrg.Subdomain,
Country: dbOrg.Country,
Timezone: dbOrg.Timezone,
Active: dbOrg.Active,
Parent: parentOrg,
}
}
8 changes: 0 additions & 8 deletions db/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,6 @@ func (ms *MongoStorage) createIndexes() error {
if _, err := ms.users.Indexes().CreateOne(ctx, userPhoneIndex); err != nil {
return fmt.Errorf("failed to create index on phone for users: %w", err)
}
// create an index for the 'name' field on organizations (must be unique)
organizationNameIndex := mongo.IndexModel{
Keys: bson.D{{Key: "name", Value: 1}}, // 1 for ascending order
Options: options.Index().SetUnique(true),
}
if _, err := ms.organizations.Indexes().CreateOne(ctx, organizationNameIndex); err != nil {
return fmt.Errorf("failed to create index on name for organizations: %w", err)
}
// create an index for the ('code', 'type') tuple on user verifications (must be unique)
verificationCodeIndex := mongo.IndexModel{
Keys: bson.D{
Expand Down
27 changes: 1 addition & 26 deletions db/organizations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ func TestOrganization(t *testing.T) {
parentAddress := "parentOrgToGet"
c.Assert(db.SetOrganization(&Organization{
Address: address,
Name: "Child Organization",
Parent: parentAddress,
}), qt.IsNil)
// test not found parent organization
Expand All @@ -32,7 +31,6 @@ func TestOrganization(t *testing.T) {
// create a new parent organization
c.Assert(db.SetOrganization(&Organization{
Address: parentAddress,
Name: "Parent Organization",
}), qt.IsNil)
// test found organization and parent organization
org, parentOrg, err = db.Organization(address, true)
Expand All @@ -52,38 +50,25 @@ func TestSetOrganization(t *testing.T) {
c := qt.New(t)
// create a new organization
address := "orgToSet"
orgName := "Organization"
c.Assert(db.SetOrganization(&Organization{
Address: address,
Name: orgName,
}), qt.IsNil)
org, _, err := db.Organization(address, false)
c.Assert(err, qt.IsNil)
c.Assert(org, qt.Not(qt.IsNil))
c.Assert(org.Address, qt.Equals, address)
c.Assert(org.Name, qt.Equals, orgName)
// update the organization
orgName = "New Organization"
c.Assert(db.SetOrganization(&Organization{
Address: address,
Name: orgName,
}), qt.IsNil)
org, _, err = db.Organization(address, false)
c.Assert(err, qt.IsNil)
c.Assert(org, qt.Not(qt.IsNil))
c.Assert(org.Address, qt.Equals, address)
c.Assert(org.Name, qt.Equals, orgName)
// try to create a new organization with the same name
newOrgAddress := "newOrgToSet"
c.Assert(db.SetOrganization(&Organization{
Address: newOrgAddress,
Name: orgName,
}), qt.IsNotNil)
// try to create a new organization with a not found creator
newOrgName := "New Organization 2"
newOrgAddress := "newOrgToSet"
c.Assert(db.SetOrganization(&Organization{
Address: newOrgAddress,
Name: newOrgName,
Creator: testUserEmail,
}), qt.IsNotNil)
// register the creator and retry to create the organization
Expand All @@ -94,7 +79,6 @@ func TestSetOrganization(t *testing.T) {
c.Assert(err, qt.IsNil)
c.Assert(db.SetOrganization(&Organization{
Address: newOrgAddress,
Name: newOrgName,
Creator: testUserEmail,
}), qt.IsNil)
}
Expand All @@ -108,16 +92,13 @@ func TestDeleteOrganization(t *testing.T) {
c := qt.New(t)
// create a new organization and delete it
address := "orgToDelete"
name := "Organization to delete"
c.Assert(db.SetOrganization(&Organization{
Address: address,
Name: name,
}), qt.IsNil)
org, _, err := db.Organization(address, false)
c.Assert(err, qt.IsNil)
c.Assert(org, qt.Not(qt.IsNil))
c.Assert(org.Address, qt.Equals, address)
c.Assert(org.Name, qt.Equals, name)
// delete the organization
c.Assert(db.DelOrganization(org), qt.IsNil)
// check the organization doesn't exist
Expand All @@ -135,22 +116,19 @@ func TestReplaceCreatorEmail(t *testing.T) {
c := qt.New(t)
// create a new organization with a creator
address := "orgToReplaceCreator"
name := "Organization to replace creator"
_, err := db.SetUser(&User{
Email: testUserEmail,
Password: testUserPass,
})
c.Assert(err, qt.IsNil)
c.Assert(db.SetOrganization(&Organization{
Address: address,
Name: name,
Creator: testUserEmail,
}), qt.IsNil)
org, _, err := db.Organization(address, false)
c.Assert(err, qt.IsNil)
c.Assert(org, qt.Not(qt.IsNil))
c.Assert(org.Address, qt.Equals, address)
c.Assert(org.Name, qt.Equals, name)
c.Assert(org.Creator, qt.Equals, testUserEmail)
// replace the creator email
newCreator := "[email protected]"
Expand All @@ -159,7 +137,6 @@ func TestReplaceCreatorEmail(t *testing.T) {
c.Assert(err, qt.IsNil)
c.Assert(org, qt.Not(qt.IsNil))
c.Assert(org.Address, qt.Equals, address)
c.Assert(org.Name, qt.Equals, name)
c.Assert(org.Creator, qt.Equals, newCreator)
}

Expand All @@ -172,15 +149,13 @@ func TestOrganizationsMembers(t *testing.T) {
c := qt.New(t)
// create a new organization with a creator
address := "orgToReplaceCreator"
name := "Organization to replace creator"
_, err := db.SetUser(&User{
Email: testUserEmail,
Password: testUserPass,
})
c.Assert(err, qt.IsNil)
c.Assert(db.SetOrganization(&Organization{
Address: address,
Name: name,
Creator: testUserEmail,
}), qt.IsNil)
_, _, err = db.Organization(address, false)
Expand Down
5 changes: 0 additions & 5 deletions db/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,16 @@ type OrganizationMember struct {

type Organization struct {
Address string `json:"address" bson:"_id"`
Name string `json:"name" bson:"name"`
Website string `json:"website" bson:"website"`
Type OrganizationType `json:"type" bson:"type"`
Creator string `json:"creator" bson:"creator"`
CreatedAt time.Time `json:"createdAt" bson:"createdAt"`
Nonce string `json:"nonce" bson:"nonce"`
Description string `json:"description" bson:"description"`
Size string `json:"size" bson:"size"`
Color string `json:"color" bson:"color"`
Logo string `json:"logo" bson:"logo"`
Header string `json:"header" bson:"header"`
Subdomain string `json:"subdomain" bson:"subdomain"`
Country string `json:"country" bson:"country"`
Timezone string `json:"timezone" bson:"timezone"`
Language string `json:"language" bson:"language"`
Active bool `json:"active" bson:"active"`
TokensPurchased uint64 `json:"tokensPurchased" bson:"tokensPurchased"`
TokensRemaining uint64 `json:"tokensRemaining" bson:"tokensRemaining"`
Expand Down

0 comments on commit 0aca91a

Please sign in to comment.