From 572120c26a1dca5355d044606c291cd776f5dfc2 Mon Sep 17 00:00:00 2001 From: Sinh Date: Thu, 18 Aug 2022 17:47:47 +0700 Subject: [PATCH 1/9] define communication, warehouse func --- client/communication.go | 26 ++++++++++++ client/util.go | 14 ++++++ client/warehouse.go | 75 +++++++++++++++++++++++++++++++++ model/communication_request.go | 17 ++++++++ model/communication_response.go | 25 +++++++++++ model/warehouse_request.go | 49 +++++++++++++++++++++ model/warehouse_response.go | 60 ++++++++++++++++++++++++++ subject/communication.go | 9 ++++ subject/warehouse.go | 10 +++++ 9 files changed, 285 insertions(+) create mode 100644 client/communication.go create mode 100644 client/util.go create mode 100644 client/warehouse.go create mode 100644 model/communication_request.go create mode 100644 model/communication_response.go create mode 100644 model/warehouse_request.go create mode 100644 model/warehouse_response.go create mode 100644 subject/communication.go create mode 100644 subject/warehouse.go diff --git a/client/communication.go b/client/communication.go new file mode 100644 index 0000000..681493f --- /dev/null +++ b/client/communication.go @@ -0,0 +1,26 @@ +package client + +import ( + "encoding/json" + + "github.com/Selly-Modules/natsio" + "github.com/Selly-Modules/natsio/subject" +) + +// Communication ... +type Communication struct{} + +// GetCommunication ... +func GetCommunication() Communication { + return Communication{} +} + +// RequestHttp ... +func (c Communication) RequestHttp(p communication.CommunicationRequestHttp) (r *communication.CommunicationHttpResponse, err error) { + msg, err := natsio.GetServer().Request(subject.CommunicationRequestHTTP, toBytes(p)) + if err != nil { + return nil, err + } + err = json.Unmarshal(msg.Data, &r) + return r, err +} diff --git a/client/util.go b/client/util.go new file mode 100644 index 0000000..3e30617 --- /dev/null +++ b/client/util.go @@ -0,0 +1,14 @@ +package client + +import ( + "encoding/json" + "log" +) + +func toBytes(data interface{}) []byte { + b, err := json.Marshal(data) + if err != nil { + log.Printf("natsio/client.toBytes: marshal_json %v", err) + } + return b +} diff --git a/client/warehouse.go b/client/warehouse.go new file mode 100644 index 0000000..f806bc5 --- /dev/null +++ b/client/warehouse.go @@ -0,0 +1,75 @@ +package client + +import ( + "encoding/json" + "errors" + + "github.com/Selly-Modules/natsio" + "github.com/Selly-Modules/natsio/model" + "github.com/Selly-Modules/natsio/subject" +) + +// Warehouse ... +type Warehouse struct{} + +// GetWarehouse ... +func GetWarehouse() Warehouse { + return Warehouse{} +} + +// CreateOutboundRequest ... +func (w Warehouse) CreateOutboundRequest(p model.OutboundRequestPayload) (*model.OutboundRequestResponse, error) { + msg, err := natsio.GetServer().Request(subject.WarehouseCreateOutboundRequest, toBytes(p)) + if err != nil { + return nil, err + } + var r struct { + Data *model.OutboundRequestResponse `json:"data"` + Error string `json:"error"` + } + if err = json.Unmarshal(msg.Data, &r); err != nil { + return nil, err + } + if r.Error != "" { + return nil, errors.New(r.Error) + } + return r.Data, nil +} + +// UpdateOutboundRequestLogisticInfo ... +func (w Warehouse) UpdateOutboundRequestLogisticInfo(p model.UpdateOutboundRequestLogisticInfoPayload) error { + msg, err := natsio.GetServer().Request(subject.WarehouseUpdateOutboundRequestLogistic, toBytes(p)) + if err != nil { + return err + } + var r struct { + Data *model.OutboundRequestResponse `json:"data"` + Error string `json:"error"` + } + if err = json.Unmarshal(msg.Data, &r); err != nil { + return err + } + if r.Error != "" { + return errors.New(r.Error) + } + return nil +} + +// GetConfigByWarehouseID ... +func (w Warehouse) GetConfigByWarehouseID(warehouseID string) (*model.WarehouseConfiguration, error) { + msg, err := natsio.GetServer().Request(subject.WarehouseGetConfiguration, toBytes(warehouseID)) + if err != nil { + return nil, err + } + var r struct { + Data *model.WarehouseConfiguration `json:"data"` + Error string `json:"error"` + } + if err = json.Unmarshal(msg.Data, &r); err != nil { + return nil, err + } + if r.Error != "" { + return nil, errors.New(r.Error) + } + return r.Data, nil +} diff --git a/model/communication_request.go b/model/communication_request.go new file mode 100644 index 0000000..eb64ef2 --- /dev/null +++ b/model/communication_request.go @@ -0,0 +1,17 @@ +package model + +// CommunicationRequestHttp ... +type CommunicationRequestHttp struct { + ResponseImmediately bool `json:"responseImmediately"` + Authentication string `json:"authentication"` + Payload HttpRequest `json:"payload"` +} + +// HttpRequest ... +type HttpRequest 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/communication_response.go b/model/communication_response.go new file mode 100644 index 0000000..f57e176 --- /dev/null +++ b/model/communication_response.go @@ -0,0 +1,25 @@ +package model + +import "encoding/json" + +// CommunicationHttpResponse ... +type CommunicationHttpResponse struct { + Response *HttpResponse `json:"response"` + Error bool `json:"error"` + Message string `json:"message"` + RequestID string `json:"requestId"` +} + +// ParseResponseData ... +func (r *CommunicationHttpResponse) ParseResponseData(result interface{}) error { + if r.Response == nil { + return nil + } + return json.Unmarshal([]byte(r.Response.Body), result) +} + +// HttpResponse ... +type HttpResponse struct { + Body string `json:"body"` + StatusCode int `json:"statusCode"` +} diff --git a/model/warehouse_request.go b/model/warehouse_request.go new file mode 100644 index 0000000..fd0b976 --- /dev/null +++ b/model/warehouse_request.go @@ -0,0 +1,49 @@ +package model + +// OutboundRequestPayload ... +type OutboundRequestPayload struct { + OrderCode string `json:"orderCode"` + TrackingCode string `json:"trackingCode"` + WarehouseID string `json:"warehouseId"` + SupplierID string `json:"supplierId"` + Note string `json:"note"` + CODAmount float64 `json:"codAmount"` + TPLCode string `json:"tplCode"` + Customer CustomerInfo `json:"customer"` + Items []OutboundRequestItem `json:"items"` +} + +// OutboundRequestItem ... +type OutboundRequestItem struct { + SupplierSKU string `json:"supplierSKU"` + Quantity int64 `json:"quantity"` + UnitCode string `json:"unitCode"` +} + +// CustomerInfo ... +type CustomerInfo struct { + Name string `json:"name"` + PhoneNumber string `json:"phoneNumber"` + Address AddressDetail `json:"address"` +} + +// AddressDetail ... +type AddressDetail struct { + Address string `json:"address"` + FullAddress string `json:"fullAddress"` + ProvinceCode int `json:"provinceCode"` + DistrictCode int `json:"districtCode"` + WardCode int `json:"wardCode"` +} + +// UpdateOutboundRequestLogisticInfoPayload ... +type UpdateOutboundRequestLogisticInfoPayload struct { + ShippingLabel string `json:"shippingLabel"` + TrackingCode string `json:"trackingCode"` + ORCode string `json:"orCode"` +} + +// CancelOutboundRequest ... +type CancelOutboundRequest struct { + ORCode string `json:"orCode"` +} diff --git a/model/warehouse_response.go b/model/warehouse_response.go new file mode 100644 index 0000000..4b996f6 --- /dev/null +++ b/model/warehouse_response.go @@ -0,0 +1,60 @@ +package model + +// OutboundRequestResponse ... +type OutboundRequestResponse struct { + // System code + OrderCode string `json:"orderCode"` + TrackingCode string `json:"trackingCode"` + + // Partner response + ORCode string `json:"orCode"` + RequestID string `json:"requestId"` + Status string `json:"status"` + Reason string `json:"reason"` +} + +// WarehouseConfiguration ... +type WarehouseConfiguration struct { + Warehouse string `json:"warehouse"` + DoesSupportSellyExpress bool `json:"doesSupportSellyExpress"` + Supplier WarehouseSupplier `json:"supplier"` + Order WarehouseOrder `json:"order"` + Partner WarehousePartner `json:"partner"` + Delivery WarehouseDelivery `json:"delivery"` +} + +// WarehouseSupplier ... +type WarehouseSupplier struct { + CanAutoSendMail bool `json:"canAutoSendMail"` + InvoiceDeliveryMethod string `json:"invoiceDeliveryMethod"` +} + +// WarehouseOrder ... +type WarehouseOrder struct { + MinimumValue float64 `json:"minimumValue"` + PaymentMethod WarehousePaymentMethod `json:"paymentMethod"` + IsLimitNumberOfPurchases bool `json:"isLimitNumberOfPurchases" json:"isLimitNumberOfPurchases"` + LimitNumberOfPurchases int64 `json:"limitNumberOfPurchases" json:"limitNumberOfPurchases"` +} + +// WarehousePaymentMethod ... +type WarehousePaymentMethod struct { + Cod bool `json:"cod"` + BankTransfer bool `json:"bankTransfer"` +} + +// WarehouseDelivery ... +type WarehouseDelivery struct { + DeliveryMethods []string `json:"deliveryMethods"` + PriorityServiceCodes []string `json:"priorityDeliveryServiceCodes"` + EnabledSources []int `json:"enabledDeliverySources"` + Types []string `json:"type"` +} + +// WarehousePartner ... +type WarehousePartner struct { + IdentityCode string `json:"identityCode"` + Code string `json:"code"` + Enabled bool `json:"enabled"` + Authentication string `json:"authentication"` +} diff --git a/subject/communication.go b/subject/communication.go new file mode 100644 index 0000000..4ee5b90 --- /dev/null +++ b/subject/communication.go @@ -0,0 +1,9 @@ +package subject + +const communicationPrefix = "communication_" + +const ( + CommunicationRequestHTTP = communicationPrefix + "request_http" + CommunicationResponseHTTP = communicationPrefix + "response_http" + CommunicationWebhookTNC = communicationPrefix + "webhook_tnc" +) diff --git a/subject/warehouse.go b/subject/warehouse.go new file mode 100644 index 0000000..92d9626 --- /dev/null +++ b/subject/warehouse.go @@ -0,0 +1,10 @@ +package subject + +const warehousePrefix = "warehouse_" + +const ( + WarehouseCreateOutboundRequest = warehousePrefix + "create_outbound_request" + WarehouseUpdateOutboundRequestLogistic = warehousePrefix + "update_outbound_request_logistic_info" + WarehouseCancelOutboundRequest = warehousePrefix + "cancel_outbound_request" + WarehouseGetConfiguration = warehousePrefix + "get_configuration" +) From 3a57e9e85f79feaf3c8b3e2ac3210efeb8ab10d6 Mon Sep 17 00:00:00 2001 From: Sinh Date: Thu, 18 Aug 2022 17:59:09 +0700 Subject: [PATCH 2/9] update model --- model/warehouse_request.go | 1 + 1 file changed, 1 insertion(+) diff --git a/model/warehouse_request.go b/model/warehouse_request.go index fd0b976..c46ff3f 100644 --- a/model/warehouse_request.go +++ b/model/warehouse_request.go @@ -46,4 +46,5 @@ type UpdateOutboundRequestLogisticInfoPayload struct { // CancelOutboundRequest ... type CancelOutboundRequest struct { ORCode string `json:"orCode"` + Note string `json:"note"` } From fcc909953367092860b0e0d90edc73a8c75a6004 Mon Sep 17 00:00:00 2001 From: Sinh Date: Fri, 19 Aug 2022 10:02:29 +0700 Subject: [PATCH 3/9] update import --- client/communication.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/client/communication.go b/client/communication.go index 681493f..2cbd644 100644 --- a/client/communication.go +++ b/client/communication.go @@ -4,6 +4,7 @@ import ( "encoding/json" "github.com/Selly-Modules/natsio" + "github.com/Selly-Modules/natsio/model" "github.com/Selly-Modules/natsio/subject" ) @@ -16,7 +17,7 @@ func GetCommunication() Communication { } // RequestHttp ... -func (c Communication) RequestHttp(p communication.CommunicationRequestHttp) (r *communication.CommunicationHttpResponse, err error) { +func (c Communication) RequestHttp(p model.CommunicationRequestHttp) (r *model.CommunicationHttpResponse, err error) { msg, err := natsio.GetServer().Request(subject.CommunicationRequestHTTP, toBytes(p)) if err != nil { return nil, err From b82c01759625d1a6a3f4813e7f3eef579a0f3b6b Mon Sep 17 00:00:00 2001 From: Sinh Date: Fri, 19 Aug 2022 11:47:34 +0700 Subject: [PATCH 4/9] add cancel OR func --- client/warehouse.go | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/client/warehouse.go b/client/warehouse.go index f806bc5..d96d440 100644 --- a/client/warehouse.go +++ b/client/warehouse.go @@ -43,8 +43,25 @@ func (w Warehouse) UpdateOutboundRequestLogisticInfo(p model.UpdateOutboundReque return err } var r struct { - Data *model.OutboundRequestResponse `json:"data"` - Error string `json:"error"` + Error string `json:"error"` + } + if err = json.Unmarshal(msg.Data, &r); err != nil { + return err + } + if r.Error != "" { + return errors.New(r.Error) + } + return nil +} + +// CancelOutboundRequest ... +func (w Warehouse) CancelOutboundRequest(p model.CancelOutboundRequest) error { + msg, err := natsio.GetServer().Request(subject.WarehouseCancelOutboundRequest, toBytes(p)) + if err != nil { + return err + } + var r struct { + Error string `json:"error"` } if err = json.Unmarshal(msg.Data, &r); err != nil { return err From 0eb670a0c5aecd3e4c2ebab8ab30a137fb44fdb2 Mon Sep 17 00:00:00 2001 From: Sinh Date: Fri, 19 Aug 2022 14:07:14 +0700 Subject: [PATCH 5/9] update json encoder --- json_encoder.go | 26 ++++++++++++++++++++++++++ server_reqres.go | 10 ++++++++-- 2 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 json_encoder.go diff --git a/json_encoder.go b/json_encoder.go new file mode 100644 index 0000000..379b408 --- /dev/null +++ b/json_encoder.go @@ -0,0 +1,26 @@ +package natsio + +import ( + "log" + + "github.com/nats-io/nats.go" +) + +// JSONEncoder ... +type JSONEncoder struct { + encConn *nats.EncodedConn +} + +// Subscribe ... +func (e JSONEncoder) Subscribe(subject string, cb nats.Handler) (*nats.Subscription, error) { + sub, err := e.encConn.Subscribe(subject, cb) + if err != nil { + log.Printf("natsio.JSONEncoder.Subscribe err: %v\n", err) + } + return sub, err +} + +// Publish ... +func (e JSONEncoder) Publish(reply string, data interface{}) error { + return e.encConn.Publish(reply, data) +} diff --git a/server_reqres.go b/server_reqres.go index c21b4dc..841f9ef 100644 --- a/server_reqres.go +++ b/server_reqres.go @@ -3,6 +3,7 @@ package natsio import ( "errors" "fmt" + "log" "time" "github.com/nats-io/nats.go" @@ -36,6 +37,11 @@ func (sv Server) Subscribe(subject string, cb nats.MsgHandler) (*nats.Subscripti } // NewJSONEncodedConn ... -func (sv Server) NewJSONEncodedConn() (*nats.EncodedConn, error) { - return nats.NewEncodedConn(sv.instance, nats.JSON_ENCODER) +func (sv Server) NewJSONEncodedConn() (*JSONEncoder, error) { + enc, err := nats.NewEncodedConn(sv.instance, nats.JSON_ENCODER) + if err != nil { + log.Printf("natsio.NewJSONEncodedConn: err %v\n", err) + return nil, err + } + return &JSONEncoder{encConn: enc}, nil } From 56e810378ee0740d0bd9cd15e32560fd18559529 Mon Sep 17 00:00:00 2001 From: Sinh Date: Sat, 20 Aug 2022 21:21:51 +0700 Subject: [PATCH 6/9] define order client --- client/order.go | 36 ++++++++++++++++++++++++++++++++++++ json_encoder.go | 2 ++ model/order_request.go | 9 +++++++++ subject/order.go | 7 +++++++ 4 files changed, 54 insertions(+) create mode 100644 client/order.go create mode 100644 model/order_request.go create mode 100644 subject/order.go diff --git a/client/order.go b/client/order.go new file mode 100644 index 0000000..adae205 --- /dev/null +++ b/client/order.go @@ -0,0 +1,36 @@ +package client + +import ( + "encoding/json" + "errors" + + "github.com/Selly-Modules/natsio" + "github.com/Selly-Modules/natsio/model" + "github.com/Selly-Modules/natsio/subject" +) + +// Order ... +type Order struct{} + +// GetOrder ... +func GetOrder() Order { + return Order{} +} + +// UpdateORStatus ... +func (w Order) UpdateORStatus(p model.OrderUpdateORStatus) error { + msg, err := natsio.GetServer().Request(subject.OrderUpdateORStatus, toBytes(p)) + if err != nil { + return err + } + var r struct { + Error string `json:"error"` + } + if err = json.Unmarshal(msg.Data, &r); err != nil { + return err + } + if r.Error != "" { + return errors.New(r.Error) + } + return nil +} diff --git a/json_encoder.go b/json_encoder.go index 379b408..4a0bdb8 100644 --- a/json_encoder.go +++ b/json_encoder.go @@ -16,6 +16,8 @@ func (e JSONEncoder) Subscribe(subject string, cb nats.Handler) (*nats.Subscript sub, err := e.encConn.Subscribe(subject, cb) if err != nil { log.Printf("natsio.JSONEncoder.Subscribe err: %v\n", err) + } else { + log.Printf("natsio.JSONEncoder - subscribed to subject %s successfully\n", subject) } return sub, err } diff --git a/model/order_request.go b/model/order_request.go new file mode 100644 index 0000000..e664b1a --- /dev/null +++ b/model/order_request.go @@ -0,0 +1,9 @@ +package model + +// OrderUpdateORStatus ... +type OrderUpdateORStatus struct { + OrderCode string `json:"orderCode"` + ORCode string `json:"orCode"` + Status string `json:"status"` + Reason string `json:"reason"` +} diff --git a/subject/order.go b/subject/order.go new file mode 100644 index 0000000..f13be90 --- /dev/null +++ b/subject/order.go @@ -0,0 +1,7 @@ +package subject + +const orderPrefix = "order_" + +const ( + OrderUpdateORStatus = orderPrefix + "update_outbound_request_status" +) From 2344c5a73886c9f6a613fa5cf785942d079dfeae Mon Sep 17 00:00:00 2001 From: Sinh Date: Mon, 22 Aug 2022 11:02:37 +0700 Subject: [PATCH 7/9] update OR payload --- model/warehouse_request.go | 1 + model/warehouse_response.go | 1 + 2 files changed, 2 insertions(+) diff --git a/model/warehouse_request.go b/model/warehouse_request.go index c46ff3f..5612532 100644 --- a/model/warehouse_request.go +++ b/model/warehouse_request.go @@ -2,6 +2,7 @@ package model // OutboundRequestPayload ... type OutboundRequestPayload struct { + OrderID string `json:"orderId"` OrderCode string `json:"orderCode"` TrackingCode string `json:"trackingCode"` WarehouseID string `json:"warehouseId"` diff --git a/model/warehouse_response.go b/model/warehouse_response.go index 4b996f6..deaeff1 100644 --- a/model/warehouse_response.go +++ b/model/warehouse_response.go @@ -5,6 +5,7 @@ type OutboundRequestResponse struct { // System code OrderCode string `json:"orderCode"` TrackingCode string `json:"trackingCode"` + ID string `json:"id"` // OR id // Partner response ORCode string `json:"orCode"` From 62206f147c627739cefad04fc0451d0566983ee4 Mon Sep 17 00:00:00 2001 From: Sinh Date: Mon, 22 Aug 2022 15:51:14 +0700 Subject: [PATCH 8/9] define subject --- subject/communication.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/subject/communication.go b/subject/communication.go index 4ee5b90..1ace687 100644 --- a/subject/communication.go +++ b/subject/communication.go @@ -3,7 +3,8 @@ package subject const communicationPrefix = "communication_" const ( - CommunicationRequestHTTP = communicationPrefix + "request_http" - CommunicationResponseHTTP = communicationPrefix + "response_http" - CommunicationWebhookTNC = communicationPrefix + "webhook_tnc" + CommunicationRequestHTTP = communicationPrefix + "request_http" + CommunicationResponseHTTP = communicationPrefix + "response_http" + CommunicationWebhookTNC = communicationPrefix + "webhook_tnc" + CommunicationWebhookGlobalCare = communicationPrefix + "webhook_global_care" ) From caaa9f098d7368fad41f316a8db20abc9d9fe0cb Mon Sep 17 00:00:00 2001 From: Sinh Date: Tue, 23 Aug 2022 14:26:49 +0700 Subject: [PATCH 9/9] define order client --- client/order.go | 18 +++++++++++++++++- model/common_response.go | 18 ++++++++++++++++++ model/order_request.go | 5 +++++ subject/order.go | 1 + 4 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 model/common_response.go diff --git a/client/order.go b/client/order.go index adae205..949bb51 100644 --- a/client/order.go +++ b/client/order.go @@ -18,7 +18,7 @@ func GetOrder() Order { } // UpdateORStatus ... -func (w Order) UpdateORStatus(p model.OrderUpdateORStatus) error { +func (o Order) UpdateORStatus(p model.OrderUpdateORStatus) error { msg, err := natsio.GetServer().Request(subject.OrderUpdateORStatus, toBytes(p)) if err != nil { return err @@ -34,3 +34,19 @@ func (w Order) UpdateORStatus(p model.OrderUpdateORStatus) error { } return nil } + +// CancelDelivery ... +func (o Order) CancelDelivery(p model.OrderCancelDelivery) error { + msg, err := natsio.GetServer().Request(subject.OrderUpdateORStatus, toBytes(p)) + if err != nil { + return err + } + var r model.CommonResponseData + if err = json.Unmarshal(msg.Data, &r); err != nil { + return err + } + if r.Error != "" { + return errors.New(r.Error) + } + return nil +} diff --git a/model/common_response.go b/model/common_response.go new file mode 100644 index 0000000..2d2aba8 --- /dev/null +++ b/model/common_response.go @@ -0,0 +1,18 @@ +package model + +import "encoding/json" + +// CommonResponseData ... +type CommonResponseData struct { + Data interface{} `json:"data"` + Error string `json:"error"` +} + +// ParseData ... +func (c CommonResponseData) ParseData(resultPointer interface{}) error { + b, err := json.Marshal(c.Data) + if err != nil { + return err + } + return json.Unmarshal(b, resultPointer) +} diff --git a/model/order_request.go b/model/order_request.go index e664b1a..da9d4fe 100644 --- a/model/order_request.go +++ b/model/order_request.go @@ -7,3 +7,8 @@ type OrderUpdateORStatus struct { Status string `json:"status"` Reason string `json:"reason"` } + +// OrderCancelDelivery ... +type OrderCancelDelivery struct { + OrderID string `json:"orderId"` +} diff --git a/subject/order.go b/subject/order.go index f13be90..e3c6f32 100644 --- a/subject/order.go +++ b/subject/order.go @@ -4,4 +4,5 @@ const orderPrefix = "order_" const ( OrderUpdateORStatus = orderPrefix + "update_outbound_request_status" + OrderCancelDelivery = orderPrefix + "cancel_delivery" )