Repository: amp-labs/connectors
Pull request #1087 (2024-10-21 07:18:51)

fix: handle recordId field in the Attio write
…nality




laurenzlong: @sanjay-a01 please fix linter then merge

Patch:
diff --git a/providers/attio/write.go b/providers/attio/write.go
index c32d84f4..06dba6f8 100644
--- a/providers/attio/write.go
+++ b/providers/attio/write.go
@@ -6,15 +6,13 @@ import (
 	"errors"
 
 	"github.com/amp-labs/connectors/common"
+	"github.com/amp-labs/connectors/common/jsonquery"
+	"github.com/amp-labs/connectors/common/naming"
+	"github.com/spyzhov/ajson"
 )
 
 var ErrEmptyResultResponse = errors.New("writing reponded with an empty result")
 
-type writeResponse struct {
-	Success bool           `json:"success"`
-	Data    map[string]any `json:"data"`
-}
-
 // Write creates/updates records in attio.
 func (c *Connector) Write(ctx context.Context, config common.WriteParams) (*common.WriteResult, error) {
 	if err := config.ValidateParams(); err != nil {
@@ -46,17 +44,39 @@ func (c *Connector) Write(ctx context.Context, config common.WriteParams) (*comm
 		return nil, err
 	}
 
-	resp, err := common.UnmarshalJSON[writeResponse](res)
+	body, ok := res.Body()
+	if !ok {
+		return &common.WriteResult{
+			Success: true,
+		}, nil
+	}
+
+	// Write response has a reference to the resource but no payload data.
+	return constructWriteResult(config.ObjectName, body)
+}
+
+func constructWriteResult(objName string, body *ajson.Node) (*common.WriteResult, error) {
+	obj := naming.NewSingularString(objName)
+
+	objectResponse, err := jsonquery.New(body).Object("data", false)
 	if err != nil {
 		return nil, err
 	}
 
-	if res.Code == 200 {
-		resp.Success = true
+	recordID, err := jsonquery.New(objectResponse, "id").Str(obj.String()+"_id", false)
+	if err != nil {
+		return nil, err
+	}
+
+	response, err := jsonquery.Convertor.ObjectToMap(objectResponse)
+	if err != nil {
+		return nil, err
 	}
 
 	return &common.WriteResult{
-		Success: resp.Success,
-		Data:    resp.Data,
+		Success:  true,
+		RecordId: *recordID,
+		Errors:   nil,
+		Data:     response,
 	}, nil
 }


Test patch:
diff --git a/providers/attio/write_test.go b/providers/attio/write_test.go
index ce44e6e7..3d876e82 100644
--- a/providers/attio/write_test.go
+++ b/providers/attio/write_test.go
@@ -55,7 +55,7 @@ func TestWrite(t *testing.T) { // nolint:funlen,gocognit,cyclop
 			})),
 			Expected: &common.WriteResult{
 				Success:  true,
-				RecordId: "",
+				RecordId: "bf012982-06a9-47f7-9e87-07dc4945d502",
 				Errors:   nil,
 				Data: map[string]any{
 					"api_slug":   "deal",
@@ -82,7 +82,7 @@ func TestWrite(t *testing.T) { // nolint:funlen,gocognit,cyclop
 			})),
 			Expected: &common.WriteResult{
 				Success:  true,
-				RecordId: "",
+				RecordId: "bf012982-06a9-47f7-9e87-07dc4945d502",
 				Errors:   nil,
 				Data: map[string]any{
 					"api_slug":   "deal",
@@ -109,7 +109,7 @@ func TestWrite(t *testing.T) { // nolint:funlen,gocognit,cyclop
 			})),
 			Expected: &common.WriteResult{
 				Success:  true,
-				RecordId: "",
+				RecordId: "e09a041c-0555-4bb2-8f6e-997bfc9b54e8",
 				Errors:   nil,
 				Data: map[string]any{
 					"api_slug":   "sales_investing",
@@ -149,7 +149,7 @@ func TestWrite(t *testing.T) { // nolint:funlen,gocognit,cyclop
 			})),
 			Expected: &common.WriteResult{
 				Success:  true,
-				RecordId: "",
+				RecordId: "e09a041c-0555-4bb2-8f6e-997bfc9b54e8",
 				Errors:   nil,
 				Data: map[string]any{
 					"api_slug":   "sales_investing",
@@ -189,7 +189,7 @@ func TestWrite(t *testing.T) { // nolint:funlen,gocognit,cyclop
 			})),
 			Expected: &common.WriteResult{
 				Success:  true,
-				RecordId: "",
+				RecordId: "126e58a5-5e3f-4644-89ff-6474e97fcecd",
 				Errors:   nil,
 				Data: map[string]any{
 					"content_plaintext": "summary",
@@ -221,7 +221,7 @@ func TestWrite(t *testing.T) { // nolint:funlen,gocognit,cyclop
 			})),
 			Expected: &common.WriteResult{
 				Success:  true,
-				RecordId: "",
+				RecordId: "b38142c7-00f6-4d92-813e-7b0f689a5873",
 				Errors:   nil,
 				Data: map[string]any{
 					"assignees": []any{
@@ -264,7 +264,7 @@ func TestWrite(t *testing.T) { // nolint:funlen,gocognit,cyclop
 			})),
 			Expected: &common.WriteResult{
 				Success:  true,
-				RecordId: "",
+				RecordId: "b38142c7-00f6-4d92-813e-7b0f689a5873",
 				Errors:   nil,
 				Data: map[string]any{
 					"assignees": []any{
@@ -307,7 +307,7 @@ func TestWrite(t *testing.T) { // nolint:funlen,gocognit,cyclop
 			})),
 			Expected: &common.WriteResult{
 				Success:  true,
-				RecordId: "",
+				RecordId: "7e5209b8-bd4e-41d9-bbcd-2f9bab7d4030",
 				Errors:   nil,
 				Data: map[string]any{
 					"created_at": "2024-10-04T10:05:01.173000000Z",
@@ -340,7 +340,7 @@ func TestWrite(t *testing.T) { // nolint:funlen,gocognit,cyclop
 			})),
 			Expected: &common.WriteResult{
 				Success:  true,
-				RecordId: "",
+				RecordId: "7e5209b8-bd4e-41d9-bbcd-2f9bab7d4030",
 				Errors:   nil,
 				Data: map[string]any{
 					"created_at": "2024-10-04T10:05:01.173000000Z",