feat: add subscription managment (#1858)

* feat: subscription managment

* fix: remove console log

* fix: webhooks

* fix linter

* fix: fix via gofumpt

* fix: review changes

* fix: Copyright 2023

* Update account.go

---------

Co-authored-by: hsluoyz <hsluoyz@qq.com>
This commit is contained in:
Ilya Sulimanov
2023-05-20 10:56:21 +03:00
committed by GitHub
parent 319031da28
commit 88c0856d17
39 changed files with 4773 additions and 13 deletions

View File

@ -240,6 +240,21 @@ func (a *Adapter) createTable() {
if err != nil {
panic(err)
}
err = a.Engine.Sync2(new(Subscription))
if err != nil {
panic(err)
}
err = a.Engine.Sync2(new(Plan))
if err != nil {
panic(err)
}
err = a.Engine.Sync2(new(Pricing))
if err != nil {
panic(err)
}
}
func GetSession(owner string, offset, limit int, field, value, sortField, sortOrder string) *xorm.Session {

145
object/plan.go Normal file
View File

@ -0,0 +1,145 @@
// Copyright 2023 The Casdoor Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package object
import (
"fmt"
"github.com/casdoor/casdoor/util"
"github.com/xorm-io/core"
)
type Plan struct {
Owner string `xorm:"varchar(100) notnull pk" json:"owner"`
Name string `xorm:"varchar(100) notnull pk" json:"name"`
CreatedTime string `xorm:"varchar(100)" json:"createdTime"`
DisplayName string `xorm:"varchar(100)" json:"displayName"`
Description string `xorm:"varchar(100)" json:"description"`
PricePerMonth float64 `json:"pricePerMonth"`
PricePerYear float64 `json:"pricePerYear"`
Currency string `xorm:"varchar(100)" json:"currency"`
IsEnabled bool `json:"isEnabled"`
Role string `xorm:"varchar(100)" json:"role"`
Options []string `xorm:"-" json:"options"`
}
func GetPlanCount(owner, field, value string) int {
session := GetSession(owner, -1, -1, field, value, "", "")
count, err := session.Count(&Plan{})
if err != nil {
panic(err)
}
return int(count)
}
func GetPlans(owner string) []*Plan {
plans := []*Plan{}
err := adapter.Engine.Desc("created_time").Find(&plans, &Plan{Owner: owner})
if err != nil {
panic(err)
}
return plans
}
func GetPaginatedPlans(owner string, offset, limit int, field, value, sortField, sortOrder string) []*Plan {
plans := []*Plan{}
session := GetSession(owner, offset, limit, field, value, sortField, sortOrder)
err := session.Find(&plans)
if err != nil {
panic(err)
}
return plans
}
func getPlan(owner, name string) *Plan {
if owner == "" || name == "" {
return nil
}
plan := Plan{Owner: owner, Name: name}
existed, err := adapter.Engine.Get(&plan)
if err != nil {
panic(err)
}
if existed {
return &plan
} else {
return nil
}
}
func GetPlan(id string) *Plan {
owner, name := util.GetOwnerAndNameFromId(id)
return getPlan(owner, name)
}
func UpdatePlan(id string, plan *Plan) bool {
owner, name := util.GetOwnerAndNameFromId(id)
if getPlan(owner, name) == nil {
return false
}
affected, err := adapter.Engine.ID(core.PK{owner, name}).AllCols().Update(plan)
if err != nil {
panic(err)
}
return affected != 0
}
func AddPlan(plan *Plan) bool {
affected, err := adapter.Engine.Insert(plan)
if err != nil {
panic(err)
}
return affected != 0
}
func DeletePlan(plan *Plan) bool {
affected, err := adapter.Engine.ID(core.PK{plan.Owner, plan.Name}).Delete(plan)
if err != nil {
panic(err)
}
return affected != 0
}
func (plan *Plan) GetId() string {
return fmt.Sprintf("%s/%s", plan.Owner, plan.Name)
}
func Subscribe(owner string, user string, plan string, pricing string) *Subscription {
selectedPricing := GetPricing(fmt.Sprintf("%s/%s", owner, pricing))
valid := selectedPricing != nil && selectedPricing.IsEnabled
if !valid {
return nil
}
planBelongToPricing := selectedPricing.HasPlan(owner, plan)
if planBelongToPricing {
newSubscription := NewSubscription(owner, user, plan, selectedPricing.TrialDuration)
affected := AddSubscription(newSubscription)
if affected {
return newSubscription
}
}
return nil
}

147
object/pricing.go Normal file
View File

@ -0,0 +1,147 @@
// Copyright 2023 The Casdoor Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package object
import (
"fmt"
"strings"
"github.com/casdoor/casdoor/util"
"github.com/xorm-io/core"
)
type Pricing struct {
Owner string `xorm:"varchar(100) notnull pk" json:"owner"`
Name string `xorm:"varchar(100) notnull pk" json:"name"`
CreatedTime string `xorm:"varchar(100)" json:"createdTime"`
DisplayName string `xorm:"varchar(100)" json:"displayName"`
Description string `xorm:"varchar(100)" json:"description"`
Plans []string `xorm:"mediumtext" json:"plans"`
IsEnabled bool `json:"isEnabled"`
HasTrial bool `json:"hasTrial"`
TrialDuration int `json:"trialDuration"`
Application string `xorm:"varchar(100)" json:"application"`
Submitter string `xorm:"varchar(100)" json:"submitter"`
Approver string `xorm:"varchar(100)" json:"approver"`
ApproveTime string `xorm:"varchar(100)" json:"approveTime"`
State string `xorm:"varchar(100)" json:"state"`
}
func GetPricingCount(owner, field, value string) int {
session := GetSession(owner, -1, -1, field, value, "", "")
count, err := session.Count(&Pricing{})
if err != nil {
panic(err)
}
return int(count)
}
func GetPricings(owner string) []*Pricing {
pricings := []*Pricing{}
err := adapter.Engine.Desc("created_time").Find(&pricings, &Pricing{Owner: owner})
if err != nil {
panic(err)
}
return pricings
}
func GetPaginatedPricings(owner string, offset, limit int, field, value, sortField, sortOrder string) []*Pricing {
pricings := []*Pricing{}
session := GetSession(owner, offset, limit, field, value, sortField, sortOrder)
err := session.Find(&pricings)
if err != nil {
panic(err)
}
return pricings
}
func getPricing(owner, name string) *Pricing {
if owner == "" || name == "" {
return nil
}
pricing := Pricing{Owner: owner, Name: name}
existed, err := adapter.Engine.Get(&pricing)
if err != nil {
panic(err)
}
if existed {
return &pricing
} else {
return nil
}
}
func GetPricing(id string) *Pricing {
owner, name := util.GetOwnerAndNameFromId(id)
return getPricing(owner, name)
}
func UpdatePricing(id string, pricing *Pricing) bool {
owner, name := util.GetOwnerAndNameFromId(id)
if getPricing(owner, name) == nil {
return false
}
affected, err := adapter.Engine.ID(core.PK{owner, name}).AllCols().Update(pricing)
if err != nil {
panic(err)
}
return affected != 0
}
func AddPricing(pricing *Pricing) bool {
affected, err := adapter.Engine.Insert(pricing)
if err != nil {
panic(err)
}
return affected != 0
}
func DeletePricing(pricing *Pricing) bool {
affected, err := adapter.Engine.ID(core.PK{pricing.Owner, pricing.Name}).Delete(pricing)
if err != nil {
panic(err)
}
return affected != 0
}
func (pricing *Pricing) GetId() string {
return fmt.Sprintf("%s/%s", pricing.Owner, pricing.Name)
}
func (pricing *Pricing) HasPlan(owner string, plan string) bool {
selectedPlan := GetPlan(fmt.Sprintf("%s/%s", owner, plan))
if selectedPlan == nil {
return false
}
result := false
for _, pricingPlan := range pricing.Plans {
if strings.Contains(pricingPlan, selectedPlan.Name) {
result = true
break
}
}
return result
}

154
object/subscription.go Normal file
View File

@ -0,0 +1,154 @@
// Copyright 2023 The Casdoor Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package object
import (
"fmt"
"time"
"github.com/casdoor/casdoor/util"
"github.com/xorm-io/core"
)
const defaultStatus = "Pending"
type Subscription struct {
Owner string `xorm:"varchar(100) notnull pk" json:"owner"`
Name string `xorm:"varchar(100) notnull pk" json:"name"`
CreatedTime string `xorm:"varchar(100)" json:"createdTime"`
DisplayName string `xorm:"varchar(100)" json:"displayName"`
Duration int `json:"duration"`
Description string `xorm:"varchar(100)" json:"description"`
Plan string `xorm:"varchar(100)" json:"plan"`
StartDate time.Time `json:"startDate"`
EndDate time.Time `json:"endDate"`
User string `xorm:"mediumtext" json:"user"`
IsEnabled bool `json:"isEnabled"`
Submitter string `xorm:"varchar(100)" json:"submitter"`
Approver string `xorm:"varchar(100)" json:"approver"`
ApproveTime string `xorm:"varchar(100)" json:"approveTime"`
State string `xorm:"varchar(100)" json:"state"`
}
func NewSubscription(owner string, user string, plan string, duration int) *Subscription {
id := util.GenerateId()[:6]
return &Subscription{
Name: "Subscription_" + id,
DisplayName: "New Subscription - " + id,
Owner: owner,
User: owner + "/" + user,
Plan: owner + "/" + plan,
CreatedTime: util.GetCurrentTime(),
State: defaultStatus,
Duration: duration,
StartDate: time.Now(),
EndDate: time.Now().AddDate(0, 0, duration),
}
}
func GetSubscriptionCount(owner, field, value string) int {
session := GetSession(owner, -1, -1, field, value, "", "")
count, err := session.Count(&Subscription{})
if err != nil {
panic(err)
}
return int(count)
}
func GetSubscriptions(owner string) []*Subscription {
subscriptions := []*Subscription{}
err := adapter.Engine.Desc("created_time").Find(&subscriptions, &Subscription{Owner: owner})
if err != nil {
panic(err)
}
return subscriptions
}
func GetPaginationSubscriptions(owner string, offset, limit int, field, value, sortField, sortOrder string) []*Subscription {
subscriptions := []*Subscription{}
session := GetSession(owner, offset, limit, field, value, sortField, sortOrder)
err := session.Find(&subscriptions)
if err != nil {
panic(err)
}
return subscriptions
}
func getSubscription(owner string, name string) *Subscription {
if owner == "" || name == "" {
return nil
}
subscription := Subscription{Owner: owner, Name: name}
existed, err := adapter.Engine.Get(&subscription)
if err != nil {
panic(err)
}
if existed {
return &subscription
} else {
return nil
}
}
func GetSubscription(id string) *Subscription {
owner, name := util.GetOwnerAndNameFromId(id)
return getSubscription(owner, name)
}
func UpdateSubscription(id string, subscription *Subscription) bool {
owner, name := util.GetOwnerAndNameFromId(id)
if getSubscription(owner, name) == nil {
return false
}
affected, err := adapter.Engine.ID(core.PK{owner, name}).AllCols().Update(subscription)
if err != nil {
panic(err)
}
return affected != 0
}
func AddSubscription(subscription *Subscription) bool {
affected, err := adapter.Engine.Insert(subscription)
if err != nil {
panic(err)
}
return affected != 0
}
func DeleteSubscription(subscription *Subscription) bool {
affected, err := adapter.Engine.ID(core.PK{subscription.Owner, subscription.Name}).Delete(&Subscription{})
if err != nil {
panic(err)
}
return affected != 0
}
func (subscription *Subscription) GetId() string {
return fmt.Sprintf("%s/%s", subscription.Owner, subscription.Name)
}