From 572120c26a1dca5355d044606c291cd776f5dfc2 Mon Sep 17 00:00:00 2001 From: Sinh Date: Thu, 18 Aug 2022 17:47:47 +0700 Subject: [PATCH] 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" +)