From aeec88129921191e9060e24da116f369d61fb832 Mon Sep 17 00:00:00 2001 From: Sinh Date: Thu, 3 Nov 2022 16:02:43 +0700 Subject: [PATCH] define nats func - supplier balance --- client/supplier.go | 53 ++++++++++++++++++++++++++++++++++++++ model/supplier_request.go | 12 +++++++++ model/supplier_response.go | 19 +++++++++++++- subject/supplier.go | 6 +++++ 4 files changed, 89 insertions(+), 1 deletion(-) diff --git a/client/supplier.go b/client/supplier.go index f742f94..f861048 100644 --- a/client/supplier.go +++ b/client/supplier.go @@ -155,3 +155,56 @@ func (s Supplier) CreateCashflow(p model.SupplierCashflowCreatePayload) (*model. } return r.Data, nil } + +func (s Supplier) UpdateBalance(p model.SupplierUpdateBalanceReq) (*model.SupplierUpdateBalanceRes, error) { + msg, err := natsio.GetServer().Request(subject.Supplier.UpdateBalance, toBytes(p)) + if err != nil { + return nil, err + } + var r struct { + Error string `json:"error"` + Data *model.SupplierUpdateBalanceRes `json:"data"` + } + 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) GetCurrentBalance(p model.SupplierGetCurrentBalanceReq) (*model.SupplierGetCurrentBalanceRes, error) { + msg, err := natsio.GetServer().Request(subject.Supplier.GetCurrentBalance, toBytes(p)) + if err != nil { + return nil, err + } + var r struct { + Error string `json:"error"` + Data *model.SupplierGetCurrentBalanceRes `json:"data"` + } + 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) DeleteCashflow(p model.SupplierDeleteCashflowReq) error { + msg, err := natsio.GetServer().Request(subject.Supplier.DeleteCashflow, toBytes(p)) + if err != nil { + return err + } + var r struct { + 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 +} diff --git a/model/supplier_request.go b/model/supplier_request.go index 0a4b6fa..745eafa 100644 --- a/model/supplier_request.go +++ b/model/supplier_request.go @@ -48,3 +48,15 @@ type SupplierCashflowCreatePayload struct { Value float64 `json:"value"` ClickAction *ClickAction `json:"clickAction"` } + +type SupplierUpdateBalanceReq struct { + SupplierID string `json:"supplierId"` +} + +type SupplierGetCurrentBalanceReq struct { + SupplierID string `json:"supplierId"` +} + +type SupplierDeleteCashflowReq struct { + CashflowID string `json:"cashflowId"` +} diff --git a/model/supplier_response.go b/model/supplier_response.go index ad0c0dd..83fec77 100644 --- a/model/supplier_response.go +++ b/model/supplier_response.go @@ -38,4 +38,21 @@ type SupplierShort struct { ID string `json:"_id"` Name string `json:"name"` Logo interface{} `json:"logo"` -} \ No newline at end of file +} + +type SupplierUpdateBalanceRes struct { + CurrentCash float64 `json:"currentCash"` + TotalPendingCash float64 `json:"totalPendingCash"` + OrderPendingCash float64 `json:"orderPendingCash"` + OrderWaitingForReconcileCash float64 `json:"orderWaitingForReconcileCash"` + OrderReconciledCash float64 `json:"orderReconciledCash"` + WithdrawPendingCash float64 `json:"withdrawPendingCash"` + WithdrawSuccessCash float64 `json:"withdrawSuccessCash"` + WithdrawRejectCash float64 `json:"withdrawRejectCash"` + ChangeBalanceRequestApproved float64 `json:"changeBalanceRequestApproved"` + UpdatedAt string `json:"updatedAt"` +} + +type SupplierGetCurrentBalanceRes struct { + CurrentCash float64 `json:"currentCash"` +} diff --git a/subject/supplier.go b/subject/supplier.go index 1b86c12..52ef076 100644 --- a/subject/supplier.go +++ b/subject/supplier.go @@ -11,9 +11,15 @@ var Supplier = struct { GetSupplierContractBySupplierID string FindAll string CreateCashflow string + DeleteCashflow string + UpdateBalance string + GetCurrentBalance string }{ GetListSupplierInfo: getSupplierValue("get_list_supplier_info"), GetSupplierContractBySupplierID: getSupplierValue("get_supplier_contract_by_supplier_id"), FindAll: getSupplierValue("find_all"), CreateCashflow: getSupplierValue("create_cashflow"), + DeleteCashflow: getSupplierValue("delete_cashflow"), + UpdateBalance: getSupplierValue("update_balance"), + GetCurrentBalance: getSupplierValue("get_current_balance"), }