2020-05-19 13:49:44 +01:00
|
|
|
// Copyright (C) 2020 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package admin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
|
|
|
|
"storj.io/common/uuid"
|
|
|
|
"storj.io/storj/satellite/payments"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (server *Server) addCoupon(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
2021-10-01 12:50:21 +01:00
|
|
|
sendJSONError(w, "failed to read body",
|
2020-08-05 14:13:11 +01:00
|
|
|
err.Error(), http.StatusInternalServerError)
|
2020-05-19 13:49:44 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var input struct {
|
|
|
|
UserID uuid.UUID `json:"userId"`
|
|
|
|
Duration int `json:"duration"`
|
|
|
|
Amount int64 `json:"amount"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.Unmarshal(body, &input)
|
|
|
|
if err != nil {
|
2021-10-01 12:50:21 +01:00
|
|
|
sendJSONError(w, "failed to unmarshal request",
|
2020-08-05 14:13:11 +01:00
|
|
|
err.Error(), http.StatusBadRequest)
|
2020-05-19 13:49:44 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
switch {
|
|
|
|
case input.Duration == 0:
|
2021-10-01 12:50:21 +01:00
|
|
|
sendJSONError(w, "Duration is not set",
|
2020-08-05 14:13:11 +01:00
|
|
|
"", http.StatusBadRequest)
|
2020-05-19 13:49:44 +01:00
|
|
|
return
|
|
|
|
case input.Amount == 0:
|
2021-10-01 12:50:21 +01:00
|
|
|
sendJSONError(w, "Amount is not set",
|
2020-08-05 14:13:11 +01:00
|
|
|
"", http.StatusBadRequest)
|
2020-05-19 13:49:44 +01:00
|
|
|
return
|
|
|
|
case input.Description == "":
|
2021-10-01 12:50:21 +01:00
|
|
|
sendJSONError(w, "Description is not set",
|
2020-08-05 14:13:11 +01:00
|
|
|
"", http.StatusBadRequest)
|
2020-05-19 13:49:44 +01:00
|
|
|
return
|
|
|
|
case input.UserID.IsZero():
|
2021-10-01 12:50:21 +01:00
|
|
|
sendJSONError(w, "UserID is not set",
|
2020-08-05 14:13:11 +01:00
|
|
|
"", http.StatusBadRequest)
|
2020-05-19 13:49:44 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-06 21:14:33 +01:00
|
|
|
coupon, err := server.db.StripeCoinPayments().Coupons().Insert(ctx, payments.CouponOld{
|
2020-05-19 13:49:44 +01:00
|
|
|
UserID: input.UserID,
|
|
|
|
Amount: input.Amount,
|
2021-03-30 00:37:46 +01:00
|
|
|
Duration: &input.Duration,
|
2020-05-19 13:49:44 +01:00
|
|
|
Description: input.Description,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2021-10-01 12:50:21 +01:00
|
|
|
sendJSONError(w, "failed to insert coupon",
|
2020-08-05 14:13:11 +01:00
|
|
|
err.Error(), http.StatusInternalServerError)
|
2020-05-19 13:49:44 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := json.Marshal(coupon.ID)
|
|
|
|
if err != nil {
|
2021-10-01 12:50:21 +01:00
|
|
|
sendJSONError(w, "json encoding failed",
|
2020-08-05 14:13:11 +01:00
|
|
|
err.Error(), http.StatusInternalServerError)
|
2020-05-19 13:49:44 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-01 12:50:21 +01:00
|
|
|
sendJSONData(w, http.StatusOK, data)
|
2020-05-19 13:49:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (server *Server) couponInfo(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
id, ok := vars["couponid"]
|
|
|
|
if !ok {
|
2021-10-01 12:50:21 +01:00
|
|
|
sendJSONError(w, "couponId missing",
|
2020-08-05 14:13:11 +01:00
|
|
|
"", http.StatusBadRequest)
|
2020-05-19 13:49:44 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
couponID, err := uuid.FromString(id)
|
|
|
|
if err != nil {
|
2021-10-01 12:50:21 +01:00
|
|
|
sendJSONError(w, "invalid couponId",
|
2020-08-05 14:13:11 +01:00
|
|
|
"", http.StatusBadRequest)
|
2020-05-19 13:49:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
coupon, err := server.db.StripeCoinPayments().Coupons().Get(ctx, couponID)
|
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
2021-10-01 12:50:21 +01:00
|
|
|
sendJSONError(w, fmt.Sprintf("coupon with id %q not found", couponID),
|
2020-08-05 14:13:11 +01:00
|
|
|
"", http.StatusNotFound)
|
2020-05-19 13:49:44 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if err != nil {
|
2021-10-01 12:50:21 +01:00
|
|
|
sendJSONError(w, "failed to get coupon",
|
2020-08-05 14:13:11 +01:00
|
|
|
err.Error(), http.StatusInternalServerError)
|
2020-05-19 13:49:44 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := json.Marshal(coupon)
|
|
|
|
if err != nil {
|
2021-10-01 12:50:21 +01:00
|
|
|
sendJSONError(w, "json encoding failed",
|
2020-08-05 14:13:11 +01:00
|
|
|
err.Error(), http.StatusInternalServerError)
|
2020-05-19 13:49:44 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-01 12:50:21 +01:00
|
|
|
sendJSONData(w, http.StatusOK, data)
|
2020-05-19 13:49:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (server *Server) deleteCoupon(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
UUIDString, ok := vars["couponid"]
|
|
|
|
if !ok {
|
2021-10-01 12:50:21 +01:00
|
|
|
sendJSONError(w, "couponid missing",
|
2020-08-05 14:13:11 +01:00
|
|
|
"", http.StatusBadRequest)
|
2020-05-19 13:49:44 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
couponID, err := uuid.FromString(UUIDString)
|
|
|
|
if err != nil {
|
2021-10-01 12:50:21 +01:00
|
|
|
sendJSONError(w, "invalid couponid",
|
2020-08-05 14:13:11 +01:00
|
|
|
err.Error(), http.StatusBadRequest)
|
2020-05-19 13:49:44 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = server.db.StripeCoinPayments().Coupons().Delete(ctx, couponID)
|
|
|
|
if err != nil {
|
2021-10-01 12:50:21 +01:00
|
|
|
sendJSONError(w, "unable to delete coupon",
|
2020-08-05 14:13:11 +01:00
|
|
|
err.Error(), http.StatusInternalServerError)
|
2020-05-19 13:49:44 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|