diff --git a/go.mod b/go.mod index fb84a56..d520f86 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.22.2 //replace github.com/wakflo/go-sdk => ../go-sdk require ( + github.com/bold-commerce/go-shopify/v4 v4.5.0 github.com/clarkmcc/go-typescript v0.7.0 github.com/dop251/goja v0.0.0-20240516125602-ccbae20bcec2 github.com/gocarina/gocsv v0.0.0-20240520201108-78e41c74b4b1 @@ -12,8 +13,10 @@ require ( github.com/open2b/scriggo v0.56.1 github.com/opus-domini/fast-shot v0.10.0 github.com/rs/zerolog v1.33.0 + github.com/shopspring/decimal v0.0.0-20200105231215-408a2507e114 github.com/wakflo/go-sdk v0.4.2 golang.org/x/crypto v0.24.0 + golang.org/x/oauth2 v0.20.0 google.golang.org/api v0.181.0 ) @@ -37,6 +40,7 @@ require ( github.com/golang-jwt/jwt/v5 v5.2.1 // indirect github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect github.com/golang/protobuf v1.5.4 // indirect + github.com/google/go-querystring v1.1.0 // indirect github.com/google/pprof v0.0.0-20230207041349-798e818bf904 // indirect github.com/google/s2a-go v0.1.7 // indirect github.com/google/uuid v1.6.0 // indirect @@ -62,7 +66,6 @@ require ( go.opentelemetry.io/otel/trace v1.27.0 // indirect golang.org/x/exp v0.0.0-20240531132922-fd00a4e0eefc // indirect golang.org/x/net v0.26.0 // indirect - golang.org/x/oauth2 v0.20.0 // indirect golang.org/x/sys v0.21.0 // indirect golang.org/x/text v0.16.0 // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20240521202816-d264139d666e // indirect diff --git a/go.sum b/go.sum index b8e4816..d420cb6 100644 --- a/go.sum +++ b/go.sum @@ -12,6 +12,8 @@ github.com/Masterminds/semver/v3 v3.2.1 h1:RN9w6+7QoMeJVGyfmbcgs28Br8cvmnucEXnY0 github.com/Masterminds/semver/v3 v3.2.1/go.mod h1:qvl/7zhW3nngYb5+80sSMF+FG2BjYrf8m9wsX0PNOMQ= github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 h1:DklsrG3dyBCFEj5IhUbnKptjxatkF07cF2ak3yi77so= github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2/go.mod h1:WaHUgvxTVq04UNunO+XhnAqY/wQc+bxr74GqbsZ/Jqw= +github.com/bold-commerce/go-shopify/v4 v4.5.0 h1:47IwwlFFYCqZudVlxVW/LZNS9iNk7YGOzMfIJViL/1o= +github.com/bold-commerce/go-shopify/v4 v4.5.0/go.mod h1:Sjg+C2CLNhYeCbwB6EedKgj2AHEBOP3ng+Eu1IfMf1U= github.com/cavaliergopher/grab/v3 v3.0.1 h1:4z7TkBfmPjmLAAmkkAZNX/6QJ1nNFdv3SdIHXju0Fr4= github.com/cavaliergopher/grab/v3 v3.0.1/go.mod h1:1U/KNnD+Ft6JJiYoYBAimKH2XrYptb8Kl3DFGmsjpq4= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= @@ -76,9 +78,13 @@ github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.3/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck= +github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8= +github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/pprof v0.0.0-20230207041349-798e818bf904 h1:4/hN5RUoecvl+RmJRE2YxKWtnnQls6rQjjW5oV7qg2U= github.com/google/pprof v0.0.0-20230207041349-798e818bf904/go.mod h1:uglQLonpP8qtYCYyzA+8c/9qtqgA3qsXGYqCPKARAFg= @@ -110,6 +116,8 @@ github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+l github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM= github.com/huandu/xstrings v1.4.0 h1:D17IlohoQq4UcpqD7fDk80P7l+lwAmlFaBHgOipl2FU= github.com/huandu/xstrings v1.4.0/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE= +github.com/jarcoal/httpmock v1.3.0 h1:2RJ8GP0IIaWwcC9Fp2BmVi8Kog3v2Hn7VXM3fTd+nuc= +github.com/jarcoal/httpmock v1.3.0/go.mod h1:3yb8rc4BI7TCBhFY8ng0gjuLKJNquuDNiPaZjnENuYg= github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ= @@ -120,6 +128,7 @@ github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/ github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/maxatome/go-testdeep v1.12.0/go.mod h1:lPZc/HAcJMP92l7yI6TRz1aZN5URwUBUAfUNvrclaNM= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= @@ -136,6 +145,8 @@ github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1: github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8= github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss= +github.com/shopspring/decimal v0.0.0-20200105231215-408a2507e114 h1:Pm6R878vxWWWR+Sa3ppsLce/Zq+JNTs6aVvRu13jv9A= +github.com/shopspring/decimal v0.0.0-20200105231215-408a2507e114/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= diff --git a/internal/connectors/googledrive/lib_test.go b/internal/connectors/googledrive/lib_test.go index d6d191d..4d158a9 100644 --- a/internal/connectors/googledrive/lib_test.go +++ b/internal/connectors/googledrive/lib_test.go @@ -106,12 +106,12 @@ func TestNewConnector(t *testing.T) { t.Errorf("NewConnector() Operations() count = %d, want %d", len(spider.Operations()), 6) } - // if testCase.isTrigger { + // if testCase.isTrigger { // _, err = spider.RunTrigger(testCase.operationName, testCase.ctx) // if err != nil { // t.Errorf("NewConnector() RunTrigger() with name %v threw an error = %v", testCase.operationName, err) // } - //} else { + // } else { // _, err = spider.RunOperation(testCase.operationName, testCase.ctx) // if err != nil { // t.Errorf("NewConnector() RunOperation() with name %v threw an error = %v", testCase.operationName, err) diff --git a/internal/connectors/shopify/lib.go b/internal/connectors/shopify/lib.go index b7f0bbb..2f86fd2 100644 --- a/internal/connectors/shopify/lib.go +++ b/internal/connectors/shopify/lib.go @@ -21,14 +21,36 @@ import ( func NewConnector() (*sdk.ConnectorPlugin, error) { return sdk.CreateConnector(&sdk.CreateConnectorArgs{ Name: "Shopify", - Description: "interacts with Shopify API", + Description: "Ecommerce platform for online stores", Logo: "logos:shopify", Version: "0.0.1", Category: sdk.Apps, Authors: []string{"Wakflo "}, - Triggers: []sdk.ITrigger{}, + Triggers: []sdk.ITrigger{NewTriggerNewCustomer(), NewTriggerNewOrder()}, Operations: []sdk.IOperation{ + NewGetLocationsOperation(), + NewAdjustInventoryLevelOperation(), + NewCreateProductOperation(), + NewUpdateProductOperation(), + NewUpdateOrderOperation(), + NewUpdateCustomerOperation(), + NewCreateDraftOrderOperation(), + NewCreateOrderOperation(), + NewCreateTransactionOperation(), + NewCreateCustomerOperation(), + NewCreateCollectOperation(), + NewGetOrderOperation(), + NewGetCustomerOrdersOperation(), + NewGetTransactionOperation(), + NewGetTransactionsOperation(), + NewListCustomersOperation(), NewListProductsOperation(), + NewListOrdersOperation(), + NewListDraftOrdersOperation(), + NewCloseOrderOperation(), + NewCancelOrderOperation(), + NewGetCustomerOperation(), + NewGetProductOperation(), }, }) } diff --git a/internal/connectors/shopify/lib_test.go b/internal/connectors/shopify/lib_test.go deleted file mode 100644 index 6710a0b..0000000 --- a/internal/connectors/shopify/lib_test.go +++ /dev/null @@ -1,105 +0,0 @@ -// Copyright 2022-present Wakflo -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package shopify - -import ( - "testing" - - sdk "github.com/wakflo/go-sdk/connector" - sdkcore "github.com/wakflo/go-sdk/core" -) - -func TestNewConnector(t *testing.T) { - testCases := []struct { - name string - operationName string - wantErr bool - }{ - { - name: "Success", - operationName: "create-new-file", - wantErr: false, - }, - } - - _ = &sdk.RunContext{ - Auth: &sdkcore.AuthContext{ - AccessToken: "", - Token: nil, - TokenType: "", - Username: "", - Password: "", - Secret: "", - }, - } - - for _, testCase := range testCases { - t.Run(testCase.name, func(t *testing.T) { - instance, err := NewConnector() - spider := sdk.NewSpiderTest(t, instance) - connector := spider.GetConfig() - - if (err != nil) != testCase.wantErr { - t.Errorf("NewConnector() error = %v, wantErr %v", err, testCase.wantErr) - return - } - - if connector == nil { - t.Fatal("NewConnector() returned nil") - } - - if connector.DisplayName != "Shopify" { - t.Errorf("NewConnector() Name = %s, want %s", connector.DisplayName, "Google Drive") - } - - if connector.Description != "interacts with Shopify API" { - t.Errorf("NewConnector() Description = %s, want %s", connector.Description, "interacts with Shopify API") - } - - if connector.Logo != "logos:shopify" { - t.Errorf("NewConnector() Logo = %s, want %s", connector.Logo, "logos:shopify") - } - - if connector.Version != "0.0.1" { - t.Errorf("NewConnector() Version = %s, want %s", connector.Version, "0.0.1") - } - - if connector.Category != sdk.Apps { - t.Errorf("NewConnector() Category = %v, want %v", connector.Category, sdk.Apps) - } - - if len(connector.Authors) != 1 || connector.Authors[0] != "Wakflo " { - t.Errorf("NewConnector() Authors = %v, want %v", connector.Authors, []string{"Wakflo "}) - } - - if len(spider.Triggers()) != 0 { - t.Errorf("NewConnector() Triggers() count = %d, want %d", len(spider.Triggers()), 0) - } - - if len(spider.Operations()) != 1 { - t.Errorf("NewConnector() Operations() count = %d, want %d", len(spider.Operations()), 1) - } - - /*_, err = spider.RunOperation(testCase.operationName, ctx) - if err != nil { - t.Errorf("NewConnector() RunOperation() with name %v threw an error = %v", testCase.operationName, err) - } - - if trigger!= "some data" { - t.Errorf("NewConnector() RunOperation() response = %v, want %v", trigger["data"], "some data") - }*/ - }) - } -} diff --git a/internal/connectors/shopify/operation_adjust_inventory_level.go b/internal/connectors/shopify/operation_adjust_inventory_level.go new file mode 100644 index 0000000..f5500c3 --- /dev/null +++ b/internal/connectors/shopify/operation_adjust_inventory_level.go @@ -0,0 +1,101 @@ +// Copyright 2022-present Wakflo +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package shopify + +import ( + "context" + "errors" + "fmt" + + shopify "github.com/bold-commerce/go-shopify/v4" + + "github.com/wakflo/go-sdk/autoform" + sdk "github.com/wakflo/go-sdk/connector" + sdkcore "github.com/wakflo/go-sdk/core" +) + +type adjustInventoryLevelOperationProps struct { + InventoryItemID uint64 `json:"inventoryItemId"` + LocationID uint64 `json:"locationId"` + AvailableAdjustment int `json:"available_adjustment"` +} +type AdjustInventoryLevelOperation struct { + options *sdk.OperationInfo +} + +func NewAdjustInventoryLevelOperation() *AdjustInventoryLevelOperation { + return &AdjustInventoryLevelOperation{ + options: &sdk.OperationInfo{ + Name: "Adjust Inventory Level", + Description: "Adjust inventory level of an item at a location.", + RequireAuth: true, + Auth: sharedAuth, + Input: map[string]*sdkcore.AutoFormSchema{ + "inventoryItemId": autoform.NewNumberField(). + SetDisplayName("Inventory Item"). + SetDescription("The ID of the inventory item."). + SetRequired(true). + Build(), + "locationId": autoform.NewNumberField(). + SetDisplayName("Location"). + SetDescription("The ID of the location."). + SetRequired(true). + Build(), + "available_adjustment": autoform.NewNumberField(). + SetDisplayName("Adjustment Quantity"). + SetDescription("Positive values increase inventory, negative values decrease it."). + SetRequired(true). + Build(), + }, + ErrorSettings: sdkcore.StepErrorSettings{ + ContinueOnError: false, + RetryOnError: false, + }, + }, + } +} + +func (c *AdjustInventoryLevelOperation) Run(ctx *sdk.RunContext) (sdk.JSON, error) { + if ctx.Auth.Extra["token"] == "" { + return nil, errors.New("missing shopify auth token") + } + input := sdk.InputToType[adjustInventoryLevelOperationProps](ctx) + domain := ctx.Auth.Extra["domain"] + shopName := domain + ".myshopify.com" + client := getShopifyClient(shopName, ctx.Auth.Extra["token"]) + + options := shopify.InventoryLevelAdjustOptions{ + InventoryItemId: input.InventoryItemID, + LocationId: input.LocationID, + Adjust: input.AvailableAdjustment, + } + + inventoryLevel, err := client.InventoryLevel.Adjust(context.Background(), options) + if err != nil { + return nil, fmt.Errorf("failed to adjust inventory: %v", err) + } + + return sdk.JSON(map[string]interface{}{ + "adjustedInventoryLevel": inventoryLevel, + }), nil +} + +func (c *AdjustInventoryLevelOperation) Test(ctx *sdk.RunContext) (sdk.JSON, error) { + return c.Run(ctx) +} + +func (c *AdjustInventoryLevelOperation) GetInfo() *sdk.OperationInfo { + return c.options +} diff --git a/internal/connectors/shopify/operation_cancel_order.go b/internal/connectors/shopify/operation_cancel_order.go new file mode 100644 index 0000000..65e6646 --- /dev/null +++ b/internal/connectors/shopify/operation_cancel_order.go @@ -0,0 +1,86 @@ +// Copyright 2022-present Wakflo +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package shopify + +import ( + "context" + "errors" + "fmt" + + "github.com/wakflo/go-sdk/autoform" + sdk "github.com/wakflo/go-sdk/connector" + sdkcore "github.com/wakflo/go-sdk/core" +) + +type cancelOrderOperationProps struct { + OrderID uint64 `json:"orderId"` + Reason string `json:"reason,omitempty"` +} +type CancelOrderOperation struct { + options *sdk.OperationInfo +} + +func NewCancelOrderOperation() *CancelOrderOperation { + return &CancelOrderOperation{ + options: &sdk.OperationInfo{ + Name: "Cancel Order", + Description: "Cancel an existing order by ID.", + RequireAuth: true, + Auth: sharedAuth, + Input: map[string]*sdkcore.AutoFormSchema{ + "orderId": autoform.NewNumberField(). + SetDisplayName("Order"). + SetDescription("The ID of the order to cancel."). + SetRequired(true). + Build(), + }, + ErrorSettings: sdkcore.StepErrorSettings{ + ContinueOnError: false, + RetryOnError: false, + }, + }, + } +} + +func (c *CancelOrderOperation) Run(ctx *sdk.RunContext) (sdk.JSON, error) { + if ctx.Auth.Extra["token"] == "" { + return nil, errors.New("missing shopify auth token") + } + input := sdk.InputToType[cancelOrderOperationProps](ctx) + + domain := ctx.Auth.Extra["domain"] + shopName := domain + ".myshopify.com" + + client := getShopifyClient(shopName, ctx.Auth.Extra["token"]) + + order, err := client.Order.Cancel(context.Background(), input.OrderID, nil) + if err != nil { + return nil, err + } + if order == nil { + return nil, fmt.Errorf("no order found with ID '%d'", input.OrderID) + } + return sdk.JSON(map[string]interface{}{ + "Result": "Order Successfully cancelled!", + }), nil +} + +func (c *CancelOrderOperation) Test(ctx *sdk.RunContext) (sdk.JSON, error) { + return c.Run(ctx) +} + +func (c *CancelOrderOperation) GetInfo() *sdk.OperationInfo { + return c.options +} diff --git a/internal/connectors/shopify/operation_close_order.go b/internal/connectors/shopify/operation_close_order.go new file mode 100644 index 0000000..f8e5243 --- /dev/null +++ b/internal/connectors/shopify/operation_close_order.go @@ -0,0 +1,89 @@ +// Copyright 2022-present Wakflo +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package shopify + +import ( + "context" + "errors" + "fmt" + + "github.com/wakflo/go-sdk/autoform" + sdk "github.com/wakflo/go-sdk/connector" + sdkcore "github.com/wakflo/go-sdk/core" +) + +type closeOrderOperationProps struct { + OrderID uint64 `json:"orderId"` +} +type CloseOrderOperation struct { + options *sdk.OperationInfo +} + +func NewCloseOrderOperation() *CloseOrderOperation { + return &CloseOrderOperation{ + options: &sdk.OperationInfo{ + Name: "Close Order", + Description: "ID of the order to close.", + RequireAuth: true, + Auth: sharedAuth, + Input: map[string]*sdkcore.AutoFormSchema{ + "orderId": autoform.NewNumberField(). + SetDisplayName("Order"). + SetDescription("The ID of the order to close."). + SetRequired(true). + Build(), + }, + ErrorSettings: sdkcore.StepErrorSettings{ + ContinueOnError: false, + RetryOnError: false, + }, + }, + } +} + +func (c *CloseOrderOperation) Run(ctx *sdk.RunContext) (sdk.JSON, error) { + if ctx.Auth.Extra["token"] == "" { + return nil, errors.New("missing shopify auth token") + } + input := sdk.InputToType[closeOrderOperationProps](ctx) + + domain := ctx.Auth.Extra["domain"] + shopName := domain + ".myshopify.com" + client := getShopifyClient(shopName, ctx.Auth.Extra["token"]) + + order, err := client.Order.Close(context.Background(), input.OrderID) + if err != nil { + return nil, err + } + if order == nil { + return nil, fmt.Errorf("no order found with ID '%d'", input.OrderID) + } + orderMap := map[string]interface{}{ + "ID": order.Id, + "Email": order.Email, + "Note": order.Note, + } + return sdk.JSON(map[string]interface{}{ + "Closed order details": orderMap, + }), nil +} + +func (c *CloseOrderOperation) Test(ctx *sdk.RunContext) (sdk.JSON, error) { + return c.Run(ctx) +} + +func (c *CloseOrderOperation) GetInfo() *sdk.OperationInfo { + return c.options +} diff --git a/internal/connectors/shopify/operation_create_collect.go b/internal/connectors/shopify/operation_create_collect.go new file mode 100644 index 0000000..140c5d9 --- /dev/null +++ b/internal/connectors/shopify/operation_create_collect.go @@ -0,0 +1,97 @@ +// Copyright 2022-present Wakflo +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package shopify + +import ( + "context" + "errors" + "fmt" + + shopify "github.com/bold-commerce/go-shopify/v4" + + "github.com/wakflo/go-sdk/autoform" + sdk "github.com/wakflo/go-sdk/connector" + sdkcore "github.com/wakflo/go-sdk/core" +) + +type createCollectOperationProps struct { + ProductID uint64 `json:"productId"` + CollectionID uint64 `json:"collectionId"` +} +type CreateCollectOperation struct { + options *sdk.OperationInfo +} + +func NewCreateCollectOperation() *CreateCollectOperation { + return &CreateCollectOperation{ + options: &sdk.OperationInfo{ + Name: "Create Collect", + Description: "Add a product to a collection.", + RequireAuth: true, + Auth: sharedAuth, + Input: map[string]*sdkcore.AutoFormSchema{ + "productId": autoform.NewNumberField(). + SetDisplayName("Product ID"). + SetDescription("The ID of the product."). + SetRequired(true). + Build(), + "collectionID": autoform.NewNumberField(). + SetDisplayName("Collection ID"). + SetDescription("The ID of the product."). + SetRequired(true). + Build(), + }, + ErrorSettings: sdkcore.StepErrorSettings{ + ContinueOnError: false, + RetryOnError: false, + }, + }, + } +} + +func (c *CreateCollectOperation) Run(ctx *sdk.RunContext) (sdk.JSON, error) { + if ctx.Auth.Extra["token"] == "" { + return nil, errors.New("missing shopify auth token") + } + input := sdk.InputToType[createCollectOperationProps](ctx) + domain := ctx.Auth.Extra["domain"] + shopName := domain + ".myshopify.com" + + client := getShopifyClient(shopName, ctx.Auth.Extra["token"]) + + collect := shopify.Collect{ + CollectionId: input.CollectionID, + ProductId: input.ProductID, + } + + newCollect, err := client.Collect.Create(context.Background(), collect) + if err != nil { + return nil, err + } + if newCollect == nil { + return nil, fmt.Errorf("no collection found with ID '%d'", input.CollectionID) + } + return sdk.JSON(map[string]interface{}{ + "collection details": newCollect, + }), nil +} + +func (c *CreateCollectOperation) Test(ctx *sdk.RunContext) (sdk.JSON, error) { + return c.Run(ctx) +} + +func (c *CreateCollectOperation) GetInfo() *sdk.OperationInfo { + return c.options +} diff --git a/internal/connectors/shopify/operation_create_customer.go b/internal/connectors/shopify/operation_create_customer.go new file mode 100644 index 0000000..2f03974 --- /dev/null +++ b/internal/connectors/shopify/operation_create_customer.go @@ -0,0 +1,117 @@ +// Copyright 2022-present Wakflo +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package shopify + +import ( + "context" + "errors" + + shopify "github.com/bold-commerce/go-shopify/v4" + + "github.com/wakflo/go-sdk/autoform" + sdk "github.com/wakflo/go-sdk/connector" + sdkcore "github.com/wakflo/go-sdk/core" +) + +type createCustomerOperationProps struct { + FirstName string `json:"firstName"` + LastName string `json:"lastName"` + Phone string `json:"phone"` + Email string `json:"email"` + Tags string `json:"tags"` +} +type CreateCustomerOperation struct { + options *sdk.OperationInfo +} + +func NewCreateCustomerOperation() *CreateCustomerOperation { + return &CreateCustomerOperation{ + options: &sdk.OperationInfo{ + Name: "Create Customer", + Description: "Create a new customer.", + RequireAuth: true, + Auth: sharedAuth, + Input: map[string]*sdkcore.AutoFormSchema{ + "firstName": autoform.NewShortTextField(). + SetDisplayName("First name"). + SetDescription("Customer first name."). + SetRequired(false). + Build(), + "lastName": autoform.NewShortTextField(). + SetDisplayName("Last name"). + SetDescription("Customer last name."). + SetRequired(false). + Build(), + "phone": autoform.NewShortTextField(). + SetDisplayName("Phone"). + SetDescription("Customer phone number."). + SetRequired(false). + Build(), + "email": autoform.NewShortTextField(). + SetDisplayName("Email"). + SetDescription("Customer email address."). + SetRequired(false). + Build(), + "tags": autoform.NewLongTextField(). + SetDisplayName("Tags"). + SetDescription("A string of comma-separated tags for filtering and search"). + SetRequired(false). + Build(), + }, + ErrorSettings: sdkcore.StepErrorSettings{ + ContinueOnError: false, + RetryOnError: false, + }, + }, + } +} + +func (c *CreateCustomerOperation) Run(ctx *sdk.RunContext) (sdk.JSON, error) { + if ctx.Auth.Extra["token"] == "" { + return nil, errors.New("missing shopify auth token") + } + + input := sdk.InputToType[createCustomerOperationProps](ctx) + + domain := ctx.Auth.Extra["domain"] + shopName := domain + ".myshopify.com" + client := getShopifyClient(shopName, ctx.Auth.Extra["token"]) + + customer := shopify.Customer{ + FirstName: input.FirstName, + LastName: input.LastName, + Email: input.Email, + Tags: input.Tags, + Phone: input.Phone, + } + newCustomer, err := client.Customer.Create(context.Background(), customer) + if err != nil { + return nil, err + } + if newCustomer == nil { + return nil, errors.New("customer not created! ") + } + return map[string]interface{}{ + "new_customer": newCustomer, + }, nil +} + +func (c *CreateCustomerOperation) Test(ctx *sdk.RunContext) (sdk.JSON, error) { + return c.Run(ctx) +} + +func (c *CreateCustomerOperation) GetInfo() *sdk.OperationInfo { + return c.options +} diff --git a/internal/connectors/shopify/operation_create_draft_order.go b/internal/connectors/shopify/operation_create_draft_order.go new file mode 100644 index 0000000..fcd9a95 --- /dev/null +++ b/internal/connectors/shopify/operation_create_draft_order.go @@ -0,0 +1,134 @@ +// Copyright 2022-present Wakflo +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package shopify + +import ( + "context" + "errors" + + goshopify "github.com/bold-commerce/go-shopify/v4" + "github.com/shopspring/decimal" + + "github.com/wakflo/go-sdk/autoform" + sdk "github.com/wakflo/go-sdk/connector" + sdkcore "github.com/wakflo/go-sdk/core" +) + +type createDraftOrderOperationProps struct { + ProductID uint64 `json:"productId"` + LineItems []goshopify.LineItem `json:"line_items"` + VariantID uint64 `json:"variantId"` + CustomerID uint64 `json:"customerId"` + Note string `json:"note"` + Title string `json:"title"` + Quantity int `json:"quantity"` + Price *decimal.Decimal `json:"price"` + Tags string `json:"tags"` +} +type CreateDraftOrderOperation struct { + options *sdk.OperationInfo +} + +func NewCreateDraftOrderOperation() *CreateDraftOrderOperation { + return &CreateDraftOrderOperation{ + options: &sdk.OperationInfo{ + Name: "Create a draft order", + Description: "Create a new draft order.", + RequireAuth: true, + Auth: sharedAuth, + Input: map[string]*sdkcore.AutoFormSchema{ + "productId": autoform.NewNumberField(). + SetDisplayName("Product ID"). + SetDescription("The ID of the product to create the order with."). + SetRequired(false). + Build(), + "variantId": autoform.NewNumberField(). + SetDisplayName("Product Variant"). + SetDescription("The ID of the variant to create the order with."). + SetRequired(false). + Build(), + "customerId": autoform.NewNumberField(). + SetDisplayName("Customer ID"). + SetDescription("The ID of the customer to use."). + SetRequired(false). + Build(), + "title": autoform.NewShortTextField(). + SetDisplayName("Title"). + SetRequired(false). + Build(), + "note": autoform.NewLongTextField(). + SetDisplayName("Note about the order"). + SetRequired(false). + Build(), + "tags": autoform.NewLongTextField(). + SetDisplayName("A string of comma-separated tags for filtering and search"). + SetRequired(false). + Build(), + "quantity": autoform.NewNumberField(). + SetDisplayName("Quantity"). + SetDescription("The ID of the variant to create the order with."). + SetRequired(false). + SetDefaultValue(1). + Build(), + "price": autoform.NewShortTextField(). + SetDisplayName("Price"). + SetRequired(false). + Build(), + }, + ErrorSettings: sdkcore.StepErrorSettings{ + ContinueOnError: false, + RetryOnError: false, + }, + }, + } +} + +func (c *CreateDraftOrderOperation) Run(ctx *sdk.RunContext) (sdk.JSON, error) { + if ctx.Auth.Extra["token"] == "" { + return nil, errors.New("missing shopify auth token") + } + input := sdk.InputToType[createDraftOrderOperationProps](ctx) + domain := ctx.Auth.Extra["domain"] + shopName := domain + ".myshopify.com" + client := getShopifyClient(shopName, ctx.Auth.Extra["token"]) + newDraftOrder := goshopify.DraftOrder{ + LineItems: []goshopify.LineItem{ + { + ProductId: input.ProductID, + VariantId: input.VariantID, + Quantity: input.Quantity, + Price: input.Price, + Title: input.Title, + }, + }, + Note: input.Note, + Tags: input.Tags, + } + order, err := client.DraftOrder.Create(context.Background(), newDraftOrder) + if err != nil { + return nil, err + } + return map[string]interface{}{ + "new_draft_order": order, + }, nil +} + +func (c *CreateDraftOrderOperation) Test(ctx *sdk.RunContext) (sdk.JSON, error) { + return c.Run(ctx) +} + +func (c *CreateDraftOrderOperation) GetInfo() *sdk.OperationInfo { + return c.options +} diff --git a/internal/connectors/shopify/operation_create_order.go b/internal/connectors/shopify/operation_create_order.go new file mode 100644 index 0000000..610dd13 --- /dev/null +++ b/internal/connectors/shopify/operation_create_order.go @@ -0,0 +1,138 @@ +// Copyright 2022-present Wakflo +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package shopify + +import ( + "context" + "errors" + + goshopify "github.com/bold-commerce/go-shopify/v4" + "github.com/shopspring/decimal" + + "github.com/wakflo/go-sdk/autoform" + sdk "github.com/wakflo/go-sdk/connector" + sdkcore "github.com/wakflo/go-sdk/core" +) + +type createOrderOperationProps struct { + ProductID uint64 `json:"productId"` + LineItems []goshopify.LineItem `json:"line_items"` + VariantID uint64 `json:"variantId"` + CustomerID uint64 `json:"customerId"` + Title string `json:"title"` + Tags string `json:"tags"` + Note string `json:"note"` + Quantity int `json:"quantity"` + Price *decimal.Decimal `json:"price"` +} +type CreateOrderOperation struct { + options *sdk.OperationInfo +} + +func NewCreateOrderOperation() *CreateOrderOperation { + return &CreateOrderOperation{ + options: &sdk.OperationInfo{ + Name: "Create orders", + Description: "Create a new orders", + RequireAuth: true, + Auth: sharedAuth, + Input: map[string]*sdkcore.AutoFormSchema{ + "productId": autoform.NewNumberField(). + SetDisplayName("Product ID"). + SetDescription("The ID of the product to create the order with."). + SetRequired(false). + Build(), + "variantId": autoform.NewNumberField(). + SetDisplayName("Product Variant"). + SetDescription("The ID of the variant to create the order with."). + SetRequired(false). + Build(), + "customerId": autoform.NewNumberField(). + SetDisplayName("Customer ID"). + SetDescription("The ID of the customer to use."). + SetRequired(false). + Build(), + "title": autoform.NewShortTextField(). + SetDisplayName("Title"). + SetRequired(false). + Build(), + "note": autoform.NewLongTextField(). + SetDisplayName("Note about the order."). + SetRequired(false). + Build(), + "tags": autoform.NewLongTextField(). + SetDisplayName("Tags"). + SetDescription("A string of comma-separated tags for filtering and search"). + SetRequired(false). + Build(), + "quantity": autoform.NewNumberField(). + SetDisplayName("Quantity"). + SetDescription("The ID of the variant to create the order with."). + SetRequired(false). + SetDefaultValue(1). + Build(), + "price": autoform.NewShortTextField(). + SetDisplayName("Price"). + SetRequired(false). + Build(), + }, + ErrorSettings: sdkcore.StepErrorSettings{ + ContinueOnError: false, + RetryOnError: false, + }, + }, + } +} + +func (c *CreateOrderOperation) Run(ctx *sdk.RunContext) (sdk.JSON, error) { + if ctx.Auth.Extra["token"] == "" { + return nil, errors.New("missing shopify auth token") + } + input := sdk.InputToType[createOrderOperationProps](ctx) + domain := ctx.Auth.Extra["domain"] + shopName := domain + ".myshopify.com" + client := getShopifyClient(shopName, ctx.Auth.Extra["token"]) + newOrder := goshopify.Order{ + LineItems: []goshopify.LineItem{ + { + ProductId: input.ProductID, + VariantId: input.VariantID, + Quantity: input.Quantity, + Price: input.Price, + Title: input.Title, + }, + }, + Note: input.Note, + Tags: input.Tags, + Customer: &goshopify.Customer{ + Id: input.CustomerID, + }, + } + order, err := client.Order.Create(context.Background(), newOrder) + if err != nil { + return nil, err + } + return map[string]interface{}{ + "new_order": order, + }, nil +} + +func (c *CreateOrderOperation) Test(ctx *sdk.RunContext) (sdk.JSON, error) { + return c.Run(ctx) +} + +func (c *CreateOrderOperation) GetInfo() *sdk.OperationInfo { + return c.options +} diff --git a/internal/connectors/shopify/operation_create_product.go b/internal/connectors/shopify/operation_create_product.go new file mode 100644 index 0000000..0351026 --- /dev/null +++ b/internal/connectors/shopify/operation_create_product.go @@ -0,0 +1,123 @@ +// Copyright 2022-present Wakflo +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package shopify + +import ( + "context" + "errors" + + goshopify "github.com/bold-commerce/go-shopify/v4" + + "github.com/wakflo/go-sdk/autoform" + sdk "github.com/wakflo/go-sdk/connector" + sdkcore "github.com/wakflo/go-sdk/core" +) + +type createProductOperationProps struct { + Title string `json:"title"` + BodyHTML string `json:"bodyHTML"` + Vendor string `json:"vendor"` + ProductType string `json:"productType"` + Status string `json:"status"` + Tags string `json:"tags"` +} +type CreateProductOperation struct { + options *sdk.OperationInfo +} + +func NewCreateProductOperation() *CreateProductOperation { + return &CreateProductOperation{ + options: &sdk.OperationInfo{ + Name: "Create Product", + Description: "Create a new product.", + RequireAuth: true, + Auth: sharedAuth, + Input: map[string]*sdkcore.AutoFormSchema{ + "title": autoform.NewShortTextField(). + SetDisplayName("Product title"). + SetDescription("The title of the product."). + SetRequired(true). + Build(), + "bodyHTML": autoform.NewLongTextField(). + SetDisplayName("Product description"). + SetDescription("The description of the product."). + SetRequired(false). + Build(), + "vendor": autoform.NewShortTextField(). + SetDisplayName("Vendor"). + SetDescription("Vendor."). + SetRequired(false). + Build(), + "productType": autoform.NewShortTextField(). + SetDisplayName("Product type"). + SetDescription("A categorization for the product used for filtering and searching products."). + SetRequired(false). + Build(), + "tags": autoform.NewLongTextField(). + SetDisplayName("Tags"). + SetDescription("A string of comma-separated tags for filtering and search."). + SetRequired(false). + Build(), + "status": autoform.NewSelectField(). + SetDisplayName("Product status"). + SetDescription("The status of the product: active or draft"). + SetOptions(statusFormat). + SetRequired(false). + Build(), + }, + ErrorSettings: sdkcore.StepErrorSettings{ + ContinueOnError: false, + RetryOnError: false, + }, + }, + } +} + +func (c *CreateProductOperation) Run(ctx *sdk.RunContext) (sdk.JSON, error) { + if ctx.Auth.Extra["token"] == "" { + return nil, errors.New("missing shopify auth token") + } + input := sdk.InputToType[createProductOperationProps](ctx) + + domain := ctx.Auth.Extra["domain"] + shopName := domain + ".myshopify.com" + client := getShopifyClient(shopName, ctx.Auth.Extra["token"]) + + newProduct := goshopify.Product{ + Title: input.Title, + BodyHTML: input.BodyHTML, + Vendor: input.Vendor, + ProductType: input.ProductType, + Status: goshopify.ProductStatus(input.Status), + Tags: input.Tags, + } + product, err := client.Product.Create(context.Background(), newProduct) + if err != nil { + return nil, err + } + if product == nil { + return nil, errors.New("product not created ") + } + return map[string]interface{}{ + "new product": product, + }, nil +} + +func (c *CreateProductOperation) Test(ctx *sdk.RunContext) (sdk.JSON, error) { + return c.Run(ctx) +} + +func (c *CreateProductOperation) GetInfo() *sdk.OperationInfo { + return c.options +} diff --git a/internal/connectors/shopify/operation_create_transaction.go b/internal/connectors/shopify/operation_create_transaction.go new file mode 100644 index 0000000..415d756 --- /dev/null +++ b/internal/connectors/shopify/operation_create_transaction.go @@ -0,0 +1,129 @@ +// Copyright 2022-present Wakflo +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package shopify + +import ( + "context" + "errors" + "fmt" + + goshopify "github.com/bold-commerce/go-shopify/v4" + "github.com/shopspring/decimal" + + "github.com/wakflo/go-sdk/autoform" + sdk "github.com/wakflo/go-sdk/connector" + sdkcore "github.com/wakflo/go-sdk/core" +) + +type createTransactionOperationProps struct { + OrderID uint64 `json:"orderId"` + ParentID *int64 `json:"parentId"` + Kind string `json:"kind"` + Currency string `json:"currency"` + Amount *decimal.Decimal `json:"amount"` + Authorization string `json:"authorization"` + Source string `json:"source"` +} +type CreateTransactionOperation struct { + options *sdk.OperationInfo +} + +func NewCreateTransactionOperation() *CreateTransactionOperation { + return &CreateTransactionOperation{ + options: &sdk.OperationInfo{ + Name: "Create Transaction", + Description: "Create a new transaction.", + RequireAuth: true, + Auth: sharedAuth, + Input: map[string]*sdkcore.AutoFormSchema{ + "orderId": autoform.NewNumberField(). + SetDisplayName("Order ID"). + SetDescription("The ID of the order to create a transaction for."). + SetRequired(true). + Build(), + "kind": autoform.NewSelectField(). + SetDisplayName("Type"). + SetOptions(shopifyTransactionKinds). + SetRequired(true). + Build(), + "parentId": autoform.NewNumberField(). + SetDisplayName("Parent ID"). + SetDescription("The ID of an associated transaction."). + SetRequired(false). + Build(), + "currency": autoform.NewShortTextField(). + SetDisplayName("Currency"). + SetRequired(false). + Build(), + "amount": autoform.NewNumberField(). + SetDisplayName("Amount"). + SetRequired(false). + Build(), + "authorization": autoform.NewShortTextField(). + SetDisplayName("Authorization Key."). + SetRequired(false). + Build(), + "source": autoform.NewShortTextField(). + SetDisplayName("Source"). + SetDescription("An optional origin of the transaction. Set to external to import a cash transaction for the associated order."). + SetRequired(false). + Build(), + }, + ErrorSettings: sdkcore.StepErrorSettings{ + ContinueOnError: false, + RetryOnError: false, + }, + }, + } +} + +func (c *CreateTransactionOperation) Run(ctx *sdk.RunContext) (sdk.JSON, error) { + if ctx.Auth.Extra["token"] == "" { + return nil, errors.New("missing shopify auth token") + } + input := sdk.InputToType[createTransactionOperationProps](ctx) + domain := ctx.Auth.Extra["domain"] + shopName := domain + ".myshopify.com" + client := getShopifyClient(shopName, ctx.Auth.Extra["token"]) + newTransaction := goshopify.Transaction{ + ParentId: input.ParentID, + Kind: input.Kind, + OrderId: input.OrderID, + Amount: input.Amount, + Authorization: input.Authorization, + Currency: input.Currency, + Source: input.Source, + } + transaction, err := client.Transaction.Create(context.Background(), input.OrderID, newTransaction) + if err != nil { + return nil, err + } + + if transaction == nil { + return nil, fmt.Errorf("no order found with ID '%d'", input.OrderID) + } + + return map[string]interface{}{ + "new_transaction": transaction, + }, nil +} + +func (c *CreateTransactionOperation) Test(ctx *sdk.RunContext) (sdk.JSON, error) { + return c.Run(ctx) +} + +func (c *CreateTransactionOperation) GetInfo() *sdk.OperationInfo { + return c.options +} diff --git a/internal/connectors/shopify/operation_get_customer.go b/internal/connectors/shopify/operation_get_customer.go new file mode 100644 index 0000000..7031c2a --- /dev/null +++ b/internal/connectors/shopify/operation_get_customer.go @@ -0,0 +1,85 @@ +// Copyright 2022-present Wakflo +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package shopify + +import ( + "context" + "errors" + "fmt" + + "github.com/wakflo/go-sdk/autoform" + sdk "github.com/wakflo/go-sdk/connector" + sdkcore "github.com/wakflo/go-sdk/core" +) + +type getCustomerOperationProps struct { + CustomerID uint64 `json:"customerId"` +} +type GetCustomerOperation struct { + options *sdk.OperationInfo +} + +func NewGetCustomerOperation() *GetCustomerOperation { + return &GetCustomerOperation{ + options: &sdk.OperationInfo{ + Name: "Get Customer", + Description: "Get an existing customer's information.", + RequireAuth: true, + Auth: sharedAuth, + Input: map[string]*sdkcore.AutoFormSchema{ + "customerId": autoform.NewNumberField(). + SetDisplayName("Customer ID"). + SetDescription("The ID of the customer."). + SetRequired(true). + Build(), + }, + ErrorSettings: sdkcore.StepErrorSettings{ + ContinueOnError: false, + RetryOnError: false, + }, + }, + } +} + +func (c *GetCustomerOperation) Run(ctx *sdk.RunContext) (sdk.JSON, error) { + if ctx.Auth.Extra["token"] == "" { + return nil, errors.New("missing shopify auth token") + } + input := sdk.InputToType[getCustomerOperationProps](ctx) + + domain := ctx.Auth.Extra["domain"] + shopName := domain + ".myshopify.com" + client := getShopifyClient(shopName, ctx.Auth.Extra["token"]) + + customer, err := client.Customer.Get(context.Background(), input.CustomerID, nil) + if err != nil { + return nil, err + } + if customer == nil { + return nil, fmt.Errorf("no customer found with ID '%d'", input.CustomerID) + } + + return sdk.JSON(map[string]interface{}{ + "raw_customer": customer, + }), nil +} + +func (c *GetCustomerOperation) Test(ctx *sdk.RunContext) (sdk.JSON, error) { + return c.Run(ctx) +} + +func (c *GetCustomerOperation) GetInfo() *sdk.OperationInfo { + return c.options +} diff --git a/internal/connectors/shopify/operation_get_customer_orders.go b/internal/connectors/shopify/operation_get_customer_orders.go new file mode 100644 index 0000000..56a3153 --- /dev/null +++ b/internal/connectors/shopify/operation_get_customer_orders.go @@ -0,0 +1,106 @@ +// Copyright 2022-present Wakflo +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package shopify + +import ( + "context" + "errors" + "fmt" + + goshopify "github.com/bold-commerce/go-shopify/v4" + + "github.com/wakflo/go-sdk/autoform" + sdk "github.com/wakflo/go-sdk/connector" + sdkcore "github.com/wakflo/go-sdk/core" +) + +type getCustomerOrdersOperationProps struct { + CustomerID uint64 `json:"customerId"` +} +type GetCustomerOrdersOperation struct { + options *sdk.OperationInfo +} + +func NewGetCustomerOrdersOperation() *GetCustomerOrdersOperation { + return &GetCustomerOrdersOperation{ + options: &sdk.OperationInfo{ + Name: "Get Customer Orders", + Description: "Get an existing customer's orders.", + RequireAuth: true, + Auth: sharedAuth, + Input: map[string]*sdkcore.AutoFormSchema{ + "customerId": autoform.NewNumberField(). + SetDisplayName("Customer ID"). + SetDescription("The ID of the customer."). + SetRequired(true). + Build(), + }, + ErrorSettings: sdkcore.StepErrorSettings{ + ContinueOnError: false, + RetryOnError: false, + }, + }, + } +} + +func (c *GetCustomerOrdersOperation) Run(ctx *sdk.RunContext) (sdk.JSON, error) { + if ctx.Auth.Extra["token"] == "" { + return nil, errors.New("missing shopify auth token") + } + input := sdk.InputToType[getCustomerOrdersOperationProps](ctx) + domain := ctx.Auth.Extra["domain"] + shopName := domain + ".myshopify.com" + client := getShopifyClient(shopName, ctx.Auth.Extra["token"]) + orders, err := client.Order.List(context.Background(), nil) + if err != nil { + return nil, fmt.Errorf("failed to get customer orders: %w", err) + } + customerOrders := filterOrdersByCustomerID(orders, input.CustomerID) + simplifiedOrders := make([]map[string]interface{}, len(customerOrders)) + for i, order := range customerOrders { + simplifiedOrders[i] = map[string]interface{}{ + "ID": order.Id, + "Name": order.Name, + "Email": order.Email, + "CreatedAt": order.CreatedAt, + "UpdatedAt": order.UpdatedAt, + "TotalPrice": order.TotalPrice, + "Currency": order.Currency, + "FinancialStatus": order.FinancialStatus, + "FulfillmentStatus": order.FulfillmentStatus, + } + } + return map[string]interface{}{ + "customer_orders": simplifiedOrders, + }, nil +} + +func filterOrdersByCustomerID(orders []goshopify.Order, customerID uint64) []goshopify.Order { + var customerOrders []goshopify.Order + for _, order := range orders { + if order.Customer != nil && order.Customer.Id == customerID { + customerOrders = append(customerOrders, order) + } + } + return customerOrders +} + +func (c *GetCustomerOrdersOperation) Test(ctx *sdk.RunContext) (sdk.JSON, error) { + return c.Run(ctx) +} + +func (c *GetCustomerOrdersOperation) GetInfo() *sdk.OperationInfo { + return c.options +} diff --git a/internal/connectors/shopify/operation_get_locations.go b/internal/connectors/shopify/operation_get_locations.go new file mode 100644 index 0000000..20fda7e --- /dev/null +++ b/internal/connectors/shopify/operation_get_locations.go @@ -0,0 +1,80 @@ +// Copyright 2022-present Wakflo +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package shopify + +import ( + "context" + "errors" + + sdk "github.com/wakflo/go-sdk/connector" + sdkcore "github.com/wakflo/go-sdk/core" +) + +type GetLocationsOperation struct { + options *sdk.OperationInfo +} + +func NewGetLocationsOperation() *GetLocationsOperation { + return &GetLocationsOperation{ + options: &sdk.OperationInfo{ + Name: "Get Locations", + Description: "get locations", + RequireAuth: true, + Auth: sharedAuth, + ErrorSettings: sdkcore.StepErrorSettings{ + ContinueOnError: false, + RetryOnError: false, + }, + }, + } +} + +func (c *GetLocationsOperation) Run(ctx *sdk.RunContext) (sdk.JSON, error) { + if ctx.Auth.Extra["token"] == "" { + return nil, errors.New("missing shopify auth token") + } + domain := ctx.Auth.Extra["domain"] + shopName := domain + ".myshopify.com" + client := getShopifyClient(shopName, ctx.Auth.Extra["token"]) + locations, err := client.Location.List(context.Background(), nil) + if err != nil { + return nil, err + } + simplifiedLocations := make([]map[string]interface{}, len(locations)) + for i, location := range locations { + simplifiedLocations[i] = map[string]interface{}{ + "Id": location.Id, + "Name": location.Name, + "Address1": location.Address1, + "Address2": location.Address2, + "City": location.City, + "Zip": location.Zip, + "Country": location.Country, + "Phone": location.Phone, + "Active": location.Active, + } + } + return map[string]interface{}{ + "locations": simplifiedLocations, + }, nil +} + +func (c *GetLocationsOperation) Test(ctx *sdk.RunContext) (sdk.JSON, error) { + return c.Run(ctx) +} + +func (c *GetLocationsOperation) GetInfo() *sdk.OperationInfo { + return c.options +} diff --git a/internal/connectors/shopify/operation_get_order.go b/internal/connectors/shopify/operation_get_order.go new file mode 100644 index 0000000..064f43e --- /dev/null +++ b/internal/connectors/shopify/operation_get_order.go @@ -0,0 +1,85 @@ +// Copyright 2022-present Wakflo +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package shopify + +import ( + "context" + "errors" + "fmt" + + "github.com/wakflo/go-sdk/autoform" + sdk "github.com/wakflo/go-sdk/connector" + sdkcore "github.com/wakflo/go-sdk/core" +) + +type getOrderOperationProps struct { + OrderID uint64 `json:"orderId"` +} +type GetOrderOperation struct { + options *sdk.OperationInfo +} + +func NewGetOrderOperation() *GetOrderOperation { + return &GetOrderOperation{ + options: &sdk.OperationInfo{ + Name: "Get Order", + Description: "Get an existing order", + RequireAuth: true, + Auth: sharedAuth, + Input: map[string]*sdkcore.AutoFormSchema{ + "orderId": autoform.NewNumberField(). + SetDisplayName("Order ID"). + SetDescription("The ID of the order."). + SetRequired(true). + Build(), + }, + ErrorSettings: sdkcore.StepErrorSettings{ + ContinueOnError: false, + RetryOnError: false, + }, + }, + } +} + +func (c *GetOrderOperation) Run(ctx *sdk.RunContext) (sdk.JSON, error) { + if ctx.Auth.Extra["token"] == "" { + return nil, errors.New("missing shopify auth token") + } + input := sdk.InputToType[getOrderOperationProps](ctx) + + domain := ctx.Auth.Extra["domain"] + shopName := domain + ".myshopify.com" + + client := getShopifyClient(shopName, ctx.Auth.Extra["token"]) + + order, err := client.Order.Get(context.Background(), input.OrderID, nil) + if err != nil { + return nil, err + } + if order == nil { + return nil, fmt.Errorf("no order found with ID '%d'", input.OrderID) + } + return sdk.JSON(map[string]interface{}{ + "order details": order, + }), nil +} + +func (c *GetOrderOperation) Test(ctx *sdk.RunContext) (sdk.JSON, error) { + return c.Run(ctx) +} + +func (c *GetOrderOperation) GetInfo() *sdk.OperationInfo { + return c.options +} diff --git a/internal/connectors/shopify/operation_get_product.go b/internal/connectors/shopify/operation_get_product.go new file mode 100644 index 0000000..12356ad --- /dev/null +++ b/internal/connectors/shopify/operation_get_product.go @@ -0,0 +1,94 @@ +// Copyright 2022-present Wakflo +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package shopify + +import ( + "context" + "errors" + "fmt" + + "github.com/wakflo/go-sdk/autoform" + sdk "github.com/wakflo/go-sdk/connector" + sdkcore "github.com/wakflo/go-sdk/core" +) + +type getProductOperationProps struct { + ProductID uint64 `json:"productId"` +} + +type GetProductOperation struct { + options *sdk.OperationInfo +} + +func NewGetProductOperation() *GetProductOperation { + return &GetProductOperation{ + options: &sdk.OperationInfo{ + Name: "Get Product", + Description: "Get an existing product by id.", + RequireAuth: true, + Auth: sharedAuth, + Input: map[string]*sdkcore.AutoFormSchema{ + "productId": autoform.NewNumberField(). + SetDisplayName("Product"). + SetDescription("The ID of the product."). + SetRequired(true). + Build(), + }, + ErrorSettings: sdkcore.StepErrorSettings{ + ContinueOnError: false, + RetryOnError: false, + }, + }, + } +} + +func (c *GetProductOperation) Run(ctx *sdk.RunContext) (sdk.JSON, error) { + fmt.Println("testtttttttt") + + if ctx.Auth.Extra["token"] == "" { + return nil, errors.New("missing shopify auth token") + } + + input := sdk.InputToType[getProductOperationProps](ctx) + domain := ctx.Auth.Extra["domain"] + shopName := domain + ".myshopify.com" + client := getShopifyClient(shopName, ctx.Auth.Extra["token"]) + + product, err := client.Product.Get(context.Background(), input.ProductID, nil) + if err != nil { + return nil, err + } + if product == nil { + return nil, fmt.Errorf("no product found with ID '%d'", input.ProductID) + } + productMap := map[string]interface{}{ + "ID": product.Id, + "Title": product.Title, + "Description": product.BodyHTML, + "Price": product.Variants[0].Price, + "Variants": product.Variants, + } + return sdk.JSON(map[string]interface{}{ + "product details": productMap, + }), err +} + +func (c *GetProductOperation) Test(ctx *sdk.RunContext) (sdk.JSON, error) { + return c.Run(ctx) +} + +func (c *GetProductOperation) GetInfo() *sdk.OperationInfo { + return c.options +} diff --git a/internal/connectors/shopify/operation_get_product_variant.go b/internal/connectors/shopify/operation_get_product_variant.go new file mode 100644 index 0000000..38dac58 --- /dev/null +++ b/internal/connectors/shopify/operation_get_product_variant.go @@ -0,0 +1,95 @@ +// Copyright 2022-present Wakflo +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package shopify + +import ( + "context" + "errors" + "fmt" + + "github.com/wakflo/go-sdk/autoform" + sdk "github.com/wakflo/go-sdk/connector" + sdkcore "github.com/wakflo/go-sdk/core" +) + +type getProductVariantOperationProps struct { + ProductID uint64 `json:"productId"` + VariantID uint64 `json:"variantId"` +} +type GetProductVariantOperation struct { + options *sdk.OperationInfo +} + +func NewGetProductVariantOperation() *GetProductVariantOperation { + return &GetProductVariantOperation{ + options: &sdk.OperationInfo{ + Name: "Get Product Variant", + Description: "Get a product variant", + RequireAuth: true, + Auth: sharedAuth, + Input: map[string]*sdkcore.AutoFormSchema{ + "productId": autoform.NewNumberField(). + SetDisplayName("Product ID"). + SetDescription("ID of product"). + SetRequired(false). + Build(), + "variantId": autoform.NewNumberField(). + SetDisplayName("Variant ID"). + SetDescription("product variant ID"). + SetRequired(false). + Build(), + }, + ErrorSettings: sdkcore.StepErrorSettings{ + ContinueOnError: false, + RetryOnError: false, + }, + }, + } +} + +func (c *GetProductVariantOperation) Run(ctx *sdk.RunContext) (sdk.JSON, error) { + if ctx.Auth.Extra["token"] == "" { + return nil, errors.New("missing shopify auth token") + } + + input := sdk.InputToType[getProductVariantOperationProps](ctx) + + domain := ctx.Auth.Extra["domain"] + shopName := domain + ".myshopify.com" + + client := getShopifyClient(shopName, ctx.Auth.Extra["token"]) + + product, err := client.Product.Get(context.Background(), input.ProductID, nil) + if err != nil { + return nil, err + } + if product == nil { + return nil, fmt.Errorf("no product variant found with ID '%d'", input.ProductID) + } + for _, variant := range product.Variants { + if variant.Id == input.VariantID { + return variant, nil + } + } + return nil, nil +} + +func (c *GetProductVariantOperation) Test(ctx *sdk.RunContext) (sdk.JSON, error) { + return c.Run(ctx) +} + +func (c *GetProductVariantOperation) GetInfo() *sdk.OperationInfo { + return c.options +} diff --git a/internal/connectors/shopify/operation_get_transaction.go b/internal/connectors/shopify/operation_get_transaction.go new file mode 100644 index 0000000..f113a91 --- /dev/null +++ b/internal/connectors/shopify/operation_get_transaction.go @@ -0,0 +1,93 @@ +// Copyright 2022-present Wakflo +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package shopify + +import ( + "context" + "errors" + + "github.com/wakflo/go-sdk/autoform" + sdk "github.com/wakflo/go-sdk/connector" + sdkcore "github.com/wakflo/go-sdk/core" +) + +type getTransactionOperationProps struct { + OrderID uint64 `json:"orderId"` + TransactionID uint64 `json:"transactionId"` +} + +type GetTransactionOperation struct { + options *sdk.OperationInfo +} + +func NewGetTransactionOperation() *GetTransactionOperation { + return &GetTransactionOperation{ + options: &sdk.OperationInfo{ + Name: "Get Transaction", + Description: "Get an existing transaction's information.", + RequireAuth: true, + Auth: sharedAuth, + Input: map[string]*sdkcore.AutoFormSchema{ + "orderId": autoform.NewNumberField(). + SetDisplayName("Order ID"). + SetDescription("The ID of the order."). + SetRequired(true). + Build(), + "transactionId": autoform.NewNumberField(). + SetDisplayName("Transaction ID"). + SetDescription("The ID of the transaction."). + SetRequired(true). + Build(), + }, + ErrorSettings: sdkcore.StepErrorSettings{ + ContinueOnError: false, + RetryOnError: false, + }, + }, + } +} + +func (c *GetTransactionOperation) Run(ctx *sdk.RunContext) (sdk.JSON, error) { + if ctx.Auth.Extra["token"] == "" { + return nil, errors.New("missing shopify auth token") + } + + input := sdk.InputToType[getTransactionOperationProps](ctx) + + domain := ctx.Auth.Extra["domain"] + shopName := domain + ".myshopify.com" + client := getShopifyClient(shopName, ctx.Auth.Extra["token"]) + + transaction, err := client.Transaction.Get(context.Background(), input.OrderID, input.TransactionID, nil) + if err != nil { + return nil, err + } + + if transaction == nil { + return nil, errors.New("no transaction found with ID ") + } + + return sdk.JSON(map[string]interface{}{ + "transaction": transaction, + }), nil +} + +func (c *GetTransactionOperation) Test(ctx *sdk.RunContext) (sdk.JSON, error) { + return c.Run(ctx) +} + +func (c *GetTransactionOperation) GetInfo() *sdk.OperationInfo { + return c.options +} diff --git a/internal/connectors/shopify/operation_get_transactions.go b/internal/connectors/shopify/operation_get_transactions.go new file mode 100644 index 0000000..a2f7f5d --- /dev/null +++ b/internal/connectors/shopify/operation_get_transactions.go @@ -0,0 +1,84 @@ +// Copyright 2022-present Wakflo +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package shopify + +import ( + "context" + "errors" + + "github.com/wakflo/go-sdk/autoform" + sdk "github.com/wakflo/go-sdk/connector" + sdkcore "github.com/wakflo/go-sdk/core" +) + +type getTransactionsOperationProps struct { + OrderID uint64 `json:"orderId"` +} +type GetTransactionsOperation struct { + options *sdk.OperationInfo +} + +func NewGetTransactionsOperation() *GetTransactionsOperation { + return &GetTransactionsOperation{ + options: &sdk.OperationInfo{ + Name: "Get Order Transactions", + Description: "Get an order's transactions.", + RequireAuth: true, + Auth: sharedAuth, + Input: map[string]*sdkcore.AutoFormSchema{ + "orderId": autoform.NewNumberField(). + SetDisplayName("Order ID"). + SetDescription("The ID of the order."). + SetRequired(true). + Build(), + }, + ErrorSettings: sdkcore.StepErrorSettings{ + ContinueOnError: false, + RetryOnError: false, + }, + }, + } +} + +func (c *GetTransactionsOperation) Run(ctx *sdk.RunContext) (sdk.JSON, error) { + if ctx.Auth.Extra["token"] == "" { + return nil, errors.New("missing shopify auth token") + } + + input := sdk.InputToType[getTransactionsOperationProps](ctx) + domain := ctx.Auth.Extra["domain"] + shopName := domain + ".myshopify.com" + client := getShopifyClient(shopName, ctx.Auth.Extra["token"]) + + transactions, err := client.Transaction.List(context.Background(), input.OrderID, nil) + if err != nil { + return nil, err + } + + if transactions == nil { + return nil, errors.New("no transactions found with ID ") + } + return sdk.JSON(map[string]interface{}{ + "transactions_details": transactions, + }), nil +} + +func (c *GetTransactionsOperation) Test(ctx *sdk.RunContext) (sdk.JSON, error) { + return c.Run(ctx) +} + +func (c *GetTransactionsOperation) GetInfo() *sdk.OperationInfo { + return c.options +} diff --git a/internal/connectors/shopify/operation_list_customers.go b/internal/connectors/shopify/operation_list_customers.go new file mode 100644 index 0000000..226aad5 --- /dev/null +++ b/internal/connectors/shopify/operation_list_customers.go @@ -0,0 +1,80 @@ +// Copyright 2022-present Wakflo +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package shopify + +import ( + "context" + "errors" + + "github.com/wakflo/go-sdk/autoform" + + sdk "github.com/wakflo/go-sdk/connector" + sdkcore "github.com/wakflo/go-sdk/core" +) + +type ListCustomersOperation struct { + options *sdk.OperationInfo +} + +func NewListCustomersOperation() *ListCustomersOperation { + return &ListCustomersOperation{ + options: &sdk.OperationInfo{ + Name: "List Customers", + Description: "List all Customers in store", + RequireAuth: true, + Auth: sharedAuth, + Input: map[string]*sdkcore.AutoFormSchema{ + "orderId": autoform.NewNumberField(). + SetDisplayName(""). + SetDescription(""). + Build(), + }, + ErrorSettings: sdkcore.StepErrorSettings{ + ContinueOnError: false, + RetryOnError: false, + }, + }, + } +} + +func (c *ListCustomersOperation) Run(ctx *sdk.RunContext) (sdk.JSON, error) { + if ctx.Auth.Extra["token"] == "" { + return nil, errors.New("missing shopify auth token") + } + + domain := ctx.Auth.Extra["domain"] + shopName := domain + ".myshopify.com" + client := getShopifyClient(shopName, ctx.Auth.Extra["token"]) + + customers, err := client.Customer.List(context.Background(), nil) + if err != nil { + return nil, err + } + if customers == nil { + return nil, errors.New("no customer found") + } + + return sdk.JSON(map[string]interface{}{ + "customers details": customers, + }), err +} + +func (c *ListCustomersOperation) Test(ctx *sdk.RunContext) (sdk.JSON, error) { + return c.Run(ctx) +} + +func (c *ListCustomersOperation) GetInfo() *sdk.OperationInfo { + return c.options +} diff --git a/internal/connectors/shopify/operation_list_draft_orders.go b/internal/connectors/shopify/operation_list_draft_orders.go new file mode 100644 index 0000000..e20b5c7 --- /dev/null +++ b/internal/connectors/shopify/operation_list_draft_orders.go @@ -0,0 +1,79 @@ +// Copyright 2022-present Wakflo +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package shopify + +import ( + "context" + "errors" + + "github.com/wakflo/go-sdk/autoform" + sdk "github.com/wakflo/go-sdk/connector" + sdkcore "github.com/wakflo/go-sdk/core" +) + +type ListDraftOrdersOperation struct { + options *sdk.OperationInfo +} + +func NewListDraftOrdersOperation() *ListDraftOrdersOperation { + return &ListDraftOrdersOperation{ + options: &sdk.OperationInfo{ + Name: "List Draft Orders", + Description: "List Draft Orders", + RequireAuth: true, + Auth: sharedAuth, + Input: map[string]*sdkcore.AutoFormSchema{ + "projectId": autoform.NewShortTextField(). + SetDisplayName(""). + SetDescription(""). + Build(), + }, + ErrorSettings: sdkcore.StepErrorSettings{ + ContinueOnError: false, + RetryOnError: false, + }, + }, + } +} + +func (c *ListDraftOrdersOperation) Run(ctx *sdk.RunContext) (sdk.JSON, error) { + if ctx.Auth.Extra["token"] == "" { + return nil, errors.New("missing shopify auth token") + } + + domain := ctx.Auth.Extra["domain"] + shopName := domain + ".myshopify.com" + client := getShopifyClient(shopName, ctx.Auth.Extra["token"]) + + darftOrders, err := client.DraftOrder.List(context.Background(), nil) + if err != nil { + return nil, err + } + if darftOrders == nil { + return nil, errors.New("no draft orders found") + } + + return sdk.JSON(map[string]interface{}{ + "Draft orders": darftOrders, + }), err +} + +func (c *ListDraftOrdersOperation) Test(ctx *sdk.RunContext) (sdk.JSON, error) { + return c.Run(ctx) +} + +func (c *ListDraftOrdersOperation) GetInfo() *sdk.OperationInfo { + return c.options +} diff --git a/internal/connectors/shopify/operation_list_orders.go b/internal/connectors/shopify/operation_list_orders.go new file mode 100644 index 0000000..1b8fd25 --- /dev/null +++ b/internal/connectors/shopify/operation_list_orders.go @@ -0,0 +1,79 @@ +// Copyright 2022-present Wakflo +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package shopify + +import ( + "context" + "errors" + + "github.com/wakflo/go-sdk/autoform" + sdk "github.com/wakflo/go-sdk/connector" + sdkcore "github.com/wakflo/go-sdk/core" +) + +type ListOrdersOperation struct { + options *sdk.OperationInfo +} + +func NewListOrdersOperation() *ListOrdersOperation { + return &ListOrdersOperation{ + options: &sdk.OperationInfo{ + Name: "List Orders", + Description: "List Orders", + RequireAuth: true, + Auth: sharedAuth, + Input: map[string]*sdkcore.AutoFormSchema{ + "projectId": autoform.NewShortTextField(). + SetDisplayName(""). + SetDescription(""). + Build(), + }, + ErrorSettings: sdkcore.StepErrorSettings{ + ContinueOnError: false, + RetryOnError: false, + }, + }, + } +} + +func (c *ListOrdersOperation) Run(ctx *sdk.RunContext) (sdk.JSON, error) { + if ctx.Auth.Extra["token"] == "" { + return nil, errors.New("missing shopify auth token") + } + + domain := ctx.Auth.Extra["domain"] + shopName := domain + ".myshopify.com" + client := getShopifyClient(shopName, ctx.Auth.Extra["token"]) + + orders, err := client.Order.List(context.Background(), nil) + if err != nil { + return nil, err + } + if orders == nil { + return nil, errors.New("no orders found") + } + + return sdk.JSON(map[string]interface{}{ + "Orders": orders, + }), err +} + +func (c *ListOrdersOperation) Test(ctx *sdk.RunContext) (sdk.JSON, error) { + return c.Run(ctx) +} + +func (c *ListOrdersOperation) GetInfo() *sdk.OperationInfo { + return c.options +} diff --git a/internal/connectors/shopify/operation_list_product.go b/internal/connectors/shopify/operation_list_product.go index 1a855fb..31d0bf0 100644 --- a/internal/connectors/shopify/operation_list_product.go +++ b/internal/connectors/shopify/operation_list_product.go @@ -15,6 +15,9 @@ package shopify import ( + "context" + "errors" + "github.com/wakflo/go-sdk/autoform" sdk "github.com/wakflo/go-sdk/connector" sdkcore "github.com/wakflo/go-sdk/core" @@ -27,15 +30,14 @@ type ListProductsOperation struct { func NewListProductsOperation() *ListProductsOperation { return &ListProductsOperation{ options: &sdk.OperationInfo{ - Name: "Create New File", - Description: "operation creates new google drive file", + Name: "List Products", + Description: "Count total amount of products in store", RequireAuth: true, Auth: sharedAuth, Input: map[string]*sdkcore.AutoFormSchema{ - "projectId": autoform.NewLongTextField(). - SetDisplayName("Project ID"). - SetDescription("project id"). - SetRequired(true). + "projectId": autoform.NewShortTextField(). + SetDisplayName(""). + SetDescription(""). Build(), }, ErrorSettings: sdkcore.StepErrorSettings{ @@ -47,13 +49,25 @@ func NewListProductsOperation() *ListProductsOperation { } func (c *ListProductsOperation) Run(ctx *sdk.RunContext) (sdk.JSON, error) { - auth, err := ctx.Auth.GetCustomAuth() + if ctx.Auth.Extra["token"] == "" { + return nil, errors.New("missing shopify auth token") + } + + domain := ctx.Auth.Extra["domain"] + shopName := domain + ".myshopify.com" + client := getShopifyClient(shopName, ctx.Auth.Extra["token"]) + + products, err := client.Product.List(context.Background(), nil) if err != nil { return nil, err } + if products == nil { + return nil, errors.New("no products found") + } - // fake implementation to prove test - return auth, nil + return sdk.JSON(map[string]interface{}{ + "Total count of products": products, + }), err } func (c *ListProductsOperation) Test(ctx *sdk.RunContext) (sdk.JSON, error) { diff --git a/internal/connectors/shopify/operation_update_customer.go b/internal/connectors/shopify/operation_update_customer.go new file mode 100644 index 0000000..f708ea9 --- /dev/null +++ b/internal/connectors/shopify/operation_update_customer.go @@ -0,0 +1,127 @@ +// Copyright 2022-present Wakflo +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package shopify + +import ( + "context" + "errors" + + "github.com/wakflo/go-sdk/autoform" + sdk "github.com/wakflo/go-sdk/connector" + sdkcore "github.com/wakflo/go-sdk/core" +) + +type updateCustomerOperationProps struct { + CustomerID uint64 `json:"customerId"` + FirstName string `json:"firstName"` + LastName string `json:"lastName"` + Phone string `json:"phone"` + Email string `json:"email"` + Tags string `json:"tags"` +} +type UpdateCustomerOperation struct { + options *sdk.OperationInfo +} + +func NewUpdateCustomerOperation() *UpdateCustomerOperation { + return &UpdateCustomerOperation{ + options: &sdk.OperationInfo{ + Name: "Update Customer", + Description: "Update an existing customer.", + RequireAuth: true, + Auth: sharedAuth, + Input: map[string]*sdkcore.AutoFormSchema{ + "customerId": autoform.NewNumberField(). + SetDisplayName("Customer ID"). + SetDescription("The id of the customer to update."). + SetRequired(true). + Build(), + "firstName": autoform.NewShortTextField(). + SetDisplayName("First name"). + SetDescription("Customer first name."). + SetRequired(false). + Build(), + "lastName": autoform.NewShortTextField(). + SetDisplayName("Last name"). + SetDescription("Customer last name."). + SetRequired(false). + Build(), + "phone": autoform.NewShortTextField(). + SetDisplayName("Phone"). + SetDescription("Customer phone number."). + SetRequired(false). + Build(), + "email": autoform.NewShortTextField(). + SetDisplayName("Email"). + SetDescription("Customer email address."). + SetRequired(false). + Build(), + "tags": autoform.NewLongTextField(). + SetDisplayName("Tags"). + SetDescription("A string of comma-separated tags for filtering and search"). + SetRequired(false). + Build(), + }, + ErrorSettings: sdkcore.StepErrorSettings{ + ContinueOnError: false, + RetryOnError: false, + }, + }, + } +} + +func (c *UpdateCustomerOperation) Run(ctx *sdk.RunContext) (sdk.JSON, error) { + if ctx.Auth.Extra["token"] == "" { + return nil, errors.New("missing shopify auth token") + } + input := sdk.InputToType[updateCustomerOperationProps](ctx) + domain := ctx.Auth.Extra["domain"] + shopName := domain + ".myshopify.com" + client := getShopifyClient(shopName, ctx.Auth.Extra["token"]) + existingCustomer, err := client.Customer.Get(context.Background(), input.CustomerID, nil) + if err != nil { + return nil, err + } + if input.FirstName != "" { + existingCustomer.FirstName = input.FirstName + } + if input.LastName != "" { + existingCustomer.LastName = input.LastName + } + if input.Email != "" { + existingCustomer.Email = input.Email + } + if input.Phone != "" { + existingCustomer.Phone = input.Phone + } + if input.Tags != "" { + existingCustomer.Tags = input.Tags + } + updatedCustomer, err := client.Customer.Update(context.Background(), *existingCustomer) + if err != nil { + return nil, errors.New("failed to update customer") + } + return map[string]interface{}{ + "updated_customer": updatedCustomer, + }, nil +} + +func (c *UpdateCustomerOperation) Test(ctx *sdk.RunContext) (sdk.JSON, error) { + return c.Run(ctx) +} + +func (c *UpdateCustomerOperation) GetInfo() *sdk.OperationInfo { + return c.options +} diff --git a/internal/connectors/shopify/operation_update_order.go b/internal/connectors/shopify/operation_update_order.go new file mode 100644 index 0000000..bf0537b --- /dev/null +++ b/internal/connectors/shopify/operation_update_order.go @@ -0,0 +1,115 @@ +// Copyright 2022-present Wakflo +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package shopify + +import ( + "context" + "errors" + + "github.com/wakflo/go-sdk/autoform" + sdk "github.com/wakflo/go-sdk/connector" + sdkcore "github.com/wakflo/go-sdk/core" +) + +type updateOrderOperationProps struct { + OrderID uint64 `json:"orderId"` + Tags string `json:"tags"` + Note string `json:"note"` + Phone string `json:"phone"` + Email string `json:"email"` +} +type UpdateOrderOperation struct { + options *sdk.OperationInfo +} + +func NewUpdateOrderOperation() *UpdateOrderOperation { + return &UpdateOrderOperation{ + options: &sdk.OperationInfo{ + Name: "Update orders", + Description: "Update an existing order", + RequireAuth: true, + Auth: sharedAuth, + Input: map[string]*sdkcore.AutoFormSchema{ + "orderId": autoform.NewNumberField(). + SetDisplayName("Order ID"). + SetDescription("The ID of the order to update"). + SetRequired(true). + Build(), + "note": autoform.NewLongTextField(). + SetDisplayName("Note about the order."). + SetRequired(false). + Build(), + "tags": autoform.NewLongTextField(). + SetDisplayName("Tags"). + SetDescription("A string of comma-separated tags for filtering and search."). + SetRequired(false). + Build(), + "phone": autoform.NewShortTextField(). + SetDisplayName("Phone number."). + SetRequired(false). + Build(), + "email": autoform.NewShortTextField(). + SetDisplayName("Email address."). + SetRequired(false). + Build(), + }, + ErrorSettings: sdkcore.StepErrorSettings{ + ContinueOnError: false, + RetryOnError: false, + }, + }, + } +} + +func (c *UpdateOrderOperation) Run(ctx *sdk.RunContext) (sdk.JSON, error) { + if ctx.Auth.Extra["token"] == "" { + return nil, errors.New("missing shopify auth token") + } + input := sdk.InputToType[updateOrderOperationProps](ctx) + domain := ctx.Auth.Extra["domain"] + shopName := domain + ".myshopify.com" + client := getShopifyClient(shopName, ctx.Auth.Extra["token"]) + existingOrder, err := client.Order.Get(context.Background(), input.OrderID, nil) + if err != nil { + return nil, err + } + if input.Note != "" { + existingOrder.Note = input.Note + } + if input.Tags != "" { + existingOrder.Tags = input.Tags + } + if input.Phone != "" { + existingOrder.Phone = input.Phone + } + if input.Email != "" { + existingOrder.Email = input.Email + } + updatedOrder, err := client.Order.Update(context.Background(), *existingOrder) + if err != nil { + return nil, err + } + return map[string]interface{}{ + "updated_order": updatedOrder, + }, nil +} + +func (c *UpdateOrderOperation) Test(ctx *sdk.RunContext) (sdk.JSON, error) { + return c.Run(ctx) +} + +func (c *UpdateOrderOperation) GetInfo() *sdk.OperationInfo { + return c.options +} diff --git a/internal/connectors/shopify/operation_update_product.go b/internal/connectors/shopify/operation_update_product.go new file mode 100644 index 0000000..b6384d6 --- /dev/null +++ b/internal/connectors/shopify/operation_update_product.go @@ -0,0 +1,142 @@ +// Copyright 2022-present Wakflo +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package shopify + +import ( + "context" + "errors" + "fmt" + + goshopify "github.com/bold-commerce/go-shopify/v4" + + "github.com/wakflo/go-sdk/autoform" + sdk "github.com/wakflo/go-sdk/connector" + sdkcore "github.com/wakflo/go-sdk/core" +) + +type updateProductOperationProps struct { + ProductID uint64 `json:"productID"` + Title string `json:"title"` + BodyHTML string `json:"bodyHTML"` + Vendor string `json:"vendor"` + ProductType string `json:"productType"` + Status string `json:"status"` + Tags string `json:"tags"` +} +type UpdateProductOperation struct { + options *sdk.OperationInfo +} + +func NewUpdateProductOperation() *UpdateProductOperation { + return &UpdateProductOperation{ + options: &sdk.OperationInfo{ + Name: "Update Product", + Description: "update an existing product.", + RequireAuth: true, + Auth: sharedAuth, + Input: map[string]*sdkcore.AutoFormSchema{ + "productID": autoform.NewNumberField(). + SetDisplayName("Product ID"). + SetDescription("The id of the product."). + SetRequired(true). + Build(), + "title": autoform.NewShortTextField(). + SetDisplayName("Product title"). + SetDescription("The title of the product."). + SetRequired(false). + Build(), + "bodyHTML": autoform.NewLongTextField(). + SetDisplayName("Product description"). + SetDescription("The description of the product."). + SetRequired(false). + Build(), + "vendor": autoform.NewShortTextField(). + SetDisplayName("Vendor"). + SetDescription("Vendor."). + SetRequired(false). + Build(), + "productType": autoform.NewShortTextField(). + SetDisplayName("Product type"). + SetDescription("A categorization for the product used for filtering and searching products."). + SetRequired(false). + Build(), + "tags": autoform.NewLongTextField(). + SetDisplayName("Tags"). + SetDescription("A string of comma-separated tags for filtering and search."). + SetRequired(false). + Build(), + "status": autoform.NewSelectField(). + SetDisplayName("Status"). + SetDescription("The status of the product: active or draft"). + SetOptions(statusFormat). + SetRequired(true). + Build(), + }, + ErrorSettings: sdkcore.StepErrorSettings{ + ContinueOnError: false, + RetryOnError: false, + }, + }, + } +} + +func (c *UpdateProductOperation) Run(ctx *sdk.RunContext) (sdk.JSON, error) { + if ctx.Auth.Extra["token"] == "" { + return nil, errors.New("missing shopify auth token") + } + input := sdk.InputToType[updateProductOperationProps](ctx) + domain := ctx.Auth.Extra["domain"] + shopName := domain + ".myshopify.com" + client := getShopifyClient(shopName, ctx.Auth.Extra["token"]) + + existingProduct, err := client.Product.Get(context.Background(), input.ProductID, nil) + if err != nil { + return nil, err + } + if input.Title != "" { + existingProduct.Title = input.Title + } + if input.BodyHTML != "" { + existingProduct.BodyHTML = input.BodyHTML + } + if input.Vendor != "" { + existingProduct.Vendor = input.Vendor + } + if input.ProductType != "" { + existingProduct.ProductType = input.ProductType + } + if input.Tags != "" { + existingProduct.Tags = input.Tags + } + if input.Status != "" { + existingProduct.Status = goshopify.ProductStatus(input.Status) + } + + updatedProduct, err := client.Product.Update(context.Background(), *existingProduct) + if err != nil { + return nil, fmt.Errorf("failed to update product: %w", err) + } + return map[string]interface{}{ + "updated_product": updatedProduct, + }, nil +} + +func (c *UpdateProductOperation) Test(ctx *sdk.RunContext) (sdk.JSON, error) { + return c.Run(ctx) +} + +func (c *UpdateProductOperation) GetInfo() *sdk.OperationInfo { + return c.options +} diff --git a/internal/connectors/shopify/shared.go b/internal/connectors/shopify/shared.go index 1fe3d9a..82985a7 100644 --- a/internal/connectors/shopify/shared.go +++ b/internal/connectors/shopify/shared.go @@ -1,8 +1,11 @@ package shopify import ( + goshopify "github.com/bold-commerce/go-shopify/v4" + "github.com/wakflo/go-sdk/autoform" sdkcore "github.com/wakflo/go-sdk/core" + // "os" ) /*var viewStyleOptions = []*sdkcore.AutoFormSchema{ @@ -12,12 +15,14 @@ import ( var sharedAuth = autoform.NewCustomAuthField(). SetFields(map[string]*sdkcore.AutoFormSchema{ - "domain": autoform.NewShortTextField().SetDisplayName("Domain Name"). - SetDescription("The domain name of the shopify app."). + "domain": autoform.NewShortTextField(). + SetDisplayName("Domain Name"). + SetDescription("The domain name of the shopify app. eg. xyz.myshopify.com, type in only 'xyz'"). SetRequired(true). Build(), "token": autoform.NewShortTextField().SetDisplayName("Authentication Token"). SetDescription("The token used to authenticate the shopify app."). + SetRequired(true). Build(), /*// will be enabled when dropdown cab show in dialog // "appMode": autoform.NewSelectField(). @@ -28,3 +33,31 @@ var sharedAuth = autoform.NewCustomAuthField(). // Build(),*/ }). Build() + +var app = goshopify.App{ + ApiKey: "", + ApiSecret: "", + RedirectUrl: "", + Scope: "write_orders, read_orders, write_customers, read_customers, read_products, write_products, write_draft_orders, read_draft_orders", +} + +var getShopifyClient = func(shopName string, accessToken string) *goshopify.Client { + client, err := goshopify.NewClient(app, shopName, accessToken) + if err != nil { + return nil + } + return client +} + +var statusFormat = []*sdkcore.AutoFormSchema{ + {Const: "active", Title: "Active"}, + {Const: "draft", Title: "Draft"}, +} + +var shopifyTransactionKinds = []*sdkcore.AutoFormSchema{ + {Const: "authorization", Title: "Authorization"}, + {Const: "sale", Title: "Sale"}, + {Const: "capture", Title: "Capture"}, + {Const: "void", Title: "Void"}, + {Const: "refund", Title: "Refund"}, +} diff --git a/internal/connectors/shopify/trigger_new_customer.go b/internal/connectors/shopify/trigger_new_customer.go new file mode 100644 index 0000000..723af62 --- /dev/null +++ b/internal/connectors/shopify/trigger_new_customer.go @@ -0,0 +1,97 @@ +// Copyright 2022-present Wakflo +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package shopify + +import ( + "context" + "errors" + "time" + + goshopify "github.com/bold-commerce/go-shopify/v4" + + "github.com/wakflo/go-sdk/autoform" + sdk "github.com/wakflo/go-sdk/connector" + sdkcore "github.com/wakflo/go-sdk/core" +) + +type TriggerNewCustomer struct { + options *sdk.TriggerInfo +} + +func NewTriggerNewCustomer() *TriggerNewCustomer { + return &TriggerNewCustomer{ + options: &sdk.TriggerInfo{ + Name: "New Customer", + Description: "Triggers when a new customer is created", + RequireAuth: true, + Auth: sharedAuth, + Type: sdkcore.TriggerTypeCron, + Input: map[string]*sdkcore.AutoFormSchema{ + "tag": autoform.NewShortTextField(). + SetDisplayName(""). + SetDescription(""). + SetRequired(false). + Build(), + }, + Settings: &sdkcore.TriggerSettings{}, + ErrorSettings: &sdkcore.StepErrorSettings{ + ContinueOnError: false, + RetryOnError: false, + }, + }, + } +} + +func (t *TriggerNewCustomer) Run(ctx *sdk.RunContext) (sdk.JSON, error) { + if ctx.Auth.Extra["token"] == "" { + return nil, errors.New("missing shopify auth token") + } + + domain := ctx.Auth.Extra["domain"] + shopName := domain + ".myshopify.com" + client := getShopifyClient(shopName, ctx.Auth.Extra["token"]) + + var lastRunTime time.Time + if ctx.Metadata.LastRun != nil { + lastRunTime = *ctx.Metadata.LastRun + } + + options := &goshopify.ListOptions{ + CreatedAtMin: lastRunTime, + } + + customers, err := client.Customer.List(context.Background(), options) + if err != nil { + return nil, err + } + + return customers, nil +} + +func (t *TriggerNewCustomer) Test(ctx *sdk.RunContext) (sdk.JSON, error) { + return t.Run(ctx) +} + +func (t *TriggerNewCustomer) OnEnabled(ctx *sdk.RunContext) error { + return nil +} + +func (t *TriggerNewCustomer) OnDisabled(ctx *sdk.RunContext) error { + return nil +} + +func (t *TriggerNewCustomer) GetInfo() *sdk.TriggerInfo { + return t.options +} diff --git a/internal/connectors/shopify/trigger_new_order.go b/internal/connectors/shopify/trigger_new_order.go new file mode 100644 index 0000000..76dafad --- /dev/null +++ b/internal/connectors/shopify/trigger_new_order.go @@ -0,0 +1,92 @@ +// Copyright 2022-present Wakflo +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package shopify + +import ( + "context" + "errors" + "time" + + goshopify "github.com/bold-commerce/go-shopify/v4" + + sdk "github.com/wakflo/go-sdk/connector" + sdkcore "github.com/wakflo/go-sdk/core" +) + +type TriggerNewOrder struct { + options *sdk.TriggerInfo +} + +func NewTriggerNewOrder() *TriggerNewOrder { + return &TriggerNewOrder{ + options: &sdk.TriggerInfo{ + Name: "New Order", + Description: "Triggers when a new order is created", + RequireAuth: true, + Auth: sharedAuth, + Type: sdkcore.TriggerTypeCron, + Input: map[string]*sdkcore.AutoFormSchema{}, + Settings: &sdkcore.TriggerSettings{}, + ErrorSettings: &sdkcore.StepErrorSettings{ + ContinueOnError: false, + RetryOnError: false, + }, + }, + } +} + +func (t *TriggerNewOrder) Run(ctx *sdk.RunContext) (sdk.JSON, error) { + if ctx.Auth.Extra["token"] == "" { + return nil, errors.New("missing shopify auth token") + } + + domain := ctx.Auth.Extra["domain"] + shopName := domain + ".myshopify.com" + client := getShopifyClient(shopName, ctx.Auth.Extra["token"]) + + var lastRunTime time.Time + if ctx.Metadata.LastRun != nil { + lastRunTime = *ctx.Metadata.LastRun + } + + options := &goshopify.ListOptions{ + CreatedAtMin: lastRunTime, + } + + orders, err := client.Order.List(context.Background(), options) + if err != nil { + return nil, err + } + + return sdk.JSON(map[string]interface{}{ + "order details": orders, + }), err +} + +func (t *TriggerNewOrder) Test(ctx *sdk.RunContext) (sdk.JSON, error) { + return t.Run(ctx) +} + +func (t *TriggerNewOrder) OnEnabled(ctx *sdk.RunContext) error { + return nil +} + +func (t *TriggerNewOrder) OnDisabled(ctx *sdk.RunContext) error { + return nil +} + +func (t *TriggerNewOrder) GetInfo() *sdk.TriggerInfo { + return t.options +}