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 (
|
require (
|
||||||
github.com/Selly-Modules/logger v0.0.1
|
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 (
|
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/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 h1:2GHm7upYn0CmqKPwfg2AXNmaAHuL7V9l36M4BgOmQ4M=
|
||||||
github.com/Selly-Modules/natsio v1.0.0/go.mod h1:NG55g9ip18nvN5tfP6PcSEKec10/lOeIOZC8HqBVNlQ=
|
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 h1:F4z6KzEeeQIMeLFa97iZU6vupzoecKdU5TX24SNppXI=
|
||||||
github.com/armon/go-radix v1.0.0/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
|
github.com/armon/go-radix v1.0.0/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
|
||||||
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
|
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/logger"
|
||||||
"github.com/Selly-Modules/natsio"
|
"github.com/Selly-Modules/natsio"
|
||||||
|
"github.com/Selly-Modules/natsio/model"
|
||||||
"github.com/nats-io/nats.go"
|
"github.com/nats-io/nats.go"
|
||||||
"github.com/thoas/go-funk"
|
"github.com/thoas/go-funk"
|
||||||
|
|
||||||
"github.com/Selly-Modules/tpl/constant"
|
"github.com/Selly-Modules/3pl/constant"
|
||||||
natsiomodel "github.com/Selly-Modules/tpl/model/natsio"
|
"github.com/Selly-Modules/3pl/util/base64"
|
||||||
"github.com/Selly-Modules/tpl/util/base64"
|
"github.com/Selly-Modules/3pl/util/pjson"
|
||||||
"github.com/Selly-Modules/tpl/util/pjson"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Client ...
|
// Client ...
|
||||||
|
@ -62,9 +62,9 @@ func (c *Client) CreateOrder(p CreateOrderPayload) (*CommonResponse, error) {
|
||||||
Signature: "", // TODO:implement
|
Signature: "", // TODO:implement
|
||||||
Data: base64.Encode(pjson.ToBytes(data)),
|
Data: base64.Encode(pjson.ToBytes(data)),
|
||||||
}
|
}
|
||||||
natsPayload := natsiomodel.NatsRequestHTTP{
|
natsPayload := model.CommunicationRequestHttp{
|
||||||
ResponseImmediately: true,
|
ResponseImmediately: true,
|
||||||
Payload: natsiomodel.HTTPPayload{
|
Payload: model.HttpRequest{
|
||||||
URL: url,
|
URL: url,
|
||||||
Method: http.MethodPost,
|
Method: http.MethodPost,
|
||||||
Data: pjson.ToJSONString(body),
|
Data: pjson.ToJSONString(body),
|
||||||
|
@ -79,7 +79,7 @@ func (c *Client) CreateOrder(p CreateOrderPayload) (*CommonResponse, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
var (
|
var (
|
||||||
r natsiomodel.NatsResponse
|
r model.CommunicationHttpResponse
|
||||||
res CommonResponse
|
res CommonResponse
|
||||||
)
|
)
|
||||||
if err = pjson.Unmarshal(msg.Data, &r); err != nil {
|
if err = pjson.Unmarshal(msg.Data, &r); err != nil {
|
||||||
|
@ -92,9 +92,9 @@ func (c *Client) CreateOrder(p CreateOrderPayload) (*CommonResponse, error) {
|
||||||
// GetOrder ...
|
// GetOrder ...
|
||||||
func (c *Client) GetOrder(orderCode string) (*CommonResponse, error) {
|
func (c *Client) GetOrder(orderCode string) (*CommonResponse, error) {
|
||||||
url := c.getBaseURL() + fmt.Sprintf(apiPathGetOrder, orderCode)
|
url := c.getBaseURL() + fmt.Sprintf(apiPathGetOrder, orderCode)
|
||||||
natsPayload := natsiomodel.NatsRequestHTTP{
|
natsPayload := model.CommunicationRequestHttp{
|
||||||
ResponseImmediately: true,
|
ResponseImmediately: true,
|
||||||
Payload: natsiomodel.HTTPPayload{
|
Payload: model.HttpRequest{
|
||||||
URL: url,
|
URL: url,
|
||||||
Method: http.MethodGet,
|
Method: http.MethodGet,
|
||||||
},
|
},
|
||||||
|
@ -108,7 +108,7 @@ func (c *Client) GetOrder(orderCode string) (*CommonResponse, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
var (
|
var (
|
||||||
r natsiomodel.NatsResponse
|
r model.CommunicationHttpResponse
|
||||||
res CommonResponse
|
res CommonResponse
|
||||||
)
|
)
|
||||||
if err = pjson.Unmarshal(msg.Data, &r); err != nil {
|
if err = pjson.Unmarshal(msg.Data, &r); err != nil {
|
||||||
|
|
|
@ -6,6 +6,7 @@ const (
|
||||||
apiPathCreateOutboundRequest = "/api/v1/ors"
|
apiPathCreateOutboundRequest = "/api/v1/ors"
|
||||||
apiPathGetOutboundRequest = "/api/v1/ors/%d"
|
apiPathGetOutboundRequest = "/api/v1/ors/%d"
|
||||||
apiPathCancelOutboundRequest = "/api/v1/ors/%d/cancel"
|
apiPathCancelOutboundRequest = "/api/v1/ors/%d/cancel"
|
||||||
|
apiPathUpdateLogisticInfoOutboundRequest = "/api/v1/ors/%d/logistic-info"
|
||||||
apiPathAuth = "/auth/realms/%s/protocol/openid-connect/token"
|
apiPathAuth = "/auth/realms/%s/protocol/openid-connect/token"
|
||||||
|
|
||||||
PriorityUrgent = 3
|
PriorityUrgent = 3
|
||||||
|
@ -19,6 +20,13 @@ const (
|
||||||
TPLCodeViettelPost = "VTP"
|
TPLCodeViettelPost = "VTP"
|
||||||
TPLCodeSellyExpress = "SE"
|
TPLCodeSellyExpress = "SE"
|
||||||
TPLCodeJTExpress = "JTE"
|
TPLCodeJTExpress = "JTE"
|
||||||
|
|
||||||
|
ShippingServiceCodeSTD = "STD"
|
||||||
|
ORTypeOrder = 1
|
||||||
|
ShippingTypeSelfShip = 1
|
||||||
|
PackTypeNormal = 1
|
||||||
|
BizTypeB2C = 1
|
||||||
|
ConditionTypeCodeNew = "NEW"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
|
@ -42,3 +42,11 @@ type OutboundRequestPayload struct {
|
||||||
TPLCode string `json:"tplCode"`
|
TPLCode string `json:"tplCode"`
|
||||||
TrackingCode string `json:"trackingCode"`
|
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/logger"
|
||||||
"github.com/Selly-Modules/natsio"
|
"github.com/Selly-Modules/natsio"
|
||||||
|
"github.com/Selly-Modules/natsio/model"
|
||||||
"github.com/nats-io/nats.go"
|
"github.com/nats-io/nats.go"
|
||||||
|
|
||||||
"github.com/Selly-Modules/tpl/constant"
|
"github.com/Selly-Modules/3pl/constant"
|
||||||
natsiomodel "github.com/Selly-Modules/tpl/model/natsio"
|
"github.com/Selly-Modules/3pl/util/pjson"
|
||||||
"github.com/Selly-Modules/tpl/util/pjson"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Client ...
|
// Client ...
|
||||||
|
@ -47,11 +47,11 @@ func NewClient(env ENV, clientID, clientSecret, realm string, natsClient natsio.
|
||||||
// CreateOutboundRequest ...
|
// CreateOutboundRequest ...
|
||||||
func (c *Client) CreateOutboundRequest(p OutboundRequestPayload) (*OutboundRequestRes, error) {
|
func (c *Client) CreateOutboundRequest(p OutboundRequestPayload) (*OutboundRequestRes, error) {
|
||||||
apiURL := c.getBaseURL() + apiPathCreateOutboundRequest
|
apiURL := c.getBaseURL() + apiPathCreateOutboundRequest
|
||||||
natsPayload := natsiomodel.NatsRequestHTTP{
|
natsPayload := model.CommunicationRequestHttp{
|
||||||
ResponseImmediately: true,
|
ResponseImmediately: true,
|
||||||
Payload: natsiomodel.HTTPPayload{
|
Payload: model.HttpRequest{
|
||||||
URL: apiURL,
|
URL: apiURL,
|
||||||
Method: http.MethodGet,
|
Method: http.MethodPost,
|
||||||
Data: pjson.ToJSONString(p),
|
Data: pjson.ToJSONString(p),
|
||||||
Header: c.getRequestHeader(),
|
Header: c.getRequestHeader(),
|
||||||
},
|
},
|
||||||
|
@ -65,7 +65,7 @@ func (c *Client) CreateOutboundRequest(p OutboundRequestPayload) (*OutboundReque
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
var (
|
var (
|
||||||
r natsiomodel.NatsResponse
|
r model.CommunicationHttpResponse
|
||||||
errRes ErrRes
|
errRes ErrRes
|
||||||
dataRes []OutboundRequestRes
|
dataRes []OutboundRequestRes
|
||||||
)
|
)
|
||||||
|
@ -88,16 +88,60 @@ func (c *Client) CreateOutboundRequest(p OutboundRequestPayload) (*OutboundReque
|
||||||
if len(dataRes) == 0 {
|
if len(dataRes) == 0 {
|
||||||
return nil, fmt.Errorf("tnc.Client.CreateOutboundRequest: empty_result")
|
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 ...
|
// GetOutboundRequestByID ...
|
||||||
func (c *Client) GetOutboundRequestByID(requestID int) (*OutboundRequestInfo, error) {
|
func (c *Client) GetOutboundRequestByID(requestID int) (*OutboundRequestInfo, error) {
|
||||||
apiURL := c.getBaseURL() + fmt.Sprintf(apiPathGetOutboundRequest, requestID)
|
apiURL := c.getBaseURL() + fmt.Sprintf(apiPathGetOutboundRequest, requestID)
|
||||||
natsPayload := natsiomodel.NatsRequestHTTP{
|
natsPayload := model.CommunicationRequestHttp{
|
||||||
ResponseImmediately: true,
|
ResponseImmediately: true,
|
||||||
Payload: natsiomodel.HTTPPayload{
|
Payload: model.HttpRequest{
|
||||||
URL: apiURL,
|
URL: apiURL,
|
||||||
Method: http.MethodGet,
|
Method: http.MethodGet,
|
||||||
Header: c.getRequestHeader(),
|
Header: c.getRequestHeader(),
|
||||||
|
@ -112,7 +156,7 @@ func (c *Client) GetOutboundRequestByID(requestID int) (*OutboundRequestInfo, er
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
var (
|
var (
|
||||||
r natsiomodel.NatsResponse
|
r model.CommunicationHttpResponse
|
||||||
errRes ErrRes
|
errRes ErrRes
|
||||||
outboundRequest OutboundRequestInfo
|
outboundRequest OutboundRequestInfo
|
||||||
)
|
)
|
||||||
|
@ -136,14 +180,16 @@ func (c *Client) GetOutboundRequestByID(requestID int) (*OutboundRequestInfo, er
|
||||||
}
|
}
|
||||||
|
|
||||||
// CancelOutboundRequest ...
|
// CancelOutboundRequest ...
|
||||||
func (c *Client) CancelOutboundRequest(requestID int) error {
|
func (c *Client) CancelOutboundRequest(requestID int, note string) error {
|
||||||
apiURL := c.getBaseURL() + fmt.Sprintf(apiPathCancelOutboundRequest, requestID)
|
apiURL := c.getBaseURL() + fmt.Sprintf(apiPathCancelOutboundRequest, requestID)
|
||||||
natsPayload := natsiomodel.NatsRequestHTTP{
|
data := map[string]string{"note": note}
|
||||||
|
natsPayload := model.CommunicationRequestHttp{
|
||||||
ResponseImmediately: true,
|
ResponseImmediately: true,
|
||||||
Payload: natsiomodel.HTTPPayload{
|
Payload: model.HttpRequest{
|
||||||
URL: apiURL,
|
URL: apiURL,
|
||||||
Method: http.MethodPost,
|
Method: http.MethodPost,
|
||||||
Header: c.getRequestHeader(),
|
Header: c.getRequestHeader(),
|
||||||
|
Data: pjson.ToJSONString(data),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
msg, err := c.requestHttpViaNats(natsPayload)
|
msg, err := c.requestHttpViaNats(natsPayload)
|
||||||
|
@ -155,7 +201,7 @@ func (c *Client) CancelOutboundRequest(requestID int) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
var (
|
var (
|
||||||
r natsiomodel.NatsResponse
|
r model.CommunicationHttpResponse
|
||||||
errRes ErrRes
|
errRes ErrRes
|
||||||
)
|
)
|
||||||
if err = pjson.Unmarshal(msg.Data, &r); err != nil {
|
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",
|
"Content-Type": "application/x-www-form-urlencoded",
|
||||||
}
|
}
|
||||||
apiURL := baseURLAuthENVMapping[c.env] + fmt.Sprintf(apiPathAuth, c.realm)
|
apiURL := baseURLAuthENVMapping[c.env] + fmt.Sprintf(apiPathAuth, c.realm)
|
||||||
natsPayload := natsiomodel.NatsRequestHTTP{
|
natsPayload := model.CommunicationRequestHttp{
|
||||||
ResponseImmediately: true,
|
ResponseImmediately: true,
|
||||||
Payload: natsiomodel.HTTPPayload{
|
Payload: model.HttpRequest{
|
||||||
URL: apiURL,
|
URL: apiURL,
|
||||||
Method: http.MethodPost,
|
Method: http.MethodPost,
|
||||||
Data: body,
|
Data: body,
|
||||||
|
@ -204,7 +250,7 @@ func (c *Client) auth() (*authRes, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
var (
|
var (
|
||||||
r natsiomodel.NatsResponse
|
r model.CommunicationHttpResponse
|
||||||
errRes ErrRes
|
errRes ErrRes
|
||||||
data authRes
|
data authRes
|
||||||
)
|
)
|
||||||
|
@ -240,7 +286,7 @@ func (c *Client) getRequestHeader() map[string]string {
|
||||||
return m
|
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
|
s := constant.NatsCommunicationSubjectRequestHTTP
|
||||||
b := pjson.ToBytes(data)
|
b := pjson.ToBytes(data)
|
||||||
return c.natsClient.Request(s, b)
|
return c.natsClient.Request(s, b)
|
||||||
|
|
Loading…
Reference in New Issue