From 65933158593abfe9f30fb7a9af57dbf7e2b4f074 Mon Sep 17 00:00:00 2001 From: namhq1989 Date: Fri, 26 Aug 2022 15:56:36 +0700 Subject: [PATCH] refactor subject name --- client/communication.go | 2 +- client/order.go | 4 ++-- client/warehouse.go | 8 ++++---- model/warehouse_response.go | 4 ++-- natsio.go | 3 +-- subject/communication.go | 23 ++++++++++++++++------- subject/config.go | 11 +++++++++++ subject/order.go | 19 ++++++++++++++----- subject/warehouse.go | 23 ++++++++++++++++------- 9 files changed, 67 insertions(+), 30 deletions(-) create mode 100644 subject/config.go diff --git a/client/communication.go b/client/communication.go index 2cbd644..6dd94cb 100644 --- a/client/communication.go +++ b/client/communication.go @@ -18,7 +18,7 @@ func GetCommunication() Communication { // RequestHttp ... func (c Communication) RequestHttp(p model.CommunicationRequestHttp) (r *model.CommunicationHttpResponse, err error) { - msg, err := natsio.GetServer().Request(subject.CommunicationRequestHTTP, toBytes(p)) + msg, err := natsio.GetServer().Request(subject.Communication.RequestHTTP, toBytes(p)) if err != nil { return nil, err } diff --git a/client/order.go b/client/order.go index 949bb51..d2ee714 100644 --- a/client/order.go +++ b/client/order.go @@ -19,7 +19,7 @@ func GetOrder() Order { // UpdateORStatus ... func (o Order) UpdateORStatus(p model.OrderUpdateORStatus) error { - msg, err := natsio.GetServer().Request(subject.OrderUpdateORStatus, toBytes(p)) + msg, err := natsio.GetServer().Request(subject.Order.UpdateORStatus, toBytes(p)) if err != nil { return err } @@ -37,7 +37,7 @@ func (o Order) UpdateORStatus(p model.OrderUpdateORStatus) error { // CancelDelivery ... func (o Order) CancelDelivery(p model.OrderCancelDelivery) error { - msg, err := natsio.GetServer().Request(subject.OrderUpdateORStatus, toBytes(p)) + msg, err := natsio.GetServer().Request(subject.Order.UpdateORStatus, toBytes(p)) if err != nil { return err } diff --git a/client/warehouse.go b/client/warehouse.go index d96d440..ed19faf 100644 --- a/client/warehouse.go +++ b/client/warehouse.go @@ -19,7 +19,7 @@ func GetWarehouse() Warehouse { // CreateOutboundRequest ... func (w Warehouse) CreateOutboundRequest(p model.OutboundRequestPayload) (*model.OutboundRequestResponse, error) { - msg, err := natsio.GetServer().Request(subject.WarehouseCreateOutboundRequest, toBytes(p)) + msg, err := natsio.GetServer().Request(subject.Warehouse.CreateOutboundRequest, toBytes(p)) if err != nil { return nil, err } @@ -38,7 +38,7 @@ func (w Warehouse) CreateOutboundRequest(p model.OutboundRequestPayload) (*model // UpdateOutboundRequestLogisticInfo ... func (w Warehouse) UpdateOutboundRequestLogisticInfo(p model.UpdateOutboundRequestLogisticInfoPayload) error { - msg, err := natsio.GetServer().Request(subject.WarehouseUpdateOutboundRequestLogistic, toBytes(p)) + msg, err := natsio.GetServer().Request(subject.Warehouse.UpdateOutboundRequestLogistic, toBytes(p)) if err != nil { return err } @@ -56,7 +56,7 @@ func (w Warehouse) UpdateOutboundRequestLogisticInfo(p model.UpdateOutboundReque // CancelOutboundRequest ... func (w Warehouse) CancelOutboundRequest(p model.CancelOutboundRequest) error { - msg, err := natsio.GetServer().Request(subject.WarehouseCancelOutboundRequest, toBytes(p)) + msg, err := natsio.GetServer().Request(subject.Warehouse.CancelOutboundRequest, toBytes(p)) if err != nil { return err } @@ -74,7 +74,7 @@ func (w Warehouse) CancelOutboundRequest(p model.CancelOutboundRequest) error { // GetConfigByWarehouseID ... func (w Warehouse) GetConfigByWarehouseID(warehouseID string) (*model.WarehouseConfiguration, error) { - msg, err := natsio.GetServer().Request(subject.WarehouseGetConfiguration, toBytes(warehouseID)) + msg, err := natsio.GetServer().Request(subject.Warehouse.GetConfiguration, toBytes(warehouseID)) if err != nil { return nil, err } diff --git a/model/warehouse_response.go b/model/warehouse_response.go index deaeff1..493a921 100644 --- a/model/warehouse_response.go +++ b/model/warehouse_response.go @@ -34,8 +34,8 @@ type WarehouseSupplier struct { type WarehouseOrder struct { MinimumValue float64 `json:"minimumValue"` PaymentMethod WarehousePaymentMethod `json:"paymentMethod"` - IsLimitNumberOfPurchases bool `json:"isLimitNumberOfPurchases" json:"isLimitNumberOfPurchases"` - LimitNumberOfPurchases int64 `json:"limitNumberOfPurchases" json:"limitNumberOfPurchases"` + IsLimitNumberOfPurchases bool `json:"isLimitNumberOfPurchases"` + LimitNumberOfPurchases int64 `json:"limitNumberOfPurchases"` } // WarehousePaymentMethod ... diff --git a/natsio.go b/natsio.go index 5ccc45d..0c2bca7 100644 --- a/natsio.go +++ b/natsio.go @@ -3,7 +3,6 @@ package natsio import ( "errors" "fmt" - "github.com/logrusorgru/aurora" "github.com/nats-io/nats.go" ) @@ -49,7 +48,7 @@ func Connect(cfg Config) error { return errors.New(msg) } - fmt.Println(aurora.Green("*** CONNECTED TO NATS: " + cfg.URL)) + fmt.Printf("⚡️[natsio]: connected to %s \n", cfg.URL) // Set client natsServer.instance = nc diff --git a/subject/communication.go b/subject/communication.go index 1ace687..bcbbe31 100644 --- a/subject/communication.go +++ b/subject/communication.go @@ -1,10 +1,19 @@ package subject -const communicationPrefix = "communication_" +import "fmt" -const ( - CommunicationRequestHTTP = communicationPrefix + "request_http" - CommunicationResponseHTTP = communicationPrefix + "response_http" - CommunicationWebhookTNC = communicationPrefix + "webhook_tnc" - CommunicationWebhookGlobalCare = communicationPrefix + "webhook_global_care" -) +func getCommunicationValue(val string) string { + return fmt.Sprintf("%s.%s", prefixes.Communication, val) +} + +var Communication = struct { + RequestHTTP string + ResponseHTTP string + WebhookTNC string + WebhookGlobalCare string +}{ + RequestHTTP: getCommunicationValue("request_http"), + ResponseHTTP: getCommunicationValue("response_http"), + WebhookTNC: getCommunicationValue("webhook_tnc"), + WebhookGlobalCare: getCommunicationValue("webhook_global_care"), +} diff --git a/subject/config.go b/subject/config.go new file mode 100644 index 0000000..fd6ccdf --- /dev/null +++ b/subject/config.go @@ -0,0 +1,11 @@ +package subject + +var prefixes = struct { + Communication string + Order string + Warehouse string +}{ + Communication: "communication", + Order: "order", + Warehouse: "warehouse", +} diff --git a/subject/order.go b/subject/order.go index e3c6f32..846cee5 100644 --- a/subject/order.go +++ b/subject/order.go @@ -1,8 +1,17 @@ package subject -const orderPrefix = "order_" +import "fmt" -const ( - OrderUpdateORStatus = orderPrefix + "update_outbound_request_status" - OrderCancelDelivery = orderPrefix + "cancel_delivery" -) +func getOrderValue(val string) string { + return fmt.Sprintf("%s.%s", prefixes.Order, val) +} + +var Order = struct { + UpdateORStatus string + CancelDelivery string + WebhookTNC string + WebhookGlobalCare string +}{ + UpdateORStatus: getOrderValue("update_outbound_request_status"), + CancelDelivery: getOrderValue("cancel_delivery"), +} diff --git a/subject/warehouse.go b/subject/warehouse.go index 92d9626..b0801b9 100644 --- a/subject/warehouse.go +++ b/subject/warehouse.go @@ -1,10 +1,19 @@ package subject -const warehousePrefix = "warehouse_" +import "fmt" -const ( - WarehouseCreateOutboundRequest = warehousePrefix + "create_outbound_request" - WarehouseUpdateOutboundRequestLogistic = warehousePrefix + "update_outbound_request_logistic_info" - WarehouseCancelOutboundRequest = warehousePrefix + "cancel_outbound_request" - WarehouseGetConfiguration = warehousePrefix + "get_configuration" -) +func getWarehouseValue(val string) string { + return fmt.Sprintf("%s.%s", prefixes.Warehouse, val) +} + +var Warehouse = struct { + CreateOutboundRequest string + UpdateOutboundRequestLogistic string + CancelOutboundRequest string + GetConfiguration string +}{ + CreateOutboundRequest: getWarehouseValue("create_outbound_request"), + UpdateOutboundRequestLogistic: getWarehouseValue("update_outbound_request_logistic_info"), + CancelOutboundRequest: getWarehouseValue("cancel_outbound_request"), + GetConfiguration: getWarehouseValue("get_configuration"), +}