define communication, warehouse func

This commit is contained in:
Sinh 2022-08-18 17:47:47 +07:00
parent 4eb30ac5d5
commit 572120c26a
9 changed files with 285 additions and 0 deletions

26
client/communication.go Normal file
View File

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

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
}

75
client/warehouse.go Normal file
View File

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

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

View File

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

View File

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

9
subject/communication.go Normal file
View File

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

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