public async Task <Result <List <int> > > Handle(CreateTagsCommand request, CancellationToken cancellationToken)
        {
            var step = await _journeyRepository.GetStepByStepIdAsync(request.StepId);

            var reqDefIds = request.Requirements.Select(r => r.RequirementDefinitionId).ToList();
            var reqDefs   = await _requirementTypeRepository.GetRequirementDefinitionsByIdsAsync(reqDefIds);

            var mode = await _modeRepository.GetByIdAsync(step.ModeId);

            var addedTags = new List <Tag>();
            var project   = await _projectRepository.GetProjectOnlyByNameAsync(request.ProjectName);

            var tagDetailList = await _tagApiService.GetTagDetailsAsync(_plantProvider.Plant, request.ProjectName, request.TagNos);

            foreach (var tagNo in request.TagNos)
            {
                var tagDetails = tagDetailList.FirstOrDefault(td => td.TagNo == tagNo);
                if (tagDetails == null)
                {
                    return(new NotFoundResult <List <int> >($"Details for Tag {tagNo} not found in project {request.ProjectName}"));
                }

                // Todo TECH This type of validation should be in validator not in handler.
                // Since we don't want to ask main api for same data both in validator and here, we do it here only
                if (mode.ForSupplier && string.IsNullOrEmpty(tagDetails.PurchaseOrderNo))
                {
                    return(new NotFoundResult <List <int> >($"Purchase Order for {tagNo} not found in project {request.ProjectName}. Tag can not be in a Supplier step"));
                }

                if (project == null)
                {
                    project = new Project(_plantProvider.Plant, request.ProjectName, tagDetails.ProjectDescription);
                    _projectRepository.Add(project);
                }

                var tagToAdd = CreateTag(request, step, tagDetails, reqDefs);

                project.AddTag(tagToAdd);
                addedTags.Add(tagToAdd);
            }

            // Todo Remove Migration handling when migration period from old to new preservation in ProCoSys is over
            await _tagApiService.MarkTagsAsMigratedAsync(_plantProvider.Plant, tagDetailList.Select(t => t.Id));

            await _unitOfWork.SaveChangesAsync(cancellationToken);

            return(new SuccessResult <List <int> >(addedTags.Select(t => t.Id).ToList()));
        }
        private Tag CreateTag(
            CreateTagsCommand request,
            Step step,
            PCSTagDetails tagDetails,
            IList <RequirementDefinition> reqDefs)
        {
            var requirements = new List <TagRequirement>();

            foreach (var requirement in request.Requirements)
            {
                var reqDef = reqDefs.Single(rd => rd.Id == requirement.RequirementDefinitionId);
                requirements.Add(new TagRequirement(_plantProvider.Plant, requirement.IntervalWeeks, reqDef));
            }

            var tag = new Tag(
                _plantProvider.Plant,
                TagType.Standard,
                tagDetails.TagNo,
                tagDetails.Description,
                step,
                requirements)
            {
                Calloff         = tagDetails.CallOffNo,
                CommPkgNo       = tagDetails.CommPkgNo,
                McPkgNo         = tagDetails.McPkgNo,
                PurchaseOrderNo = tagDetails.PurchaseOrderNo,
                Remark          = request.Remark,
                StorageArea     = request.StorageArea,
                TagFunctionCode = tagDetails.TagFunctionCode
            };

            tag.SetArea(tagDetails.AreaCode, tagDetails.AreaDescription);
            tag.SetDiscipline(tagDetails.DisciplineCode, tagDetails.DisciplineDescription);

            return(tag);
        }