From 26390c46331f67c2bb50e442c45e334d547e6c46 Mon Sep 17 00:00:00 2001 From: Sinh Date: Mon, 19 Sep 2022 14:34:55 +0700 Subject: [PATCH] add webhook util for onpoint --- partnerapi/onpoint/const.go | 3 ++ partnerapi/onpoint/model_request.go | 50 ++++++++++++++++++++++++----- util/pjson/marshal.go | 15 +++++++++ util/pjson/unmarshal.go | 8 ++--- 4 files changed, 62 insertions(+), 14 deletions(-) create mode 100644 util/pjson/marshal.go diff --git a/partnerapi/onpoint/const.go b/partnerapi/onpoint/const.go index cc7fda3..509d2e7 100644 --- a/partnerapi/onpoint/const.go +++ b/partnerapi/onpoint/const.go @@ -11,6 +11,9 @@ const ( headerXAPIKey = "x-api-key" headerXTimestamp = "x-timestamp" headerXSignature = "x-signature" + + webhookEventUpdateOrderStatus = "update_order_status" + webhookEventUpdateInventory = "update_inventory" ) var ( diff --git a/partnerapi/onpoint/model_request.go b/partnerapi/onpoint/model_request.go index cd5754a..42e7b6f 100644 --- a/partnerapi/onpoint/model_request.go +++ b/partnerapi/onpoint/model_request.go @@ -1,6 +1,10 @@ package onpoint -import "time" +import ( + "time" + + "github.com/Selly-Modules/3pl/util/pjson" +) /* * Request payload @@ -55,13 +59,6 @@ type CancelOrderRequest struct { * WEBHOOK ONPOINT */ -// WebhookPayload ... -type WebhookPayload struct { - Event string `json:"event"` - RequestedAt time.Time `json:"requested_at"` - Data interface{} `json:"data"` -} - // WebhookDataUpdateInventory ... type WebhookDataUpdateInventory struct { Sku string `json:"sku"` @@ -81,3 +78,40 @@ type WebhookDataUpdateOrderStatus struct { DeliveryStatus string `json:"delivery_status"` 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 +} diff --git a/util/pjson/marshal.go b/util/pjson/marshal.go new file mode 100644 index 0000000..3484fae --- /dev/null +++ b/util/pjson/marshal.go @@ -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 +} diff --git a/util/pjson/unmarshal.go b/util/pjson/unmarshal.go index ca804f0..641067d 100644 --- a/util/pjson/unmarshal.go +++ b/util/pjson/unmarshal.go @@ -2,18 +2,14 @@ package pjson import ( "encoding/json" - - "github.com/Selly-Modules/logger" + "log" ) // Unmarshal ... func Unmarshal(b []byte, resultP interface{}) error { err := json.Unmarshal(b, resultP) if err != nil { - logger.Error("pjson.Unmarshal", logger.LogData{ - "raw": string(b), - "err": err.Error(), - }) + log.Printf("3pl/util/pjson.Unmarshal: err %v, payload %s", err, string(b)) } return err }