Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions providers/gusto/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ package gusto
import (
"github.com/amp-labs/connectors/common"
"github.com/amp-labs/connectors/internal/components"
"github.com/amp-labs/connectors/internal/components/deleter"
"github.com/amp-labs/connectors/internal/components/operations"
"github.com/amp-labs/connectors/internal/components/reader"
"github.com/amp-labs/connectors/internal/components/schema"
"github.com/amp-labs/connectors/internal/components/writer"
"github.com/amp-labs/connectors/providers"
"github.com/amp-labs/connectors/providers/gusto/metadata"
)
Expand All @@ -25,6 +27,8 @@ type Connector struct {

components.SchemaProvider
components.Reader
components.Writer
components.Deleter

companyID string
}
Expand Down Expand Up @@ -62,6 +66,28 @@ func constructor(params common.ConnectorParams) func(*components.Connector) (*Co
},
)

connector.Writer = writer.NewHTTPWriter(
connector.HTTPClient().Client,
components.NewEmptyEndpointRegistry(),
connector.ProviderContext.Module(),
operations.WriteHandlers{
BuildRequest: connector.buildWriteRequest,
ParseResponse: connector.parseWriteResponse,
ErrorHandler: common.InterpretError,
},
)

connector.Deleter = deleter.NewHTTPDeleter(
connector.HTTPClient().Client,
components.NewEmptyEndpointRegistry(),
connector.ProviderContext.Module(),
operations.DeleteHandlers{
BuildRequest: connector.buildDeleteRequest,
ParseResponse: connector.parseDeleteResponse,
ErrorHandler: common.InterpretError,
},
)

return connector, nil
}
}
99 changes: 99 additions & 0 deletions providers/gusto/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package gusto

import (
"context"
"fmt"
"net/http"

"github.com/amp-labs/connectors/common"
"github.com/amp-labs/connectors/common/urlbuilder"
"github.com/amp-labs/connectors/internal/datautils"
)

// Gusto delete API conventions (App Integrations track):
// - Most deletes are TOP-LEVEL by uuid: DELETE /v1/{object}/{uuid}
// - earning_types is COMPANY-SCOPED: DELETE /v1/companies/{cid}/earning_types/{uuid}
//
// Workflow-style deletes (terminations, rehire) follow a different shape and
// are out of scope here — same reasoning as their POST/PUT counterparts in
// write.go (proxy-only).
//
// API references (slug = delete-v1-{path-with-dashes}):
// https://docs.gusto.com/app-integrations/reference/
// - delete-v1-employee
// - delete-v1-jobs-job_id
// - delete-v1-compensations-compensation_id
// - delete-v1-home_addresses-home_address_uuid
// - delete-v1-work_addresses-work_address_uuid
// - delete-v1-employee_benefits-employee_benefit_id
// - delete-v1-company_benefits-company_benefit_id
// - delete-department
// - delete-v1-companies-company_id-earning_types-earning_type_uuid

// supportedDeleteObjects enumerates objects Gusto exposes a DELETE endpoint
// for. Many Gusto resources have no DELETE (companies, contractors,
// locations, payrolls, pay_schedules, garnishments, admins,
// contractor_payments, custom_fields). For those we reject with
// ErrOperationNotSupportedForObject.
//
//nolint:gochecknoglobals
var supportedDeleteObjects = datautils.NewStringSet(
objectEmployees,
objectJobs,
objectCompensations,
objectHomeAddresses,
objectWorkAddresses,
objectEmployeeBenefits,
objectCompanyBenefits,
objectDepartments,
objectEarningTypes,
)

func (c *Connector) buildDeleteRequest(ctx context.Context, params common.DeleteParams) (*http.Request, error) {
if !supportedDeleteObjects.Has(params.ObjectName) {
return nil, common.ErrOperationNotSupportedForObject
}

url, err := c.buildDeleteURL(params)
if err != nil {
return nil, err
}

req, err := http.NewRequestWithContext(ctx, http.MethodDelete, url.String(), nil)
if err != nil {
return nil, err
}

req.Header.Set("Accept", "application/json")

return req, nil
}

// buildDeleteURL routes earning_types through the company-scoped path and all
// other supported objects through the flat top-level path.
func (c *Connector) buildDeleteURL(params common.DeleteParams) (*urlbuilder.URL, error) {
baseURL := c.ProviderInfo().BaseURL

if companyScopedUpdate.Has(params.ObjectName) {
if c.companyID == "" {
return nil, ErrMissingCompanyID
}

return urlbuilder.New(baseURL, "v1", "companies", c.companyID, params.ObjectName, params.RecordId)
}

return urlbuilder.New(baseURL, "v1", params.ObjectName, params.RecordId)
}

func (c *Connector) parseDeleteResponse(
_ context.Context,
_ common.DeleteParams,
_ *http.Request,
response *common.JSONHTTPResponse,
) (*common.DeleteResult, error) {
if response.Code != http.StatusOK && response.Code != http.StatusNoContent && response.Code != http.StatusAccepted {
return nil, fmt.Errorf("%w: failed to delete record: %d", common.ErrRequestFailed, response.Code)
}

return &common.DeleteResult{Success: true}, nil
}
113 changes: 113 additions & 0 deletions providers/gusto/delete_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package gusto

