diff --git a/service/account/api/api.go b/service/account/api/api.go index d27ce996a77..9e601cd2a4a 100644 --- a/service/account/api/api.go +++ b/service/account/api/api.go @@ -46,14 +46,7 @@ func GetBillingHistoryNamespaceList(c *gin.Context) { c.JSON(http.StatusBadRequest, helper.ErrorMessage{Error: fmt.Sprintf("failed to parse namespace billing history request: %v", err)}) return } - if req.Auth, err = ParseAuthTokenUser(c); err != nil { - if errors.Is(err, helper.ErrNullAuth) { - // Check authentication - if err := CheckAuthAndCalibrate(req.Auth); err != nil { - c.JSON(http.StatusUnauthorized, helper.ErrorMessage{Error: fmt.Sprintf("authenticate error: %v", err)}) - return - } - } + if err := authenticateRequest(c, req); err != nil { c.JSON(http.StatusUnauthorized, helper.ErrorMessage{Error: fmt.Sprintf("authenticate error : %v", err)}) return } @@ -112,15 +105,8 @@ func GetConsumptionAmount(c *gin.Context) { c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("failed to parse user consumption amount request: %v", err)}) return } - if req.Auth, err = ParseAuthTokenUser(c); err != nil { - if errors.Is(err, helper.ErrNullAuth) { - // Check authentication - if err := CheckAuthAndCalibrate(req.Auth); err != nil { - c.JSON(http.StatusUnauthorized, helper.ErrorMessage{Error: fmt.Sprintf("authenticate error: %v", err)}) - return - } - } - c.JSON(http.StatusUnauthorized, gin.H{"error": fmt.Sprintf("authenticate error : %v", err)}) + if err := authenticateRequest(c, req); err != nil { + c.JSON(http.StatusUnauthorized, helper.ErrorMessage{Error: fmt.Sprintf("authenticate error : %v", err)}) return } amount, err := dao.DBClient.GetConsumptionAmount(*req) @@ -151,18 +137,11 @@ func GetPayment(c *gin.Context) { c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("failed to parse user payment request: %v", err)}) return } - if req.Auth, err = ParseAuthTokenUser(c); err != nil { - if errors.Is(err, helper.ErrNullAuth) { - // Check authentication - if err := CheckAuthAndCalibrate(req.Auth); err != nil { - c.JSON(http.StatusUnauthorized, helper.ErrorMessage{Error: fmt.Sprintf("authenticate error: %v", err)}) - return - } - } - c.JSON(http.StatusUnauthorized, gin.H{"error": fmt.Sprintf("authenticate error : %v", err)}) + if err := authenticateRequest(c, req); err != nil { + c.JSON(http.StatusUnauthorized, helper.ErrorMessage{Error: fmt.Sprintf("authenticate error : %v", err)}) return } - payment, limitResp, err := dao.DBClient.GetPayment(&types.UserQueryOpts{Owner: req.Owner}, req) + payment, limitResp, err := dao.DBClient.GetPayment(&types.UserQueryOpts{Owner: req.Auth.Owner}, req) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("failed to get payment : %v", err)}) return @@ -197,18 +176,11 @@ func GetRechargeAmount(c *gin.Context) { c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("failed to parse user recharge amount request: %v", err)}) return } - if req.Auth, err = ParseAuthTokenUser(c); err != nil { - if errors.Is(err, helper.ErrNullAuth) { - // Check authentication - if err := CheckAuthAndCalibrate(req.Auth); err != nil { - c.JSON(http.StatusUnauthorized, helper.ErrorMessage{Error: fmt.Sprintf("authenticate error: %v", err)}) - return - } - } - c.JSON(http.StatusUnauthorized, gin.H{"error": fmt.Sprintf("authenticate error : %v", err)}) + if err := authenticateRequest(c, req); err != nil { + c.JSON(http.StatusUnauthorized, helper.ErrorMessage{Error: fmt.Sprintf("authenticate error : %v", err)}) return } - amount, err := dao.DBClient.GetRechargeAmount(types.UserQueryOpts{Owner: req.Owner}, req.TimeRange.StartTime, req.TimeRange.EndTime) + amount, err := dao.DBClient.GetRechargeAmount(types.UserQueryOpts{Owner: req.Auth.Owner}, req.TimeRange.StartTime, req.TimeRange.EndTime) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("failed to get recharge amount : %v", err)}) return @@ -236,18 +208,11 @@ func GetPropertiesUsedAmount(c *gin.Context) { c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("failed to parse user properties used amount request: %v", err)}) return } - if req.Auth, err = ParseAuthTokenUser(c); err != nil { - if errors.Is(err, helper.ErrNullAuth) { - // Check authentication - if err := CheckAuthAndCalibrate(req.Auth); err != nil { - c.JSON(http.StatusUnauthorized, helper.ErrorMessage{Error: fmt.Sprintf("authenticate error: %v", err)}) - return - } - } - c.JSON(http.StatusUnauthorized, gin.H{"error": fmt.Sprintf("authenticate error : %v", err)}) + if err := authenticateRequest(c, req); err != nil { + c.JSON(http.StatusUnauthorized, helper.ErrorMessage{Error: fmt.Sprintf("authenticate error : %v", err)}) return } - amount, err := dao.DBClient.GetPropertiesUsedAmount(req.Owner, req.TimeRange.StartTime, req.TimeRange.EndTime) + amount, err := dao.DBClient.GetPropertiesUsedAmount(req.Auth.Owner, req.TimeRange.StartTime, req.TimeRange.EndTime) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("failed to get properties used amount : %v", err)}) return @@ -283,15 +248,8 @@ func GetCosts(c *gin.Context) { c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("failed to parse user hour costs amount request: %v", err)}) return } - if req.Auth, err = ParseAuthTokenUser(c); err != nil { - if errors.Is(err, helper.ErrNullAuth) { - // Check authentication - if err := CheckAuthAndCalibrate(req.Auth); err != nil { - c.JSON(http.StatusUnauthorized, helper.ErrorMessage{Error: fmt.Sprintf("authenticate error: %v", err)}) - return - } - } - c.JSON(http.StatusUnauthorized, gin.H{"error": fmt.Sprintf("authenticate error : %v", err)}) + if err := authenticateRequest(c, req); err != nil { + c.JSON(http.StatusUnauthorized, helper.ErrorMessage{Error: fmt.Sprintf("authenticate error : %v", err)}) return } costs, err := dao.DBClient.GetCosts(*req) @@ -322,15 +280,8 @@ func GetAccount(c *gin.Context) { c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("failed to parse user hour costs amount request: %v", err)}) return } - if req.Auth, err = ParseAuthTokenUser(c); err != nil { - if errors.Is(err, helper.ErrNullAuth) { - // Check authentication - if err := CheckAuthAndCalibrate(req.Auth); err != nil { - c.JSON(http.StatusUnauthorized, helper.ErrorMessage{Error: fmt.Sprintf("authenticate error: %v", err)}) - return - } - } - c.JSON(http.StatusUnauthorized, gin.H{"error": fmt.Sprintf("authenticate error : %v", err)}) + if err := authenticateRequest(c, req); err != nil { + c.JSON(http.StatusUnauthorized, helper.ErrorMessage{Error: fmt.Sprintf("authenticate error : %v", err)}) return } account, err := dao.DBClient.GetAccount(types.UserQueryOpts{Owner: req.Auth.Owner}) @@ -362,15 +313,8 @@ func SetPaymentInvoice(c *gin.Context) { c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("failed to parse set payment invoice request: %v", err)}) return } - if req.Auth, err = ParseAuthTokenUser(c); err != nil { - if errors.Is(err, helper.ErrNullAuth) { - // Check authentication - if err := CheckAuthAndCalibrate(req.Auth); err != nil { - c.JSON(http.StatusUnauthorized, helper.ErrorMessage{Error: fmt.Sprintf("authenticate error: %v", err)}) - return - } - } - c.JSON(http.StatusUnauthorized, gin.H{"error": fmt.Sprintf("authenticate error : %v", err)}) + if err := authenticateRequest(c, req); err != nil { + c.JSON(http.StatusUnauthorized, helper.ErrorMessage{Error: fmt.Sprintf("authenticate error : %v", err)}) return } if err := dao.DBClient.SetPaymentInvoice(req); err != nil { @@ -400,15 +344,8 @@ func TransferAmount(c *gin.Context) { c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("failed to parse transfer amount request: %v", err)}) return } - if req.Auth, err = ParseAuthTokenUser(c); err != nil { - if errors.Is(err, helper.ErrNullAuth) { - // Check authentication - if err := CheckAuthAndCalibrate(req.Auth); err != nil { - c.JSON(http.StatusUnauthorized, helper.ErrorMessage{Error: fmt.Sprintf("authenticate error: %v", err)}) - return - } - } - c.JSON(http.StatusUnauthorized, gin.H{"error": fmt.Sprintf("authenticate error : %v", err)}) + if err := authenticateRequest(c, req); err != nil { + c.JSON(http.StatusUnauthorized, helper.ErrorMessage{Error: fmt.Sprintf("authenticate error : %v", err)}) return } if err := dao.DBClient.Transfer(req); err != nil { @@ -443,15 +380,8 @@ func GetTransfer(c *gin.Context) { c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("failed to parse get transfer amount request: %v", err)}) return } - if req.Auth, err = ParseAuthTokenUser(c); err != nil { - if errors.Is(err, helper.ErrNullAuth) { - // Check authentication - if err := CheckAuthAndCalibrate(req.Auth); err != nil { - c.JSON(http.StatusUnauthorized, helper.ErrorMessage{Error: fmt.Sprintf("authenticate error: %v", err)}) - return - } - } - c.JSON(http.StatusUnauthorized, gin.H{"error": fmt.Sprintf("authenticate error : %v", err)}) + if err := authenticateRequest(c, req); err != nil { + c.JSON(http.StatusUnauthorized, helper.ErrorMessage{Error: fmt.Sprintf("authenticate error : %v", err)}) return } ops := types.GetTransfersReq{ @@ -495,15 +425,8 @@ func GetAPPCosts(c *gin.Context) { c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("failed to parse get app cost request: %v", err)}) return } - if req.Auth, err = ParseAuthTokenUser(c); err != nil { - if errors.Is(err, helper.ErrNullAuth) { - // Check authentication - if err := CheckAuthAndCalibrate(req.Auth); err != nil { - c.JSON(http.StatusUnauthorized, helper.ErrorMessage{Error: fmt.Sprintf("authenticate error: %v", err)}) - return - } - } - c.JSON(http.StatusUnauthorized, gin.H{"error": fmt.Sprintf("authenticate error : %v", err)}) + if err := authenticateRequest(c, req); err != nil { + c.JSON(http.StatusUnauthorized, helper.ErrorMessage{Error: fmt.Sprintf("authenticate error : %v", err)}) return } cost, err := dao.DBClient.GetAppCosts(req) @@ -534,15 +457,8 @@ func CheckPermission(c *gin.Context) { c.JSON(http.StatusUnauthorized, gin.H{"error": fmt.Sprintf("failed to parse check permission request: %v", err)}) return } - if req.Auth, err = ParseAuthTokenUser(c); err != nil { - if errors.Is(err, helper.ErrNullAuth) { - // Check authentication - if err := CheckAuthAndCalibrate(req.Auth); err != nil { - c.JSON(http.StatusUnauthorized, helper.ErrorMessage{Error: fmt.Sprintf("authenticate error: %v", err)}) - return - } - } - c.JSON(http.StatusUnauthorized, gin.H{"error": fmt.Sprintf("authenticate error : %v", err)}) + if err := authenticateRequest(c, req); err != nil { + c.JSON(http.StatusUnauthorized, helper.ErrorMessage{Error: fmt.Sprintf("authenticate error : %v", err)}) return } @@ -585,15 +501,8 @@ func GetCostOverview(c *gin.Context) { c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("failed to parse cost overview request: %v", err)}) return } - if req.Auth, err = ParseAuthTokenUser(c); err != nil { - if errors.Is(err, helper.ErrNullAuth) { - // Check authentication - if err := CheckAuthAndCalibrate(req.Auth); err != nil { - c.JSON(http.StatusUnauthorized, helper.ErrorMessage{Error: fmt.Sprintf("authenticate error: %v", err)}) - return - } - } - c.JSON(http.StatusUnauthorized, gin.H{"error": fmt.Sprintf("authenticate error : %v", err)}) + if err := authenticateRequest(c, req); err != nil { + c.JSON(http.StatusUnauthorized, helper.ErrorMessage{Error: fmt.Sprintf("authenticate error : %v", err)}) return } overview, err := dao.DBClient.GetCostOverview(*req) @@ -624,15 +533,8 @@ func GetCostAppList(c *gin.Context) { c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("failed to parse cost app list request: %v", err)}) return } - if req.Auth, err = ParseAuthTokenUser(c); err != nil { - if errors.Is(err, helper.ErrNullAuth) { - // Check authentication - if err := CheckAuthAndCalibrate(req.Auth); err != nil { - c.JSON(http.StatusUnauthorized, helper.ErrorMessage{Error: fmt.Sprintf("authenticate error: %v", err)}) - return - } - } - c.JSON(http.StatusUnauthorized, gin.H{"error": fmt.Sprintf("authenticate error : %v", err)}) + if err := authenticateRequest(c, req); err != nil { + c.JSON(http.StatusUnauthorized, helper.ErrorMessage{Error: fmt.Sprintf("authenticate error : %v", err)}) return } apps, err := dao.DBClient.GetCostAppList(*req) @@ -678,15 +580,8 @@ func GetBasicCostDistribution(c *gin.Context) { c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("failed to parse basic cost distribution request: %v", err)}) return } - if req.Auth, err = ParseAuthTokenUser(c); err != nil { - if errors.Is(err, helper.ErrNullAuth) { - // Check authentication - if err := CheckAuthAndCalibrate(req.Auth); err != nil { - c.JSON(http.StatusUnauthorized, helper.ErrorMessage{Error: fmt.Sprintf("authenticate error: %v", err)}) - return - } - } - c.JSON(http.StatusUnauthorized, gin.H{"error": fmt.Sprintf("authenticate error : %v", err)}) + if err := authenticateRequest(c, req); err != nil { + c.JSON(http.StatusUnauthorized, helper.ErrorMessage{Error: fmt.Sprintf("authenticate error : %v", err)}) return } costs, err := dao.DBClient.GetBasicCostDistribution(*req) @@ -717,15 +612,8 @@ func GetAppCostTimeRange(c *gin.Context) { c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("failed to parse app cost time range request: %v", err)}) return } - if req.Auth, err = ParseAuthTokenUser(c); err != nil { - if errors.Is(err, helper.ErrNullAuth) { - // Check authentication - if err := CheckAuthAndCalibrate(req.Auth); err != nil { - c.JSON(http.StatusUnauthorized, helper.ErrorMessage{Error: fmt.Sprintf("authenticate error: %v", err)}) - return - } - } - c.JSON(http.StatusUnauthorized, gin.H{"error": fmt.Sprintf("authenticate error : %v", err)}) + if err := authenticateRequest(c, req); err != nil { + c.JSON(http.StatusUnauthorized, helper.ErrorMessage{Error: fmt.Sprintf("authenticate error : %v", err)}) return } timeRange, err := dao.DBClient.GetAppCostTimeRange(*req) @@ -786,15 +674,8 @@ func ApplyInvoice(c *gin.Context) { c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("failed to parse apply invoice request: %v", err)}) return } - if req.Auth, err = ParseAuthTokenUser(c); err != nil { - if errors.Is(err, helper.ErrNullAuth) { - // Check authentication - if err := CheckAuthAndCalibrate(req.Auth); err != nil { - c.JSON(http.StatusUnauthorized, helper.ErrorMessage{Error: fmt.Sprintf("authenticate error: %v", err)}) - return - } - } - c.JSON(http.StatusUnauthorized, gin.H{"error": fmt.Sprintf("authenticate error : %v", err)}) + if err := authenticateRequest(c, req); err != nil { + c.JSON(http.StatusUnauthorized, helper.ErrorMessage{Error: fmt.Sprintf("authenticate error : %v", err)}) return } invoice, payments, err := dao.DBClient.ApplyInvoice(req) @@ -830,20 +711,8 @@ func GetInvoice(c *gin.Context) { c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("failed to parse get invoice request: %v", err)}) return } - if req.Token != "" { - err = checkInvoiceToken(req.Token) - } else { - req.Auth, err = ParseAuthTokenUser(c) - } - if err != nil { - if errors.Is(err, helper.ErrNullAuth) { - // Check authentication - if err := CheckAuthAndCalibrate(req.Auth); err != nil { - c.JSON(http.StatusUnauthorized, helper.ErrorMessage{Error: fmt.Sprintf("authenticate error: %v", err)}) - return - } - } - c.JSON(http.StatusUnauthorized, gin.H{"error": fmt.Sprintf("authenticate error : %v", err)}) + if err := authenticateRequest(c, req); err != nil { + c.JSON(http.StatusUnauthorized, helper.ErrorMessage{Error: fmt.Sprintf("authenticate error : %v", err)}) return } invoices, limits, err := dao.DBClient.GetInvoice(req) @@ -863,6 +732,24 @@ func GetInvoice(c *gin.Context) { }) } +func authenticateRequest(c *gin.Context, req helper.AuthReq) error { + if req.GetAuth() != nil && req.GetAuth().Token != "" { + return checkInvoiceToken(req.GetAuth().Token) + } + + auth, err := ParseAuthTokenUser(c) + if err == nil { + req.SetAuth(auth) + return nil + } + + if !errors.Is(err, helper.ErrNullAuth) { + return err + } + + return CheckAuthAndCalibrate(req.GetAuth()) +} + // SetStatusInvoice // @Summary Set status invoice // @Description Set status invoice @@ -881,7 +768,13 @@ func SetStatusInvoice(c *gin.Context) { c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("failed to parse set status invoice request: %v", err)}) return } - if err = checkInvoiceToken(req.Token); err != nil { + auth := req.GetAuth() + if auth != nil { + err = checkInvoiceToken(auth.Token) + } else { + err = fmt.Errorf("no auth provided") + } + if err != nil { c.JSON(http.StatusUnauthorized, gin.H{"error": fmt.Sprintf("authenticate error : %v", err)}) return } @@ -913,20 +806,8 @@ func GetInvoicePayment(c *gin.Context) { c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("failed to parse get invoice payment request: %v", err)}) return } - if req.Token != "" { - err = checkInvoiceToken(req.Token) - } else { - req.Auth, err = ParseAuthTokenUser(c) - } - if err != nil { - if errors.Is(err, helper.ErrNullAuth) { - // Check authentication - if err := CheckAuthAndCalibrate(req.Auth); err != nil { - c.JSON(http.StatusUnauthorized, helper.ErrorMessage{Error: fmt.Sprintf("authenticate error: %v", err)}) - return - } - } - c.JSON(http.StatusUnauthorized, gin.H{"error": fmt.Sprintf("authenticate error : %v", err)}) + if err := authenticateRequest(c, req); err != nil { + c.JSON(http.StatusUnauthorized, helper.ErrorMessage{Error: fmt.Sprintf("authenticate error : %v", err)}) return } payments, err := dao.DBClient.GetInvoicePayments(req.InvoiceID) @@ -959,15 +840,8 @@ func UseGiftCode(c *gin.Context) { return } - if req.Auth, err = ParseAuthTokenUser(c); err != nil { - if errors.Is(err, helper.ErrNullAuth) { - // Check authentication - if err := CheckAuthAndCalibrate(req.Auth); err != nil { - c.JSON(http.StatusUnauthorized, helper.ErrorMessage{Error: fmt.Sprintf("authenticate error: %v", err)}) - return - } - } - c.JSON(http.StatusUnauthorized, gin.H{"error": fmt.Sprintf("authenticate error : %v", err)}) + if err := authenticateRequest(c, req); err != nil { + c.JSON(http.StatusUnauthorized, helper.ErrorMessage{Error: fmt.Sprintf("authenticate error : %v", err)}) return } @@ -980,7 +854,7 @@ func UseGiftCode(c *gin.Context) { // Return success response c.JSON(http.StatusOK, helper.UseGiftCodeResp{ Data: helper.UseGiftCodeRespData{ - UserID: req.UserID, + UserID: req.Auth.UserID, }, Message: "Gift code successfully redeemed", }) @@ -1004,8 +878,8 @@ func UserUsage(c *gin.Context) { c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("failed to parse user usage request: %v", err)}) return } - if err = checkInvoiceToken(req.Token); err != nil { - c.JSON(http.StatusUnauthorized, gin.H{"error": fmt.Sprintf("authenticate error : %v", err)}) + if err := authenticateRequest(c, req); err != nil { + c.JSON(http.StatusUnauthorized, helper.ErrorMessage{Error: fmt.Sprintf("authenticate error : %v", err)}) return } usage, err := dao.DBClient.GetMonitorUniqueValues(req.StartTime, req.EndTime, req.NamespaceList) @@ -1019,6 +893,9 @@ func UserUsage(c *gin.Context) { } func CheckAuthAndCalibrate(auth *helper.Auth) (err error) { + if auth == nil { + return helper.ErrNullAuth + } if !dao.Debug || auth.KubeConfig != "" { if err = checkAuth(auth); err != nil { return fmt.Errorf("check auth error: %v", err) diff --git a/service/account/dao/interface_test.go b/service/account/dao/interface_test.go index 44fcc75757b..96c80dac85c 100644 --- a/service/account/dao/interface_test.go +++ b/service/account/dao/interface_test.go @@ -40,8 +40,10 @@ func TestMongoDB_GetAppCosts(t *testing.T) { StartTime: time.Now().Add(-24 * time.Hour * 30), EndTime: time.Now(), }, - Auth: &helper.Auth{ - Owner: "xxx", + AuthBase: helper.AuthBase{ + Auth: &helper.Auth{ + Owner: "5uxfy8jl", + }, }, }, Namespace: "ns-xxx", @@ -103,8 +105,10 @@ func TestMongoDB_GetCostAppList(t *testing.T) { } }() req := helper.GetCostAppListReq{ - Auth: &helper.Auth{ - Owner: "5uxfy8jl", + AuthBase: helper.AuthBase{ + Auth: &helper.Auth{ + Owner: "E1xAJ0fy4k", + }, }, //Namespace: "ns-hwhbg4vf", //AppType: "APP-STORE", @@ -140,8 +144,10 @@ func TestMongoDB_GetCostOverview(t *testing.T) { } }() req := helper.GetCostAppListReq{ - Auth: &helper.Auth{ - Owner: "5uxfy8jl", + AuthBase: helper.AuthBase{ + Auth: &helper.Auth{ + Owner: "E1xAJ0fy4k", + }, }, //Namespace: "ns-hwhbg4vf", //AppType: "APP", @@ -332,8 +338,10 @@ func TestMongoDB_GetBasicCostDistribution(t *testing.T) { } }() req := helper.GetCostAppListReq{ - Auth: &helper.Auth{ - Owner: "5uxfy8jl", + AuthBase: helper.AuthBase{ + Auth: &helper.Auth{ + Owner: "5uxfy8jl", + }, }, //Namespace: "ns-hwhbg4vf", AppType: "APP-STORE", @@ -363,8 +371,10 @@ func TestMongoDB_GetAppCostTimeRange(t *testing.T) { } }() req := helper.GetCostAppListReq{ - Auth: &helper.Auth{ - Owner: "5uxfy8jl", + AuthBase: helper.AuthBase{ + Auth: &helper.Auth{ + Owner: "5uxfy8jl", + }, }, //Namespace: "ns-hwhbg4vf", //AppType: "APP-STORE", @@ -395,8 +405,10 @@ func TestMongoDB_GetConsumptionAmount(t *testing.T) { } }() req := helper.GetCostAppListReq{ - Auth: &helper.Auth{ - Owner: "uy771xun", + AuthBase: helper.AuthBase{ + Auth: &helper.Auth{ + Owner: "5uxfy8jl", + }, }, Namespace: "ns-uy771xun", AppType: "APP-STORE", @@ -421,8 +433,10 @@ func TestMongoDB_GetConsumptionAmount(t *testing.T) { } t.Logf("amountAll: %v", amountAll) amount2, err := m.GetConsumptionAmount(helper.ConsumptionRecordReq{ - Auth: &helper.Auth{ - Owner: req.Owner, + AuthBase: helper.AuthBase{ + Auth: &helper.Auth{ + Owner: req.Owner, + }, }, TimeRange: helper.TimeRange{ StartTime: req.StartTime, @@ -456,8 +470,10 @@ func TestMongoDB_GetAppCost1(t *testing.T) { StartTime: time.Now().Add(-24 * time.Hour * 30), EndTime: time.Now(), }, - Auth: &helper.Auth{ - Owner: "uy771xun", + AuthBase: helper.AuthBase{ + Auth: &helper.Auth{ + Owner: "5uxfy8jl", + }, }, }, //Namespace: "ns-hwhbg4vf", @@ -524,9 +540,11 @@ func TestAccount_ApplyInvoice(t *testing.T) { } }() req := &helper.ApplyInvoiceReq{ - Auth: &helper.Auth{ - Owner: "uy771xun", - UserID: "Vobqe43JUs", + AuthBase: helper.AuthBase{ + Auth: &helper.Auth{ + Owner: "5uxfy8jl", + UserID: "Vobqe43JUs", + }, }, PaymentIDList: []string{ "vrCBsLt1oIf0", @@ -543,9 +561,11 @@ func TestAccount_ApplyInvoice(t *testing.T) { t.Logf("success to apply invoice") invoice, resp, err := m.GetInvoice(&helper.GetInvoiceReq{ - Auth: &helper.Auth{ - Owner: "uy771xun", - UserID: "Vobqe43JUs", + AuthBase: helper.AuthBase{ + Auth: &helper.Auth{ + Owner: "E1xAJ0fy4k", + UserID: "", + }, }, LimitReq: helper.LimitReq{ Page: 1, @@ -589,8 +609,10 @@ func TestAccount_UseGiftCode(t *testing.T) { giftcode, err := db.UseGiftCode(&helper.UseGiftCodeReq{ Code: "DfxAffaeEf", - Auth: &helper.Auth{ - UserID: "E1xAJ0fy4k", + AuthBase: helper.AuthBase{ + Auth: &helper.Auth{ + Owner: "E1xAJ0fy4k", + }, }, }) diff --git a/service/account/helper/request.go b/service/account/helper/request.go index 8204a621608..2cbf53ff6f5 100644 --- a/service/account/helper/request.go +++ b/service/account/helper/request.go @@ -12,6 +12,11 @@ import ( "github.com/gin-gonic/gin" ) +type AuthReq interface { + GetAuth() *Auth + SetAuth(auth *Auth) +} + type NamespaceBillingHistoryReq struct { // @Summary Start and end time for the request // @Description Start and end time for the request @@ -21,7 +26,7 @@ type NamespaceBillingHistoryReq struct { // @Summary Authentication information // @Description Authentication information // @JSONSchema required - *Auth `json:",inline" bson:",inline"` + AuthBase `json:",inline" bson:",inline"` // @Summary Type of the request (optional) // @Description Type of the request (optional) @@ -38,7 +43,7 @@ type SetPaymentInvoiceReq struct { // @Summary Authentication information // @Description Authentication information // @JSONSchema required - *Auth `json:",inline" bson:",inline"` + AuthBase `json:",inline" bson:",inline"` } type TransferAmountReq struct { @@ -55,7 +60,7 @@ type TransferAmountReq struct { // @Summary Authentication information // @Description Authentication information // @JSONSchema required - *Auth `json:",inline" bson:",inline"` + AuthBase `json:",inline" bson:",inline"` // @Summary Transfer all // @Description Transfer all amount @@ -76,7 +81,7 @@ type ConsumptionRecordReq struct { // @Summary Authentication information // @Description Authentication information // @JSONSchema required - *Auth `json:",inline" bson:",inline"` + AuthBase `json:",inline" bson:",inline"` // @Summary App type // @Description App type @@ -87,6 +92,195 @@ type ConsumptionRecordReq struct { AppName string `json:"appName,omitempty" bson:"appName" example:"app"` } +type UserBaseReq struct { + + // @Summary Start and end time for the request + // @Description Start and end time for the request + // @JSONSchema required + TimeRange `json:",inline" bson:",inline"` + + // @Summary Authentication information + // @Description Authentication information + // @JSONSchema required + AuthBase `json:",inline" bson:",inline"` +} + +type AppCostsReq struct { + // @Summary Order ID + // @Description Order ID + // @JSONSchema + OrderID string `json:"orderID,omitempty" bson:"orderID" example:"order-id-1"` + + UserBaseReq `json:",inline" bson:",inline"` + + // @Summary Namespace + // @Description Namespace + Namespace string `json:"namespace,omitempty" bson:"namespace" example:"ns-admin"` + // @Summary App type + // @Description App type + AppType string `json:"appType,omitempty" bson:"appType" example:"app"` + + // @Summary App Name + // @Description App Name + AppName string `json:"appName,omitempty" bson:"appName" example:"app"` + + // @Summary Page + // @Description Page + Page int `json:"page,omitempty" bson:"page" example:"1"` + + // @Summary Page Size + // @Description Page Size + PageSize int `json:"pageSize,omitempty" bson:"pageSize" example:"10"` +} + +type GetPaymentReq struct { + // @Summary Payment ID + // @Description Payment ID + // @JSONSchema + PaymentID string `json:"paymentID,omitempty" bson:"paymentID" example:"payment-id-1"` + + // @Summary Authentication information + // @Description Authentication information + // @JSONSchema required + AuthBase `json:",inline" bson:",inline"` + + // @Summary Limit request + // @Description Limit request + LimitReq `json:",inline" bson:",inline"` +} + +type ApplyInvoiceReq struct { + // @Summary Authentication information + // @Description Authentication information + // @JSONSchema required + AuthBase `json:",inline" bson:",inline"` + + // payment id list + // @Summary Payment ID list + // @Description Payment ID list + // @JSONSchema required + PaymentIDList []string `json:"paymentIDList" bson:"paymentIDList" binding:"required" example:"[\"payment-id-1\",\"payment-id-2\"]"` + + // invoice detail information json + // @Summary Invoice detail information + // @Description Invoice detail information + // @JSONSchema required + Detail string `json:"detail" bson:"detail" binding:"required" example:"{\"title\":\"title\",\"amount\":100,\"taxRate\":0.06,\"tax\":6,\"total\":106,\"invoiceType\":1,\"invoiceContent\":1,\"invoiceStatus\":1,\"invoiceTime\":\"2021-01-01T00:00:00Z\",\"invoiceNumber\":\"invoice-number-1\",\"invoiceCode\":\"invoice-code-1\",\"invoiceFile\":\"invoice-file-1\"}"` +} + +type LimitReq struct { + // @Summary Page + // @Description Page + Page int `json:"page" bson:"page"` + + // @Summary Page Size + // @Description Page Size + PageSize int `json:"pageSize" bson:"pageSize"` + + // @Summary Time range + // @Description Time range + TimeRange `json:",inline" bson:",inline"` +} + +type SetInvoiceStatusReq struct { + // Invoice id list + // @Summary Invoice ID list + // @Description Invoice ID list + // @JSONSchema required + InvoiceIDList []string `json:"invoiceIDList" bson:"invoiceIDList" binding:"required" example:"[\"invoice-id-1\",\"invoice-id-2\"]"` + + // Invoice status + // @Summary Invoice status + // @Description Invoice status + // @JSONSchema required + Status string `json:"status" bson:"status" binding:"required" example:"COMPLETED,REJECTED,PENDING"` + + // @Summary Authentication information + // @Description Authentication information + // @JSONSchema required + AuthBase `json:",inline" bson:",inline"` +} + +type UseGiftCodeReq struct { + // @Summary Gift code to be used + // @Description The code of the gift card to be redeemed + // @JSONSchema required + Code string `json:"code" bson:"code" binding:"required" example:"HAPPY2024"` + + // @Summary Authentication information + // @Description Authentication information + // @JSONSchema required + AuthBase `json:",inline" bson:",inline"` +} + +type UserUsageReq struct { + // @Summary Start and end time for the request + // @Description Start and end time for the request + // @JSONSchema required + TimeRange `json:",inline" bson:",inline"` + + // @Summary Authentication information + // @Description Authentication information + // @JSONSchema required + AuthBase `json:",inline" bson:",inline"` + + // NamespaceList + // @Summary Namespace list + // @Description Namespace list + // @JSONSchema + NamespaceList []string `json:"namespaceList" bson:"namespaceList" example:"[\"ns-admin\",\"ns-test1\"]"` +} + +type GetInvoiceReq struct { + // @Summary Invoice ID + // @Description Invoice ID + // @JSONSchema + InvoiceID string `json:"invoiceID,omitempty" bson:"invoiceID" example:"invoice-id-1"` + + // @Summary Authentication information + // @Description Authentication information + // @JSONSchema required + AuthBase `json:",inline" bson:",inline"` + + // @Summary Limit request + // @Description Limit request + LimitReq `json:",inline" bson:",inline"` +} + +type GetCostAppListReq struct { + // @Summary Authentication information + // @Description Authentication information + AuthBase `json:",inline" bson:",inline"` + + // @Summary Namespace + // @Description Namespace + Namespace string `json:"namespace" bson:"namespace"` + + // @Summary App type + // @Description App type + AppType string `json:"appType" bson:"appType"` + + // @Summary App Name + // @Description App Name + AppName string `json:"appName" bson:"appName"` + + // @Summary Limit request + // @Description Limit request + LimitReq `json:",inline" bson:",inline"` +} + +type AuthBase struct { + *Auth `json:",inline" bson:",inline"` +} + +func (a *AuthBase) GetAuth() *Auth { + return a.Auth +} + +func (a *AuthBase) SetAuth(auth *Auth) { + a.Auth = auth +} + type NamespaceBillingHistoryResp struct { Data NamespaceBillingHistoryRespData `json:"data,omitempty" bson:"data,omitempty"` Message string `json:"message,omitempty" bson:"message" example:"successfully retrieved namespace list"` @@ -159,19 +353,6 @@ func ParseConsumptionRecordReq(c *gin.Context) (*ConsumptionRecordReq, error) { return consumptionRecord, nil } -type UserBaseReq struct { - - // @Summary Start and end time for the request - // @Description Start and end time for the request - // @JSONSchema required - TimeRange `json:",inline" bson:",inline"` - - // @Summary Authentication information - // @Description Authentication information - // @JSONSchema required - *Auth `json:",inline" bson:",inline"` -} - func ParseUserBaseReq(c *gin.Context) (*UserBaseReq, error) { userCosts := &UserBaseReq{} if err := c.ShouldBindJSON(userCosts); err != nil { @@ -196,34 +377,6 @@ func ParsePaymentReq(c *gin.Context) (*GetPaymentReq, error) { return payment, nil } -type AppCostsReq struct { - // @Summary Order ID - // @Description Order ID - // @JSONSchema - OrderID string `json:"orderID,omitempty" bson:"orderID" example:"order-id-1"` - - UserBaseReq `json:",inline" bson:",inline"` - - // @Summary Namespace - // @Description Namespace - Namespace string `json:"namespace,omitempty" bson:"namespace" example:"ns-admin"` - // @Summary App type - // @Description App type - AppType string `json:"appType,omitempty" bson:"appType" example:"app"` - - // @Summary App Name - // @Description App Name - AppName string `json:"appName,omitempty" bson:"appName" example:"app"` - - // @Summary Page - // @Description Page - Page int `json:"page,omitempty" bson:"page" example:"1"` - - // @Summary Page Size - // @Description Page Size - PageSize int `json:"pageSize,omitempty" bson:"pageSize" example:"10"` -} - func ParseAppCostsReq(c *gin.Context) (*AppCostsReq, error) { userCosts := &AppCostsReq{} if err := c.ShouldBindJSON(userCosts); err != nil { @@ -261,7 +414,7 @@ func ParseGetTransferRecordReq(c *gin.Context) (*GetTransferRecordReq, error) { return nil, fmt.Errorf("bind json error: %v", err) } setDefaultTimeRange(&transferReq.TimeRange) - transferReq.Owner = strings.TrimPrefix(transferReq.Owner, "ns-") + transferReq.Auth.Owner = strings.TrimPrefix(transferReq.Auth.Owner, "ns-") return transferReq, nil } @@ -283,22 +436,6 @@ func setDefaultTimeRange(timeRange *TimeRange) { } } -type GetPaymentReq struct { - // @Summary Payment ID - // @Description Payment ID - // @JSONSchema - PaymentID string `json:"paymentID,omitempty" bson:"paymentID" example:"payment-id-1"` - - // @Summary Authentication information - // @Description Authentication information - // @JSONSchema required - *Auth `json:",inline" bson:",inline"` - - // @Summary Limit request - // @Description Limit request - LimitReq `json:",inline" bson:",inline"` -} - type CostOverviewResp struct { // @Summary Cost overview // @Description Cost overview @@ -328,28 +465,6 @@ type CostOverview struct { AppName string `json:"appName" bson:"appName"` } -type GetCostAppListReq struct { - // @Summary Authentication information - // @Description Authentication information - *Auth `json:",inline" bson:",inline"` - - // @Summary Namespace - // @Description Namespace - Namespace string `json:"namespace" bson:"namespace"` - - // @Summary App type - // @Description App type - AppType string `json:"appType" bson:"appType"` - - // @Summary App Name - // @Description App Name - AppName string `json:"appName" bson:"appName"` - - // @Summary Limit request - // @Description Limit request - LimitReq `json:",inline" bson:",inline"` -} - type CostAppListResp struct { // @Summary Cost app list // @Description Cost app list @@ -374,20 +489,6 @@ type CostApp struct { AppName string `json:"appName" bson:"appName"` } -type LimitReq struct { - // @Summary Page - // @Description Page - Page int `json:"page" bson:"page"` - - // @Summary Page Size - // @Description Page Size - PageSize int `json:"pageSize" bson:"pageSize"` - - // @Summary Time range - // @Description Time range - TimeRange `json:",inline" bson:",inline"` -} - type LimitResp struct { // @Summary Total // @Description Total @@ -398,25 +499,6 @@ type LimitResp struct { TotalPage int64 `json:"totalPage" bson:"totalPage"` } -type ApplyInvoiceReq struct { - // @Summary Authentication information - // @Description Authentication information - // @JSONSchema required - *Auth `json:",inline" bson:",inline"` - - // payment id list - // @Summary Payment ID list - // @Description Payment ID list - // @JSONSchema required - PaymentIDList []string `json:"paymentIDList" bson:"paymentIDList" binding:"required" example:"[\"payment-id-1\",\"payment-id-2\"]"` - - // invoice detail information json - // @Summary Invoice detail information - // @Description Invoice detail information - // @JSONSchema required - Detail string `json:"detail" bson:"detail" binding:"required" example:"{\"title\":\"title\",\"amount\":100,\"taxRate\":0.06,\"tax\":6,\"total\":106,\"invoiceType\":1,\"invoiceContent\":1,\"invoiceStatus\":1,\"invoiceTime\":\"2021-01-01T00:00:00Z\",\"invoiceNumber\":\"invoice-number-1\",\"invoiceCode\":\"invoice-code-1\",\"invoiceFile\":\"invoice-file-1\"}"` -} - func ParseApplyInvoiceReq(c *gin.Context) (*ApplyInvoiceReq, error) { applyInvoice := &ApplyInvoiceReq{} if err := c.ShouldBindJSON(applyInvoice); err != nil { @@ -425,41 +507,6 @@ func ParseApplyInvoiceReq(c *gin.Context) (*ApplyInvoiceReq, error) { return applyInvoice, nil } -type GetInvoiceReq struct { - // @Summary Invoice ID - // @Description Invoice ID - // @JSONSchema - InvoiceID string `json:"invoiceID,omitempty" bson:"invoiceID" example:"invoice-id-1"` - - // @Summary Authentication information - // @Description Authentication information - // @JSONSchema required - *Auth `json:",inline" bson:",inline"` - - // @Summary Limit request - // @Description Limit request - LimitReq `json:",inline" bson:",inline"` -} - -type SetInvoiceStatusReq struct { - // Invoice id list - // @Summary Invoice ID list - // @Description Invoice ID list - // @JSONSchema required - InvoiceIDList []string `json:"invoiceIDList" bson:"invoiceIDList" binding:"required" example:"[\"invoice-id-1\",\"invoice-id-2\"]"` - - // Invoice status - // @Summary Invoice status - // @Description Invoice status - // @JSONSchema required - Status string `json:"status" bson:"status" binding:"required" example:"COMPLETED,REJECTED,PENDING"` - - // @Summary Authentication token - // @Description Authentication token - // @JSONSchema required - Token string `json:"token" bson:"token" binding:"required" example:"token"` -} - func ParseGetInvoiceReq(c *gin.Context) (*GetInvoiceReq, error) { invoiceList := &GetInvoiceReq{} if err := c.ShouldBindJSON(invoiceList); err != nil { @@ -485,18 +532,6 @@ type UseGiftCodeResp struct { Message string `json:"message,omitempty" bson:"message" example:"Gift code successfully redeemed"` } -type UseGiftCodeReq struct { - // @Summary Gift code to be used - // @Description The code of the gift card to be redeemed - // @JSONSchema required - Code string `json:"code" bson:"code" binding:"required" example:"HAPPY2024"` - - // @Summary Authentication information - // @Description Authentication information - // @JSONSchema required - *Auth `json:",inline" bson:",inline"` -} - func ParseUseGiftCodeReq(c *gin.Context) (*UseGiftCodeReq, error) { useGiftCode := &UseGiftCodeReq{} if err := c.ShouldBindJSON(useGiftCode); err != nil { @@ -511,24 +546,6 @@ func ParseUseGiftCodeReq(c *gin.Context) (*UseGiftCodeReq, error) { return useGiftCode, nil } -type UserUsageReq struct { - // @Summary Start and end time for the request - // @Description Start and end time for the request - // @JSONSchema required - TimeRange `json:",inline" bson:",inline"` - - // @Summary Authentication information - // @Description Authentication information - // @JSONSchema required - *Auth `json:",inline" bson:",inline"` - - // NamespaceList - // @Summary Namespace list - // @Description Namespace list - // @JSONSchema - NamespaceList []string `json:"namespaceList" bson:"namespaceList" example:"[\"ns-admin\",\"ns-test1\"]"` -} - func ParseUserUsageReq(c *gin.Context) (*UserUsageReq, error) { userUsage := &UserUsageReq{} if err := c.ShouldBindJSON(userUsage); err != nil {