fix: add plugin id conflict checker (#78)
This commit is contained in:
@@ -60,6 +60,7 @@ import (
|
||||
"github.com/coze-dev/coze-studio/backend/pkg/lang/conv"
|
||||
"github.com/coze-dev/coze-studio/backend/pkg/lang/ptr"
|
||||
"github.com/coze-dev/coze-studio/backend/pkg/logs"
|
||||
"github.com/coze-dev/coze-studio/backend/pkg/safego"
|
||||
"github.com/coze-dev/coze-studio/backend/pkg/taskgroup"
|
||||
"github.com/coze-dev/coze-studio/backend/types/consts"
|
||||
"github.com/coze-dev/coze-studio/backend/types/errno"
|
||||
@@ -183,18 +184,19 @@ func (a *APPApplicationService) DraftProjectDelete(ctx context.Context, req *pro
|
||||
logs.CtxErrorf(ctx, "publish project '%d' failed, err=%v", req.ProjectID, err)
|
||||
}
|
||||
|
||||
err = a.deleteAPPResources(ctx, req.ProjectID)
|
||||
if err != nil {
|
||||
logs.CtxErrorf(ctx, "delete app '%d' resources failed, err=%v", req.ProjectID, err)
|
||||
}
|
||||
safego.Go(ctx, func() {
|
||||
// When an app is deleted, resource deletion is currently handled as a weak dependency, meaning some resources might not be deleted, but they will be inaccessible to the user.
|
||||
// TODO:: Application resources need to check the deletion status of the application
|
||||
a.deleteAPPResources(ctx, req.ProjectID)
|
||||
})
|
||||
|
||||
resp = &projectAPI.DraftProjectDeleteResponse{}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (a *APPApplicationService) deleteAPPResources(ctx context.Context, appID int64) (err error) {
|
||||
err = plugin.PluginApplicationSVC.DeleteAPPAllPlugins(ctx, appID)
|
||||
func (a *APPApplicationService) deleteAPPResources(ctx context.Context, appID int64) {
|
||||
err := plugin.PluginApplicationSVC.DeleteAPPAllPlugins(ctx, appID)
|
||||
if err != nil {
|
||||
logs.CtxErrorf(ctx, "delete app '%d' plugins failed, err=%v", appID, err)
|
||||
}
|
||||
@@ -218,8 +220,6 @@ func (a *APPApplicationService) deleteAPPResources(ctx context.Context, appID in
|
||||
if err != nil {
|
||||
logs.CtxErrorf(ctx, "delete app '%d' workflow failed, err=%v", appID, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *APPApplicationService) DraftProjectUpdate(ctx context.Context, req *projectAPI.DraftProjectUpdateRequest) (resp *projectAPI.DraftProjectUpdateResponse, err error) {
|
||||
|
||||
@@ -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