diff --git a/connectors.go b/connectors.go index fdd3578..05bde5a 100644 --- a/connectors.go +++ b/connectors.go @@ -29,6 +29,7 @@ import ( "github.com/wakflo/extensions/internal/connectors/slack" "github.com/wakflo/extensions/internal/connectors/todoist" "github.com/wakflo/extensions/internal/connectors/webhook" + "github.com/wakflo/extensions/internal/connectors/woocommerce" "github.com/wakflo/extensions/internal/logger" sdk "github.com/wakflo/go-sdk/connector" ) @@ -57,6 +58,7 @@ func RegisterConnectors() []*sdk.ConnectorPlugin { calculator.NewConnector, // Calculator shopify.NewConnector, // Shopify webhook.NewConnector, // Webhook + woocommerce.NewConnector, // Woocommerce } // 🛑Do-Not-Edit diff --git a/internal/connectors/shopify/lib.go b/internal/connectors/shopify/lib.go index 9e4bcfd..960f287 100644 --- a/internal/connectors/shopify/lib.go +++ b/internal/connectors/shopify/lib.go @@ -26,7 +26,7 @@ func NewConnector() (*sdk.ConnectorPlugin, error) { Version: "0.0.1", Category: sdk.Apps, Authors: []string{"Wakflo "}, - Triggers: []sdk.ITrigger{NewTriggerNewCustomer()}, + Triggers: []sdk.ITrigger{NewTriggerNewCustomer(), NewTriggerNewOrder()}, Operations: []sdk.IOperation{ NewGetLocationsOperation(), NewAdjustInventoryLevelOperation(), diff --git a/internal/connectors/shopify/operation_list_customers.go b/internal/connectors/shopify/operation_list_customers.go index 3cc6079..1ff1480 100644 --- a/internal/connectors/shopify/operation_list_customers.go +++ b/internal/connectors/shopify/operation_list_customers.go @@ -17,6 +17,7 @@ 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" @@ -33,7 +34,12 @@ func NewListCustomersOperation() *ListCustomersOperation { Description: "List all Customers in store", RequireAuth: true, Auth: sharedAuth, - Input: map[string]*sdkcore.AutoFormSchema{}, + Input: map[string]*sdkcore.AutoFormSchema{ + "orderId": autoform.NewNumberField(). + SetDisplayName(""). + SetDescription(""). + Build(), + }, ErrorSettings: sdkcore.StepErrorSettings{ ContinueOnError: false, RetryOnError: false, diff --git a/internal/connectors/shopify/trigger_new_customer.go b/internal/connectors/shopify/trigger_new_customer.go index c3a15d5..df8f8f7 100644 --- a/internal/connectors/shopify/trigger_new_customer.go +++ b/internal/connectors/shopify/trigger_new_customer.go @@ -17,7 +17,6 @@ package shopify import ( "context" "errors" - "fmt" "time" goshopify "github.com/bold-commerce/go-shopify/v4" @@ -35,7 +34,7 @@ func NewTriggerNewCustomer() *TriggerNewCustomer { return &TriggerNewCustomer{ options: &sdk.TriggerInfo{ Name: "New Customer", - Description: "Triggers workflow when a new customer is created", + Description: "Triggers when a new customer is created", RequireAuth: true, Auth: sharedAuth, Type: sdkcore.TriggerTypeCron, @@ -65,20 +64,18 @@ func (t *TriggerNewCustomer) Run(ctx *sdk.RunContext) (sdk.JSON, error) { client := getShopifyClient(shopName, ctx.Auth.Extra["token"]) // Get the last run time from metadata, or use a default if it's the first run - lastRunTime := ctx.Metadata.LastRun - if lastRunTime == nil { - defaultTime := time.Now().Add(-24 * time.Hour) - lastRunTime = &defaultTime + var lastRunTime time.Time + if ctx.Metadata.LastRun != nil { + lastRunTime = *ctx.Metadata.LastRun + } else { + lastRunTime = time.Now().Add(-24 * time.Hour) } - query := fmt.Sprintf("created_at:>='%s'", lastRunTime.Format("2006-01-02T15:04:05-07:00")) - - // Set up the search options - options := &goshopify.CustomerSearchOptions{ - Query: query, + options := &goshopify.ListOptions{ + CreatedAtMin: lastRunTime, } - customers, err := client.Customer.Search(context.Background(), options) + customers, err := client.Customer.List(context.Background(), options) if err != nil { return nil, err } diff --git a/internal/connectors/shopify/trigger_new_order.go b/internal/connectors/shopify/trigger_new_order.go new file mode 100644 index 0000000..9c9e00e --- /dev/null +++ b/internal/connectors/shopify/trigger_new_order.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" + "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() *TriggerNewCustomer { + return &TriggerNewCustomer{ + 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"]) + + // Get the last run time from metadata, or use a default if it's the first run + var lastRunTime time.Time + if ctx.Metadata.LastRun != nil { + lastRunTime = *ctx.Metadata.LastRun + } else { + lastRunTime = time.Now().Add(-24 * time.Hour) + } + + options := &goshopify.ListOptions{ + CreatedAtMin: lastRunTime, + } + + orders, err := client.Order.List(context.Background(), options) + if err != nil { + return nil, err + } + + return orders, nil +} + +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 +} diff --git a/internal/connectors/woocommerce/lib.go b/internal/connectors/woocommerce/lib.go new file mode 100644 index 0000000..0ee57e9 --- /dev/null +++ b/internal/connectors/woocommerce/lib.go @@ -0,0 +1,34 @@ +// 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 woocommerce + +import ( + sdk "github.com/wakflo/go-sdk/connector" +) + +func NewConnector() (*sdk.ConnectorPlugin, error) { + return sdk.CreateConnector(&sdk.CreateConnectorArgs{ + Name: "Woocommerce", + Description: "E-commerce platform built on WordPress", + Logo: "devicon:woocommerce", + Version: "0.0.1", + Category: sdk.Apps, + Authors: []string{"Wakflo "}, + Triggers: []sdk.ITrigger{}, + Operations: []sdk.IOperation{ + NewListProductsOperation(), + }, + }) +} diff --git a/internal/connectors/woocommerce/operation_list_products.go b/internal/connectors/woocommerce/operation_list_products.go new file mode 100644 index 0000000..67ed2c2 --- /dev/null +++ b/internal/connectors/woocommerce/operation_list_products.go @@ -0,0 +1,81 @@ +package woocommerce + +import ( + "errors" + "github.com/hiscaler/woocommerce-go" + "github.com/hiscaler/woocommerce-go/config" + "github.com/wakflo/go-sdk/autoform" + sdk "github.com/wakflo/go-sdk/connector" + sdkcore "github.com/wakflo/go-sdk/core" +) + +type ListProductsOperation struct { + options *sdk.OperationInfo +} + +func NewListProductsOperation() *ListProductsOperation { + return &ListProductsOperation{ + options: &sdk.OperationInfo{ + Name: "List products", + Description: "List products in store", + RequireAuth: true, + Auth: sharedAuth, + Input: map[string]*sdkcore.AutoFormSchema{ + "projectId": autoform.NewLongTextField(). + SetDisplayName(""). + SetDescription(""). + Build(), + }, + ErrorSettings: sdkcore.StepErrorSettings{ + ContinueOnError: false, + RetryOnError: false, + }, + }, + } +} + +func (c *ListProductsOperation) Run(ctx *sdk.RunContext) (sdk.JSON, error) { + baseURL := ctx.Auth.Extra["shop-url"] + consumerKey := ctx.Auth.Extra["consumer-key"] + consumerSecret := ctx.Auth.Extra["consumer-secret"] + + if baseURL == "" || consumerKey == "" || consumerSecret == "" { + return nil, errors.New("missing WooCommerce authentication credentials") + } + + //URL := "https://shut-present.localsite.io/wp-json/wc/v3" + + con := config.Config{ + Debug: true, + URL: "https://shut-present.localsite.io/", + Version: "v3", + ConsumerKey: consumerKey, + ConsumerSecret: consumerSecret, + AddAuthenticationToURL: false, + Timeout: 10, + VerifySSL: true, + } + + wooClient := woocommerce.NewClient(con) + + // Create a query parameters struct + //params := woocommerce.ProductsQueryParams{} + + // Get all products + product, _ := wooClient.Services.Product.One(1) + + // Process the products + //for _, product := range products { + // fmt.Printf("Product ID: %d, Name: %s, Price: %s\n", product.ID, product.Name, product.Price) + //} + + return product, nil +} + +func (c *ListProductsOperation) Test(ctx *sdk.RunContext) (sdk.JSON, error) { + return c.Run(ctx) +} + +func (c *ListProductsOperation) GetInfo() *sdk.OperationInfo { + return c.options +} diff --git a/internal/connectors/woocommerce/shared.go b/internal/connectors/woocommerce/shared.go new file mode 100644 index 0000000..cb775d4 --- /dev/null +++ b/internal/connectors/woocommerce/shared.go @@ -0,0 +1,33 @@ +package woocommerce + +import ( + //"encoding/json" + //"fmt" + //"io/ioutil" + //"log" + //"net/http" + + //"encoding/base64" + //"fmt" + //"strings" + // + //"github.com/go-resty/resty/v2" + + "github.com/wakflo/go-sdk/autoform" + sdkcore "github.com/wakflo/go-sdk/core" +) + +var sharedAuth = autoform.NewCustomAuthField(). + SetFields(map[string]*sdkcore.AutoFormSchema{ + "shop-url": autoform.NewShortTextField().SetDisplayName("ShopUrl"). + SetDescription("The base URL of your app (e.g https://mystore.com) and it should start with HTTPS only"). + SetRequired(true). + Build(), + "consumer-key": autoform.NewShortTextField().SetDisplayName("Consumer Key"). + SetDescription("The consumer key generated from your app."). + Build(), + "consumer-secret": autoform.NewShortTextField().SetDisplayName("Consumer Secret"). + SetDescription("The consumer secret generated from your app."). + Build(), + }). + Build()