fix: add plugin id conflict checker (#78)
This commit is contained in:
@@ -18,9 +18,12 @@ package plugin
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"gorm.io/gorm"
|
||||
|
||||
"github.com/coze-dev/coze-studio/backend/domain/plugin/conf"
|
||||
pluginConf "github.com/coze-dev/coze-studio/backend/domain/plugin/conf"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/plugin/repository"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/plugin/service"
|
||||
@@ -28,6 +31,9 @@ import (
|
||||
user "github.com/coze-dev/coze-studio/backend/domain/user/service"
|
||||
"github.com/coze-dev/coze-studio/backend/infra/contract/idgen"
|
||||
"github.com/coze-dev/coze-studio/backend/infra/contract/storage"
|
||||
"github.com/coze-dev/coze-studio/backend/pkg/errorx"
|
||||
"github.com/coze-dev/coze-studio/backend/pkg/lang/slices"
|
||||
"github.com/coze-dev/coze-studio/backend/types/errno"
|
||||
)
|
||||
|
||||
type ServiceComponents struct {
|
||||
@@ -68,6 +74,11 @@ func InitService(ctx context.Context, components *ServiceComponents) (*PluginApp
|
||||
OAuthRepo: oauthRepo,
|
||||
})
|
||||
|
||||
err = checkIDExist(ctx, pluginSVC)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
PluginApplicationSVC.DomainSVC = pluginSVC
|
||||
PluginApplicationSVC.eventbus = components.EventBus
|
||||
PluginApplicationSVC.oss = components.OSS
|
||||
@@ -77,3 +88,51 @@ func InitService(ctx context.Context, components *ServiceComponents) (*PluginApp
|
||||
|
||||
return PluginApplicationSVC, nil
|
||||
}
|
||||
|
||||
func checkIDExist(ctx context.Context, pluginService service.PluginService) error {
|
||||
pluginProducts := conf.GetAllPluginProducts()
|
||||
|
||||
pluginIDs := make([]int64, 0, len(pluginProducts))
|
||||
var toolIDs []int64
|
||||
for _, p := range pluginProducts {
|
||||
pluginIDs = append(pluginIDs, p.Info.ID)
|
||||
toolIDs = append(toolIDs, p.ToolIDs...)
|
||||
}
|
||||
|
||||
pluginInfos, err := pluginService.MGetDraftPlugins(ctx, pluginIDs)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(pluginInfos) > 0 {
|
||||
conflictsIDs := make([]int64, 0, len(pluginInfos))
|
||||
for _, p := range pluginInfos {
|
||||
conflictsIDs = append(conflictsIDs, p.ID)
|
||||
}
|
||||
|
||||
return errorx.New(errno.ErrPluginIDExist,
|
||||
errorx.KV("plugin_id", strings.Join(slices.Transform(conflictsIDs, func(id int64) string {
|
||||
return strconv.FormatInt(id, 10)
|
||||
}), ",")),
|
||||
)
|
||||
}
|
||||
|
||||
tools, err := pluginService.MGetDraftTools(ctx, toolIDs)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(tools) > 0 {
|
||||
conflictsIDs := make([]int64, 0, len(tools))
|
||||
for _, t := range tools {
|
||||
conflictsIDs = append(conflictsIDs, t.ID)
|
||||
}
|
||||
|
||||
return errorx.New(errno.ErrToolIDExist,
|
||||
errorx.KV("tool_id", strings.Join(slices.Transform(conflictsIDs, func(id int64) string {
|
||||
return strconv.FormatInt(id, 10)
|
||||
}), ",")),
|
||||
)
|
||||
}
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user