diff --git a/client/order.go b/client/order.go index d2ee714..4379747 100644 --- a/client/order.go +++ b/client/order.go @@ -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.Order.UpdateORStatus, toBytes(p)) + msg, err := natsio.GetServer().Request(subject.Order.CancelDelivery, toBytes(p)) if err != nil { return err } diff --git a/client/supplier.go b/client/supplier.go index 01da757..5b4881a 100644 --- a/client/supplier.go +++ b/client/supplier.go @@ -17,15 +17,36 @@ func GetSupplier() Supplier { return Supplier{} } -func (s Supplier) GetSupplierInfo(supplierID model.GetSupplierRequest) (*model.ResponseSupplierInfo, error) { - msg, err := natsio.GetServer().Request(subject.Supplier.GetSupplierInfo, toBytes(supplierID)) +func (s Supplier) GetListSupplierInfo(p model.GetSupplierRequest) ([]*model.ResponseSupplierInfo, error) { + msg, err := natsio.GetServer().Request(subject.Supplier.GetListSupplierInfo, toBytes(p)) if err != nil { return nil, err } var r struct { - Data *model.ResponseSupplierInfo `json:"data"` - Error string `json:"error"` + Data []*model.ResponseSupplierInfo `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 +} + +func (s Supplier) GetSupplierContractBySupplierID(p model.GetSupplierContractRequest) (*model.ResponseSupplierContract, error) { + msg, err := natsio.GetServer().Request(subject.Supplier.GetSupplierContractBySupplierID, toBytes(p)) + if err != nil { + return nil, err + } + + var r struct { + Data *model.ResponseSupplierContract `json:"data"` + Error string `json:"error"` } if err := json.Unmarshal(msg.Data, &r); err != nil { diff --git a/client/util.go b/client/util.go index 3e30617..7676610 100644 --- a/client/util.go +++ b/client/util.go @@ -2,6 +2,7 @@ package client import ( "encoding/json" + "go.mongodb.org/mongo-driver/bson" "log" ) @@ -12,3 +13,9 @@ func toBytes(data interface{}) []byte { } return b } + +// bsonToBytes ... +func bsonToBytes(data interface{}) []byte { + b, _ := bson.Marshal(data) + return b +} diff --git a/client/warehouse_dao.go b/client/warehouse_dao.go new file mode 100644 index 0000000..69c2245 --- /dev/null +++ b/client/warehouse_dao.go @@ -0,0 +1,85 @@ +package client + +import ( + "encoding/json" + "errors" + "github.com/Selly-Modules/natsio" + "github.com/Selly-Modules/natsio/model" + "github.com/Selly-Modules/natsio/subject" +) + +// DistinctWithField ... +func (w Warehouse) DistinctWithField(p model.DistinctWithField) ([]interface{}, error) { + msg, err := natsio.GetServer().Request(subject.Warehouse.Distinct, bsonToBytes(p)) + if err != nil { + return nil, err + } + var r struct { + Data []interface{} `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 +} + +// FindOneByCondition ... +func (w Warehouse) FindOneByCondition(p model.FindOneCondition) (*model.WarehouseNatsResponse, error) { + msg, err := natsio.GetServer().Request(subject.Warehouse.FindOne, bsonToBytes(p)) + if err != nil { + return nil, err + } + var r struct { + Data *model.WarehouseNatsResponse `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 +} + +// CountByCondition ... +func (w Warehouse) CountByCondition(p model.FindWithCondition) (int64, error) { + msg, err := natsio.GetServer().Request(subject.Warehouse.Count, bsonToBytes(p)) + if err != nil { + return 0, err + } + var r struct { + Data int64 `json:"data"` + Error string `json:"error"` + } + if err = json.Unmarshal(msg.Data, &r); err != nil { + return 0, err + } + if r.Error != "" { + return 0, errors.New(r.Error) + } + return r.Data, nil +} + +// FindByCondition ... +func (w Warehouse) FindByCondition(p model.FindWithCondition) ([]*model.WarehouseNatsResponse, error) { + msg, err := natsio.GetServer().Request(subject.Warehouse.FindByCondition, bsonToBytes(p)) + if err != nil { + return nil, err + } + var r struct { + Data []*model.WarehouseNatsResponse `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/go.sum b/go.sum index edfc98b..f3e58d6 100644 --- a/go.sum +++ b/go.sum @@ -24,6 +24,8 @@ github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47e github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/logrusorgru/aurora v2.0.3+incompatible h1:tOpm7WcpBTn4fjmVfgpQq0EfczGlG91VSDkswnjF5A8= +github.com/logrusorgru/aurora v2.0.3+incompatible/go.mod h1:7rIyQOR62GCctdiQpZ/zOJlFyk6y+94wXzv6RNZgaR4= github.com/minio/highwayhash v1.0.1 h1:dZ6IIu8Z14VlC0VpfKofAhCy74wu/Qb5gcn52yWoz/0= github.com/minio/highwayhash v1.0.1/go.mod h1:BQskDq+xkJ12lmlUUi7U0M5Swg3EWR+dLTk+kldvVxY= github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc= diff --git a/model/common_request.go b/model/common_request.go new file mode 100644 index 0000000..4d6a719 --- /dev/null +++ b/model/common_request.go @@ -0,0 +1,17 @@ +package model + +import "go.mongodb.org/mongo-driver/mongo/options" + +type FindWithCondition struct { + Conditions interface{} `json:"conditions"` + Opts []*options.FindOptions `json:"opts"` +} + +type FindOneCondition struct { + Conditions interface{} `json:"conditions"` +} + +type DistinctWithField struct { + Conditions interface{} `json:"conditions"` + Filed string `json:"filed"` +} diff --git a/model/location_response.go b/model/location_response.go index 82866ec..436d1f7 100644 --- a/model/location_response.go +++ b/model/location_response.go @@ -10,16 +10,19 @@ type ResponseLocationAddress struct { type LocationProvince struct { ID string `json:"id"` Name string `json:"name"` + Code int `json:"code"` } // LocationDistrict ... type LocationDistrict struct { ID string `json:"id"` Name string `json:"name"` + Code int `json:"code"` } // LocationWard ... type LocationWard struct { ID string `json:"id"` Name string `json:"name"` + Code int `json:"code"` } diff --git a/model/supplier_request.go b/model/supplier_request.go index 2fede05..8372569 100644 --- a/model/supplier_request.go +++ b/model/supplier_request.go @@ -6,5 +6,9 @@ import ( // GetSupplierRequest ... type GetSupplierRequest struct { - ID primitive.ObjectID `json:"_id"` + ListID []primitive.ObjectID `json:"listID"` +} + +type GetSupplierContractRequest struct { + SupplierID primitive.ObjectID `json:"supplierID"` } diff --git a/model/supplier_response.go b/model/supplier_response.go index de2f579..da33a9f 100644 --- a/model/supplier_response.go +++ b/model/supplier_response.go @@ -5,3 +5,11 @@ type ResponseSupplierInfo struct { ID string `json:"id"` Name string `json:"name"` } + +// ResponseSupplierContract ... +type ResponseSupplierContract struct { + ID string `json:"id"` + Supplier string `json:"supplier"` + Name string `json:"name"` + Status string `json:"status"` +} diff --git a/model/warehouse_response.go b/model/warehouse_response.go index cb78ae0..e7de165 100644 --- a/model/warehouse_response.go +++ b/model/warehouse_response.go @@ -1,5 +1,7 @@ package model +import "time" + // OutboundRequestResponse ... type OutboundRequestResponse struct { // System code @@ -47,9 +49,9 @@ type WarehousePaymentMethod struct { // WarehouseDelivery ... type WarehouseDelivery struct { DeliveryMethods []string `json:"deliveryMethods"` - PriorityServiceCodes []string `json:"priorityDeliveryServiceCodes"` - EnabledSources []int `json:"enabledDeliverySources"` - Types []string `json:"type"` + PriorityServiceCodes []string `json:"priorityServiceCodes"` + EnabledSources []int `json:"enabledSources"` + Types []string `json:"types"` } // WarehousePartner ... @@ -67,3 +69,47 @@ type SyncORStatusResponse struct { Status string `json:"status"` DeliveryStatus string `json:"deliveryStatus"` } + +// ResponseWarehouseContact ... +type ResponseWarehouseContact struct { + Name string `json:"name"` + Phone string `json:"phone"` + Address string `json:"address"` + Email string `json:"email"` +} + +// ResponseWarehouseLocation ... +type ResponseWarehouseLocation struct { + Province CommonLocation `json:"province"` + District CommonLocation `json:"district"` + Ward CommonLocation `json:"ward"` + Address string `json:"address"` + LocationCoordinates ResponseLatLng `json:"locationCoordinates"` +} + +type CommonLocation struct { + ID string `json:"id"` + Name string `json:"name"` + Code int `json:"code"` +} + +// ResponseLatLng ... +type ResponseLatLng struct { + Latitude float64 `json:"latitude"` + Longitude float64 `json:"longitude"` +} + +// WarehouseNatsResponse ... +type WarehouseNatsResponse struct { + ID string `json:"_id"` + Name string `json:"name"` + SearchString string `json:"searchString"` + Slug string `json:"slug"` + Status string `json:"status"` + Supplier string `json:"supplier"` + Contact ResponseWarehouseContact `json:"contact"` + Location ResponseWarehouseLocation `json:"location"` + Configurations WarehouseConfiguration `json:"configurations"` + CreatedAt time.Time `json:"createdAt"` + UpdatedAt time.Time `json:"updatedAt"` +} diff --git a/server_reqres.go b/server_reqres.go index 841f9ef..8979ab0 100644 --- a/server_reqres.go +++ b/server_reqres.go @@ -18,7 +18,11 @@ func (sv Server) Request(subject string, payload []byte) (*nats.Msg, error) { if sv.Config.RequestTimeout > 0 { timeout = sv.Config.RequestTimeout } - return sv.instance.Request(subject, payload, timeout) + msg, err := sv.instance.Request(subject, payload, timeout) + if errors.Is(err, nats.ErrNoResponders) { + log.Printf("[NATS SERVER]: request - no responders for subject: %s", subject) + } + return msg, err } // Reply ... diff --git a/subject/supplier.go b/subject/supplier.go index 4b4cf52..b2505cf 100644 --- a/subject/supplier.go +++ b/subject/supplier.go @@ -7,7 +7,9 @@ func getSupplierValue(val string) string { } var Supplier = struct { - GetSupplierInfo string + GetListSupplierInfo string + GetSupplierContractBySupplierID string }{ - GetSupplierInfo: getSupplierValue("get_supplier_info"), + GetListSupplierInfo: getSupplierValue("get_list_supplier_info"), + GetSupplierContractBySupplierID: getSupplierValue("get_supplier_contract_by_supplier_id"), } diff --git a/subject/warehouse.go b/subject/warehouse.go index b6e5878..1c8951a 100644 --- a/subject/warehouse.go +++ b/subject/warehouse.go @@ -14,6 +14,10 @@ var Warehouse = struct { SyncORStatus string WebhookTNC string WebhookGlobalCare string + FindOne string + FindByCondition string + Distinct string + Count string }{ CreateOutboundRequest: getWarehouseValue("create_outbound_request"), UpdateOutboundRequestLogistic: getWarehouseValue("update_outbound_request_logistic_info"), @@ -22,4 +26,8 @@ var Warehouse = struct { SyncORStatus: getWarehouseValue("sync_or_status"), WebhookTNC: getWarehouseValue("webhook_tnc"), WebhookGlobalCare: getWarehouseValue("webhook_global_care"), + FindOne: getWarehouseValue("find_one"), + FindByCondition: getWarehouseValue("find_all_by_condition"), + Distinct: getWarehouseValue("distinct"), + Count: getWarehouseValue("count"), }