From ec9bc803921edd6b52a0b892f5eb9db52687f351 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Mon, 20 Feb 2023 22:30:18 +0100 Subject: [PATCH] Finished auto role rules #134 --- kdb-bot/src/bot_data/model/auto_role_rule.py | 6 +- .../mutations/auto_role_rule_mutation.py | 4 +- .../src/app/models/graphql/mutations.model.ts | 15 ++++- .../src/app/models/graphql/result.model.ts | 11 +++- .../auto-roles-rules.component.html | 2 +- .../auto-roles-rules.component.ts | 56 ++++++++++++------- kdb-web/src/assets/i18n/de.json | 24 ++++---- 7 files changed, 81 insertions(+), 37 deletions(-) diff --git a/kdb-bot/src/bot_data/model/auto_role_rule.py b/kdb-bot/src/bot_data/model/auto_role_rule.py index 3630f134..34188caa 100644 --- a/kdb-bot/src/bot_data/model/auto_role_rule.py +++ b/kdb-bot/src/bot_data/model/auto_role_rule.py @@ -89,7 +89,7 @@ class AutoRoleRule(TableABC): INSERT INTO `AutoRoleRules` ( `AutoRoleId`, `DiscordEmojiName`, `DiscordRoleId`, `CreatedAt`, `LastModifiedAt` ) VALUES ( - {self._auto_role}, + {self._auto_role.id}, '{self._discord_emoji_name}', {self._discord_role_id}, '{self._created_at}', @@ -103,8 +103,8 @@ class AutoRoleRule(TableABC): return str( f""" UPDATE `AutoRoleRules` - SET `AutoRoleId` = {self._auto_role}, - `DiscordEmojiName` = {self._discord_emoji_name}, + SET `AutoRoleId` = {self._auto_role.id}, + `DiscordEmojiName` = '{self._discord_emoji_name}', `DiscordRoleId` = {self._discord_role_id}, `LastModifiedAt` = '{self._modified_at}' WHERE `AutoRoleRuleId` = {self._auto_role_rule_id}; diff --git a/kdb-bot/src/bot_graphql/mutations/auto_role_rule_mutation.py b/kdb-bot/src/bot_graphql/mutations/auto_role_rule_mutation.py index b6b6a943..76dce97c 100644 --- a/kdb-bot/src/bot_graphql/mutations/auto_role_rule_mutation.py +++ b/kdb-bot/src/bot_graphql/mutations/auto_role_rule_mutation.py @@ -35,9 +35,9 @@ class AutoRoleRuleMutation(QueryABC): def get_new(x: AutoRoleRule): return ( - x.auto_role.id == input["autoRoleId"] + x.auto_role.id == int(input["autoRoleId"]) and x.emoji_name == input["emojiName"] - and x.role_id == input["roleId"] + and x.role_id == int(input["roleId"]) ) return self._auto_roles.get_auto_role_rules_by_auto_role_id(auto_role_rule.auto_role.id).where(get_new).last() diff --git a/kdb-web/src/app/models/graphql/mutations.model.ts b/kdb-web/src/app/models/graphql/mutations.model.ts index 310f300f..4d10e87d 100644 --- a/kdb-web/src/app/models/graphql/mutations.model.ts +++ b/kdb-web/src/app/models/graphql/mutations.model.ts @@ -42,7 +42,7 @@ export class Mutations { `; static createAutoRoleRule = ` - mutation createAutoRoleRule($autoRoleId: ID, $emojiName: String, roleId: String) { + mutation createAutoRoleRule($autoRoleId: ID, $emojiName: String, $roleId: String) { autoRoleRule { createAutoRoleRule(input: { autoRoleId: $autoRoleId, emojiName: $emojiName, roleId: $roleId }) { id @@ -54,6 +54,19 @@ export class Mutations { } `; + static updateAutoRoleRule = ` + mutation updateAutoRoleRule($id: ID, $emojiName: String, $roleId: String) { + autoRoleRule { + updateAutoRoleRule(input: { id: $id, emojiName: $emojiName, roleId: $roleId }) { + id + emojiName + roleId + roleName + } + } + } + `; + static deleteAutoRoleRule = ` mutation deleteAutoRoleRule($id: ID) { autoRoleRule { diff --git a/kdb-web/src/app/models/graphql/result.model.ts b/kdb-web/src/app/models/graphql/result.model.ts index 8198fda2..5ad5cd8a 100644 --- a/kdb-web/src/app/models/graphql/result.model.ts +++ b/kdb-web/src/app/models/graphql/result.model.ts @@ -1,5 +1,5 @@ import { User } from "../data/user.model"; -import { AutoRole } from "../data/auto_role.model"; +import { AutoRole, AutoRoleRule } from "../data/auto_role.model"; export interface GraphQLResult { data: any; @@ -15,6 +15,7 @@ export interface UpdateUserMutationResult { updateUser: User }; } + export interface AutoRoleMutationResult { autoRole: { createAutoRole?: AutoRole @@ -22,3 +23,11 @@ export interface AutoRoleMutationResult { deleteAutoRole?: AutoRole }; } + +export interface AutoRoleRuleMutationResult { + autoRoleRule: { + createAutoRoleRule?: AutoRoleRule + updateAutoRoleRule?: AutoRoleRule + deleteAutoRoleRule?: AutoRoleRule + }; +} diff --git a/kdb-web/src/app/modules/view/server/auto-role/components/auto-roles-rules/auto-roles-rules.component.html b/kdb-web/src/app/modules/view/server/auto-role/components/auto-roles-rules/auto-roles-rules.component.html index fd1a4536..12176fec 100644 --- a/kdb-web/src/app/modules/view/server/auto-role/components/auto-roles-rules/auto-roles-rules.component.html +++ b/kdb-web/src/app/modules/view/server/auto-role/components/auto-roles-rules/auto-roles-rules.component.html @@ -102,7 +102,7 @@ - diff --git a/kdb-web/src/app/modules/view/server/auto-role/components/auto-roles-rules/auto-roles-rules.component.ts b/kdb-web/src/app/modules/view/server/auto-role/components/auto-roles-rules/auto-roles-rules.component.ts index cca972a0..48aa83f4 100644 --- a/kdb-web/src/app/modules/view/server/auto-role/components/auto-roles-rules/auto-roles-rules.component.ts +++ b/kdb-web/src/app/modules/view/server/auto-role/components/auto-roles-rules/auto-roles-rules.component.ts @@ -19,7 +19,7 @@ import { Queries } from "../../../../../../models/graphql/queries.model"; import { Server } from "../../../../../../models/data/server.model"; import { catchError, debounceTime } from "rxjs/operators"; import { Table } from "primeng/table"; -import { AutoRoleMutationResult } from "../../../../../../models/graphql/result.model"; +import { AutoRoleMutationResult, AutoRoleRuleMutationResult } from "../../../../../../models/graphql/result.model"; import { Mutations } from "../../../../../../models/graphql/mutations.model"; import { throwError } from "rxjs"; @@ -32,8 +32,8 @@ export class AutoRolesRulesComponent implements OnInit { rules!: AutoRoleRule[]; guild!: Guild; - emojis!: MenuItem[]; - roles!: MenuItem[]; + emojis: MenuItem[] = []; + roles: MenuItem[] = []; loading = true; autoRoleId!: number; @@ -80,7 +80,7 @@ export class AutoRolesRulesComponent implements OnInit { } getEmojiUrl(name: string): string { - return this.guild.emojis.filter(x => x.name == name)[0].url ?? ""; + return this.guild?.emojis.filter(x => x.name == name)[0].url ?? ""; } ngOnInit(): void { @@ -191,30 +191,50 @@ export class AutoRolesRulesComponent implements OnInit { this.clonedUsers[index] = { ...autoRoleRule }; } - onRowEditSave(table: Table, newAutoRole: AutoRoleRule, index: number) { - if (this.isEditingNew && JSON.stringify(newAutoRole) === JSON.stringify(this.newAutoRoleTemplate)) { + onRowEditSave(table: Table, newAutoRoleRule: AutoRoleRule, index: number) { + if (this.isEditingNew && JSON.stringify(newAutoRoleRule) === JSON.stringify(this.newAutoRoleTemplate)) { this.isEditingNew = false; this.rules.splice(index, 1); return; } - if (!newAutoRole.id || !newAutoRole.emojiName && !newAutoRole.roleId) { + if (!newAutoRoleRule.id && !this.isEditingNew || !newAutoRoleRule.emojiName && !newAutoRoleRule.roleId) { + return; + } + + if (this.isEditingNew) { + this.spinner.showSpinner(); + this.data.mutation(Mutations.createAutoRoleRule, { + autoRoleId: this.autoRoleId, + emojiName: newAutoRoleRule.emojiName, + roleId: newAutoRoleRule.roleId + } + ).pipe(catchError(err => { + this.spinner.hideSpinner(); + this.toastService.error(this.translate.instant("view.server.auto_roles.rules.message.auto_role_rule_create_failed"), this.translate.instant("view.server.auto_roles.rules.message.auto_role_rule_create_failed_d")); + return throwError(err); + })).subscribe(result => { + this.spinner.hideSpinner(); + this.toastService.success(this.translate.instant("view.server.auto_roles.rules.message.auto_role_rule_created"), this.translate.instant("view.server.auto_roles.rules.message.auto_role_rule_create_d", { id: result.autoRoleRule.createAutoRoleRule?.id })); + this.isEditingNew = false; + this.loadNextPage(); + }); return; } this.spinner.showSpinner(); - this.data.mutation(Mutations.createAutoRoleRule, { - serverId: this.sidebar.server$.value?.id, - emojiName: newAutoRole.emojiName, - roleId: newAutoRole.roleId + this.data.mutation(Mutations.updateAutoRoleRule, { + id: newAutoRoleRule.id, + emojiName: newAutoRoleRule.emojiName, + roleId: newAutoRoleRule.roleId } ).pipe(catchError(err => { this.spinner.hideSpinner(); - this.toastService.error(this.translate.instant("view.server.auto_roles.message.auto_role_create_failed"), this.translate.instant("view.server.auto_roles.message.auto_role_create_failed_d")); + this.toastService.error(this.translate.instant("view.server.auto_roles.rules.message.auto_role_rule_update_failed"), this.translate.instant("view.server.auto_roles.rules.message.auto_role_rule_update_failed_d")); return throwError(err); })).subscribe(result => { this.spinner.hideSpinner(); - this.toastService.success(this.translate.instant("view.server.auto_roles.message.auto_role_created"), this.translate.instant("view.server.auto_roles.message.auto_role_create_d", { id: result.autoRole.createAutoRole?.id })); + this.toastService.success(this.translate.instant("view.server.auto_roles.rules.message.auto_role_rule_updated"), this.translate.instant("view.server.auto_roles.rules.message.auto_role_rule_update_d", { id: result.autoRole.createAutoRole?.id })); this.loadNextPage(); }); } @@ -233,24 +253,22 @@ export class AutoRolesRulesComponent implements OnInit { deleteAutoRoleRule(autoRoleRule: AutoRoleRule) { this.confirmDialog.confirmDialog( - this.translate.instant("view.server.auto_roles.message.auto_role_delete"), this.translate.instant("view.server.auto_roles.message.auto_role_delete_q", { id: autoRoleRule.id }), + this.translate.instant("view.server.auto_roles.rules.message.auto_role_rule_delete"), this.translate.instant("view.server.auto_roles.rules.message.auto_role_rule_delete_q", { id: autoRoleRule.id }), () => { this.spinner.showSpinner(); - this.data.mutation(Mutations.deleteAutoRole, { + this.data.mutation(Mutations.deleteAutoRoleRule, { id: autoRoleRule.id } ).pipe(catchError(err => { this.spinner.hideSpinner(); - this.toastService.error(this.translate.instant("view.server.auto_roles.message.auto_role_delete_failed"), this.translate.instant("view.server.auto_roles.message.auto_role_delete_failed_d", { id: autoRoleRule.id })); + this.toastService.error(this.translate.instant("view.server.auto_roles.rules.message.auto_role_rule_delete_failed"), this.translate.instant("view.server.auto_roles.rules.message.auto_role_rule_delete_failed_d", { id: autoRoleRule.id })); return throwError(err); })).subscribe(_ => { this.spinner.hideSpinner(); - this.toastService.success(this.translate.instant("view.server.auto_roles.message.auto_role_deleted"), this.translate.instant("view.server.auto_roles.message.auto_role_deleted_d", { id: autoRoleRule.id })); + this.toastService.success(this.translate.instant("view.server.auto_roles.rules.message.auto_role_rule_deleted"), this.translate.instant("view.server.auto_roles.rules.message.auto_role_rule_deleted_d", { id: autoRoleRule.id })); this.loadNextPage(); }); }); - - } addAutoRoleRule(table: Table) { diff --git a/kdb-web/src/assets/i18n/de.json b/kdb-web/src/assets/i18n/de.json index 2e8e2ea5..8a7b7eaf 100644 --- a/kdb-web/src/assets/i18n/de.json +++ b/kdb-web/src/assets/i18n/de.json @@ -261,16 +261,20 @@ }, "no_entries_found": "Keine Einträge gefunden", "message": { - "auto_role_create": "Auto Rolle erstellt", - "auto_role_create_d": "Auto Rolle {{id}} erfolgreich erstellt", - "auto_role_create_failed": "Auto Rolle Erstellung fehlgeschlagen", - "auto_role_create_failed_d": "Die Erstellung der Auto Rolle ist fehlgeschlagen!", - "auto_role_delete": "Auto Rolle löschen", - "auto_role_delete_q": "Sind Sie sich sicher, dass Sie die Auto Rolle {{id}} löschen möchten?", - "auto_role_deleted": "Auto Rolle gelöscht", - "auto_role_deleted_d": "Auto Rolle {{id}} erfolgreich gelöscht", - "auto_role_delete_failed": "Auto Rolle Löschung fehlgeschlagen", - "auto_role_delete_failed_d": "Die Löschung der Auto Rolle {{id}} ist fehlgeschlagen!" + "auto_role_rule_create": "Auto Rollen Regel erstellt", + "auto_role_rule_create_d": "Auto Rollen Regel {{id}} erfolgreich erstellt", + "auto_role_rule_create_failed": "Auto Rollen Regel Erstellung fehlgeschlagen", + "auto_role_rule_create_failed_d": "Die Erstellung der Auto Rollen Regel ist fehlgeschlagen!", + "auto_role_rule_update": "Auto Rollen Regel bearbeitet", + "auto_role_rule_update_d": "Auto Rollen Regel {{id}} erfolgreich bearbeitet", + "auto_role_rule_update_failed": "Auto Rollen Regel Bearbeitung fehlgeschlagen", + "auto_role_rule_update_failed_d": "Die Bearbeitung der Auto Rollen Regel ist fehlgeschlagen!", + "auto_role_rule_delete": "Auto Rollen Regel löschen", + "auto_role_rule_delete_q": "Sind Sie sich sicher, dass Sie die Auto Rollen Regel {{id}} löschen möchten?", + "auto_role_rule_deleted": "Auto Rollen Regel gelöscht", + "auto_role_rule_deleted_d": "Auto Rollen Regel {{id}} erfolgreich gelöscht", + "auto_role_rule_delete_failed": "Auto Rollen Regel Löschung fehlgeschlagen", + "auto_role_rule_delete_failed_d": "Die Löschung der Auto Rollen Regel {{id}} ist fehlgeschlagen!" } } }