satellite/admin: Use helper for sending JSON
A previous commit added a helper function for sending JSON data back to the client. This commit makes use of it for homogenizing the current implementation. It also renames the existing helper message to send JSON errors to starts with "send" because the new helper starts with it and they helpers are clearer with their name starting with it. Change-Id: I53ee0b4ca33d677a8ccd366c9ba6d73f4f472247
This commit is contained in:
parent
bb575ef739
commit
fed09316b8
@ -21,21 +21,21 @@ func (server *Server) addAPIKey(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
projectUUIDString, ok := vars["project"]
|
||||
if !ok {
|
||||
httpJSONError(w, "project-uuid missing",
|
||||
sendJSONError(w, "project-uuid missing",
|
||||
"", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
projectUUID, err := uuid.FromString(projectUUIDString)
|
||||
if err != nil {
|
||||
httpJSONError(w, "invalid project-uuid",
|
||||
sendJSONError(w, "invalid project-uuid",
|
||||
err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
body, err := ioutil.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
httpJSONError(w, "failed to read body",
|
||||
sendJSONError(w, "failed to read body",
|
||||
err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
@ -51,34 +51,34 @@ func (server *Server) addAPIKey(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
err = json.Unmarshal(body, &input)
|
||||
if err != nil {
|
||||
httpJSONError(w, "failed to unmarshal request",
|
||||
sendJSONError(w, "failed to unmarshal request",
|
||||
err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if input.Name == "" {
|
||||
httpJSONError(w, "Name is not set",
|
||||
sendJSONError(w, "Name is not set",
|
||||
"", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
_, err = server.db.Console().APIKeys().GetByNameAndProjectID(ctx, input.Name, projectUUID)
|
||||
if err == nil {
|
||||
httpJSONError(w, "api-key with given name already exists",
|
||||
sendJSONError(w, "api-key with given name already exists",
|
||||
"", http.StatusConflict)
|
||||
return
|
||||
}
|
||||
|
||||
secret, err := macaroon.NewSecret()
|
||||
if err != nil {
|
||||
httpJSONError(w, "could not create macaroon secret",
|
||||
sendJSONError(w, "could not create macaroon secret",
|
||||
err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
key, err := macaroon.NewAPIKey(secret)
|
||||
if err != nil {
|
||||
httpJSONError(w, "could not create api-key",
|
||||
sendJSONError(w, "could not create api-key",
|
||||
err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
@ -92,7 +92,7 @@ func (server *Server) addAPIKey(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
_, err = server.db.Console().APIKeys().Create(ctx, key.Head(), apikey)
|
||||
if err != nil {
|
||||
httpJSONError(w, "unable to add api-key to database",
|
||||
sendJSONError(w, "unable to add api-key to database",
|
||||
err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
@ -100,13 +100,12 @@ func (server *Server) addAPIKey(w http.ResponseWriter, r *http.Request) {
|
||||
output.APIKey = key.Serialize()
|
||||
data, err := json.Marshal(output)
|
||||
if err != nil {
|
||||
httpJSONError(w, "json encoding failed",
|
||||
sendJSONError(w, "json encoding failed",
|
||||
err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write(data) // nothing to do with the error response, probably the client requesting disappeared
|
||||
sendJSONData(w, http.StatusOK, data)
|
||||
}
|
||||
|
||||
func (server *Server) deleteAPIKey(w http.ResponseWriter, r *http.Request) {
|
||||
@ -115,28 +114,28 @@ func (server *Server) deleteAPIKey(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
apikeyString, ok := vars["apikey"]
|
||||
if !ok {
|
||||
httpJSONError(w, "apikey missing",
|
||||
sendJSONError(w, "apikey missing",
|
||||
"", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
apikey, err := macaroon.ParseAPIKey(apikeyString)
|
||||
if err != nil {
|
||||
httpJSONError(w, "invalid apikey format",
|
||||
sendJSONError(w, "invalid apikey format",
|
||||
err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
info, err := server.db.Console().APIKeys().GetByHead(ctx, apikey.Head())
|
||||
if err != nil {
|
||||
httpJSONError(w, "could not get apikey id",
|
||||
sendJSONError(w, "could not get apikey id",
|
||||
err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
err = server.db.Console().APIKeys().Delete(ctx, info.ID)
|
||||
if err != nil {
|
||||
httpJSONError(w, "unable to delete apikey",
|
||||
sendJSONError(w, "unable to delete apikey",
|
||||
err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
@ -148,35 +147,35 @@ func (server *Server) deleteAPIKeyByName(w http.ResponseWriter, r *http.Request)
|
||||
vars := mux.Vars(r)
|
||||
projectUUIDString, ok := vars["project"]
|
||||
if !ok {
|
||||
httpJSONError(w, "project-uuid missing",
|
||||
sendJSONError(w, "project-uuid missing",
|
||||
"", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
projectUUID, err := uuid.FromString(projectUUIDString)
|
||||
if err != nil {
|
||||
httpJSONError(w, "invalid project-uuid",
|
||||
sendJSONError(w, "invalid project-uuid",
|
||||
err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
apikeyName, ok := vars["name"]
|
||||
if !ok {
|
||||
httpJSONError(w, "apikey name missing",
|
||||
sendJSONError(w, "apikey name missing",
|
||||
"", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
info, err := server.db.Console().APIKeys().GetByNameAndProjectID(ctx, apikeyName, projectUUID)
|
||||
if err != nil {
|
||||
httpJSONError(w, "could not get apikey id",
|
||||
sendJSONError(w, "could not get apikey id",
|
||||
err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
err = server.db.Console().APIKeys().Delete(ctx, info.ID)
|
||||
if err != nil {
|
||||
httpJSONError(w, "unable to delete apikey",
|
||||
sendJSONError(w, "unable to delete apikey",
|
||||
err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
@ -188,14 +187,14 @@ func (server *Server) listAPIKeys(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
projectUUIDString, ok := vars["project"]
|
||||
if !ok {
|
||||
httpJSONError(w, "project-uuid missing",
|
||||
sendJSONError(w, "project-uuid missing",
|
||||
"", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
projectUUID, err := uuid.FromString(projectUUIDString)
|
||||
if err != nil {
|
||||
httpJSONError(w, "invalid project-uuid",
|
||||
sendJSONError(w, "invalid project-uuid",
|
||||
err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
@ -212,7 +211,7 @@ func (server *Server) listAPIKeys(w http.ResponseWriter, r *http.Request) {
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
httpJSONError(w, "failed retrieving a cursor page of API Keys list",
|
||||
sendJSONError(w, "failed retrieving a cursor page of API Keys list",
|
||||
err.Error(), http.StatusInternalServerError,
|
||||
)
|
||||
return
|
||||
@ -230,12 +229,11 @@ func (server *Server) listAPIKeys(w http.ResponseWriter, r *http.Request) {
|
||||
} else {
|
||||
data, err = json.Marshal(apiKeys)
|
||||
if err != nil {
|
||||
httpJSONError(w, "json encoding failed",
|
||||
sendJSONError(w, "json encoding failed",
|
||||
err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write(data)
|
||||
sendJSONData(w, http.StatusOK, data)
|
||||
}
|
||||
|
@ -13,7 +13,7 @@ import (
|
||||
// Error is default error class for admin package.
|
||||
var Error = errs.Class("admin")
|
||||
|
||||
func httpJSONError(w http.ResponseWriter, errMsg, detail string, statusCode int) {
|
||||
func sendJSONError(w http.ResponseWriter, errMsg, detail string, statusCode int) {
|
||||
errStr := struct {
|
||||
Error string `json:"error"`
|
||||
Detail string `json:"detail"`
|
||||
|
@ -22,7 +22,7 @@ func (server *Server) addCoupon(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
body, err := ioutil.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
httpJSONError(w, "failed to read body",
|
||||
sendJSONError(w, "failed to read body",
|
||||
err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
@ -36,25 +36,25 @@ func (server *Server) addCoupon(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
err = json.Unmarshal(body, &input)
|
||||
if err != nil {
|
||||
httpJSONError(w, "failed to unmarshal request",
|
||||
sendJSONError(w, "failed to unmarshal request",
|
||||
err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
switch {
|
||||
case input.Duration == 0:
|
||||
httpJSONError(w, "Duration is not set",
|
||||
sendJSONError(w, "Duration is not set",
|
||||
"", http.StatusBadRequest)
|
||||
return
|
||||
case input.Amount == 0:
|
||||
httpJSONError(w, "Amount is not set",
|
||||
sendJSONError(w, "Amount is not set",
|
||||
"", http.StatusBadRequest)
|
||||
return
|
||||
case input.Description == "":
|
||||
httpJSONError(w, "Description is not set",
|
||||
sendJSONError(w, "Description is not set",
|
||||
"", http.StatusBadRequest)
|
||||
return
|
||||
case input.UserID.IsZero():
|
||||
httpJSONError(w, "UserID is not set",
|
||||
sendJSONError(w, "UserID is not set",
|
||||
"", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
@ -66,20 +66,19 @@ func (server *Server) addCoupon(w http.ResponseWriter, r *http.Request) {
|
||||
Description: input.Description,
|
||||
})
|
||||
if err != nil {
|
||||
httpJSONError(w, "failed to insert coupon",
|
||||
sendJSONError(w, "failed to insert coupon",
|
||||
err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
data, err := json.Marshal(coupon.ID)
|
||||
if err != nil {
|
||||
httpJSONError(w, "json encoding failed",
|
||||
sendJSONError(w, "json encoding failed",
|
||||
err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write(data) // nothing to do with the error response, probably the client requesting disappeared
|
||||
sendJSONData(w, http.StatusOK, data)
|
||||
}
|
||||
|
||||
func (server *Server) couponInfo(w http.ResponseWriter, r *http.Request) {
|
||||
@ -88,38 +87,37 @@ func (server *Server) couponInfo(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
id, ok := vars["couponid"]
|
||||
if !ok {
|
||||
httpJSONError(w, "couponId missing",
|
||||
sendJSONError(w, "couponId missing",
|
||||
"", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
couponID, err := uuid.FromString(id)
|
||||
if err != nil {
|
||||
httpJSONError(w, "invalid couponId",
|
||||
sendJSONError(w, "invalid couponId",
|
||||
"", http.StatusBadRequest)
|
||||
}
|
||||
|
||||
coupon, err := server.db.StripeCoinPayments().Coupons().Get(ctx, couponID)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
httpJSONError(w, fmt.Sprintf("coupon with id %q not found", couponID),
|
||||
sendJSONError(w, fmt.Sprintf("coupon with id %q not found", couponID),
|
||||
"", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
httpJSONError(w, "failed to get coupon",
|
||||
sendJSONError(w, "failed to get coupon",
|
||||
err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
data, err := json.Marshal(coupon)
|
||||
if err != nil {
|
||||
httpJSONError(w, "json encoding failed",
|
||||
sendJSONError(w, "json encoding failed",
|
||||
err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write(data) // nothing to do with the error response, probably the client requesting disappeared
|
||||
sendJSONData(w, http.StatusOK, data)
|
||||
}
|
||||
|
||||
func (server *Server) deleteCoupon(w http.ResponseWriter, r *http.Request) {
|
||||
@ -128,21 +126,21 @@ func (server *Server) deleteCoupon(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
UUIDString, ok := vars["couponid"]
|
||||
if !ok {
|
||||
httpJSONError(w, "couponid missing",
|
||||
sendJSONError(w, "couponid missing",
|
||||
"", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
couponID, err := uuid.FromString(UUIDString)
|
||||
if err != nil {
|
||||
httpJSONError(w, "invalid couponid",
|
||||
sendJSONError(w, "invalid couponid",
|
||||
err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
err = server.db.StripeCoinPayments().Coupons().Delete(ctx, couponID)
|
||||
if err != nil {
|
||||
httpJSONError(w, "unable to delete coupon",
|
||||
sendJSONError(w, "unable to delete coupon",
|
||||
err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
@ -30,22 +30,20 @@ func (server *Server) checkProjectUsage(w http.ResponseWriter, r *http.Request)
|
||||
vars := mux.Vars(r)
|
||||
projectUUIDString, ok := vars["project"]
|
||||
if !ok {
|
||||
httpJSONError(w, "project-uuid missing",
|
||||
sendJSONError(w, "project-uuid missing",
|
||||
"", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
projectUUID, err := uuid.FromString(projectUUIDString)
|
||||
if err != nil {
|
||||
httpJSONError(w, "invalid project-uuid",
|
||||
sendJSONError(w, "invalid project-uuid",
|
||||
err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if !server.checkUsage(ctx, w, projectUUID) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(200)
|
||||
_, _ = w.Write([]byte(`{"result":"no project usage exist"}`))
|
||||
sendJSONData(w, http.StatusOK, []byte(`{"result":"no project usage exist"}`))
|
||||
}
|
||||
}
|
||||
|
||||
@ -55,40 +53,39 @@ func (server *Server) getProject(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
projectUUIDString, ok := vars["project"]
|
||||
if !ok {
|
||||
httpJSONError(w, "project-uuid missing",
|
||||
sendJSONError(w, "project-uuid missing",
|
||||
"", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
projectUUID, err := uuid.FromString(projectUUIDString)
|
||||
if err != nil {
|
||||
httpJSONError(w, "invalid project-uuid",
|
||||
sendJSONError(w, "invalid project-uuid",
|
||||
err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if err := r.ParseForm(); err != nil {
|
||||
httpJSONError(w, "invalid form",
|
||||
sendJSONError(w, "invalid form",
|
||||
err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
project, err := server.db.Console().Projects().Get(ctx, projectUUID)
|
||||
if err != nil {
|
||||
httpJSONError(w, "unable to fetch project details",
|
||||
sendJSONError(w, "unable to fetch project details",
|
||||
err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
data, err := json.Marshal(project)
|
||||
if err != nil {
|
||||
httpJSONError(w, "json encoding failed",
|
||||
sendJSONError(w, "json encoding failed",
|
||||
err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write(data) // nothing to do with the error response, probably the client requesting disappeared
|
||||
sendJSONData(w, http.StatusOK, data)
|
||||
}
|
||||
|
||||
func (server *Server) getProjectLimit(w http.ResponseWriter, r *http.Request) {
|
||||
@ -97,21 +94,21 @@ func (server *Server) getProjectLimit(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
projectUUIDString, ok := vars["project"]
|
||||
if !ok {
|
||||
httpJSONError(w, "project-uuid missing",
|
||||
sendJSONError(w, "project-uuid missing",
|
||||
"", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
projectUUID, err := uuid.FromString(projectUUIDString)
|
||||
if err != nil {
|
||||
httpJSONError(w, "invalid project-uuid",
|
||||
sendJSONError(w, "invalid project-uuid",
|
||||
err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
project, err := server.db.Console().Projects().Get(ctx, projectUUID)
|
||||
if err != nil {
|
||||
httpJSONError(w, "failed to get project",
|
||||
sendJSONError(w, "failed to get project",
|
||||
err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
@ -147,13 +144,12 @@ func (server *Server) getProjectLimit(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
data, err := json.Marshal(output)
|
||||
if err != nil {
|
||||
httpJSONError(w, "json encoding failed",
|
||||
sendJSONError(w, "json encoding failed",
|
||||
err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write(data) // nothing to do with the error response, probably the client requesting disappeared
|
||||
sendJSONData(w, http.StatusOK, data)
|
||||
}
|
||||
|
||||
func (server *Server) putProjectLimit(w http.ResponseWriter, r *http.Request) {
|
||||
@ -162,14 +158,14 @@ func (server *Server) putProjectLimit(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
projectUUIDString, ok := vars["project"]
|
||||
if !ok {
|
||||
httpJSONError(w, "project-uuid missing",
|
||||
sendJSONError(w, "project-uuid missing",
|
||||
"", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
projectUUID, err := uuid.FromString(projectUUIDString)
|
||||
if err != nil {
|
||||
httpJSONError(w, "invalid project-uuid",
|
||||
sendJSONError(w, "invalid project-uuid",
|
||||
err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
@ -183,7 +179,7 @@ func (server *Server) putProjectLimit(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
if err := r.ParseForm(); err != nil {
|
||||
httpJSONError(w, "invalid form",
|
||||
sendJSONError(w, "invalid form",
|
||||
err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
@ -191,21 +187,21 @@ func (server *Server) putProjectLimit(w http.ResponseWriter, r *http.Request) {
|
||||
decoder := schema.NewDecoder()
|
||||
err = decoder.Decode(&arguments, r.Form)
|
||||
if err != nil {
|
||||
httpJSONError(w, "invalid arguments",
|
||||
sendJSONError(w, "invalid arguments",
|
||||
err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if arguments.Usage != nil {
|
||||
if *arguments.Usage < 0 {
|
||||
httpJSONError(w, "negative usage",
|
||||
sendJSONError(w, "negative usage",
|
||||
fmt.Sprintf("%v", arguments.Usage), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
err = server.db.ProjectAccounting().UpdateProjectUsageLimit(ctx, projectUUID, *arguments.Usage)
|
||||
if err != nil {
|
||||
httpJSONError(w, "failed to update usage",
|
||||
sendJSONError(w, "failed to update usage",
|
||||
err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
@ -213,14 +209,14 @@ func (server *Server) putProjectLimit(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
if arguments.Bandwidth != nil {
|
||||
if *arguments.Bandwidth < 0 {
|
||||
httpJSONError(w, "negative bandwidth",
|
||||
sendJSONError(w, "negative bandwidth",
|
||||
fmt.Sprintf("%v", arguments.Usage), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
err = server.db.ProjectAccounting().UpdateProjectBandwidthLimit(ctx, projectUUID, *arguments.Bandwidth)
|
||||
if err != nil {
|
||||
httpJSONError(w, "failed to update bandwidth",
|
||||
sendJSONError(w, "failed to update bandwidth",
|
||||
err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
@ -228,14 +224,14 @@ func (server *Server) putProjectLimit(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
if arguments.Rate != nil {
|
||||
if *arguments.Rate < 0 {
|
||||
httpJSONError(w, "negative rate",
|
||||
sendJSONError(w, "negative rate",
|
||||
fmt.Sprintf("%v", arguments.Rate), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
err = server.db.Console().Projects().UpdateRateLimit(ctx, projectUUID, *arguments.Rate)
|
||||
if err != nil {
|
||||
httpJSONError(w, "failed to update rate",
|
||||
sendJSONError(w, "failed to update rate",
|
||||
err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
@ -243,14 +239,14 @@ func (server *Server) putProjectLimit(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
if arguments.Burst != nil {
|
||||
if *arguments.Burst < 0 {
|
||||
httpJSONError(w, "negative burst rate",
|
||||
sendJSONError(w, "negative burst rate",
|
||||
fmt.Sprintf("%v", arguments.Burst), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
err = server.db.Console().Projects().UpdateBurstLimit(ctx, projectUUID, *arguments.Burst)
|
||||
if err != nil {
|
||||
httpJSONError(w, "failed to update burst",
|
||||
sendJSONError(w, "failed to update burst",
|
||||
err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
@ -258,14 +254,14 @@ func (server *Server) putProjectLimit(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
if arguments.Buckets != nil {
|
||||
if *arguments.Buckets < 0 {
|
||||
httpJSONError(w, "negative bucket coun",
|
||||
sendJSONError(w, "negative bucket coun",
|
||||
fmt.Sprintf("t: %v", arguments.Buckets), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
err = server.db.Console().Projects().UpdateBucketLimit(ctx, projectUUID, *arguments.Buckets)
|
||||
if err != nil {
|
||||
httpJSONError(w, "failed to update bucket limit",
|
||||
sendJSONError(w, "failed to update bucket limit",
|
||||
err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
@ -277,7 +273,7 @@ func (server *Server) addProject(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
body, err := ioutil.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
httpJSONError(w, "failed to read body",
|
||||
sendJSONError(w, "failed to read body",
|
||||
err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
@ -293,19 +289,19 @@ func (server *Server) addProject(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
err = json.Unmarshal(body, &input)
|
||||
if err != nil {
|
||||
httpJSONError(w, "failed to unmarshal request",
|
||||
sendJSONError(w, "failed to unmarshal request",
|
||||
err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if input.OwnerID.IsZero() {
|
||||
httpJSONError(w, "OwnerID is not set",
|
||||
sendJSONError(w, "OwnerID is not set",
|
||||
"", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if input.ProjectName == "" {
|
||||
httpJSONError(w, "ProjectName is not set",
|
||||
sendJSONError(w, "ProjectName is not set",
|
||||
"", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
@ -315,14 +311,14 @@ func (server *Server) addProject(w http.ResponseWriter, r *http.Request) {
|
||||
OwnerID: input.OwnerID,
|
||||
})
|
||||
if err != nil {
|
||||
httpJSONError(w, "failed to insert project",
|
||||
sendJSONError(w, "failed to insert project",
|
||||
err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
_, err = server.db.Console().ProjectMembers().Insert(ctx, project.OwnerID, project.ID)
|
||||
if err != nil {
|
||||
httpJSONError(w, "failed to insert project member",
|
||||
sendJSONError(w, "failed to insert project member",
|
||||
err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
@ -330,13 +326,12 @@ func (server *Server) addProject(w http.ResponseWriter, r *http.Request) {
|
||||
output.ProjectID = project.ID
|
||||
data, err := json.Marshal(output)
|
||||
if err != nil {
|
||||
httpJSONError(w, "json encoding failed",
|
||||
sendJSONError(w, "json encoding failed",
|
||||
err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write(data) // nothing to do with the error response, probably the client requesting disappeared
|
||||
sendJSONData(w, http.StatusOK, data)
|
||||
}
|
||||
|
||||
func (server *Server) renameProject(w http.ResponseWriter, r *http.Request) {
|
||||
@ -345,33 +340,33 @@ func (server *Server) renameProject(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
projectUUIDString, ok := vars["project"]
|
||||
if !ok {
|
||||
httpJSONError(w, "project-uuid missing",
|
||||
sendJSONError(w, "project-uuid missing",
|
||||
"", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
projectUUID, err := uuid.FromString(projectUUIDString)
|
||||
if err != nil {
|
||||
httpJSONError(w, "invalid project-uuid",
|
||||
sendJSONError(w, "invalid project-uuid",
|
||||
err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
project, err := server.db.Console().Projects().Get(ctx, projectUUID)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
httpJSONError(w, "project with specified uuid does not exist",
|
||||
sendJSONError(w, "project with specified uuid does not exist",
|
||||
"", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
httpJSONError(w, "error getting project",
|
||||
sendJSONError(w, "error getting project",
|
||||
err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
body, err := ioutil.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
httpJSONError(w, "ailed to read body",
|
||||
sendJSONError(w, "ailed to read body",
|
||||
err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
@ -383,13 +378,13 @@ func (server *Server) renameProject(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
err = json.Unmarshal(body, &input)
|
||||
if err != nil {
|
||||
httpJSONError(w, "failed to unmarshal request",
|
||||
sendJSONError(w, "failed to unmarshal request",
|
||||
err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if input.ProjectName == "" {
|
||||
httpJSONError(w, "ProjectName is not set",
|
||||
sendJSONError(w, "ProjectName is not set",
|
||||
"", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
@ -401,7 +396,7 @@ func (server *Server) renameProject(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
err = server.db.Console().Projects().Update(ctx, project)
|
||||
if err != nil {
|
||||
httpJSONError(w, "error renaming project",
|
||||
sendJSONError(w, "error renaming project",
|
||||
err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
@ -413,20 +408,20 @@ func (server *Server) deleteProject(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
projectUUIDString, ok := vars["project"]
|
||||
if !ok {
|
||||
httpJSONError(w, "project-uuid missing",
|
||||
sendJSONError(w, "project-uuid missing",
|
||||
"", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
projectUUID, err := uuid.FromString(projectUUIDString)
|
||||
if err != nil {
|
||||
httpJSONError(w, "invalid project-uuid",
|
||||
sendJSONError(w, "invalid project-uuid",
|
||||
err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if err := r.ParseForm(); err != nil {
|
||||
httpJSONError(w, "invalid form",
|
||||
sendJSONError(w, "invalid form",
|
||||
err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
@ -434,24 +429,24 @@ func (server *Server) deleteProject(w http.ResponseWriter, r *http.Request) {
|
||||
options := storj.BucketListOptions{Limit: 1, Direction: storj.Forward}
|
||||
buckets, err := server.db.Buckets().ListBuckets(ctx, projectUUID, options, macaroon.AllowedBuckets{All: true})
|
||||
if err != nil {
|
||||
httpJSONError(w, "unable to list buckets",
|
||||
sendJSONError(w, "unable to list buckets",
|
||||
err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if len(buckets.Items) > 0 {
|
||||
httpJSONError(w, "buckets still exist",
|
||||
sendJSONError(w, "buckets still exist",
|
||||
fmt.Sprintf("%v", bucketNames(buckets.Items)), http.StatusConflict)
|
||||
return
|
||||
}
|
||||
|
||||
keys, err := server.db.Console().APIKeys().GetPagedByProjectID(ctx, projectUUID, console.APIKeyCursor{Limit: 1, Page: 1})
|
||||
if err != nil {
|
||||
httpJSONError(w, "unable to list api-keys",
|
||||
sendJSONError(w, "unable to list api-keys",
|
||||
err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if keys.TotalCount > 0 {
|
||||
httpJSONError(w, "api-keys still exist",
|
||||
sendJSONError(w, "api-keys still exist",
|
||||
fmt.Sprintf("count %d", keys.TotalCount), http.StatusConflict)
|
||||
return
|
||||
}
|
||||
@ -463,7 +458,7 @@ func (server *Server) deleteProject(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
err = server.db.Console().Projects().Delete(ctx, projectUUID)
|
||||
if err != nil {
|
||||
httpJSONError(w, "unable to delete project",
|
||||
sendJSONError(w, "unable to delete project",
|
||||
err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
@ -476,18 +471,18 @@ func (server *Server) checkUsage(ctx context.Context, w http.ResponseWriter, pro
|
||||
|
||||
currentUsage, err := server.db.ProjectAccounting().GetProjectTotal(ctx, projectID, firstOfMonth, server.nowFn())
|
||||
if err != nil {
|
||||
httpJSONError(w, "unable to list project usage", err.Error(), http.StatusInternalServerError)
|
||||
sendJSONError(w, "unable to list project usage", err.Error(), http.StatusInternalServerError)
|
||||
return true
|
||||
}
|
||||
if currentUsage.Storage > 0 || currentUsage.Egress > 0 || currentUsage.ObjectCount > 0 {
|
||||
httpJSONError(w, "usage for current month exists", "", http.StatusConflict)
|
||||
sendJSONError(w, "usage for current month exists", "", http.StatusConflict)
|
||||
return true
|
||||
}
|
||||
|
||||
// if usage of last month exist, make sure to look for billing records
|
||||
lastMonthUsage, err := server.db.ProjectAccounting().GetProjectTotal(ctx, projectID, firstOfMonth.AddDate(0, -1, 0), firstOfMonth.AddDate(0, 0, -1))
|
||||
if err != nil {
|
||||
httpJSONError(w, "error getting project totals",
|
||||
sendJSONError(w, "error getting project totals",
|
||||
"", http.StatusInternalServerError)
|
||||
return true
|
||||
}
|
||||
@ -500,22 +495,22 @@ func (server *Server) checkUsage(ctx context.Context, w http.ResponseWriter, pro
|
||||
if errors.Is(err, stripecoinpayments.ErrProjectRecordExists) {
|
||||
record, err := server.db.StripeCoinPayments().ProjectRecords().Get(ctx, projectID, firstOfMonth.AddDate(0, -1, 0), firstOfMonth)
|
||||
if err != nil {
|
||||
httpJSONError(w, "unable to get project records", err.Error(), http.StatusInternalServerError)
|
||||
sendJSONError(w, "unable to get project records", err.Error(), http.StatusInternalServerError)
|
||||
return true
|
||||
}
|
||||
// state = 0 means unapplied and not invoiced yet.
|
||||
if record.State == 0 {
|
||||
httpJSONError(w, "unapplied project invoice record exist", "", http.StatusConflict)
|
||||
sendJSONError(w, "unapplied project invoice record exist", "", http.StatusConflict)
|
||||
return true
|
||||
}
|
||||
// Record has been applied, so project can be deleted.
|
||||
return false
|
||||
}
|
||||
if err != nil {
|
||||
httpJSONError(w, "unable to get project records", err.Error(), http.StatusInternalServerError)
|
||||
sendJSONError(w, "unable to get project records", err.Error(), http.StatusInternalServerError)
|
||||
return true
|
||||
}
|
||||
httpJSONError(w, "usage for last month exist, but is not billed yet", "", http.StatusConflict)
|
||||
sendJSONError(w, "usage for last month exist, but is not billed yet", "", http.StatusConflict)
|
||||
return true
|
||||
}
|
||||
|
||||
|
@ -107,7 +107,7 @@ type protectedServer struct {
|
||||
|
||||
func (server *protectedServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
if server.allowedAuthorization == "" {
|
||||
httpJSONError(w, "Authorization not enabled.",
|
||||
sendJSONError(w, "Authorization not enabled.",
|
||||
"", http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
@ -117,7 +117,7 @@ func (server *protectedServer) ServeHTTP(w http.ResponseWriter, r *http.Request)
|
||||
[]byte(server.allowedAuthorization),
|
||||
)
|
||||
if equality != 1 {
|
||||
httpJSONError(w, "Forbidden",
|
||||
sendJSONError(w, "Forbidden",
|
||||
"", http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
|
@ -24,7 +24,7 @@ func (server *Server) addUser(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
body, err := ioutil.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
httpJSONError(w, "failed to read body",
|
||||
sendJSONError(w, "failed to read body",
|
||||
err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
@ -33,44 +33,44 @@ func (server *Server) addUser(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
err = json.Unmarshal(body, &input)
|
||||
if err != nil {
|
||||
httpJSONError(w, "failed to unmarshal request",
|
||||
sendJSONError(w, "failed to unmarshal request",
|
||||
err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
switch {
|
||||
case input.Email == "":
|
||||
httpJSONError(w, "email is not set",
|
||||
sendJSONError(w, "email is not set",
|
||||
"", http.StatusBadRequest)
|
||||
return
|
||||
case input.Password == "":
|
||||
httpJSONError(w, "password is not set",
|
||||
sendJSONError(w, "password is not set",
|
||||
"", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
existingUser, err := server.db.Console().Users().GetByEmail(ctx, input.Email)
|
||||
if err != nil && !errors.Is(sql.ErrNoRows, err) {
|
||||
httpJSONError(w, "failed to check for user email",
|
||||
sendJSONError(w, "failed to check for user email",
|
||||
err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if existingUser != nil {
|
||||
httpJSONError(w, fmt.Sprintf("user with email already exists %s", input.Email),
|
||||
sendJSONError(w, fmt.Sprintf("user with email already exists %s", input.Email),
|
||||
"", http.StatusConflict)
|
||||
return
|
||||
}
|
||||
|
||||
hash, err := bcrypt.GenerateFromPassword([]byte(input.Password), 0)
|
||||
if err != nil {
|
||||
httpJSONError(w, "unable to save password hash",
|
||||
sendJSONError(w, "unable to save password hash",
|
||||
"", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
userID, err := uuid.New()
|
||||
if err != nil {
|
||||
httpJSONError(w, "unable to create UUID",
|
||||
sendJSONError(w, "unable to create UUID",
|
||||
"", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
@ -83,7 +83,7 @@ func (server *Server) addUser(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
err = user.IsValid()
|
||||
if err != nil {
|
||||
httpJSONError(w, "user data is not valid",
|
||||
sendJSONError(w, "user data is not valid",
|
||||
err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
@ -96,14 +96,14 @@ func (server *Server) addUser(w http.ResponseWriter, r *http.Request) {
|
||||
PasswordHash: hash,
|
||||
})
|
||||
if err != nil {
|
||||
httpJSONError(w, "failed to insert user",
|
||||
sendJSONError(w, "failed to insert user",
|
||||
err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
err = server.payments.Setup(ctx, newuser.ID, newuser.Email)
|
||||
if err != nil {
|
||||
httpJSONError(w, "failed to create payment account for user",
|
||||
sendJSONError(w, "failed to create payment account for user",
|
||||
err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
@ -113,20 +113,19 @@ func (server *Server) addUser(w http.ResponseWriter, r *http.Request) {
|
||||
newuser.PasswordHash = nil
|
||||
err = server.db.Console().Users().Update(ctx, newuser)
|
||||
if err != nil {
|
||||
httpJSONError(w, "failed to activate user",
|
||||
sendJSONError(w, "failed to activate user",
|
||||
err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
data, err := json.Marshal(newuser)
|
||||
if err != nil {
|
||||
httpJSONError(w, "json encoding failed",
|
||||
sendJSONError(w, "json encoding failed",
|
||||
err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write(data) // nothing to do with the error response, probably the client requesting disappeared
|
||||
sendJSONData(w, http.StatusOK, data)
|
||||
}
|
||||
|
||||
func (server *Server) userInfo(w http.ResponseWriter, r *http.Request) {
|
||||
@ -135,19 +134,19 @@ func (server *Server) userInfo(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
userEmail, ok := vars["useremail"]
|
||||
if !ok {
|
||||
httpJSONError(w, "user-email missing",
|
||||
sendJSONError(w, "user-email missing",
|
||||
"", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
user, err := server.db.Console().Users().GetByEmail(ctx, userEmail)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
httpJSONError(w, fmt.Sprintf("user with email %q not found", userEmail),
|
||||
sendJSONError(w, fmt.Sprintf("user with email %q not found", userEmail),
|
||||
"", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
httpJSONError(w, "failed to get user",
|
||||
sendJSONError(w, "failed to get user",
|
||||
err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
@ -155,14 +154,14 @@ func (server *Server) userInfo(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
projects, err := server.db.Console().Projects().GetByUserID(ctx, user.ID)
|
||||
if err != nil {
|
||||
httpJSONError(w, "failed to get user projects",
|
||||
sendJSONError(w, "failed to get user projects",
|
||||
err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
coupons, err := server.db.StripeCoinPayments().Coupons().ListByUserID(ctx, user.ID)
|
||||
if err != nil {
|
||||
httpJSONError(w, "failed to get user coupons",
|
||||
sendJSONError(w, "failed to get user coupons",
|
||||
err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
@ -204,13 +203,12 @@ func (server *Server) userInfo(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
data, err := json.Marshal(output)
|
||||
if err != nil {
|
||||
httpJSONError(w, "json encoding failed",
|
||||
sendJSONError(w, "json encoding failed",
|
||||
err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write(data) // nothing to do with the error response, probably the client requesting disappeared
|
||||
sendJSONData(w, http.StatusOK, data)
|
||||
}
|
||||
|
||||
func (server *Server) updateUser(w http.ResponseWriter, r *http.Request) {
|
||||
@ -219,26 +217,26 @@ func (server *Server) updateUser(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
userEmail, ok := vars["useremail"]
|
||||
if !ok {
|
||||
httpJSONError(w, "user-email missing",
|
||||
sendJSONError(w, "user-email missing",
|
||||
"", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
user, err := server.db.Console().Users().GetByEmail(ctx, userEmail)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
httpJSONError(w, fmt.Sprintf("user with email %q not found", userEmail),
|
||||
sendJSONError(w, fmt.Sprintf("user with email %q not found", userEmail),
|
||||
"", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
httpJSONError(w, "failed to get user",
|
||||
sendJSONError(w, "failed to get user",
|
||||
err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
body, err := ioutil.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
httpJSONError(w, "failed to read body",
|
||||
sendJSONError(w, "failed to read body",
|
||||
err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
@ -247,7 +245,7 @@ func (server *Server) updateUser(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
err = json.Unmarshal(body, &input)
|
||||
if err != nil {
|
||||
httpJSONError(w, "failed to unmarshal request",
|
||||
sendJSONError(w, "failed to unmarshal request",
|
||||
err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
@ -273,7 +271,7 @@ func (server *Server) updateUser(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
err = server.db.Console().Users().Update(ctx, user)
|
||||
if err != nil {
|
||||
httpJSONError(w, "failed to update user",
|
||||
sendJSONError(w, "failed to update user",
|
||||
err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
@ -285,18 +283,18 @@ func (server *Server) deleteUser(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
userEmail, ok := vars["useremail"]
|
||||
if !ok {
|
||||
httpJSONError(w, "user-email missing", "", http.StatusBadRequest)
|
||||
sendJSONError(w, "user-email missing", "", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
user, err := server.db.Console().Users().GetByEmail(ctx, userEmail)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
httpJSONError(w, fmt.Sprintf("user with email %q not found", userEmail),
|
||||
sendJSONError(w, fmt.Sprintf("user with email %q not found", userEmail),
|
||||
"", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
httpJSONError(w, "failed to get user details",
|
||||
sendJSONError(w, "failed to get user details",
|
||||
err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
@ -304,12 +302,12 @@ func (server *Server) deleteUser(w http.ResponseWriter, r *http.Request) {
|
||||
// Ensure user has no own projects any longer
|
||||
projects, err := server.db.Console().Projects().GetByUserID(ctx, user.ID)
|
||||
if err != nil {
|
||||
httpJSONError(w, "unable to list projects",
|
||||
sendJSONError(w, "unable to list projects",
|
||||
err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if len(projects) > 0 {
|
||||
httpJSONError(w, "some projects still exist",
|
||||
sendJSONError(w, "some projects still exist",
|
||||
fmt.Sprintf("%v", projects), http.StatusConflict)
|
||||
return
|
||||
}
|
||||
@ -317,7 +315,7 @@ func (server *Server) deleteUser(w http.ResponseWriter, r *http.Request) {
|
||||
// Delete memberships in foreign projects
|
||||
members, err := server.db.Console().ProjectMembers().GetByMemberID(ctx, user.ID)
|
||||
if err != nil {
|
||||
httpJSONError(w, "unable to search for user project memberships",
|
||||
sendJSONError(w, "unable to search for user project memberships",
|
||||
err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
@ -325,7 +323,7 @@ func (server *Server) deleteUser(w http.ResponseWriter, r *http.Request) {
|
||||
for _, project := range members {
|
||||
err := server.db.Console().ProjectMembers().Delete(ctx, user.ID, project.ProjectID)
|
||||
if err != nil {
|
||||
httpJSONError(w, "unable to delete user project membership",
|
||||
sendJSONError(w, "unable to delete user project membership",
|
||||
err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
@ -335,14 +333,14 @@ func (server *Server) deleteUser(w http.ResponseWriter, r *http.Request) {
|
||||
// ensure no unpaid invoices exist.
|
||||
invoices, err := server.payments.Invoices().List(ctx, user.ID)
|
||||
if err != nil {
|
||||
httpJSONError(w, "unable to list user invoices",
|
||||
sendJSONError(w, "unable to list user invoices",
|
||||
err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if len(invoices) > 0 {
|
||||
for _, invoice := range invoices {
|
||||
if invoice.Status == "draft" || invoice.Status == "open" {
|
||||
httpJSONError(w, "user has unpaid/pending invoices",
|
||||
sendJSONError(w, "user has unpaid/pending invoices",
|
||||
"", http.StatusConflict)
|
||||
return
|
||||
}
|
||||
@ -351,12 +349,12 @@ func (server *Server) deleteUser(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
hasItems, err := server.payments.Invoices().CheckPendingItems(ctx, user.ID)
|
||||
if err != nil {
|
||||
httpJSONError(w, "unable to list pending invoice items",
|
||||
sendJSONError(w, "unable to list pending invoice items",
|
||||
err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if hasItems {
|
||||
httpJSONError(w, "user has pending invoice items",
|
||||
sendJSONError(w, "user has pending invoice items",
|
||||
"", http.StatusConflict)
|
||||
return
|
||||
}
|
||||
@ -371,14 +369,14 @@ func (server *Server) deleteUser(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
err = server.db.Console().Users().Update(ctx, userInfo)
|
||||
if err != nil {
|
||||
httpJSONError(w, "unable to delete user",
|
||||
sendJSONError(w, "unable to delete user",
|
||||
err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
err = server.payments.CreditCards().RemoveAll(ctx, user.ID)
|
||||
if err != nil {
|
||||
httpJSONError(w, "unable to delete credit card(s) from stripe account",
|
||||
sendJSONError(w, "unable to delete credit card(s) from stripe account",
|
||||
err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user