public async Task <Result <string> > Handle(UpdateRequirementsCommand request, CancellationToken cancellationToken)
        {
            var tagFunction = await _tagFunctionRepository.GetByCodesAsync(request.TagFunctionCode, request.RegisterCode);

            var requirements = request.Requirements.ToList();

            if (tagFunction == null)
            {
                tagFunction = await CreateNewTagFunctionAsync(request.TagFunctionCode, request.RegisterCode);

                if (tagFunction == null)
                {
                    return(new NotFoundResult <string>($"TagFunction {request.TagFunctionCode} not found in register {request.RegisterCode}"));
                }
            }
            else
            {
                RemoveChangedOrRemovedRequirementsFromTagFunction(tagFunction, requirements);
            }

            await AddRequirementsToTagFunctionAsync(tagFunction, requirements);

            await _unitOfWork.SaveChangesAsync(cancellationToken);

            return(new SuccessResult <string>(tagFunction.RowVersion.ConvertToString()));
        }
        public async Task <Result <string> > Handle(VoidTagFunctionCommand request, CancellationToken cancellationToken)
        {
            var tagFunction = await _tagFunctionRepository.GetByCodesAsync(request.TagFunctionCode, request.RegisterCode);

            tagFunction.IsVoided = true;
            tagFunction.SetRowVersion(request.RowVersion);
            await _unitOfWork.SaveChangesAsync(cancellationToken);

            return(new SuccessResult <string>(tagFunction.RowVersion.ConvertToString()));
        }
        private async Task ProcessTagFunctionEvent(string messageJson)
        {
            var tagFunctionEvent = JsonSerializer.Deserialize <TagFunctionTopic>(messageJson);

            if (tagFunctionEvent.Plant.IsEmpty() ||
                tagFunctionEvent.Code.IsEmpty() ||
                tagFunctionEvent.RegisterCode.IsEmpty())
            {
                throw new ArgumentNullException($"Deserialized JSON is not a valid TagFunctionEvent {messageJson}");
            }

            TrackTagFunctionEvent(tagFunctionEvent);

            _plantSetter.SetPlant(tagFunctionEvent.Plant);

            if (!tagFunctionEvent.CodeOld.IsEmpty() ||
                !tagFunctionEvent.RegisterCodeOld.IsEmpty())
            {
                var tagFunction =
                    await _tagFunctionRepository.GetByCodesAsync(tagFunctionEvent.CodeOld, tagFunctionEvent.RegisterCodeOld);

                if (tagFunction != null)
                {
                    tagFunction.RenameTagFunction(tagFunctionEvent.Code, tagFunctionEvent.RegisterCode);
                    tagFunction.Description = tagFunctionEvent.Description;
                    tagFunction.IsVoided    = tagFunctionEvent.IsVoided;
                }
            }
            else
            {
                var tagFunction =
                    await _tagFunctionRepository.GetByCodesAsync(tagFunctionEvent.Code, tagFunctionEvent.RegisterCode);

                if (tagFunction != null)
                {
                    tagFunction.Description = tagFunctionEvent.Description;
                    tagFunction.IsVoided    = tagFunctionEvent.IsVoided;
                }
            }
        }