public override async Task Execute()
        {
            var isEnabled          = CommandArgumentValue.GetArgumentScalarValue <bool>(_isEnabledArg);
            var name               = CommandArgumentValue.GetArgumentScalarValue <string>(_nameArg);
            var serviceHookBaseUri = CommandArgumentValue.GetArgumentScalarValue <string>(_serviceHookBaseUriArg);
            var serviceHookAction  = CommandArgumentValue.GetArgumentScalarValue <string>(_serviceHookActionArg);
            var ckId               = CommandArgumentValue.GetArgumentScalarValue <string>(_ckIdArg);

            var filterArgData = CommandArgumentValue.GetArgumentValue(_filterArg);

            Logger.Info(
                $"Creating service hook for entity '{ckId}' at '{ServiceClient.ServiceUri}'");

            List <FieldFilterDto> fieldFilters = new List <FieldFilterDto>();

            foreach (var filterArg in filterArgData.Values)
            {
                var terms = filterArg.Split(" ");
                if (terms.Length != 3)
                {
                    throw new InvalidOperationException($"Filter term '{filterArg}' is invalid. Three terms needed.");
                }

                var attribute = terms[0].Trim('\'');
                if (!Enum.TryParse(terms[1], true, out FieldFilterOperatorDto operatorDto))
                {
                    throw new InvalidOperationException($"Operator '{terms[1]}' of term '{filterArg}' is invalid.");
                }

                var comparisionValue = terms[2].Trim('\'');

                fieldFilters.Add(new FieldFilterDto
                {
                    AttributeName = attribute, Operator = operatorDto, ComparisonValue = comparisionValue
                });
            }

            var createServiceHookDto = new ServiceHookMutationDto
            {
                Enabled            = isEnabled,
                Name               = name,
                QueryCkId          = ckId,
                FieldFilter        = JsonConvert.SerializeObject(fieldFilters),
                ServiceHookBaseUri = serviceHookBaseUri,
                ServiceHookAction  = serviceHookAction
            };

            var query = new GraphQLRequest
            {
                Query     = GraphQl.CreateServiceHook,
                Variables = new { entities = new[] { createServiceHookDto } }
            };

            var result = await ServiceClient.SendMutationAsync <IEnumerable <RtServiceHookDto> >(query);

            Logger.Info($"Service hook '{name}' added (ID '{result.First().RtId}').");
        }
예제 #2
0
        public override async Task Execute()
        {
            var serviceHookId      = CommandArgumentValue.GetArgumentScalarValue <string>(_serviceHookIdArg);
            var isEnabled          = CommandArgumentValue.GetArgumentScalarValueOrDefault <bool>(_isEnabledArg);
            var name               = CommandArgumentValue.GetArgumentScalarValueOrDefault <string>(_nameArg);
            var serviceHookBaseUri = CommandArgumentValue.GetArgumentScalarValueOrDefault <string>(_serviceHookBaseUriArg);
            var serviceHookAction  = CommandArgumentValue.GetArgumentScalarValueOrDefault <string>(_serviceHookActionArg);
            var ckId               = CommandArgumentValue.GetArgumentScalarValueOrDefault <string>(_ckIdArg);

            Logger.Info(
                $"Update service hook '{serviceHookId}' at '{_tenantClient.ServiceUri}'");

            List <FieldFilterDto> fieldFilters = new List <FieldFilterDto>();

            if (CommandArgumentValue.IsArgumentUsed(_filterArg))
            {
                var filterArgData = CommandArgumentValue.GetArgumentValue(_filterArg);
                foreach (var filterArg in filterArgData.Values)
                {
                    var terms = filterArg.Split(" ");
                    if (terms.Length != 3)
                    {
                        throw new InvalidOperationException(
                                  $"Filter term '{filterArg}' is invalid. Three terms needed.");
                    }

                    var attribute = terms[0].Trim('\'');
                    if (!Enum.TryParse(terms[1], true, out FieldFilterOperatorDto operatorDto))
                    {
                        throw new InvalidOperationException($"Operator '{terms[1]}' of term '{filterArg}' is invalid.");
                    }

                    var comparisionValue = terms[2].Trim('\'');

                    fieldFilters.Add(new FieldFilterDto
                    {
                        AttributeName = attribute, Operator = operatorDto, ComparisonValue = comparisionValue
                    });
                }
            }

            var getQuery = new GraphQLRequest
            {
                Query     = GraphQl.GetServiceHookDetails,
                Variables = new
                {
                    rtId = serviceHookId
                }
            };

            var getResult = await _tenantClient.SendQueryAsync <RtServiceHookDto>(getQuery);

            if (!getResult.Items.Any())
            {
                throw new InvalidOperationException(
                          $"Service Hook with ID '{serviceHookId}' does not exist.");
            }

            var serviceHookDto = getResult.Items.First();

            var updateServiceHook = new ServiceHookMutationDto
            {
                Enabled            = isEnabled,
                Name               = name,
                QueryCkId          = ckId,
                FieldFilter        = JsonConvert.SerializeObject(fieldFilters),
                ServiceHookBaseUri = serviceHookBaseUri,
                ServiceHookAction  = serviceHookAction
            };

            var updateQuery = new GraphQLRequest
            {
                Query     = GraphQl.UpdateServiceHook,
                Variables = new
                {
                    entities = new[]
                    {
                        new MutationDto <ServiceHookMutationDto>
                        {
                            RtId = serviceHookDto.RtId,
                            Item = updateServiceHook
                        }
                    }
                }
            };

            var result = await _tenantClient.SendMutationAsync <IEnumerable <RtServiceHookDto> >(updateQuery);

            Logger.Info($"Service hook '{serviceHookId}' updated (ID '{result.First().RtId}').");
        }