From 8e41f704bf1b065a1bdac8a0c58933d942f26a12 Mon Sep 17 00:00:00 2001 From: sp Date: Fri, 26 Mar 2021 22:25:36 +1100 Subject: [PATCH] reward overview worked. --- apiV1BrokerList.go | 6 +++++- apiV1LoanSingle.go | 9 +++++++++ apiV1PeopleList.go | 48 +++++++++++++++++++++++++++++++++++++++++++++ apiv1.go | 6 ++++++ apiv1ChartReward.go | 12 ++++++++++++ 5 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 apiv1ChartReward.go diff --git a/apiV1BrokerList.go b/apiV1BrokerList.go index 2da11f6..4e5f323 100644 --- a/apiV1BrokerList.go +++ b/apiV1BrokerList.go @@ -62,7 +62,11 @@ func apiV1BrokerPost(w http.ResponseWriter, r *http.Request, ss *loan.Session) { b.First = input.First b.Last = input.Last b.License = input.License - b.Organization = input.Organization + + if ss.GetRole() == "admin" { + b.Login = input.Login + b.Organization = input.Organization + } e = b.Write() if e != nil { diff --git a/apiV1LoanSingle.go b/apiV1LoanSingle.go index 53e39d8..c88ecb3 100644 --- a/apiV1LoanSingle.go +++ b/apiV1LoanSingle.go @@ -74,3 +74,12 @@ func apiV1LoanSingleDelete(w http.ResponseWriter, r *http.Request, ss *loan.Sess apiV1SendJson(loanId, w, r, ss) } + +func apiV1LoanByClient(w http.ResponseWriter, r *http.Request, ss *loan.Session) { + clientId := r.URL.Path[len(apiV1Prefix+"loan-by-client/"):] //remove prefix + + data := loan.GetLoansByClient(clientId) + + apiV1SendJson(data, w, r, ss) + +} diff --git a/apiV1PeopleList.go b/apiV1PeopleList.go index 9158d14..c62b515 100644 --- a/apiV1PeopleList.go +++ b/apiV1PeopleList.go @@ -2,6 +2,7 @@ package main import ( "biukop.com/sfm/loan" + "encoding/json" log "github.com/sirupsen/logrus" "net/http" ) @@ -27,3 +28,50 @@ func apiV1PeopleGet(w http.ResponseWriter, r *http.Request, ss *loan.Session) { } apiV1SendJson(p, w, r, ss) } + +func decodeJsonPeopleEdit(r *http.Request) (ret loan.People, e error) { + decoder := json.NewDecoder(r.Body) + //decoder.DisallowUnknownFields() + e = decoder.Decode(&ret) + if e != nil { + log.Error("failed decoding json for Filtering full_loan_summary ", e.Error()) + return + } + return +} + +func apiV1PeoplePost(w http.ResponseWriter, r *http.Request, ss *loan.Session) { + id := r.URL.Path[len(apiV1Prefix+"people/"):] + p := loan.People{} + e := p.Read(id) + if e != nil { + log.Error("cannot find people by id", id, e.Error()) + apiV1Client404Error(w, r, ss) + return + } + + input, e := decodeJsonPeopleEdit(r) + if e != nil { + log.Error("invalid input for update people", id, e.Error()) + apiV1Client404Error(w, r, ss) + return + } + + p.First = input.First + p.Last = input.Last + p.Display = input.Display + + if ss.GetRole() == "admin" { + p.Title = input.Title + p.Nick = input.Nick + p.Title = input.Title + } + + e = p.Write() + if e != nil { + log.Error("fail to update people", p, e.Error()) + apiV1Server500Error(w, r) + return + } + apiV1SendJson(p, w, r, ss) +} diff --git a/apiv1.go b/apiv1.go index 7913f04..9596030 100644 --- a/apiv1.go +++ b/apiv1.go @@ -36,9 +36,12 @@ func setupApiV1Handler() []apiV1HandlerMap { {"GET", "chart/recent-10-loans", apiV1ChartRecent10Loans}, {"GET", "chart/top-broker", apiV1ChartTopBroker}, {"POST", "grid/loan/full-loan-overview", apiV1GridLoanFullOverview}, + {"GET", "chart/reward-vs-income-monthly", apiV1ChartRewardVsIncomeMonthly}, {"GET", "loan/", apiV1LoanSingleGet}, {"DELETE", "loan/", apiV1LoanSingleDelete}, + {"GET", "loan-by-client/", apiV1LoanByClient}, {"GET", "people/", apiV1PeopleGet}, + {"POST", "people/", apiV1PeoplePost}, {"GET", "broker/", apiV1BrokerGet}, {"POST", "broker/", apiV1BrokerPost}, {"POST", "change-pass/", apiV1ChangePass}, @@ -65,9 +68,12 @@ func setupApiV1Handler() []apiV1HandlerMap { {"GET", "chart/recent-10-loans", apiV1ChartRecent10Loans}, {"GET", "chart/top-broker", apiV1ChartTopBroker}, {"POST", "grid/loan/full-loan-overview", apiV1GridLoanFullOverview}, + {"GET", "chart/reward-vs-income-monthly", apiV1ChartRewardVsIncomeMonthly}, {"GET", "loan/", apiV1LoanSingleGet}, {"DELETE", "loan/", apiV1LoanSingleDelete}, + {"GET", "loan-by-client/", apiV1LoanByClient}, {"GET", "people/", apiV1PeopleGet}, + {"POST", "people/", apiV1PeoplePost}, {"GET", "broker/", apiV1BrokerGet}, {"POST", "broker/", apiV1BrokerPost}, {"POST", "change-pass/", apiV1ChangePass}, diff --git a/apiv1ChartReward.go b/apiv1ChartReward.go new file mode 100644 index 0000000..be3878a --- /dev/null +++ b/apiv1ChartReward.go @@ -0,0 +1,12 @@ +package main + +import ( + "biukop.com/sfm/loan" + "net/http" +) + +func apiV1ChartRewardVsIncomeMonthly(w http.ResponseWriter, r *http.Request, ss *loan.Session) { + data := loan.RewardVsIncomeMonthly() + //send out + apiV1SendJson(data, w, r, ss) +}