diff --git a/api/crm/spec.json b/api/crm/spec.json index 9e646eb0d..6c21cb91c 100644 --- a/api/crm/spec.json +++ b/api/crm/spec.json @@ -6,29 +6,7 @@ "entrypoint": "field", "path": "/field", "authentication": [], - "struct": [ - { - "name": "Field", - "fields": [ - { - "name": "Name", - "type": "string", - "db": "field_name" - }, - { - "name": "Type", - "type": "string", - "db": "field_type" - }, - { - "name": "Template", - "type": "string", - "db": "field_template", - "omitempty": true - } - ] - } - ], + "struct": [], "apis": [ { "name": "list", @@ -259,71 +237,9 @@ "authentication": [], "struct": [ { - "name": "Module", "imports": [ - "sqlxTypes github.com/jmoiron/sqlx/types" - ], - "fields": [ - { - "name": "ID", - "type": "uint64" - }, - { - "name": "Name", - "type": "string" - }, - { - "name": "Fields", - "type": "sqlxTypes.JSONText", - "json": "fields", - "db": "json" - } - ] - }, - { - "name": "ModuleField", - "fields": [ - { - "name": "Name", - "type": "string" - }, - { - "name": "Title", - "type": "string" - }, - { - "name": "Kind", - "type": "string" - }, - { - "name": "GDPR", - "type": "bool" - }, - { - "name": "Show", - "type": "bool" - } - ] - }, - { - "name": "Content", - "imports": [ - "sqlxTypes github.com/jmoiron/sqlx/types" - ], - "fields": [ - { - "name": "ID", - "type": "uint64" - }, - { - "name": "moduleID", - "type": "uint64" - }, - { - "name": "Fields", - "type": "sqlxTypes.JSONText", - "db": "json" - } + "sqlxTypes github.com/jmoiron/sqlx/types", + "github.com/crusttech/crust/crm/types" ] } ], @@ -358,7 +274,7 @@ "title": "Module Name" }, { - "type": "sqlxTypes.JSONText", + "type": "types.ModuleFieldSet", "name": "fields", "required": true, "title": "Fields JSON" @@ -482,7 +398,7 @@ "title": "Module Name" }, { - "type": "sqlxTypes.JSONText", + "type": "types.ModuleFieldSet", "name": "fields", "required": true, "title": "Fields JSON" diff --git a/api/crm/spec/field.json b/api/crm/spec/field.json index f63893c0d..a66bc619e 100644 --- a/api/crm/spec/field.json +++ b/api/crm/spec/field.json @@ -3,29 +3,7 @@ "Description": "CRM input field definitions", "Package": "crm", "Interface": "Field", - "Struct": [ - { - "fields": [ - { - "db": "field_name", - "name": "Name", - "type": "string" - }, - { - "db": "field_type", - "name": "Type", - "type": "string" - }, - { - "db": "field_template", - "name": "Template", - "omitempty": true, - "type": "string" - } - ], - "name": "Field" - } - ], + "Struct": [], "Parameters": null, "Protocol": "", "Authentication": [], diff --git a/api/crm/spec/module.json b/api/crm/spec/module.json index 3945210f7..8287993d2 100644 --- a/api/crm/spec/module.json +++ b/api/crm/spec/module.json @@ -5,72 +5,10 @@ "Interface": "Module", "Struct": [ { - "fields": [ - { - "name": "ID", - "type": "uint64" - }, - { - "name": "Name", - "type": "string" - }, - { - "db": "json", - "json": "fields", - "name": "Fields", - "type": "sqlxTypes.JSONText" - } - ], "imports": [ - "sqlxTypes github.com/jmoiron/sqlx/types" - ], - "name": "Module" - }, - { - "fields": [ - { - "name": "Name", - "type": "string" - }, - { - "name": "Title", - "type": "string" - }, - { - "name": "Kind", - "type": "string" - }, - { - "name": "GDPR", - "type": "bool" - }, - { - "name": "Show", - "type": "bool" - } - ], - "name": "ModuleField" - }, - { - "fields": [ - { - "name": "ID", - "type": "uint64" - }, - { - "name": "moduleID", - "type": "uint64" - }, - { - "db": "json", - "name": "Fields", - "type": "sqlxTypes.JSONText" - } - ], - "imports": [ - "sqlxTypes github.com/jmoiron/sqlx/types" - ], - "name": "Content" + "sqlxTypes github.com/jmoiron/sqlx/types", + "github.com/crusttech/crust/crm/types" + ] } ], "Parameters": null, @@ -111,7 +49,7 @@ "name": "fields", "required": true, "title": "Fields JSON", - "type": "sqlxTypes.JSONText" + "type": "types.ModuleFieldSet" } ] } @@ -235,7 +173,7 @@ "name": "fields", "required": true, "title": "Fields JSON", - "type": "sqlxTypes.JSONText" + "type": "types.ModuleFieldSet" } ] } diff --git a/crm/rest/request/module.go b/crm/rest/request/module.go index 720470412..19474e2e4 100644 --- a/crm/rest/request/module.go +++ b/crm/rest/request/module.go @@ -25,6 +25,7 @@ import ( "github.com/go-chi/chi" "github.com/pkg/errors" + "github.com/crusttech/crust/crm/types" sqlxTypes "github.com/jmoiron/sqlx/types" ) @@ -80,7 +81,7 @@ var _ RequestFiller = NewModuleList() // Module create request parameters type ModuleCreate struct { Name string - Fields sqlxTypes.JSONText + Fields types.ModuleFieldSet } func NewModuleCreate() *ModuleCreate { @@ -118,12 +119,6 @@ func (m *ModuleCreate) Fill(r *http.Request) (err error) { m.Name = val } - if val, ok := post["fields"]; ok { - - if m.Fields, err = parseJSONTextWithErr(val); err != nil { - return err - } - } return err } @@ -270,7 +265,7 @@ var _ RequestFiller = NewModuleChart() type ModuleEdit struct { ModuleID uint64 `json:",string"` Name string - Fields sqlxTypes.JSONText + Fields types.ModuleFieldSet } func NewModuleEdit() *ModuleEdit { @@ -309,12 +304,6 @@ func (m *ModuleEdit) Fill(r *http.Request) (err error) { m.Name = val } - if val, ok := post["fields"]; ok { - - if m.Fields, err = parseJSONTextWithErr(val); err != nil { - return err - } - } return err } diff --git a/crm/service/content_test.go b/crm/service/content_test.go index 80a0c519f..d1f4cdcc0 100644 --- a/crm/service/content_test.go +++ b/crm/service/content_test.go @@ -2,9 +2,10 @@ package service import ( "context" - "encoding/json" "testing" + "encoding/json" + "github.com/pkg/errors" "github.com/crusttech/crust/crm/types" @@ -32,26 +33,23 @@ func TestContent(t *testing.T) { ctx := auth.SetIdentityToContext(context.Background(), auth.NewIdentity(user.Identity())) repository := Content().With(ctx) - fields, err := json.Marshal([]types.Field{ - types.Field{ - Name: "name", - Type: "input", - }, - types.Field{ - Name: "email", - Type: "email", - }, - types.Field{ - Name: "options", - Type: "select_multi", - }, - }) - assert(t, err == nil, "Error when encoding JSON fields: %+v", err) - module := &types.Module{ Name: "Test", + Fields: []types.ModuleField{ + types.ModuleField{ + Name: "name", + Kind: "input", + }, + types.ModuleField{ + Name: "email", + Kind: "email", + }, + types.ModuleField{ + Name: "options", + Kind: "select_multi", + }, + }, } - (&module.Fields).Scan(fields) // set up a module { diff --git a/crm/service/module.go b/crm/service/module.go index 8b585809f..4a61e0261 100644 --- a/crm/service/module.go +++ b/crm/service/module.go @@ -3,6 +3,7 @@ package service import ( "context" + "github.com/pkg/errors" "github.com/titpetric/factory" "github.com/crusttech/crust/crm/repository" @@ -69,10 +70,19 @@ func (s *module) Chart(r *request.ModuleChart) (interface{}, error) { } func (s *module) Create(mod *types.Module) (*types.Module, error) { + if len(mod.Fields) == 0 { + return nil, errors.New("Error creating module: no fields") + } return s.moduleRepo.Create(mod) } func (s *module) Update(mod *types.Module) (*types.Module, error) { + if mod.ID == 0 { + return nil, errors.New("Error updating module: invalid ID") + } + if len(mod.Fields) == 0 { + return nil, errors.New("Error updating module: no fields") + } return s.moduleRepo.Update(mod) } diff --git a/crm/service/module_test.go b/crm/service/module_test.go index 766df3db5..a79780e6f 100644 --- a/crm/service/module_test.go +++ b/crm/service/module_test.go @@ -2,7 +2,6 @@ package service import ( "context" - "encoding/json" "testing" "github.com/crusttech/crust/crm/types" @@ -11,31 +10,28 @@ import ( func TestModule(t *testing.T) { repository := Module().With(context.Background()) - fields, err := json.Marshal([]types.Field{ - types.Field{ - Name: "name", - Type: "input", - }, - types.Field{ - Name: "email", - Type: "email", - }, - types.Field{ - Name: "options", - Type: "select_multi", - }, - types.Field{ - Name: "description", - Type: "text", - }, - }) - assert(t, err == nil, "Error when encoding JSON fields: %+v", err) - // the module object we're working with module := &types.Module{ Name: "Test", + Fields: []types.ModuleField{ + types.ModuleField{ + Name: "name", + Kind: "input", + }, + types.ModuleField{ + Name: "email", + Kind: "email", + }, + types.ModuleField{ + Name: "options", + Kind: "select_multi", + }, + types.ModuleField{ + Name: "description", + Kind: "text", + }, + }, } - (&module.Fields).Scan(fields) prevModuleCount := 0 @@ -57,6 +53,7 @@ func TestModule(t *testing.T) { assert(t, err == nil, "Error when retrieving module by id: %+v", err) assert(t, ms.ID == m.ID, "Expected ID from database to match, %d != %d", m.ID, ms.ID) assert(t, ms.Name == m.Name, "Expected Name from database to match, %s != %s", m.Name, ms.Name) + assert(t, len(ms.Fields) == 4, "Expected Fields count from database to match, 4 != %d", len(ms.Fields)) } // update created module diff --git a/docs/crm/README.md b/docs/crm/README.md index ad8110add..636870319 100644 --- a/docs/crm/README.md +++ b/docs/crm/README.md @@ -63,7 +63,7 @@ CRM module definitions | Parameter | Type | Method | Description | Default | Required? | | --------- | ---- | ------ | ----------- | ------- | --------- | | name | string | POST | Module Name | N/A | YES | -| fields | sqlxTypes.JSONText | POST | Fields JSON | N/A | YES | +| fields | types.ModuleFieldSet | POST | Fields JSON | N/A | YES | ## Read module @@ -302,7 +302,7 @@ Example bar chart with number of leads per country: select lead.country from lea | --------- | ---- | ------ | ----------- | ------- | --------- | | moduleID | uint64 | PATH | Module ID | N/A | YES | | name | string | POST | Module Name | N/A | YES | -| fields | sqlxTypes.JSONText | POST | Fields JSON | N/A | YES | +| fields | types.ModuleFieldSet | POST | Fields JSON | N/A | YES | ## Delete module