-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api-gql): check command existence by unique name
- Loading branch information
Showing
3 changed files
with
130 additions
and
16 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
69 changes: 69 additions & 0 deletions
69
apps/api-gql/internal/gql/resolvers/commands.resolver.service.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package resolvers | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"slices" | ||
|
||
model "github.com/satont/twir/libs/gomodels" | ||
"github.com/vektah/gqlparser/v2/gqlerror" | ||
) | ||
|
||
var commandAlreadyExistsError = gqlerror.Error{ | ||
Message: "command with this name or aliase already exists", | ||
Path: nil, | ||
Locations: nil, | ||
Extensions: map[string]interface{}{ | ||
"code": "ALREADY_EXISTS", | ||
}, | ||
} | ||
|
||
func (r *Resolver) checkIsCommandWithNameOrAliaseExists( | ||
ctx context.Context, | ||
commandId *string, | ||
name string, | ||
aliases []string, | ||
) error { | ||
dashboardId, err := r.sessions.GetSelectedDashboard(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var existedCommands []model.ChannelsCommands | ||
|
||
query := r.gorm.WithContext(ctx). | ||
Where(`"channelId" = ?`, dashboardId). | ||
Select([]string{"id", "name", "aliases", `"channelId"`}) | ||
|
||
if commandId != nil { | ||
query = query.Not(`"id" = ?`, *commandId) | ||
} | ||
|
||
if err := query. | ||
Find(&existedCommands).Error; err != nil { | ||
return fmt.Errorf("failed to get existed commands: %w", err) | ||
} | ||
|
||
for _, existedCommand := range existedCommands { | ||
if existedCommand.Name == name { | ||
return &commandAlreadyExistsError | ||
} | ||
for _, aliase := range existedCommand.Aliases { | ||
if aliase == name { | ||
return &commandAlreadyExistsError | ||
} | ||
} | ||
|
||
for _, aliase := range aliases { | ||
if existedCommand.Name == aliase { | ||
return &commandAlreadyExistsError | ||
} | ||
|
||
if slices.Contains(existedCommand.Aliases, aliase) { | ||
return &commandAlreadyExistsError | ||
} | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters