133 lines
3.1 KiB
Go
133 lines
3.1 KiB
Go
package privileges
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
)
|
|
|
|
type Privilege struct {
|
|
ID string `json:"_id"`
|
|
PrivilegeID string `json:"privilegeId"`
|
|
Name string `json:"name"`
|
|
ServiceKey string `json:"serviceKey"`
|
|
Description string `json:"description"`
|
|
Type string `json:"type"`
|
|
Value string `json:"value"`
|
|
Price float64 `json:"price"`
|
|
IsDeleted bool `json:"isDeleted"`
|
|
CreatedAt time.Time `json:"createdAt"`
|
|
UpdatedAt time.Time `json:"updatedAt"`
|
|
DeletedAt time.Time `json:"deletedAt"`
|
|
}
|
|
|
|
const skey = "templategen"
|
|
|
|
var (
|
|
Privileges = []Privilege{
|
|
{
|
|
PrivilegeID: "templateCnt",
|
|
Name: "Количество Шаблонов",
|
|
ServiceKey: skey,
|
|
Description: "Количество шаблонов, которые может сделать пользователь сервиса",
|
|
Type: "count",
|
|
Value: "шаблон",
|
|
},
|
|
{
|
|
PrivilegeID: "templateUnlimTime",
|
|
Name: "Безлимит",
|
|
ServiceKey: skey,
|
|
Description: "Количество дней, в течении которых пользование сервисом безлимитно",
|
|
Type: "day",
|
|
Value: "день",
|
|
},
|
|
{
|
|
PrivilegeID: "templateStorage",
|
|
Name: "Размер Диска",
|
|
ServiceKey: skey,
|
|
Description: "Обьём ПенаДиска для хранения шаблонов и результатов шаблонизации",
|
|
Type: "count",
|
|
Value: "МБ",
|
|
},
|
|
}
|
|
)
|
|
|
|
func PublishPrivileges(ctx context.Context, domain string) error {
|
|
old, err := GetActualPrivileges(ctx, domain)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if len(old) == 0 {
|
|
return setupActualPrivileges(ctx, Privileges, domain)
|
|
}
|
|
|
|
found := false
|
|
|
|
for _, oldPriv := range old {
|
|
for _, newPriv := range Privileges {
|
|
if newPriv.PrivilegeID == oldPriv.PrivilegeID {
|
|
found = true
|
|
}
|
|
}
|
|
if !found {
|
|
if err := removePrivilege(ctx, domain, oldPriv.PrivilegeID); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
found = false
|
|
}
|
|
|
|
return updatePrivileges(ctx, Privileges, domain)
|
|
}
|
|
|
|
func GetActualPrivileges(
|
|
_ context.Context,
|
|
domain string) ([]Privilege, error) {
|
|
res := []Privilege{}
|
|
_, _, err := fiber.Get(domain + "/privilege/service/templategen").Struct(&res)
|
|
if err != nil {
|
|
return res, err[0]
|
|
}
|
|
fmt.Println("str", res)
|
|
return res, nil
|
|
}
|
|
|
|
func updatePrivileges(_ context.Context, data []Privilege, domain string) error {
|
|
_, _, err := fiber.Put(domain + "/privilege/many").JSON(map[string][]Privilege{
|
|
"privilege": data,
|
|
}).Bytes()
|
|
if err != nil {
|
|
return err[0]
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func setupActualPrivileges(
|
|
_ context.Context,
|
|
data []Privilege,
|
|
domain string) error {
|
|
res := []Privilege{}
|
|
_, _, err := fiber.Post(domain + "/privilege/many").JSON(map[string][]Privilege{
|
|
"privilege": data,
|
|
}).Struct(&res)
|
|
|
|
if err != nil {
|
|
return err[0]
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func removePrivilege(_ context.Context, domain, id string) error {
|
|
_, _, err := fiber.Delete(domain + "/privilege/").JSON(map[string]string{"privilegeId": id}).Bytes()
|
|
if err != nil {
|
|
return err[0]
|
|
}
|
|
|
|
return nil
|
|
}
|