Finished auto role rules #134

This commit is contained in:
Sven Heidemann 2023-02-20 22:30:18 +01:00
parent 02d04725bd
commit ec9bc80392
7 changed files with 81 additions and 37 deletions

View File

@ -89,7 +89,7 @@ class AutoRoleRule(TableABC):
INSERT INTO `AutoRoleRules` ( INSERT INTO `AutoRoleRules` (
`AutoRoleId`, `DiscordEmojiName`, `DiscordRoleId`, `CreatedAt`, `LastModifiedAt` `AutoRoleId`, `DiscordEmojiName`, `DiscordRoleId`, `CreatedAt`, `LastModifiedAt`
) VALUES ( ) VALUES (
{self._auto_role}, {self._auto_role.id},
'{self._discord_emoji_name}', '{self._discord_emoji_name}',
{self._discord_role_id}, {self._discord_role_id},
'{self._created_at}', '{self._created_at}',
@ -103,8 +103,8 @@ class AutoRoleRule(TableABC):
return str( return str(
f""" f"""
UPDATE `AutoRoleRules` UPDATE `AutoRoleRules`
SET `AutoRoleId` = {self._auto_role}, SET `AutoRoleId` = {self._auto_role.id},
`DiscordEmojiName` = {self._discord_emoji_name}, `DiscordEmojiName` = '{self._discord_emoji_name}',
`DiscordRoleId` = {self._discord_role_id}, `DiscordRoleId` = {self._discord_role_id},
`LastModifiedAt` = '{self._modified_at}' `LastModifiedAt` = '{self._modified_at}'
WHERE `AutoRoleRuleId` = {self._auto_role_rule_id}; WHERE `AutoRoleRuleId` = {self._auto_role_rule_id};

View File

@ -35,9 +35,9 @@ class AutoRoleRuleMutation(QueryABC):
def get_new(x: AutoRoleRule): def get_new(x: AutoRoleRule):
return ( return (
x.auto_role.id == input["autoRoleId"] x.auto_role.id == int(input["autoRoleId"])
and x.emoji_name == input["emojiName"] 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() return self._auto_roles.get_auto_role_rules_by_auto_role_id(auto_role_rule.auto_role.id).where(get_new).last()

View File

@ -42,7 +42,7 @@ export class Mutations {
`; `;
static createAutoRoleRule = ` static createAutoRoleRule = `
mutation createAutoRoleRule($autoRoleId: ID, $emojiName: String, roleId: String) { mutation createAutoRoleRule($autoRoleId: ID, $emojiName: String, $roleId: String) {
autoRoleRule { autoRoleRule {
createAutoRoleRule(input: { autoRoleId: $autoRoleId, emojiName: $emojiName, roleId: $roleId }) { createAutoRoleRule(input: { autoRoleId: $autoRoleId, emojiName: $emojiName, roleId: $roleId }) {
id 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 = ` static deleteAutoRoleRule = `
mutation deleteAutoRoleRule($id: ID) { mutation deleteAutoRoleRule($id: ID) {
autoRoleRule { autoRoleRule {

View File

@ -1,5 +1,5 @@
import { User } from "../data/user.model"; import { User } from "../data/user.model";
import { AutoRole } from "../data/auto_role.model"; import { AutoRole, AutoRoleRule } from "../data/auto_role.model";
export interface GraphQLResult { export interface GraphQLResult {
data: any; data: any;
@ -15,6 +15,7 @@ export interface UpdateUserMutationResult {
updateUser: User updateUser: User
}; };
} }
export interface AutoRoleMutationResult { export interface AutoRoleMutationResult {
autoRole: { autoRole: {
createAutoRole?: AutoRole createAutoRole?: AutoRole
@ -22,3 +23,11 @@ export interface AutoRoleMutationResult {
deleteAutoRole?: AutoRole deleteAutoRole?: AutoRole
}; };
} }
export interface AutoRoleRuleMutationResult {
autoRoleRule: {
createAutoRoleRule?: AutoRoleRule
updateAutoRoleRule?: AutoRoleRule
deleteAutoRoleRule?: AutoRoleRule
};
}

View File

@ -102,7 +102,7 @@
<td> <td>
<p-cellEditor> <p-cellEditor>
<ng-template pTemplate="input"> <ng-template pTemplate="input">
<p-dropdown [options]="roles" optionValue="value.name" [(ngModel)]="autoRoleRule.roleName" <p-dropdown [options]="roles" optionValue="value.id" [(ngModel)]="autoRoleRule.roleId"
placeholder="{{'view.server.auto_roles.rules.headers.role' | translate}}"></p-dropdown> placeholder="{{'view.server.auto_roles.rules.headers.role' | translate}}"></p-dropdown>
</ng-template> </ng-template>
<ng-template pTemplate="output"> <ng-template pTemplate="output">

View File

@ -19,7 +19,7 @@ import { Queries } from "../../../../../../models/graphql/queries.model";
import { Server } from "../../../../../../models/data/server.model"; import { Server } from "../../../../../../models/data/server.model";
import { catchError, debounceTime } from "rxjs/operators"; import { catchError, debounceTime } from "rxjs/operators";
import { Table } from "primeng/table"; 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 { Mutations } from "../../../../../../models/graphql/mutations.model";
import { throwError } from "rxjs"; import { throwError } from "rxjs";
@ -32,8 +32,8 @@ export class AutoRolesRulesComponent implements OnInit {
rules!: AutoRoleRule[]; rules!: AutoRoleRule[];
guild!: Guild; guild!: Guild;
emojis!: MenuItem[]; emojis: MenuItem[] = [];
roles!: MenuItem[]; roles: MenuItem[] = [];
loading = true; loading = true;
autoRoleId!: number; autoRoleId!: number;
@ -80,7 +80,7 @@ export class AutoRolesRulesComponent implements OnInit {
} }
getEmojiUrl(name: string): string { 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 { ngOnInit(): void {
@ -191,30 +191,50 @@ export class AutoRolesRulesComponent implements OnInit {
this.clonedUsers[index] = { ...autoRoleRule }; this.clonedUsers[index] = { ...autoRoleRule };
} }
onRowEditSave(table: Table, newAutoRole: AutoRoleRule, index: number) { onRowEditSave(table: Table, newAutoRoleRule: AutoRoleRule, index: number) {
if (this.isEditingNew && JSON.stringify(newAutoRole) === JSON.stringify(this.newAutoRoleTemplate)) { if (this.isEditingNew && JSON.stringify(newAutoRoleRule) === JSON.stringify(this.newAutoRoleTemplate)) {
this.isEditingNew = false; this.isEditingNew = false;
this.rules.splice(index, 1); this.rules.splice(index, 1);
return; 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<AutoRoleRuleMutationResult>(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; return;
} }
this.spinner.showSpinner(); this.spinner.showSpinner();
this.data.mutation<AutoRoleMutationResult>(Mutations.createAutoRoleRule, { this.data.mutation<AutoRoleMutationResult>(Mutations.updateAutoRoleRule, {
serverId: this.sidebar.server$.value?.id, id: newAutoRoleRule.id,
emojiName: newAutoRole.emojiName, emojiName: newAutoRoleRule.emojiName,
roleId: newAutoRole.roleId roleId: newAutoRoleRule.roleId
} }
).pipe(catchError(err => { ).pipe(catchError(err => {
this.spinner.hideSpinner(); 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); return throwError(err);
})).subscribe(result => { })).subscribe(result => {
this.spinner.hideSpinner(); 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(); this.loadNextPage();
}); });
} }
@ -233,24 +253,22 @@ export class AutoRolesRulesComponent implements OnInit {
deleteAutoRoleRule(autoRoleRule: AutoRoleRule) { deleteAutoRoleRule(autoRoleRule: AutoRoleRule) {
this.confirmDialog.confirmDialog( 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.spinner.showSpinner();
this.data.mutation<AutoRoleMutationResult>(Mutations.deleteAutoRole, { this.data.mutation<AutoRoleMutationResult>(Mutations.deleteAutoRoleRule, {
id: autoRoleRule.id id: autoRoleRule.id
} }
).pipe(catchError(err => { ).pipe(catchError(err => {
this.spinner.hideSpinner(); 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); return throwError(err);
})).subscribe(_ => { })).subscribe(_ => {
this.spinner.hideSpinner(); 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(); this.loadNextPage();
}); });
}); });
} }
addAutoRoleRule(table: Table) { addAutoRoleRule(table: Table) {

View File

@ -261,16 +261,20 @@
}, },
"no_entries_found": "Keine Einträge gefunden", "no_entries_found": "Keine Einträge gefunden",
"message": { "message": {
"auto_role_create": "Auto Rolle erstellt", "auto_role_rule_create": "Auto Rollen Regel erstellt",
"auto_role_create_d": "Auto Rolle {{id}} erfolgreich erstellt", "auto_role_rule_create_d": "Auto Rollen Regel {{id}} erfolgreich erstellt",
"auto_role_create_failed": "Auto Rolle Erstellung fehlgeschlagen", "auto_role_rule_create_failed": "Auto Rollen Regel Erstellung fehlgeschlagen",
"auto_role_create_failed_d": "Die Erstellung der Auto Rolle ist fehlgeschlagen!", "auto_role_rule_create_failed_d": "Die Erstellung der Auto Rollen Regel ist fehlgeschlagen!",
"auto_role_delete": "Auto Rolle löschen", "auto_role_rule_update": "Auto Rollen Regel bearbeitet",
"auto_role_delete_q": "Sind Sie sich sicher, dass Sie die Auto Rolle {{id}} löschen möchten?", "auto_role_rule_update_d": "Auto Rollen Regel {{id}} erfolgreich bearbeitet",
"auto_role_deleted": "Auto Rolle gelöscht", "auto_role_rule_update_failed": "Auto Rollen Regel Bearbeitung fehlgeschlagen",
"auto_role_deleted_d": "Auto Rolle {{id}} erfolgreich gelöscht", "auto_role_rule_update_failed_d": "Die Bearbeitung der Auto Rollen Regel ist fehlgeschlagen!",
"auto_role_delete_failed": "Auto Rolle Löschung fehlgeschlagen", "auto_role_rule_delete": "Auto Rollen Regel löschen",
"auto_role_delete_failed_d": "Die Löschung der Auto Rolle {{id}} ist fehlgeschlagen!" "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!"
} }
} }
} }