3
0

upd(crm): update module api fields parameter

This commit is contained in:
Tit Petric
2018-12-04 16:40:02 +01:00
parent c7d4b4d629
commit 99f8f4e872
8 changed files with 61 additions and 235 deletions

View File

@@ -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"

View File

@@ -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": [],

View File

@@ -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"
}
]
}

View File

@@ -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
}

View File

@@ -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
{

View File

@@ -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)
}

View File

@@ -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

View File

@@ -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