From b1dcd30213dd09d99b4a56eee07604a2d6b97979 Mon Sep 17 00:00:00 2001 From: Sinh Date: Thu, 18 Aug 2022 17:58:22 +0700 Subject: [PATCH] define tnc api --- go.mod | 2 +- go.sum | 2 + model/natsio/communication_request.go | 17 ------ model/natsio/communication_response.go | 26 -------- partnerapi/globalcare/globale_care.go | 20 +++--- partnerapi/tnc/const.go | 16 +++-- partnerapi/tnc/model_request.go | 8 +++ partnerapi/tnc/tnc.go | 84 ++++++++++++++++++++------ 8 files changed, 98 insertions(+), 77 deletions(-) delete mode 100644 model/natsio/communication_request.go delete mode 100644 model/natsio/communication_response.go diff --git a/go.mod b/go.mod index 27acef5..aed1d01 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.17 require ( github.com/Selly-Modules/logger v0.0.1 - github.com/Selly-Modules/natsio v1.0.0 + github.com/Selly-Modules/natsio v1.0.1-0.20220818104747-572120c26a1d ) require ( diff --git a/go.sum b/go.sum index 318d807..bf59b4e 100644 --- a/go.sum +++ b/go.sum @@ -2,6 +2,8 @@ github.com/Selly-Modules/logger v0.0.1 h1:dwLLtW53FfVBlklhdtFRB63eP0ofIh0IUQ/Gjg github.com/Selly-Modules/logger v0.0.1/go.mod h1:RWhSQ3F01an8KD00VjzRBZOMcE5eV2Cy0/l4ZkeieyU= github.com/Selly-Modules/natsio v1.0.0 h1:2GHm7upYn0CmqKPwfg2AXNmaAHuL7V9l36M4BgOmQ4M= github.com/Selly-Modules/natsio v1.0.0/go.mod h1:NG55g9ip18nvN5tfP6PcSEKec10/lOeIOZC8HqBVNlQ= +github.com/Selly-Modules/natsio v1.0.1-0.20220818104747-572120c26a1d h1:NnRejDZPFqvtphr2rtfwi0rGx+oN8yD8RwttViVddjY= +github.com/Selly-Modules/natsio v1.0.1-0.20220818104747-572120c26a1d/go.mod h1:NG55g9ip18nvN5tfP6PcSEKec10/lOeIOZC8HqBVNlQ= github.com/armon/go-radix v1.0.0 h1:F4z6KzEeeQIMeLFa97iZU6vupzoecKdU5TX24SNppXI= github.com/armon/go-radix v1.0.0/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8= github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8= diff --git a/model/natsio/communication_request.go b/model/natsio/communication_request.go deleted file mode 100644 index ad1b55c..0000000 --- a/model/natsio/communication_request.go +++ /dev/null @@ -1,17 +0,0 @@ -package natsiomodel - -// NatsRequestHTTP ... -type NatsRequestHTTP struct { - ResponseImmediately bool `json:"responseImmediately"` - AuthenticationID string `json:"authenticationId"` - Payload HTTPPayload `json:"payload"` -} - -// HTTPPayload ... -type HTTPPayload struct { - URL string `json:"url"` - Method string `json:"method"` - Data string `json:"data"` - Header map[string]string `json:"header"` - Query map[string]string `json:"query"` -} diff --git a/model/natsio/communication_response.go b/model/natsio/communication_response.go deleted file mode 100644 index ab8a512..0000000 --- a/model/natsio/communication_response.go +++ /dev/null @@ -1,26 +0,0 @@ -package natsiomodel - -import "github.com/Selly-Modules/tpl/util/pjson" - -// NatsResponse ... -type NatsResponse struct { - Response *HttpResponse `json:"response"` - Error bool `json:"error"` - Message string `json:"message"` - RequestID string `json:"requestId"` -} - -// ParseResponseData ... -func (r *NatsResponse) ParseResponseData(result interface{}) error { - if r.Response == nil { - return nil - } - b := pjson.ToBytes(r.Response.Body) - return pjson.Unmarshal(b, result) -} - -// HttpResponse ... -type HttpResponse struct { - Body string `json:"body"` - StatusCode int `json:"statusCode"` -} diff --git a/partnerapi/globalcare/globale_care.go b/partnerapi/globalcare/globale_care.go index 847bc84..56ed5fd 100644 --- a/partnerapi/globalcare/globale_care.go +++ b/partnerapi/globalcare/globale_care.go @@ -7,13 +7,13 @@ import ( "github.com/Selly-Modules/logger" "github.com/Selly-Modules/natsio" + "github.com/Selly-Modules/natsio/model" "github.com/nats-io/nats.go" "github.com/thoas/go-funk" - "github.com/Selly-Modules/tpl/constant" - natsiomodel "github.com/Selly-Modules/tpl/model/natsio" - "github.com/Selly-Modules/tpl/util/base64" - "github.com/Selly-Modules/tpl/util/pjson" + "github.com/Selly-Modules/3pl/constant" + "github.com/Selly-Modules/3pl/util/base64" + "github.com/Selly-Modules/3pl/util/pjson" ) // Client ... @@ -62,9 +62,9 @@ func (c *Client) CreateOrder(p CreateOrderPayload) (*CommonResponse, error) { Signature: "", // TODO:implement Data: base64.Encode(pjson.ToBytes(data)), } - natsPayload := natsiomodel.NatsRequestHTTP{ + natsPayload := model.CommunicationRequestHttp{ ResponseImmediately: true, - Payload: natsiomodel.HTTPPayload{ + Payload: model.HttpRequest{ URL: url, Method: http.MethodPost, Data: pjson.ToJSONString(body), @@ -79,7 +79,7 @@ func (c *Client) CreateOrder(p CreateOrderPayload) (*CommonResponse, error) { return nil, err } var ( - r natsiomodel.NatsResponse + r model.CommunicationHttpResponse res CommonResponse ) if err = pjson.Unmarshal(msg.Data, &r); err != nil { @@ -92,9 +92,9 @@ func (c *Client) CreateOrder(p CreateOrderPayload) (*CommonResponse, error) { // GetOrder ... func (c *Client) GetOrder(orderCode string) (*CommonResponse, error) { url := c.getBaseURL() + fmt.Sprintf(apiPathGetOrder, orderCode) - natsPayload := natsiomodel.NatsRequestHTTP{ + natsPayload := model.CommunicationRequestHttp{ ResponseImmediately: true, - Payload: natsiomodel.HTTPPayload{ + Payload: model.HttpRequest{ URL: url, Method: http.MethodGet, }, @@ -108,7 +108,7 @@ func (c *Client) GetOrder(orderCode string) (*CommonResponse, error) { return nil, err } var ( - r natsiomodel.NatsResponse + r model.CommunicationHttpResponse res CommonResponse ) if err = pjson.Unmarshal(msg.Data, &r); err != nil { diff --git a/partnerapi/tnc/const.go b/partnerapi/tnc/const.go index 00f417d..fd3bbf6 100644 --- a/partnerapi/tnc/const.go +++ b/partnerapi/tnc/const.go @@ -3,10 +3,11 @@ package tnc const ( TimeLayout = "2006-01-02 15:04:05" - apiPathCreateOutboundRequest = "/api/v1/ors" - apiPathGetOutboundRequest = "/api/v1/ors/%d" - apiPathCancelOutboundRequest = "/api/v1/ors/%d/cancel" - apiPathAuth = "/auth/realms/%s/protocol/openid-connect/token" + apiPathCreateOutboundRequest = "/api/v1/ors" + apiPathGetOutboundRequest = "/api/v1/ors/%d" + apiPathCancelOutboundRequest = "/api/v1/ors/%d/cancel" + apiPathUpdateLogisticInfoOutboundRequest = "/api/v1/ors/%d/logistic-info" + apiPathAuth = "/auth/realms/%s/protocol/openid-connect/token" PriorityUrgent = 3 PriorityHigh = 2 @@ -19,6 +20,13 @@ const ( TPLCodeViettelPost = "VTP" TPLCodeSellyExpress = "SE" TPLCodeJTExpress = "JTE" + + ShippingServiceCodeSTD = "STD" + ORTypeOrder = 1 + ShippingTypeSelfShip = 1 + PackTypeNormal = 1 + BizTypeB2C = 1 + ConditionTypeCodeNew = "NEW" ) const ( diff --git a/partnerapi/tnc/model_request.go b/partnerapi/tnc/model_request.go index 52ee55d..5f0a41d 100644 --- a/partnerapi/tnc/model_request.go +++ b/partnerapi/tnc/model_request.go @@ -42,3 +42,11 @@ type OutboundRequestPayload struct { TPLCode string `json:"tplCode"` TrackingCode string `json:"trackingCode"` } + +// UpdateORLogisticInfoPayload ... +type UpdateORLogisticInfoPayload struct { + OrID int `json:"orId"` + TrackingCode string `json:"trackingCode"` + ShippingLabel string `json:"shippingLabel"` + SlaShipDate string `json:"slaShipDate"` +} diff --git a/partnerapi/tnc/tnc.go b/partnerapi/tnc/tnc.go index 1faa2db..232644b 100644 --- a/partnerapi/tnc/tnc.go +++ b/partnerapi/tnc/tnc.go @@ -8,11 +8,11 @@ import ( "github.com/Selly-Modules/logger" "github.com/Selly-Modules/natsio" + "github.com/Selly-Modules/natsio/model" "github.com/nats-io/nats.go" - "github.com/Selly-Modules/tpl/constant" - natsiomodel "github.com/Selly-Modules/tpl/model/natsio" - "github.com/Selly-Modules/tpl/util/pjson" + "github.com/Selly-Modules/3pl/constant" + "github.com/Selly-Modules/3pl/util/pjson" ) // Client ... @@ -47,11 +47,11 @@ func NewClient(env ENV, clientID, clientSecret, realm string, natsClient natsio. // CreateOutboundRequest ... func (c *Client) CreateOutboundRequest(p OutboundRequestPayload) (*OutboundRequestRes, error) { apiURL := c.getBaseURL() + apiPathCreateOutboundRequest - natsPayload := natsiomodel.NatsRequestHTTP{ + natsPayload := model.CommunicationRequestHttp{ ResponseImmediately: true, - Payload: natsiomodel.HTTPPayload{ + Payload: model.HttpRequest{ URL: apiURL, - Method: http.MethodGet, + Method: http.MethodPost, Data: pjson.ToJSONString(p), Header: c.getRequestHeader(), }, @@ -65,7 +65,7 @@ func (c *Client) CreateOutboundRequest(p OutboundRequestPayload) (*OutboundReque return nil, err } var ( - r natsiomodel.NatsResponse + r model.CommunicationHttpResponse errRes ErrRes dataRes []OutboundRequestRes ) @@ -88,16 +88,60 @@ func (c *Client) CreateOutboundRequest(p OutboundRequestPayload) (*OutboundReque if len(dataRes) == 0 { return nil, fmt.Errorf("tnc.Client.CreateOutboundRequest: empty_result") } + item := &dataRes[0] + e := item.Error + if e != nil { + return nil, fmt.Errorf("tnc.Client.CreateOutboundRequest: failed, code %s - message %s", e.Code, e.ErrorMessage) + } - return &dataRes[0], err + return item, err +} + +// UpdateOutboundRequestLogisticInfo ... +func (c *Client) UpdateOutboundRequestLogisticInfo(p UpdateORLogisticInfoPayload) error { + apiURL := c.getBaseURL() + fmt.Sprintf(apiPathUpdateLogisticInfoOutboundRequest, p.OrID) + natsPayload := model.CommunicationRequestHttp{ + ResponseImmediately: true, + Payload: model.HttpRequest{ + URL: apiURL, + Method: http.MethodPost, + Header: c.getRequestHeader(), + }, + } + msg, err := c.requestHttpViaNats(natsPayload) + if err != nil { + logger.Error("tnc.Client.UpdateOutboundRequestLogisticInfo - requestHttpViaNats", logger.LogData{ + "err": err.Error(), + "payload": natsPayload, + }) + return err + } + var ( + r model.CommunicationHttpResponse + errRes ErrRes + ) + if err = pjson.Unmarshal(msg.Data, &r); err != nil { + return fmt.Errorf("tnc.Client.UpdateOutboundRequestLogisticInfo: parse_data %v", err) + } + res := r.Response + if res == nil { + return fmt.Errorf("tnc.Client.UpdateOutboundRequestLogisticInfo: empty_response") + } + if res.StatusCode >= http.StatusBadRequest { + if err = r.ParseResponseData(&errRes); err != nil { + return fmt.Errorf("tnc.Client.UpdateOutboundRequestLogisticInfo: parse_response_err: %v", err) + } + return fmt.Errorf("tnc.Client.UpdateOutboundRequestLogisticInfo: failed code %s, message %s", errRes.Code, errRes.ErrorMessage) + } + return nil } // GetOutboundRequestByID ... func (c *Client) GetOutboundRequestByID(requestID int) (*OutboundRequestInfo, error) { apiURL := c.getBaseURL() + fmt.Sprintf(apiPathGetOutboundRequest, requestID) - natsPayload := natsiomodel.NatsRequestHTTP{ + natsPayload := model.CommunicationRequestHttp{ ResponseImmediately: true, - Payload: natsiomodel.HTTPPayload{ + Payload: model.HttpRequest{ URL: apiURL, Method: http.MethodGet, Header: c.getRequestHeader(), @@ -112,7 +156,7 @@ func (c *Client) GetOutboundRequestByID(requestID int) (*OutboundRequestInfo, er return nil, err } var ( - r natsiomodel.NatsResponse + r model.CommunicationHttpResponse errRes ErrRes outboundRequest OutboundRequestInfo ) @@ -136,14 +180,16 @@ func (c *Client) GetOutboundRequestByID(requestID int) (*OutboundRequestInfo, er } // CancelOutboundRequest ... -func (c *Client) CancelOutboundRequest(requestID int) error { +func (c *Client) CancelOutboundRequest(requestID int, note string) error { apiURL := c.getBaseURL() + fmt.Sprintf(apiPathCancelOutboundRequest, requestID) - natsPayload := natsiomodel.NatsRequestHTTP{ + data := map[string]string{"note": note} + natsPayload := model.CommunicationRequestHttp{ ResponseImmediately: true, - Payload: natsiomodel.HTTPPayload{ + Payload: model.HttpRequest{ URL: apiURL, Method: http.MethodPost, Header: c.getRequestHeader(), + Data: pjson.ToJSONString(data), }, } msg, err := c.requestHttpViaNats(natsPayload) @@ -155,7 +201,7 @@ func (c *Client) CancelOutboundRequest(requestID int) error { return err } var ( - r natsiomodel.NatsResponse + r model.CommunicationHttpResponse errRes ErrRes ) if err = pjson.Unmarshal(msg.Data, &r); err != nil { @@ -186,9 +232,9 @@ func (c *Client) auth() (*authRes, error) { "Content-Type": "application/x-www-form-urlencoded", } apiURL := baseURLAuthENVMapping[c.env] + fmt.Sprintf(apiPathAuth, c.realm) - natsPayload := natsiomodel.NatsRequestHTTP{ + natsPayload := model.CommunicationRequestHttp{ ResponseImmediately: true, - Payload: natsiomodel.HTTPPayload{ + Payload: model.HttpRequest{ URL: apiURL, Method: http.MethodPost, Data: body, @@ -204,7 +250,7 @@ func (c *Client) auth() (*authRes, error) { return nil, err } var ( - r natsiomodel.NatsResponse + r model.CommunicationHttpResponse errRes ErrRes data authRes ) @@ -240,7 +286,7 @@ func (c *Client) getRequestHeader() map[string]string { return m } -func (c *Client) requestHttpViaNats(data natsiomodel.NatsRequestHTTP) (*nats.Msg, error) { +func (c *Client) requestHttpViaNats(data model.CommunicationRequestHttp) (*nats.Msg, error) { s := constant.NatsCommunicationSubjectRequestHTTP b := pjson.ToBytes(data) return c.natsClient.Request(s, b)