From f02acd7f944219a60dd7437c9402f1161f857804 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Mon, 7 Aug 2023 14:31:54 +0200 Subject: [PATCH] Added technician config mutation #127 --- kdb-bot/src/bot/config | 2 +- kdb-bot/src/bot_api/config | 2 +- .../bot_data/model/technician_id_config.py | 2 +- .../model/technician_ping_url_config.py | 2 +- kdb-bot/src/bot_graphql/graphql_module.py | 2 + kdb-bot/src/bot_graphql/model/mutation.gql | 1 + ...ig_technician.gql => technicianConfig.gql} | 2 - kdb-bot/src/bot_graphql/mutation.py | 3 + .../mutations/technician_config_mutation.py | 83 ++++++++++++------- 9 files changed, 63 insertions(+), 36 deletions(-) rename kdb-bot/src/bot_graphql/model/{config_technician.gql => technicianConfig.gql} (91%) diff --git a/kdb-bot/src/bot/config b/kdb-bot/src/bot/config index 4264ba15..359f9c38 160000 --- a/kdb-bot/src/bot/config +++ b/kdb-bot/src/bot/config @@ -1 +1 @@ -Subproject commit 4264ba15970a28f8a50c888bbf635a15c438bcf9 +Subproject commit 359f9c38c3ec825a89f3bf289a65ec035cfcb693 diff --git a/kdb-bot/src/bot_api/config b/kdb-bot/src/bot_api/config index c712f856..6d3f253f 160000 --- a/kdb-bot/src/bot_api/config +++ b/kdb-bot/src/bot_api/config @@ -1 +1 @@ -Subproject commit c712f856ebe30c71ac0b144045599ed2f91a1cba +Subproject commit 6d3f253f4121191308b4b0d5f01fa1c61b61406a diff --git a/kdb-bot/src/bot_data/model/technician_id_config.py b/kdb-bot/src/bot_data/model/technician_id_config.py index b89983e7..6ba5f581 100644 --- a/kdb-bot/src/bot_data/model/technician_id_config.py +++ b/kdb-bot/src/bot_data/model/technician_id_config.py @@ -74,6 +74,6 @@ class TechnicianIdConfig(TableABC): return str( f""" DELETE FROM `CFG_TechnicianIds` - WHERE `Id` = {self._id}; + WHERE `TechnicianId` = {self._technician_id}; """ ) diff --git a/kdb-bot/src/bot_data/model/technician_ping_url_config.py b/kdb-bot/src/bot_data/model/technician_ping_url_config.py index 59c4cfad..75d51c29 100644 --- a/kdb-bot/src/bot_data/model/technician_ping_url_config.py +++ b/kdb-bot/src/bot_data/model/technician_ping_url_config.py @@ -74,6 +74,6 @@ class TechnicianPingUrlConfig(TableABC): return str( f""" DELETE FROM `CFG_TechnicianPingUrls` - WHERE `Id` = {self._id}; + WHERE `URL` = {self._ping_url}; """ ) diff --git a/kdb-bot/src/bot_graphql/graphql_module.py b/kdb-bot/src/bot_graphql/graphql_module.py index 8b3db926..96c0720f 100644 --- a/kdb-bot/src/bot_graphql/graphql_module.py +++ b/kdb-bot/src/bot_graphql/graphql_module.py @@ -24,6 +24,7 @@ from bot_graphql.mutations.achievement_mutation import AchievementMutation from bot_graphql.mutations.auto_role_mutation import AutoRoleMutation from bot_graphql.mutations.auto_role_rule_mutation import AutoRoleRuleMutation from bot_graphql.mutations.level_mutation import LevelMutation +from bot_graphql.mutations.technician_config_mutation import TechnicianConfigMutation from bot_graphql.mutations.user_joined_game_server_mutation import UserJoinedGameServerMutation from bot_graphql.mutations.user_mutation import UserMutation from bot_graphql.queries.achievement_attribute_query import AchievementAttributeQuery @@ -120,5 +121,6 @@ class GraphQLModule(ModuleABC): services.add_transient(QueryABC, UserMutation) services.add_transient(QueryABC, AchievementMutation) services.add_transient(QueryABC, UserJoinedGameServerMutation) + services.add_transient(QueryABC, TechnicianConfigMutation) services.add_transient(SeederService) diff --git a/kdb-bot/src/bot_graphql/model/mutation.gql b/kdb-bot/src/bot_graphql/model/mutation.gql index 0fb69b50..5f57cb8f 100644 --- a/kdb-bot/src/bot_graphql/model/mutation.gql +++ b/kdb-bot/src/bot_graphql/model/mutation.gql @@ -5,4 +5,5 @@ type Mutation { user: UserMutation userJoinedGameServer: UserJoinedGameServerMutation achievement: AchievementMutation + technicianConfig: TechnicianConfigMutation } \ No newline at end of file diff --git a/kdb-bot/src/bot_graphql/model/config_technician.gql b/kdb-bot/src/bot_graphql/model/technicianConfig.gql similarity index 91% rename from kdb-bot/src/bot_graphql/model/config_technician.gql rename to kdb-bot/src/bot_graphql/model/technicianConfig.gql index d0c48ec7..dc35704a 100644 --- a/kdb-bot/src/bot_graphql/model/config_technician.gql +++ b/kdb-bot/src/bot_graphql/model/technicianConfig.gql @@ -46,9 +46,7 @@ type TechnicianIdConfigHistory implements HistoryTableQuery { } type TechnicianConfigMutation { - createTechnicianConfig(input: TechnicianConfigInput!): TechnicianConfig updateTechnicianConfig(input: TechnicianConfigInput!): TechnicianConfig - deleteTechnicianConfig(id: ID): TechnicianConfig } input TechnicianConfigInput { diff --git a/kdb-bot/src/bot_graphql/mutation.py b/kdb-bot/src/bot_graphql/mutation.py index d8287d96..d5a62123 100644 --- a/kdb-bot/src/bot_graphql/mutation.py +++ b/kdb-bot/src/bot_graphql/mutation.py @@ -4,6 +4,7 @@ from bot_graphql.mutations.achievement_mutation import AchievementMutation from bot_graphql.mutations.auto_role_mutation import AutoRoleMutation from bot_graphql.mutations.auto_role_rule_mutation import AutoRoleRuleMutation from bot_graphql.mutations.level_mutation import LevelMutation +from bot_graphql.mutations.technician_config_mutation import TechnicianConfigMutation from bot_graphql.mutations.user_joined_game_server_mutation import UserJoinedGameServerMutation from bot_graphql.mutations.user_mutation import UserMutation @@ -17,6 +18,7 @@ class Mutation(MutationType): user_mutation: UserMutation, achievement_mutation: AchievementMutation, user_joined_game_server: UserJoinedGameServerMutation, + technician_config: TechnicianConfigMutation, ): MutationType.__init__(self) @@ -26,3 +28,4 @@ class Mutation(MutationType): self.set_field("user", lambda *_: user_mutation) self.set_field("achievement", lambda *_: achievement_mutation) self.set_field("userJoinedGameServer", lambda *_: user_joined_game_server) + self.set_field("technicianConfig", lambda *_: technician_config) diff --git a/kdb-bot/src/bot_graphql/mutations/technician_config_mutation.py b/kdb-bot/src/bot_graphql/mutations/technician_config_mutation.py index 7a7ed873..ed89ee48 100644 --- a/kdb-bot/src/bot_graphql/mutations/technician_config_mutation.py +++ b/kdb-bot/src/bot_graphql/mutations/technician_config_mutation.py @@ -4,6 +4,8 @@ from cpl_discord.service import DiscordBotServiceABC from bot_data.abc.server_repository_abc import ServerRepositoryABC from bot_data.abc.technician_config_repository_abc import TechnicianConfigRepositoryABC from bot_data.model.technician_config import TechnicianConfig +from bot_data.model.technician_id_config import TechnicianIdConfig +from bot_data.model.technician_ping_url_config import TechnicianPingUrlConfig from bot_data.model.user_role_enum import UserRoleEnum from bot_graphql.abc.query_abc import QueryABC @@ -23,44 +25,65 @@ class TechnicianConfigMutation(QueryABC): self._technician_configs = technician_configs self._db = db - self.set_field("createTechnicianConfig", self.resolve_create_TechnicianConfig) - self.set_field("updateTechnicianConfig", self.resolve_update_TechnicianConfig) - self.set_field("deleteTechnicianConfig", self.resolve_delete_TechnicianConfig) - - def resolve_create_technician_config(self, *_, input: dict): - if self._technician_configs.does_technician_config_exists(): - return None - - technician_config = TechnicianConfig( - input["helpCommandReferenceUrl"], - input["waitForRestart"], - input["waitForShutdown"], - input["cacheMaxMessages"], - input["pingURLs"], - input["technicianIds"], - ) - self._technician_configs.add_technician_config(technician_config) - self._db.save_changes() - - return self._technician_configs.get_technician_config() + self.set_field("updateTechnicianConfig", self.resolve_update_technician_config) def resolve_update_technician_config(self, *_, input: dict): technician_config = self._technician_configs.get_technician_config() self._can_user_mutate_data(technician_config, UserRoleEnum.admin) - technician_config.name = input["name"] if "name" in input else technician_config.name + technician_config.help_command_reference_url = ( + input["helpCommandReferenceUrl"] + if "helpCommandReferenceUrl" in input + else technician_config.help_command_reference_url + ) + technician_config.wait_for_restart = ( + input["waitForRestart"] if "waitForRestart" in input else technician_config.wait_for_restart + ) + technician_config.wait_for_shutdown = ( + input["waitForShutdown"] if "waitForShutdown" in input else technician_config.wait_for_shutdown + ) + technician_config.cache_max_messages = ( + input["cacheMaxMessages"] if "cacheMaxMessages" in input else technician_config.cache_max_messages + ) + technician_config.ping_urls = input["pingURLs"] if "pingURLs" in input else technician_config.ping_urls + technician_config.technician_ids = ( + input["technicianIds"] if "technicianIds" in input else technician_config.technician_ids + ) + + self._technician_configs.update_technician_config(technician_config) + if "pingURLs" in input: + self._update_ping_urls(technician_config) + + if "technicianIds" in input: + self._update_technician_ids(technician_config) - self._technician_configs.update_TechnicianConfig(technician_config) self._db.save_changes() - - technician_config = self._technician_configs.get_TechnicianConfig_by_id(input["id"]) return technician_config - def resolve_delete_technician_config(self, *_, id: int): - technician_config = self._technician_configs.get_TechnicianConfig_by_id(id) - self._can_user_mutate_data(technician_config.server, UserRoleEnum.admin) + def _update_ping_urls(self, new_config: TechnicianConfig): + old_config = self._technician_configs.get_technician_config() + for url in old_config.ping_urls: + if url in new_config.ping_urls: + continue - self._technician_configs.delete_TechnicianConfig(technician_config) - self._db.save_changes() + self._technician_configs.delete_technician_ping_url_config(TechnicianPingUrlConfig(url)) - return technician_config + for url in new_config.ping_urls: + if url in old_config.ping_urls: + continue + + self._technician_configs.add_technician_ping_url_config(TechnicianPingUrlConfig(url)) + + def _update_technician_ids(self, new_config: TechnicianConfig): + old_config = self._technician_configs.get_technician_config() + for url in old_config.technician_ids: + if url in new_config.technician_ids: + continue + + self._technician_configs.delete_technician_id_config(TechnicianIdConfig(url)) + + for url in new_config.technician_ids: + if url in old_config.technician_ids: + continue + + self._technician_configs.add_technician_id_config(TechnicianIdConfig(url))