Skip to content

Commit

Permalink
feat(api-gql): check command existence by unique name
Browse files Browse the repository at this point in the history
  • Loading branch information
Satont committed May 10, 2024
1 parent fc4da70 commit 1489aab
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 16 deletions.
26 changes: 26 additions & 0 deletions apps/api-gql/internal/gql/resolvers/commands.resolver.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

69 changes: 69 additions & 0 deletions apps/api-gql/internal/gql/resolvers/commands.resolver.service.go
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
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ref, toRaw } from 'vue'
import { useI18n } from 'vue-i18n'

import type { CommandsCreateOpts } from '@/gql/graphql'
import type { CombinedError } from '@urql/vue'

import { useCommandsApi } from '@/api/commands/commands.js'
import { useToast } from '@/components/ui/toast'
Expand Down Expand Up @@ -90,24 +91,42 @@ export const useCommandEdit = createGlobalState(() => {
})),
}

if (formValue.value.id) {
await update.executeMutation({
id: formValue.value.id,
opts: transformedOpts,
})
} else {
await create.executeMutation({
opts: transformedOpts,
let combinedError: CombinedError | undefined
try {
if (formValue.value.id) {
const { error } = await update.executeMutation({
id: formValue.value.id,
opts: transformedOpts,
})
combinedError = error
} else {
const { error } = await create.executeMutation({
opts: transformedOpts,
})
combinedError = error
}

if (combinedError) {
throw combinedError
}

close()
toast({
title: t('sharedTexts.saved'),
variant: 'success',
duration: 1500,
})
} catch (e) {
if (combinedError) {
if (combinedError.graphQLErrors.some(e => e.extensions.code === 'ALREADY_EXISTS')) {
toast({
title: 'Command with this name or aliase already exists',
variant: 'destructive',
duration: 5000,
})
}
}
}

close()

toast({
title: t('sharedTexts.saved'),
variant: 'success',
duration: 1500,
})
}

return {
Expand Down

0 comments on commit 1489aab

Please sign in to comment.