Skip to content

Commit a8ee367

Browse files
authored
feat(account service): get pricing/role for creating a workspace plan (#6753)
* optimize preview new workspace plan amount * get workspace subscription info api add role info
1 parent b9164c8 commit a8ee367

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-13
lines changed

service/account/api/workspace_subscription.go

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ func GetWorkspaceSubscriptionInfo(c *gin.Context) {
9696
*types.WorkspaceSubscription
9797
Type string `json:"type"`
9898
InvoiceInfo *InvoiceInfo `json:"InvoiceInfo,omitempty"`
99+
Role string `json:"role,omitempty"`
99100
}{
100101
WorkspaceSubscription: subscription,
101102
Type: WorkspaceTypeSubscription,
@@ -195,6 +196,13 @@ func GetWorkspaceSubscriptionInfo(c *gin.Context) {
195196
)
196197
}
197198
}
199+
// Get user workspace role
200+
userRole, err := dao.DBClient.GetUserWorkspaceRole(req.UserUID, req.Workspace)
201+
if err != nil {
202+
dao.Logger.Errorf("failed to get user workspace role: %v", err)
203+
// Continue processing, role will be empty string
204+
}
205+
workspaceSubInfo.Role = string(userRole)
198206
}
199207

200208
c.JSON(http.StatusOK, gin.H{
@@ -668,12 +676,14 @@ func GetWorkspaceSubscriptionUpgradeAmount(c *gin.Context) {
668676
)
669677
return
670678
}
671-
if err := authenticateWorkspaceSubscriptionOperatorRequest(c, req); err != nil {
672-
c.JSON(
673-
http.StatusUnauthorized,
674-
helper.ErrorMessage{Error: fmt.Sprintf("authenticate error : %v", err)},
675-
)
676-
return
679+
if req.Workspace != "" {
680+
if err := authenticateWorkspaceSubscriptionOperatorRequest(c, req); err != nil {
681+
c.JSON(
682+
http.StatusUnauthorized,
683+
helper.ErrorMessage{Error: fmt.Sprintf("authenticate error : %v", err)},
684+
)
685+
return
686+
}
677687
}
678688

679689
// Validate promotion code once at the beginning and reuse the result throughout the method
@@ -739,7 +749,7 @@ func GetWorkspaceSubscriptionUpgradeAmount(c *gin.Context) {
739749

740750
// Handle subscription creation
741751
if req.Operator == types.SubscriptionTransactionTypeCreated ||
742-
currentSubscription.PlanName == types.FreeSubscriptionPlanName {
752+
currentSubscription.PlanName == types.FreeSubscriptionPlanName || req.Workspace == "" {
743753
handleSubscriptionCreation(c, req, validatedPromotionCode)
744754
return
745755
}

service/account/helper/request.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -704,9 +704,9 @@ type WorkspaceSubscriptionOperatorReq struct {
704704
AuthBase `json:",inline" bson:",inline"`
705705

706706
// @Summary Workspace name
707-
// @Description Workspace name
708-
// @JSONSchema required
709-
Workspace string `json:"workspace" bson:"workspace" binding:"required" example:"my-workspace"`
707+
// @Description Workspace name (optional for price preview, required for actual subscription)
708+
// @JSONSchema optional
709+
Workspace string `json:"workspace,omitempty" bson:"workspace" example:"my-workspace"`
710710

711711
// @Summary Region domain
712712
// @Description Region domain
@@ -812,9 +812,10 @@ func ParseWorkspaceSubscriptionOperatorReq(
812812
if err := c.ShouldBindJSON(req); err != nil {
813813
return nil, fmt.Errorf("bind json error: %w", err)
814814
}
815-
if req.Workspace == "" {
816-
return nil, errors.New("workspace cannot be empty")
817-
}
815+
// Workspace is optional for price preview scenarios
816+
// if req.Workspace == "" {
817+
// return nil, errors.New("workspace cannot be empty")
818+
// }
818819
if req.RegionDomain == "" {
819820
return nil, errors.New("regionDomain cannot be empty")
820821
}

0 commit comments

Comments
 (0)