integrate-onpoint #5

Merged
sinhluu merged 15 commits from integrate-onpoint into master 2022-10-31 09:36:25 +00:00
4 changed files with 62 additions and 14 deletions
Showing only changes of commit 26390c4633 - Show all commits

View File

@ -11,6 +11,9 @@ const (
headerXAPIKey = "x-api-key" headerXAPIKey = "x-api-key"
headerXTimestamp = "x-timestamp" headerXTimestamp = "x-timestamp"
headerXSignature = "x-signature" headerXSignature = "x-signature"
webhookEventUpdateOrderStatus = "update_order_status"
webhookEventUpdateInventory = "update_inventory"
) )
var ( var (

View File

@ -1,6 +1,10 @@
package onpoint package onpoint
import "time" import (
"time"
"github.com/Selly-Modules/3pl/util/pjson"
)
/* /*
* Request payload * Request payload
@ -55,13 +59,6 @@ type CancelOrderRequest struct {
* WEBHOOK ONPOINT * WEBHOOK ONPOINT
*/ */
// WebhookPayload ...
type WebhookPayload struct {
Event string `json:"event"`
RequestedAt time.Time `json:"requested_at"`
Data interface{} `json:"data"`
}
// WebhookDataUpdateInventory ... // WebhookDataUpdateInventory ...
type WebhookDataUpdateInventory struct { type WebhookDataUpdateInventory struct {
Sku string `json:"sku"` Sku string `json:"sku"`
@ -81,3 +78,40 @@ type WebhookDataUpdateOrderStatus struct {
DeliveryStatus string `json:"delivery_status"` DeliveryStatus string `json:"delivery_status"`
UpdatedAt time.Time `json:"updated_at"` UpdatedAt time.Time `json:"updated_at"`
} }
// WebhookPayload ...
type WebhookPayload struct {
Event string `json:"event"`
RequestedAt time.Time `json:"requested_at"`
Data interface{} `json:"data"`
}
// GetDataEventUpdateOrderStatus ...
func (p WebhookPayload) GetDataEventUpdateOrderStatus() (data *WebhookDataUpdateOrderStatus, ok bool) {
if p.Event != webhookEventUpdateOrderStatus {
return nil, false
}
b, err := pjson.Marshal(p.Data)
if err != nil {
return nil, false
}
if err = pjson.Unmarshal(b, &data); err != nil {
return nil, false
}
return data, true
}
// GetDataEventUpdateInventory ...
func (p WebhookPayload) GetDataEventUpdateInventory() (data *WebhookDataUpdateInventory, ok bool) {
if p.Event != webhookEventUpdateInventory {
return nil, false
}
b, err := pjson.Marshal(p.Data)
if err != nil {
return nil, false
}
if err = pjson.Unmarshal(b, &data); err != nil {
return nil, false
}
return data, true
}

15
util/pjson/marshal.go Normal file
View File

@ -0,0 +1,15 @@
package pjson
import (
"encoding/json"
"log"
)
// Marshal ...
func Marshal(data interface{}) ([]byte, error) {
b, err := json.Marshal(data)
if err != nil {
log.Printf("3pl/util/pjson.Marshal: err %v, payload %v", err, data)
}
return b, err
}

View File

@ -2,18 +2,14 @@ package pjson
import ( import (
"encoding/json" "encoding/json"
"log"
"github.com/Selly-Modules/logger"
) )
// Unmarshal ... // Unmarshal ...
func Unmarshal(b []byte, resultP interface{}) error { func Unmarshal(b []byte, resultP interface{}) error {
err := json.Unmarshal(b, resultP) err := json.Unmarshal(b, resultP)
if err != nil { if err != nil {
logger.Error("pjson.Unmarshal", logger.LogData{ log.Printf("3pl/util/pjson.Unmarshal: err %v, payload %s", err, string(b))
"raw": string(b),
"err": err.Error(),
})
} }
return err return err
} }