Skip to content

Commit

Permalink
CRM Schemas CRUD support
Browse files Browse the repository at this point in the history
  • Loading branch information
sblackstone committed Mar 13, 2023
1 parent e0c0f38 commit 62d8373
Show file tree
Hide file tree
Showing 3 changed files with 290 additions and 0 deletions.
5 changes: 5 additions & 0 deletions crm.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type CRM struct {
Contact ContactService
Deal DealService
Imports CrmImportsService
Schemas CrmSchemasService
}

func newCRM(c *Client) *CRM {
Expand All @@ -29,5 +30,9 @@ func newCRM(c *Client) *CRM {
crmImportsPath: fmt.Sprintf("%s/%s", crmPath, crmImportsBasePath),
client: c,
},
Schemas: &CrmSchemasServiceOp{
crmSchemasPath: fmt.Sprintf("%s/%s", crmPath, crmSchemasPath),
client: c,
},
}
}
64 changes: 64 additions & 0 deletions crm_schemas.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package hubspot

import "fmt"

const (
crmSchemasPath = "schemas"
)

// CrmSchemas is an interface of CRM crm/v3/schemas interface..
// Reference: https://developers.hubspot.com/docs/api/crm/crm-custom-objects
type CrmSchemasService interface {
List() (map[string]interface{}, error)
Create(map[string]interface{}) (map[string]interface{}, error)
Get(string) (map[string]interface{}, error)
Delete(string, bool) error
Update(string, map[string]interface{}) (map[string]interface{}, error)
}

// CrmSchemasServiceOp handles communication with the CRM schemas endpoint.
type CrmSchemasServiceOp struct {
client *Client
crmSchemasPath string
}

var _ CrmSchemasService = (*CrmSchemasServiceOp)(nil)

func (s *CrmSchemasServiceOp) List() (map[string]interface{}, error) {
resource := make(map[string]interface{})
if err := s.client.Get(s.crmSchemasPath, &resource, nil); err != nil {
return nil, err
}
return resource, nil
}

func (s *CrmSchemasServiceOp) Create(reqData map[string]interface{}) (map[string]interface{}, error) {
resource := make(map[string]interface{})
if err := s.client.Post(s.crmSchemasPath, reqData, &resource); err != nil {
return nil, err
}
return resource, nil
}

func (s *CrmSchemasServiceOp) Get(schemaIdentifier string) (map[string]interface{}, error) {
resource := make(map[string]interface{})
path := fmt.Sprintf("%s/%s", s.crmSchemasPath, schemaIdentifier)
if err := s.client.Get(path, &resource, nil); err != nil {
return nil, err
}
return resource, nil
}

func (s *CrmSchemasServiceOp) Delete(schemaIdentifier string, archived bool) error {
path := fmt.Sprintf("%s/%s?archived=%t", s.crmSchemasPath, schemaIdentifier, archived)
return s.client.Delete(path)
}

func (s *CrmSchemasServiceOp) Update(schemaIdentifier string, reqData map[string]interface{}) (map[string]interface{}, error) {
resource := make(map[string]interface{})
path := fmt.Sprintf("%s/%s", s.crmSchemasPath, schemaIdentifier)
if err := s.client.Patch(path, reqData, &resource); err != nil {
return nil, err
}
return resource, nil
}
221 changes: 221 additions & 0 deletions crm_schemas_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
package hubspot

import (
"encoding/json"
"fmt"
"os"
"testing"
)

func TestGetSchemas(t *testing.T) {
t.SkipNow()

cli, _ := NewClient(SetPrivateAppToken(os.Getenv("PRIVATE_APP_TOKEN")))
res, err := cli.CRM.Schemas.List()
if err != nil {
t.Error(err)
}
_, ok := res["results"]
if !ok {
t.Errorf("expected List schemas to respond with results key on success")
}
}

