From acc26c9c6926c2d2af1944cfaaca1adb3567baff Mon Sep 17 00:00:00 2001 From: sx328 Date: Thu, 7 Sep 2023 00:32:23 -0500 Subject: [PATCH] adds `assigned_fulfillment_order` (#233) --- assigned_fulfillment_order.go | 76 ++++++++++++++++++++++++++++++ assigned_fulfillment_order_test.go | 63 +++++++++++++++++++++++++ fulfillment_order.go | 14 +++--- goshopify.go | 2 + 4 files changed, 148 insertions(+), 7 deletions(-) create mode 100644 assigned_fulfillment_order.go create mode 100644 assigned_fulfillment_order_test.go diff --git a/assigned_fulfillment_order.go b/assigned_fulfillment_order.go new file mode 100644 index 00000000..9484f8db --- /dev/null +++ b/assigned_fulfillment_order.go @@ -0,0 +1,76 @@ +package goshopify + +import "fmt" + +const ( + assignedFulfillmentOrderBasePath = "assigned_fulfillment_orders" +) + +// AssignedFulfillmentService is an interface for interfacing with the assigned fulfillment orders +// of the Shopify API. +// https://shopify.dev/docs/api/admin-rest/2023-07/resources/assignedfulfillmentorder +type AssignedFulfillmentOrderService interface { + Get(interface{}) ([]AssignedFulfillmentOrder, error) +} + +type AssignedFulfillmentOrder struct { + Id int64 `json:"id,omitempty"` + AssignedLocationId int64 `json:"assigned_location_id,omitempty"` + Destination AssignedFulfillmentOrderDestination `json:"destination,omitempty"` + LineItems []AssignedFulfillmentOrderLineItem `json:"line_items,omitempty"` + OrderId int64 `json:"order_id,omitempty"` + RequestStatus string `json:"request_status,omitempty"` + ShopId int64 `json:"shop_id,omitempty"` + Status string `json:"status,omitempty"` +} + +// AssignedFulfillmentOrderDestination represents a destination for a AssignedFulfillmentOrder +type AssignedFulfillmentOrderDestination struct { + Id int64 `json:"id,omitempty"` + Address1 string `json:"address1,omitempty"` + Address2 string `json:"address2,omitempty"` + City string `json:"city,omitempty"` + Company string `json:"company,omitempty"` + Country string `json:"country,omitempty"` + Email string `json:"email,omitempty"` + FirstName string `json:"first_name,omitempty"` + LastName string `json:"last_name,omitempty"` + Phone string `json:"phone,omitempty"` + Province string `json:"province,omitempty"` + Zip string `json:"zip,omitempty"` +} + +// AssignedFulfillmentOrderLineItem represents a line item for a AssignedFulfillmentOrder +type AssignedFulfillmentOrderLineItem struct { + Id int64 `json:"id,omitempty"` + ShopId int64 `json:"shop_id,omitempty"` + FulfillmentOrderId int64 `json:"fulfillment_order_id,omitempty"` + LineItemId int64 `json:"line_item_id,omitempty"` + InventoryItemId int64 `json:"inventory_item_id,omitempty"` + Quantity int64 `json:"quantity,omitempty"` + FulfillableQuantity int64 `json:"fulfillable_quantity,omitempty"` +} + +// AssignedFulfillmentOrderResource represents the result from the assigned_fulfillment_order.json endpoint +type AssignedFulfillmentOrdersResource struct { + AssignedFulfillmentOrders []AssignedFulfillmentOrder `json:"fulfillment_orders,omitempty"` +} + +type AssignedFulfillmentOrderOptions struct { + AssignmentStatus string `url:"assignment_status,omitempty"` + LocationIds string `url:"location_ids,omitempty"` +} + +// AssignedFulfillmentOrderServiceOp handles communication with the AssignedFulfillmentOrderService +// related methods of the Shopify API +type AssignedFulfillmentOrderServiceOp struct { + client *Client +} + +// Gets a list of all the fulfillment orders that are assigned to an app at the shop level +func (s *AssignedFulfillmentOrderServiceOp) Get(options interface{}) ([]AssignedFulfillmentOrder, error) { + path := fmt.Sprintf("%s.json", assignedFulfillmentOrderBasePath) + resource := new(AssignedFulfillmentOrdersResource) + err := s.client.Get(path, resource, options) + return resource.AssignedFulfillmentOrders, err +} diff --git a/assigned_fulfillment_order_test.go b/assigned_fulfillment_order_test.go new file mode 100644 index 00000000..69636baf --- /dev/null +++ b/assigned_fulfillment_order_test.go @@ -0,0 +1,63 @@ +package goshopify + +import ( + "fmt" + "reflect" + "testing" + + "github.com/jarcoal/httpmock" +) + +func AssignedFulfillmentOrderTests(t *testing.T, assignedFulfillmentOrder AssignedFulfillmentOrder) { + // Check that ID is assigned to the returned fulfillment + expectedInt := int64(255858046) // in assigned_fulfillment_orders.json fixture + if assignedFulfillmentOrder.Id != expectedInt { + t.Errorf("AssignedFulfillmentOrder.ID returned %+v, expected %+v", assignedFulfillmentOrder.Id, expectedInt) + } +} + +func TestAssignedFulfillmentOrderGet(t *testing.T) { + setup() + defer teardown() + + httpmock.RegisterResponder("GET", fmt.Sprintf("https://fooshop.myshopify.com/%s/assigned_fulfillment_orders.json", client.pathPrefix), + httpmock.NewStringResponder(200, `{"fulfillment_orders": [{"id":1},{"id":2}]}`)) + + assignedFulfillmentOrderService := &AssignedFulfillmentOrderServiceOp{client: client} + + assignedFulfillmentOrders, err := assignedFulfillmentOrderService.Get(nil) + if err != nil { + t.Errorf("AssignedFulfillmentOrder.List returned error: %v", err) + } + + expected := []AssignedFulfillmentOrder{{Id: 1}, {Id: 2}} + if !reflect.DeepEqual(assignedFulfillmentOrders, expected) { + t.Errorf("AssignedFulfillmentOrder.List returned %+v, expected %+v", assignedFulfillmentOrders, expected) + } +} + +// func TestFulfillmentOrderGet(t *testing.T) { +// setup() +// defer teardown() + +// fixture := loadFixture("fulfillment_order.json") +// httpmock.RegisterResponder("GET", fmt.Sprintf("https://fooshop.myshopify.com/%s/fulfillment_orders/255858046.json", client.pathPrefix), +// httpmock.NewBytesResponder(200, fixture)) + +// fulfillmentOrderService := &FulfillmentOrderServiceOp{client: client} + +// fulfillment, err := fulfillmentOrderService.Get(255858046, nil) +// if err != nil { +// t.Errorf("FulfillmentOrder.Get returned error: %v", err) +// } + +// expected := FulfillmentOrderResource{} +// err = json.Unmarshal(fixture, &expected) +// if err != nil { +// t.Errorf("json.Unmarshall returned error : %v", err) +// } + +// if !reflect.DeepEqual(fulfillment, expected.FulfillmentOrder) { +// t.Errorf("FulfillmentOrder.Get returned %+v, expected %+v", fulfillment, expected) +// } +// } diff --git a/fulfillment_order.go b/fulfillment_order.go index 23a3fe7a..e30a6ddb 100644 --- a/fulfillment_order.go +++ b/fulfillment_order.go @@ -26,10 +26,10 @@ type FulfillmentOrderHoldReason string const ( HoldReasonAwaitingPayment FulfillmentOrderHoldReason = "awaiting_payment" - HoldReasonHighRiskOfFraud = "high_risk_of_fraud" - HoldReasonIncorrectAddress = "incorrect_address" - HoldReasonOutOfStock = "inventory_out_of_stock" - HoldReasonOther = "other" + HoldReasonHighRiskOfFraud FulfillmentOrderHoldReason = "high_risk_of_fraud" + HoldReasonIncorrectAddress FulfillmentOrderHoldReason = "incorrect_address" + HoldReasonOutOfStock FulfillmentOrderHoldReason = "inventory_out_of_stock" + HoldReasonOther FulfillmentOrderHoldReason = "other" ) // FulfillmentOrderServiceOp handles communication with the fulfillment order @@ -141,12 +141,12 @@ type FulfillmentOrder struct { UpdatedAt *time.Time `json:"updated_at,omitempty"` } -// FulfillmentOrdersResource represents the result from the fulfilment_orders.json endpoint +// FulfillmentOrdersResource represents the result from the fulfillment_orders.json endpoint type FulfillmentOrdersResource struct { FulfillmentOrders []FulfillmentOrder `json:"fulfillment_orders"` } -// FulfillmentOrderResource represents the result from the fulfilment_orders/.json endpoint +// FulfillmentOrderResource represents the result from the fulfillment_orders/.json endpoint type FulfillmentOrderResource struct { FulfillmentOrder *FulfillmentOrder `json:"fulfillment_order"` } @@ -157,7 +157,7 @@ type FulfillmentOrderMoveResource struct { MovedFulfillmentOrder FulfillmentOrder `json:"moved_fulfillment_order"` } -// FulfillmentOrderPathPrefix returns the prefix for a fulfillmentorder path +// FulfillmentOrderPathPrefix returns the prefix for a fulfillmentOrder path func FulfillmentOrderPathPrefix(resource string, resourceID int64) string { return fmt.Sprintf("%s/%d", resource, resourceID) } diff --git a/goshopify.go b/goshopify.go index b1159c05..6ebf565c 100644 --- a/goshopify.go +++ b/goshopify.go @@ -125,6 +125,7 @@ type Client struct { GiftCard GiftCardService FulfillmentOrder FulfillmentOrderService GraphQL GraphQLService + AssignedFulfillmentOrder AssignedFulfillmentOrderService } // A general response error that follows a similar layout to Shopify's response @@ -308,6 +309,7 @@ func NewClient(app App, shopName, token string, opts ...Option) *Client { c.GiftCard = &GiftCardServiceOp{client: c} c.FulfillmentOrder = &FulfillmentOrderServiceOp{client: c} c.GraphQL = &GraphQLServiceOp{client: c} + c.AssignedFulfillmentOrder = &AssignedFulfillmentOrderServiceOp{client: c} // apply any options for _, opt := range opts {