Merge pull request #1 from Selly-Modules/integrate-tnc

Integrate tnc
This commit is contained in:
Sinh Luu 2022-08-30 16:09:04 +07:00 committed by GitHub
commit 31350b22e6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 51 additions and 45 deletions

View File

@ -1,9 +0,0 @@
package constant
const natsCommunicationPrefix = "communication_"
const (
NatsCommunicationSubjectRequestHTTP = natsCommunicationPrefix + "request_http"
NatsCommunicationSubjectResponseHTTP = natsCommunicationPrefix + "response_http"
NatsCommunicationSubjectWebhookTNC = natsCommunicationPrefix + "webhook_tnc"
)

7
go.mod
View File

@ -4,7 +4,9 @@ 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.1-0.20220818104747-572120c26a1d github.com/Selly-Modules/natsio v1.0.1
github.com/nats-io/nats.go v1.13.0
github.com/thoas/go-funk v0.9.1
) )
require ( require (
@ -14,14 +16,11 @@ require (
github.com/elastic/go-windows v1.0.0 // indirect github.com/elastic/go-windows v1.0.0 // indirect
github.com/jcchavezs/porto v0.1.0 // indirect github.com/jcchavezs/porto v0.1.0 // indirect
github.com/joeshaw/multierror v0.0.0-20140124173710-69b34d4ec901 // indirect github.com/joeshaw/multierror v0.0.0-20140124173710-69b34d4ec901 // indirect
github.com/logrusorgru/aurora v2.0.3+incompatible // indirect
github.com/nats-io/nats.go v1.13.0 // indirect
github.com/nats-io/nkeys v0.3.0 // indirect github.com/nats-io/nkeys v0.3.0 // indirect
github.com/nats-io/nuid v1.0.1 // indirect github.com/nats-io/nuid v1.0.1 // indirect
github.com/pkg/errors v0.8.1 // indirect github.com/pkg/errors v0.8.1 // indirect
github.com/prometheus/procfs v0.0.0-20190425082905-87a4384529e0 // indirect github.com/prometheus/procfs v0.0.0-20190425082905-87a4384529e0 // indirect
github.com/santhosh-tekuri/jsonschema v1.2.4 // indirect github.com/santhosh-tekuri/jsonschema v1.2.4 // indirect
github.com/thoas/go-funk v0.9.1 // indirect
go.elastic.co/apm v1.13.1 // indirect go.elastic.co/apm v1.13.1 // indirect
go.elastic.co/apm/module/apmzap v1.13.1 // indirect go.elastic.co/apm/module/apmzap v1.13.1 // indirect
go.elastic.co/fastjson v1.1.0 // indirect go.elastic.co/fastjson v1.1.0 // indirect

7
go.sum
View File

@ -1,9 +1,7 @@
github.com/Selly-Modules/logger v0.0.1 h1:dwLLtW53FfVBlklhdtFRB63eP0ofIh0IUQ/Gjgi0r5k= github.com/Selly-Modules/logger v0.0.1 h1:dwLLtW53FfVBlklhdtFRB63eP0ofIh0IUQ/Gjgi0r5k=
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.1 h1:0HoP00hP0Fz5h03p23Al3aARZwg+JSKpOrBo5Oh7pwY=
github.com/Selly-Modules/natsio v1.0.0/go.mod h1:NG55g9ip18nvN5tfP6PcSEKec10/lOeIOZC8HqBVNlQ= github.com/Selly-Modules/natsio v1.0.1/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=
@ -42,7 +40,6 @@ github.com/klauspost/compress v1.13.4/go.mod h1:8dP1Hq4DHOhN9w426knH3Rhby4rFm6D8
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/logrusorgru/aurora v2.0.3+incompatible h1:tOpm7WcpBTn4fjmVfgpQq0EfczGlG91VSDkswnjF5A8=
github.com/logrusorgru/aurora v2.0.3+incompatible/go.mod h1:7rIyQOR62GCctdiQpZ/zOJlFyk6y+94wXzv6RNZgaR4= github.com/logrusorgru/aurora v2.0.3+incompatible/go.mod h1:7rIyQOR62GCctdiQpZ/zOJlFyk6y+94wXzv6RNZgaR4=
github.com/minio/highwayhash v1.0.1 h1:dZ6IIu8Z14VlC0VpfKofAhCy74wu/Qb5gcn52yWoz/0= github.com/minio/highwayhash v1.0.1 h1:dZ6IIu8Z14VlC0VpfKofAhCy74wu/Qb5gcn52yWoz/0=
github.com/minio/highwayhash v1.0.1/go.mod h1:BQskDq+xkJ12lmlUUi7U0M5Swg3EWR+dLTk+kldvVxY= github.com/minio/highwayhash v1.0.1/go.mod h1:BQskDq+xkJ12lmlUUi7U0M5Swg3EWR+dLTk+kldvVxY=

View File

@ -8,10 +8,10 @@ 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/Selly-Modules/natsio/model"
"github.com/Selly-Modules/natsio/subject"
"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/3pl/constant"
"github.com/Selly-Modules/3pl/util/base64" "github.com/Selly-Modules/3pl/util/base64"
"github.com/Selly-Modules/3pl/util/pjson" "github.com/Selly-Modules/3pl/util/pjson"
) )
@ -70,7 +70,7 @@ func (c *Client) CreateOrder(p CreateOrderPayload) (*CommonResponse, error) {
Data: pjson.ToJSONString(body), Data: pjson.ToJSONString(body),
}, },
} }
msg, err := c.requestNats(constant.NatsCommunicationSubjectRequestHTTP, natsPayload) msg, err := c.requestNats(subject.Communication.RequestHTTP, natsPayload)
if err != nil { if err != nil {
logger.Error("globalcare.Client.CreateOrder", logger.LogData{ logger.Error("globalcare.Client.CreateOrder", logger.LogData{
"err": err.Error(), "err": err.Error(),
@ -99,7 +99,7 @@ func (c *Client) GetOrder(orderCode string) (*CommonResponse, error) {
Method: http.MethodGet, Method: http.MethodGet,
}, },
} }
msg, err := c.requestNats(constant.NatsCommunicationSubjectRequestHTTP, natsPayload) msg, err := c.requestNats(subject.Communication.RequestHTTP, natsPayload)
if err != nil { if err != nil {
logger.Error("globalcare.Client.GetOrder", logger.LogData{ logger.Error("globalcare.Client.GetOrder", logger.LogData{
"err": err.Error(), "err": err.Error(),

View File

@ -37,6 +37,10 @@ const (
baseURLProd = "https://ext-api.tnclog.vn" baseURLProd = "https://ext-api.tnclog.vn"
) )
const (
ErrCodeExistPartnerCode = "exist_partner_code"
)
var ( var (
baseURLENVMapping = map[ENV]string{ baseURLENVMapping = map[ENV]string{
EnvProd: baseURLProd, EnvProd: baseURLProd,

22
partnerapi/tnc/error.go Normal file
View File

@ -0,0 +1,22 @@
package tnc
import (
"fmt"
)
// Error ...
type Error struct {
Code string `json:"code"`
Message string `json:"errorMessage"`
}
// Error ...
func (e Error) Error() string {
return fmt.Sprintf("tnc_err: code %s, messsage %s", e.Code, e.Message)
}
// IsErrExistPartnerCode ...
func IsErrExistPartnerCode(err error) bool {
e, ok := err.(Error)
return ok && e.Code == ErrCodeExistPartnerCode
}

View File

@ -5,13 +5,7 @@ type OutboundRequestRes struct {
OrID int `json:"orId"` OrID int `json:"orId"`
OrCode string `json:"orCode"` OrCode string `json:"orCode"`
PartnerORCode string `json:"partnerORCode"` PartnerORCode string `json:"partnerORCode"`
Error *ErrRes `json:"error"` Error *Error `json:"error"`
}
// ErrRes ...
type ErrRes struct {
Code string `json:"code"`
ErrorMessage string `json:"errorMessage"`
} }
type authRes struct { type authRes struct {

View File

@ -9,9 +9,9 @@ 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/Selly-Modules/natsio/model"
"github.com/Selly-Modules/natsio/subject"
"github.com/nats-io/nats.go" "github.com/nats-io/nats.go"
"github.com/Selly-Modules/3pl/constant"
"github.com/Selly-Modules/3pl/util/pjson" "github.com/Selly-Modules/3pl/util/pjson"
) )
@ -66,7 +66,7 @@ func (c *Client) CreateOutboundRequest(p OutboundRequestPayload) (*OutboundReque
} }
var ( var (
r model.CommunicationHttpResponse r model.CommunicationHttpResponse
errRes ErrRes errRes Error
dataRes []OutboundRequestRes dataRes []OutboundRequestRes
) )
if err = pjson.Unmarshal(msg.Data, &r); err != nil { if err = pjson.Unmarshal(msg.Data, &r); err != nil {
@ -80,7 +80,7 @@ func (c *Client) CreateOutboundRequest(p OutboundRequestPayload) (*OutboundReque
if err = r.ParseResponseData(&errRes); err != nil { if err = r.ParseResponseData(&errRes); err != nil {
return nil, fmt.Errorf("tnc.Client.CreateOutboundRequest: parse_response_err: %v", err) return nil, fmt.Errorf("tnc.Client.CreateOutboundRequest: parse_response_err: %v", err)
} }
return nil, fmt.Errorf("tnc.Client.CreateOutboundRequest: failed code %s, message %s", errRes.Code, errRes.ErrorMessage) return nil, errRes
} }
if err = r.ParseResponseData(&dataRes); err != nil { if err = r.ParseResponseData(&dataRes); err != nil {
return nil, fmt.Errorf("tnc.Client.CreateOutboundRequest: parse_response_data: %v", err) return nil, fmt.Errorf("tnc.Client.CreateOutboundRequest: parse_response_data: %v", err)
@ -91,7 +91,7 @@ func (c *Client) CreateOutboundRequest(p OutboundRequestPayload) (*OutboundReque
item := &dataRes[0] item := &dataRes[0]
e := item.Error e := item.Error
if e != nil { if e != nil {
return nil, fmt.Errorf("tnc.Client.CreateOutboundRequest: failed, code %s - message %s", e.Code, e.ErrorMessage) return nil, errRes
} }
return item, err return item, err
@ -119,7 +119,7 @@ func (c *Client) UpdateOutboundRequestLogisticInfo(p UpdateORLogisticInfoPayload
} }
var ( var (
r model.CommunicationHttpResponse r model.CommunicationHttpResponse
errRes ErrRes errRes Error
) )
if err = pjson.Unmarshal(msg.Data, &r); err != nil { if err = pjson.Unmarshal(msg.Data, &r); err != nil {
return fmt.Errorf("tnc.Client.UpdateOutboundRequestLogisticInfo: parse_data %v", err) return fmt.Errorf("tnc.Client.UpdateOutboundRequestLogisticInfo: parse_data %v", err)
@ -132,7 +132,7 @@ func (c *Client) UpdateOutboundRequestLogisticInfo(p UpdateORLogisticInfoPayload
if err = r.ParseResponseData(&errRes); err != nil { if err = r.ParseResponseData(&errRes); err != nil {
return fmt.Errorf("tnc.Client.UpdateOutboundRequestLogisticInfo: parse_response_err: %v", err) 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 errRes
} }
return nil return nil
} }
@ -158,7 +158,7 @@ func (c *Client) GetOutboundRequestByID(requestID int) (*OutboundRequestInfo, er
} }
var ( var (
r model.CommunicationHttpResponse r model.CommunicationHttpResponse
errRes ErrRes errRes Error
outboundRequest OutboundRequestInfo outboundRequest OutboundRequestInfo
) )
if err = pjson.Unmarshal(msg.Data, &r); err != nil { if err = pjson.Unmarshal(msg.Data, &r); err != nil {
@ -172,7 +172,7 @@ func (c *Client) GetOutboundRequestByID(requestID int) (*OutboundRequestInfo, er
if err = r.ParseResponseData(&errRes); err != nil { if err = r.ParseResponseData(&errRes); err != nil {
return nil, fmt.Errorf("tnc.Client.GetOutboundRequestByID: parse_response_err: %v", err) return nil, fmt.Errorf("tnc.Client.GetOutboundRequestByID: parse_response_err: %v", err)
} }
return nil, fmt.Errorf("tnc.Client.GetOutboundRequestByID: failed code %s, message %s", errRes.Code, errRes.ErrorMessage) return nil, errRes
} }
if err = r.ParseResponseData(&outboundRequest); err != nil { if err = r.ParseResponseData(&outboundRequest); err != nil {
return nil, fmt.Errorf("tnc.Client.GetOutboundRequestByID: parse_response_data: %v", err) return nil, fmt.Errorf("tnc.Client.GetOutboundRequestByID: parse_response_data: %v", err)
@ -203,7 +203,7 @@ func (c *Client) CancelOutboundRequest(requestID int, note string) error {
} }
var ( var (
r model.CommunicationHttpResponse r model.CommunicationHttpResponse
errRes ErrRes errRes Error
) )
if err = pjson.Unmarshal(msg.Data, &r); err != nil { if err = pjson.Unmarshal(msg.Data, &r); err != nil {
return fmt.Errorf("tnc.Client.CancelOutboundRequest: parse_data %v", err) return fmt.Errorf("tnc.Client.CancelOutboundRequest: parse_data %v", err)
@ -216,7 +216,7 @@ func (c *Client) CancelOutboundRequest(requestID int, note string) error {
if err = r.ParseResponseData(&errRes); err != nil { if err = r.ParseResponseData(&errRes); err != nil {
return fmt.Errorf("tnc.Client.CancelOutboundRequest: parse_response_err: %v", err) return fmt.Errorf("tnc.Client.CancelOutboundRequest: parse_response_err: %v", err)
} }
return fmt.Errorf("tnc.Client.CancelOutboundRequest: failed code %s, message %s", errRes.Code, errRes.ErrorMessage) return errRes
} }
return nil return nil
} }
@ -252,7 +252,7 @@ func (c *Client) auth() (*authRes, error) {
} }
var ( var (
r model.CommunicationHttpResponse r model.CommunicationHttpResponse
errRes ErrRes errRes Error
data authRes data authRes
) )
if err = pjson.Unmarshal(msg.Data, &r); err != nil { if err = pjson.Unmarshal(msg.Data, &r); err != nil {
@ -266,7 +266,7 @@ func (c *Client) auth() (*authRes, error) {
if err = r.ParseResponseData(&errRes); err != nil { if err = r.ParseResponseData(&errRes); err != nil {
return nil, fmt.Errorf("tnc.Client.auth: parse_response_err: %v", err) return nil, fmt.Errorf("tnc.Client.auth: parse_response_err: %v", err)
} }
return nil, fmt.Errorf("tnc.Client.auth: failed code %s, message %s", errRes.Code, errRes.ErrorMessage) return nil, errRes
} }
if err = r.ParseResponseData(&data); err != nil { if err = r.ParseResponseData(&data); err != nil {
return nil, fmt.Errorf("tnc.Client.auth: parse_response_data: %v", err) return nil, fmt.Errorf("tnc.Client.auth: parse_response_data: %v", err)
@ -288,9 +288,8 @@ func (c *Client) getRequestHeader() map[string]string {
} }
func (c *Client) requestHttpViaNats(data model.CommunicationRequestHttp) (*nats.Msg, error) { func (c *Client) requestHttpViaNats(data model.CommunicationRequestHttp) (*nats.Msg, error) {
s := constant.NatsCommunicationSubjectRequestHTTP
b := pjson.ToBytes(data) b := pjson.ToBytes(data)
return c.natsClient.Request(s, b) return c.natsClient.Request(subject.Communication.RequestHTTP, b)
} }
func (c *Client) getBaseURL() string { func (c *Client) getBaseURL() string {