From 178652fcefec5cd18108acbb3f2380706807b78e Mon Sep 17 00:00:00 2001 From: Sinh Date: Tue, 18 Jun 2024 16:02:52 +0700 Subject: [PATCH] feat: refactor logistics functions in Viettel FFM module - Add a new type `CancelORPayload` to the file `model.go` - Add a new constant `pathCancelOR` in the `viettel_ffm.go` file - Change the method from `POST` to `PUT` in the `UpdateORLogisticInfo` function in `viettel_ffm.go` - Add a new function `CancelOR` in the `viettel_ffm.go` file Signed-off-by: Sinh --- partnerapi/viettelffm/model.go | 4 ++++ partnerapi/viettelffm/viettel_ffm.go | 35 ++++++++++++++++++++++++++-- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/partnerapi/viettelffm/model.go b/partnerapi/viettelffm/model.go index b247f52..3b9496a 100644 --- a/partnerapi/viettelffm/model.go +++ b/partnerapi/viettelffm/model.go @@ -16,6 +16,10 @@ type UpdateLogisticInfoPayload struct { TrackingCode string `json:"tracking_code"` } +type CancelORPayload struct { + OrID int `json:"or_id"` +} + type ORPayload struct { OrProductLines []ORProductLine `json:"or_product_lines"` AmountPaid float64 `json:"amount_paid"` diff --git a/partnerapi/viettelffm/viettel_ffm.go b/partnerapi/viettelffm/viettel_ffm.go index daefd13..e83ce32 100644 --- a/partnerapi/viettelffm/viettel_ffm.go +++ b/partnerapi/viettelffm/viettel_ffm.go @@ -27,6 +27,7 @@ const ( pathAuth = "/realms/wms/protocol/openid-connect/token" pathUpdateORLogisticInfo = "/wms-core/api/v1/obms/outbound-request/outbound-request-partner/%s/bill" pathCreateOR = "/wms-core/api/v1/obms/outbound-request/outbound-request-partner" + pathCancelOR = "/wms-core/api/v1/obms/outbound-request/cancel" logTarget = "viettel-ffm" ) @@ -96,12 +97,42 @@ func (c *Client) CreateOR(p ORPayload) (*ORResult, error) { return &data, nil } -func (c *Client) UpdateOutboundRequestLogisticInfo(p UpdateLogisticInfoPayload) error { +func (c *Client) UpdateORLogisticInfo(p UpdateLogisticInfoPayload) error { apiURL := c.getBaseURL() + pathUpdateORLogisticInfo token, err := c.getToken() if err != nil { return err } + natsPayload := model.CommunicationRequestHttp{ + ResponseImmediately: true, + Payload: model.HttpRequest{ + URL: apiURL, + Method: http.MethodPut, + Data: pjson.ToJSONString(p), + Header: map[string]string{ + httputil.HeaderKeyAuthorization: fmt.Sprintf("Bearer %s", token), + httputil.HeaderKeyContentType: httputil.HeaderValueApplicationJSON, + }, + }, + LogTarget: logTarget, + } + r, err := c.requestHttpViaNats(natsPayload) + if err != nil { + return err + } + res := r.Response + if res.StatusCode >= http.StatusBadRequest { + return fmt.Errorf("viettelffm.Client.UpdateOutboundRequestLogisticInfo - requestHttpViaNats %s", res.Body) + } + return nil +} + +func (c *Client) CancelOR(p CancelORPayload) error { + apiURL := c.getBaseURL() + pathCancelOR + token, err := c.getToken() + if err != nil { + return err + } natsPayload := model.CommunicationRequestHttp{ ResponseImmediately: true, Payload: model.HttpRequest{ @@ -121,7 +152,7 @@ func (c *Client) UpdateOutboundRequestLogisticInfo(p UpdateLogisticInfoPayload) } res := r.Response if res.StatusCode >= http.StatusBadRequest { - return fmt.Errorf("viettelffm.Client.UpdateOutboundRequestLogisticInfo - requestHttpViaNats %s", res.Body) + return fmt.Errorf("viettelffm.Client.CancelOR - requestHttpViaNats %s", res.Body) } return nil }