func TestCreateSchema(t *testing.T) {
t.SkipNow()
// this example is from Hubspot..
var exampleJson string = `
{
"name": "cars",
"labels": {
"singular": "Car",
"plural": "Cars"
},
"primaryDisplayProperty": "model",
"secondaryDisplayProperties": [
"make"
],
"searchableProperties": [
"year",
"make",
"vin",
"model"
],
"requiredProperties": [
"year",
"make",
"vin",
"model"
],
"properties": [
{
"name": "condition",
"label": "Condition",
"type": "enumeration",
"fieldType": "select",
"options": [
{
"label": "New",
"value": "new"
},
{
"label": "Used",
"value": "used"
}
]
},
{
"name": "date_received",
"label": "Date received",
"type": "date",
"fieldType": "date"
},
{
"name": "year",
"label": "Year",
"type": "number",
"fieldType": "number"
},
{
"name": "make",
"label": "Make",
"type": "string",
"fieldType": "text"
},
{
"name": "model",
"label": "Model",
"type": "string",
"fieldType": "text"
},
{
"name": "vin",
"label": "VIN",
"type": "string",
"hasUniqueValue": true,
"fieldType": "text"
},
{
"name": "price",
"label": "Price",
"type": "number",
"fieldType": "number"
},
{
"name": "notes",
"label": "Notes",
"type": "string",
"fieldType": "text"
}
],
"associatedObjects": [
"CONTACT"
]
}
`

req := make(map[string]interface{})
err := json.Unmarshal([]byte(exampleJson), &req)
if err != nil {
t.Error(err)
}

cli, _ := NewClient(SetPrivateAppToken(os.Getenv("PRIVATE_APP_TOKEN")))
res, err := cli.CRM.Schemas.Create(req)
if err != nil {
t.Error(err)
}

_, ok := res["id"]
if !ok {
t.Errorf("expected post schema result to have an id field")
}
}

func TestGetSchema(t *testing.T) {
t.SkipNow()
cli, _ := NewClient(SetPrivateAppToken(os.Getenv("PRIVATE_APP_TOKEN")))
res, err := cli.CRM.Schemas.Get("cars")
if err != nil {
t.Error(err)
}
fmt.Printf("%+v\n", res)

_, ok := res["id"]
if !ok {
t.Errorf("expected post schema result to have an id field")
}
}

func TestDeleteSchema(t *testing.T) {
t.SkipNow()
cli, _ := NewClient(SetPrivateAppToken(os.Getenv("PRIVATE_APP_TOKEN")))

res, err := cli.CRM.Schemas.Get("cars")
if err != nil {
t.Error(err)
}

qName, ok := res["fullyQualifiedName"]
if !ok {
t.Error(err)
}
err = cli.CRM.Schemas.Delete(qName.(string), false)
if err != nil {
t.Error(err)
}

err = cli.CRM.Schemas.Delete(qName.(string), true)
if err != nil {
t.Error(err)
}

}

func TestUpdateSchema(t *testing.T) {
t.SkipNow()
// Note: You need to wait some time after calling create before calling Update as it'll return an error message.
req := make(map[string]interface{})
req["primaryDisplayProperty"] = "year"

cli, _ := NewClient(SetPrivateAppToken(os.Getenv("PRIVATE_APP_TOKEN")))

res, err := cli.CRM.Schemas.Get("cars")
if err != nil {
t.Error(err)
return
}

primaryDisplayProperty, ok := res["primaryDisplayProperty"]
if !ok {
t.Error(err)
return
}

if primaryDisplayProperty != "model" {
t.Error("expected primaryDisplayProperty to be model before update")
return
}

qName, ok := res["fullyQualifiedName"]
if !ok {
t.Error(err)
return
}

res, err = cli.CRM.Schemas.Update(qName.(string), req)
if err != nil {
t.Error(err)
return
}

primaryDisplayProperty, ok = res["primaryDisplayProperty"]
if !ok {
t.Error(err)
}

if primaryDisplayProperty != "year" {
t.Errorf("expected primaryDisplayProperty to be year after update, got %s", primaryDisplayProperty.(string))
}

}

0 comments on commit 62d8373

Please sign in to comment.