import (
"net/http"
"testing"

"github.com/amp-labs/connectors"
"github.com/amp-labs/connectors/common"
"github.com/amp-labs/connectors/test/utils/mockutils/mockcond"
"github.com/amp-labs/connectors/test/utils/mockutils/mockserver"
"github.com/amp-labs/connectors/test/utils/testroutines"
)

func TestDelete(t *testing.T) { //nolint:funlen
t.Parallel()

tests := []testroutines.Delete{
{
Name: "Delete object must be included",
Server: mockserver.Dummy(),
ExpectedErrs: []error{common.ErrMissingObjects},
},
{
Name: "Delete record ID must be included",
Input: common.DeleteParams{ObjectName: "jobs"},
Server: mockserver.Dummy(),
ExpectedErrs: []error{common.ErrMissingRecordID},
},
{
Name: "Unsupported object returns ErrOperationNotSupportedForObject",
// locations has no DELETE per Gusto's docs; the connector rejects.
Input: common.DeleteParams{ObjectName: "locations", RecordId: "loc_001"},
Server: mockserver.Dummy(),
ExpectedErrs: []error{common.ErrOperationNotSupportedForObject},
},
{
Name: "Top-level DELETE on jobs hits /v1/jobs/{uuid}",
Input: common.DeleteParams{ObjectName: "jobs", RecordId: "job_001"},
Server: mockserver.Switch{
Setup: mockserver.ContentJSON(),
Cases: []mockserver.Case{
{
If: mockcond.And{
mockcond.MethodDELETE(),
mockcond.Path("/v1/jobs/job_001"),
},
Then: mockserver.Response(http.StatusNoContent, nil),
},
},
Default: mockserver.ResponseString(http.StatusInternalServerError, `{"error":"unexpected"}`),
}.Server(),
Expected: &common.DeleteResult{Success: true},
},
{
Name: "Top-level DELETE on home_addresses hits /v1/home_addresses/{uuid}",
Input: common.DeleteParams{ObjectName: "home_addresses", RecordId: "addr_001"},
Server: mockserver.Switch{
Setup: mockserver.ContentJSON(),
Cases: []mockserver.Case{
{
If: mockcond.And{
mockcond.MethodDELETE(),
mockcond.Path("/v1/home_addresses/addr_001"),
},
Then: mockserver.Response(http.StatusNoContent, nil),
},
},
Default: mockserver.ResponseString(http.StatusInternalServerError, `{"error":"unexpected"}`),
}.Server(),
Expected: &common.DeleteResult{Success: true},
},
{
Name: "Company-scoped DELETE on earning_types hits /v1/companies/{cid}/earning_types/{uuid}",
// earning_types delete is nested under company per Gusto's docs
// (delete-v1-companies-company_id-earning_types-earning_type_uuid).
Input: common.DeleteParams{ObjectName: "earning_types", RecordId: "et_001"},
Server: mockserver.Switch{
Setup: mockserver.ContentJSON(),
Cases: []mockserver.Case{
{
If: mockcond.And{
mockcond.MethodDELETE(),
mockcond.Path("/v1/companies/" + testCompanyID + "/earning_types/et_001"),
},
Then: mockserver.Response(http.StatusNoContent, nil),
},
},
Default: mockserver.ResponseString(http.StatusInternalServerError, `{"error":"unexpected"}`),
}.Server(),
Expected: &common.DeleteResult{Success: true},
},
{
Name: "DELETE earning_types without companyID returns ErrMissingCompanyID",
Input: common.DeleteParams{ObjectName: "earning_types", RecordId: "et_001"},
Server: mockserver.Dummy(),
ExpectedErrs: []error{ErrMissingCompanyID},
},
}

for _, tt := range tests {
t.Run(tt.Name, func(t *testing.T) {
t.Parallel()

tt.Run(t, func() (connectors.DeleteConnector, error) {
if tt.Name == "DELETE earning_types without companyID returns ErrMissingCompanyID" {
return constructTestWriteConnector(tt.Server.URL, "")
}

return constructTestWriteConnector(tt.Server.URL, testCompanyID)
})
})
}
}
8 changes: 8 additions & 0 deletions providers/gusto/test/write/compensation-create.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"uuid": "comp_001",
"job_uuid": "job_001",
"rate": "100.00",
"payment_unit": "Hour",
"flsa_status": "Nonexempt",
"version": "v1"
}
6 changes: 6 additions & 0 deletions providers/gusto/test/write/earning-type-update.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"uuid": "et_001",
"name": "Bonus Updated",
"company_uuid": "test-company-uuid",
"version": "v2"
}
8 changes: 8 additions & 0 deletions providers/gusto/test/write/employee-create.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"uuid": "emp_001",
"first_name": "Alice",
"last_name": "Anderson",
"email": "alice@example.com",
"company_uuid": "test-company-uuid",
"version": "v1"
}
8 changes: 8 additions & 0 deletions providers/gusto/test/write/employee-update.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"uuid": "emp_001",
"first_name": "Alicia",
"last_name": "Anderson",
"email": "alice@example.com",
"company_uuid": "test-company-uuid",
"version": "v2"
}
6 changes: 6 additions & 0 deletions providers/gusto/test/write/job-create.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"uuid": "job_001",
"employee_uuid": "emp_001",
"title": "Software Engineer",
"version": "v1"
}
8 changes: 8 additions & 0 deletions providers/gusto/test/write/location-update.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"uuid": "loc_001",
"street_1": "100 Main St",
"city": "San Francisco",
"state": "CA",
"zip": "94105",
"version": "v2"
}
Loading
Loading