Skip to content

Commit

Permalink
fix: update shopify trigger
Browse files Browse the repository at this point in the history
  • Loading branch information
jaymesC committed Jul 18, 2024
1 parent 858c2d5 commit 0d51745
Show file tree
Hide file tree
Showing 8 changed files with 260 additions and 14 deletions.
2 changes: 2 additions & 0 deletions connectors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -57,6 +58,7 @@ func RegisterConnectors() []*sdk.ConnectorPlugin {
calculator.NewConnector, // Calculator
shopify.NewConnector, // Shopify
webhook.NewConnector, // Webhook
woocommerce.NewConnector, // Woocommerce
}

// 🛑Do-Not-Edit
Expand Down
2 changes: 1 addition & 1 deletion internal/connectors/shopify/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func NewConnector() (*sdk.ConnectorPlugin, error) {
Version: "0.0.1",
Category: sdk.Apps,
Authors: []string{"Wakflo <integrations@wakflo.com>"},
Triggers: []sdk.ITrigger{NewTriggerNewCustomer()},
Triggers: []sdk.ITrigger{NewTriggerNewCustomer(), NewTriggerNewOrder()},
Operations: []sdk.IOperation{
NewGetLocationsOperation(),
NewAdjustInventoryLevelOperation(),
Expand Down
8 changes: 7 additions & 1 deletion internal/connectors/shopify/operation_list_customers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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,
Expand Down
21 changes: 9 additions & 12 deletions internal/connectors/shopify/trigger_new_customer.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package shopify
import (
"context"
"errors"
"fmt"
"time"

goshopify "github.com/bold-commerce/go-shopify/v4"
Expand All @@ -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,
Expand Down Expand Up @@ -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
}
Expand Down
93 changes: 93 additions & 0 deletions internal/connectors/shopify/trigger_new_order.go
Original file line number Diff line number Diff line change
@@ -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
}
34 changes: 34 additions & 0 deletions internal/connectors/woocommerce/lib.go
Original file line number Diff line number Diff line change
@@ -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 <integrations@wakflo.com>"},
Triggers: []sdk.ITrigger{},
Operations: []sdk.IOperation{
NewListProductsOperation(),
},
})
}
81 changes: 81 additions & 0 deletions internal/connectors/woocommerce/operation_list_products.go
Original file line number Diff line number Diff line change
@@ -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
}
33 changes: 33 additions & 0 deletions internal/connectors/woocommerce/shared.go
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 0d51745

Please sign in to comment.