define tnc api
This commit is contained in:
parent
c8bf12c3e9
commit
b1dcd30213
2
go.mod
2
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 (
|
||||
|
|
2
go.sum
2
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=
|
||||
|
|
|
@ -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"`
|
||||
}
|
|
@ -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"`
|
||||
}
|
|
@ -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 {
|
||||
|
|
|
@ -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 (
|
||||
|
|
|
@ -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"`
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue