MSP-5223 update response CreateUpdateConfig: add IsChanged field

This commit is contained in:
Кузнецов Иван Александрович committed 2025-04-16 16:05:17 +03:00
1 parent 28be68322c
commit 3c19a8d532
4 files changed
+30 -18

No files matched your search

+5 -5
View File
@@ -16,7 +16,7 @@ import (
type ConfigService interface {
GetActiveConfigByModuleName(ctx context.Context, moduleName string) (*domain.Config, error)
GetConfigsByModuleId(ctx context.Context, moduleId string) ([]domain.Config, error)
CreateUpdateConfig(ctx context.Context, adminId int, req domain.CreateUpdateConfigRequest) (*domain.Config, error)
CreateUpdateConfig(ctx context.Context, adminId int, req domain.CreateUpdateConfigRequest) (*domain.CreateUpdateConfigResponse, error)
GetConfigById(ctx context.Context, configId string) (*domain.Config, error)
MarkConfigAsActive(ctx context.Context, configId string) error
DeleteConfig(ctx context.Context, id string) error
@@ -96,7 +96,7 @@ func (c Config) GetConfigsByModuleId(ctx context.Context, req domain.GetByModule
// @Accept json
// @Produce json
// @Param body body domain.CreateUpdateConfigRequest true "объект для сохранения"
// @Success 200 {object} domain.Config
// @Success 200 {object} domain.CreateUpdateConfigResponse
// @Failure 400 {object} apierrors.Error "`errorCode: 2003` - конфиг не соотвествует текущей схеме<br/>`errorCode: 2002` - указанного id не сущесвует<br/>`errorCode: 2004` - кто-то уже обновил конфигурацию<br/>`errorCode: 2005` - схема конфигурации не найдена<br/>"
// @Failure 500 {object} apierrors.Error
// @Router /config/create_update_config [POST]
@@ -104,14 +104,14 @@ func (c Config) CreateUpdateConfig(
ctx context.Context,
authData grpc.AuthData,
req domain.CreateUpdateConfigRequest,
) (*domain.Config, error) {
) (*domain.CreateUpdateConfigResponse, error) {
adminIdValue, _ := grpc.StringFromMd("x-admin-id", metadata.MD(authData))
var adminId int
if adminIdValue != "" {
adminId, _ = strconv.Atoi(adminIdValue)
}
config, err := c.service.CreateUpdateConfig(ctx, adminId, req)
resp, err := c.service.CreateUpdateConfig(ctx, adminId, req)
var validationError domain.ConfigValidationError
switch {
@@ -146,7 +146,7 @@ func (c Config) CreateUpdateConfig(
case err != nil:
return nil, apierrors.NewInternalServiceError(err)
default:
return config, nil
return resp, nil
}
}
+8 -1
View File
@@ -93,6 +93,13 @@ definitions:
- moduleId
- name
type: object
domain.CreateUpdateConfigResponse:
properties:
config:
$ref: '#/definitions/domain.Config'
isChanged:
type: boolean
type: object
domain.CreateVariableRequest:
properties:
description:
@@ -286,7 +293,7 @@ paths:
"200":
description: OK
schema:
$ref: '#/definitions/domain.Config'
$ref: '#/definitions/domain.CreateUpdateConfigResponse'
"400":
description: '`errorCode: 2003` - конфиг не соотвествует текущей схеме<br/>`errorCode:
2002` - указанного id не сущесвует<br/>`errorCode: 2004` - кто-то уже
+5
View File
@@ -10,6 +10,11 @@ type DeleteResponse struct {
Deleted int
}
type CreateUpdateConfigResponse struct {
IsChanged bool
Config Config
}
type ModuleInfo struct {
Id string
Name string
+12 -12
View File
@@ -121,7 +121,7 @@ func (c Config) CreateUpdateConfig(
ctx context.Context,
adminId int,
req domain.CreateUpdateConfigRequest,
) (*domain.Config, error) {
) (*domain.CreateUpdateConfigResponse, error) {
if !req.Unsafe {
err := c.validateConfigUpdate(ctx, req.ModuleId, req.Data)
if err != nil {
@@ -130,7 +130,9 @@ func (c Config) CreateUpdateConfig(
}
if req.Id == "" {
return c.insertNewConfig(ctx, adminId, req)
newConfig, err := c.insertNewConfig(ctx, adminId, req)
return &domain.CreateUpdateConfigResponse{
Config: newConfig, IsChanged: true}, err
}
variableExtractionResult, err := c.extractAndCheckVariables(ctx, req.Id, req.Data)
@@ -152,8 +154,7 @@ func (c Config) CreateUpdateConfig(
}
if isMatch {
c.logger.Info(ctx, "no changes in config; skip update")
result := configToDto(*oldConfig, nil)
return &result, nil
return &domain.CreateUpdateConfigResponse{Config: configToDto(*oldConfig, nil), IsChanged: false}, nil
}
config := entity.Config{
@@ -175,7 +176,6 @@ func (c Config) CreateUpdateConfig(
if err != nil {
return nil, errors.WithMessage(err, "change config history")
}
err = c.variableService.SaveVariableLinks(ctx, config.Id, variableExtractionResult.VariableLinks)
if err != nil {
return nil, errors.WithMessage(err, "save variable links")
@@ -196,8 +196,8 @@ func (c Config) CreateUpdateConfig(
Version: req.Version + 1,
Active: oldConfig.Active,
}
result := configToDto(newConfig, nil)
return &result, nil
return &domain.CreateUpdateConfigResponse{
Config: configToDto(newConfig, nil), IsChanged: true}, nil
}
func (c Config) GetConfigById(ctx context.Context, configId string) (*domain.Config, error) {
@@ -287,7 +287,7 @@ func (c Config) SyncConfig(ctx context.Context, moduleName string) error {
return nil
}
func (c Config) insertNewConfig(ctx context.Context, adminId int, req domain.CreateUpdateConfigRequest) (*domain.Config, error) {
func (c Config) insertNewConfig(ctx context.Context, adminId int, req domain.CreateUpdateConfigRequest) (domain.Config, error) {
config := entity.Config{
Id: uuid.NewString(),
Name: req.Name,
@@ -299,20 +299,20 @@ func (c Config) insertNewConfig(ctx context.Context, adminId int, req domain.Cre
}
err := c.configRepo.Insert(ctx, config)
if err != nil {
return nil, errors.WithMessage(err, "insert new config")
return domain.Config{}, errors.WithMessage(err, "insert new config")
}
extractResult, err := c.extractAndCheckVariables(ctx, config.Id, config.Data)
if err != nil {
return nil, err
return domain.Config{}, err
}
err = c.variableService.SaveVariableLinks(ctx, config.Id, extractResult.VariableLinks)
if err != nil {
return nil, errors.WithMessage(err, "save variable links")
return domain.Config{}, errors.WithMessage(err, "save variable links")
}
result := configToDto(config, nil)
return &result, nil
return result, nil
}
func (c Config) extractAndCheckVariables(ctx context.Context, configId string, input []byte) (*domain.VariableExtractionResult, error) {