Merge pull request #5 from Selly-Modules/define-communication-func

define communication, warehouse func
This commit is contained in:
Sinh Luu 2022-08-24 09:42:24 +07:00 committed by GitHub
commit c0b13e3539
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 435 additions and 2 deletions

27
client/communication.go Normal file
View File

@ -0,0 +1,27 @@
package client
import (
"encoding/json"
"github.com/Selly-Modules/natsio"
"github.com/Selly-Modules/natsio/model"
"github.com/Selly-Modules/natsio/subject"
)
// Communication ...
type Communication struct{}
// GetCommunication ...
func GetCommunication() Communication {
return Communication{}
}
// RequestHttp ...
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
}
err = json.Unmarshal(msg.Data, &r)
return r, err
}

52
client/order.go Normal file
View File

@ -0,0 +1,52 @@
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 (o 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
}
// 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
}

14
client/util.go Normal file
View File

@ -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
}

92
client/warehouse.go Normal file
View File

@ -0,0 +1,92 @@
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 {
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
}
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
}

28
json_encoder.go Normal file
View File

@ -0,0 +1,28 @@
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)
} else {
log.Printf("natsio.JSONEncoder - subscribed to subject %s successfully\n", subject)
}
return sub, err
}
// Publish ...
func (e JSONEncoder) Publish(reply string, data interface{}) error {
return e.encConn.Publish(reply, data)
}

18
model/common_response.go Normal file
View File

@ -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)
}

View File

@ -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"`
}

View File

@ -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"`
}

14
model/order_request.go Normal file
View File

@ -0,0 +1,14 @@
package model
// OrderUpdateORStatus ...
type OrderUpdateORStatus struct {
OrderCode string `json:"orderCode"`
ORCode string `json:"orCode"`
Status string `json:"status"`
Reason string `json:"reason"`
}
// OrderCancelDelivery ...
type OrderCancelDelivery struct {
OrderID string `json:"orderId"`
}

View File

@ -0,0 +1,51 @@
package model
// OutboundRequestPayload ...
type OutboundRequestPayload struct {
OrderID string `json:"orderId"`
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"`
Note string `json:"note"`
}

View File

@ -0,0 +1,61 @@
package model
// OutboundRequestResponse ...
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"`
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"`
}

View File

@ -3,6 +3,7 @@ package natsio
import ( import (
"errors" "errors"
"fmt" "fmt"
"log"
"time" "time"
"github.com/nats-io/nats.go" "github.com/nats-io/nats.go"
@ -36,6 +37,11 @@ func (sv Server) Subscribe(subject string, cb nats.MsgHandler) (*nats.Subscripti
} }
// NewJSONEncodedConn ... // NewJSONEncodedConn ...
func (sv Server) NewJSONEncodedConn() (*nats.EncodedConn, error) { func (sv Server) NewJSONEncodedConn() (*JSONEncoder, error) {
return nats.NewEncodedConn(sv.instance, nats.JSON_ENCODER) 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
} }

10
subject/communication.go Normal file
View File

@ -0,0 +1,10 @@
package subject
const communicationPrefix = "communication_"
const (
CommunicationRequestHTTP = communicationPrefix + "request_http"
CommunicationResponseHTTP = communicationPrefix + "response_http"
CommunicationWebhookTNC = communicationPrefix + "webhook_tnc"
CommunicationWebhookGlobalCare = communicationPrefix + "webhook_global_care"
)

8
subject/order.go Normal file
View File

@ -0,0 +1,8 @@
package subject
const orderPrefix = "order_"
const (
OrderUpdateORStatus = orderPrefix + "update_outbound_request_status"
OrderCancelDelivery = orderPrefix + "cancel_delivery"
)

10
subject/warehouse.go Normal file
View File

@ -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"
)