public async Task TrackingEvents_AddCaseFolder_Success()
        {
            var unitOfWork           = _provider.GetService <UnitOfWork>();
            var caseFolderRepository = _provider.GetService <ICaseFolderRepository>();
            var caseTypeRepository   = _provider.GetService <ICaseTypeRepository>();

            CaseFolder caseFolder;

            using (unitOfWork.Begin())
            {
                caseFolder = new CaseFolder();
                caseFolder.AddCaseCard(caseTypeRepository.GetFirst(), Guid.NewGuid());
                await caseFolderRepository.Add(caseFolder);

                await unitOfWork.CommitAsync();
            }

            using (unitOfWork.Begin())
            {
                CaseFolder fromDb = await caseFolderRepository.GetById(caseFolder.Id);

                Assert.NotNull(fromDb);
                Assert.Equal(caseFolder.Id, fromDb.Id);
                Assert.NotNull(fromDb.Cases);
                Assert.True(fromDb.Cases.Any());
                Assert.True(fromDb.TrackingEvents.Any());
            }
        }
        public async void Remove_CaseFolder_CaseMustBeRemoved()
        {
            //Arrange
            var caseType   = GetCaseType();
            var caseFolder = new CaseFolder();

            caseFolder.AddCaseCard(caseType, new Guid());
            var caseCard = caseFolder.Cases.FirstOrDefault();

            await SaveCaseFolder(caseFolder);

            //action
            await RemoveEntity(caseFolder);

            var result = await GetEntity <Case>(x => x.Id == caseCard.Id);

            //arrange
            result.ShouldBeNull();
        }
        public async void Add_CaseWithoutCall_SuccessfullySaved()
        {
            //Arrange
            var  unitOfWork             = _provider.GetService <UnitOfWork>();
            var  caseCardRepository     = _provider.GetService <ICaseRepository>();
            var  caseCardTypeRepository = _provider.GetService <ICaseTypeRepository>();
            Guid case112TypeId          = new Guid(AddCaseTableAndCaseTemplateTable.CaseTypeId112);

            //Action
            CaseType caseType;

            using (unitOfWork.Begin())
            {
                caseType = await caseCardTypeRepository.GetById(case112TypeId);

                await caseCardTypeRepository.Add(caseType);

                await unitOfWork.CommitAsync();
            }

            Case caseCard;

            using (unitOfWork.Begin())
            {
                var caseTypeFromDb = await caseCardTypeRepository.GetById(caseType.Id);

                var caseFolder = new CaseFolder();
                caseFolder.AddCaseCard(caseTypeFromDb, Guid.NewGuid());
                caseCard = caseFolder.Cases.FirstOrDefault();
                await caseCardRepository.Add(caseCard);

                await unitOfWork.CommitAsync();
            }

            //Assert
            using (unitOfWork.Begin())
            {
                var caseCardFromDb = await caseCardRepository.GetById(caseCard.Id);

                caseCardFromDb.ShouldNotBeNull();
                caseCardFromDb.Type.ShouldNotBeNull();
            }
        }
        /// <summary>
        /// Создание Line, CaseFolder и добавление Data к CaseFolder
        /// </summary>
        /// <param name="caseData"></param>
        /// <returns>LineId</returns>
        private async Task <Guid> CreateCaseFolder(string caseData, Guid userId)
        {
            using var uow = _provider.GetScopedService <UnitOfWork>();
            uow.Begin();
            var caseTypeRepo = new CaseTypeRepository(uow);
            var caseType     = await caseTypeRepo.GetById(_case112TypeId);

            var caseFolder = new CaseFolder {
                Data = caseData
            };

            caseFolder.AddCaseCard(caseType, userId);
            var caseFolderRepository = new CaseFolderRepository(uow);
            await caseFolderRepository.Add(caseFolder);

            await uow.CommitAsync();

            return(caseFolder.Id);
        }
        private async Task <Result <Guid> > PrepareCase(UserItemClientDto userItem)
        {
            Result <CallClientDto> callInfoResult = await _callManagementServiceClient.GetCallById(userItem.ItemId);

            if (callInfoResult.IsFailure)
            {
                _logger.Error($"ProcessCall. {callInfoResult.ErrorMessage}");
                return(Result.Failure(callInfoResult.ErrorCode, callInfoResult.ErrorMessage));
            }

            var caseFolderIdResult = callInfoResult.Value.Line.CaseFolderId;

            if (!userItem.CreateCaseCard)
            {
                if (caseFolderIdResult.HasValue)
                {
                    return(Result.Success(caseFolderIdResult.Value));
                }

                var newEmptyCaseFolder = new CaseFolder();
                await _caseFolderRepository.Add(newEmptyCaseFolder);

                return(Result.Success(newEmptyCaseFolder.Id));
            }

            var caseType = await _caseTypeRepository.GetById(userItem.CaseTypeId);

            if (caseFolderIdResult.HasValue)
            {
                var caseFolder = await _caseFolderRepository.GetById(caseFolderIdResult.Value);

                caseFolder.AddCaseCard(caseType, userItem.UserId);
                return(Result.Success(caseFolderIdResult.Value));
            }

            var newCaseFolder = new CaseFolder();

            newCaseFolder.AddCaseCard(caseType, userItem.UserId);
            await _caseFolderRepository.Add(newCaseFolder);

            return(Result.Success(newCaseFolder.Id));
        }
        /// <summary>
        /// Принять СМС
        /// </summary>
        /// <param name="sms"></param>
        /// <param name="caseType"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        private Result <CaseFolder> AcceptSms(Sms sms, CaseType caseType, Guid userId)
        {
            var applicantExt = sms.Applicant.ParticipantExtension;

            sms.Status = SmsStatus.Accepted;

            var caseFolder = new CaseFolder {
                Sms = sms
            };

            caseFolder.AddCaseCard(caseType, userId);

            var result = caseFolder.FillSmsData(applicantExt, sms);

            if (result.IsFailure)
            {
                return(result);
            }

            return(Result.Success(caseFolder));
        }
        public async Task TrackingEvents_UpdateCase_Success()
        {
            var unitOfWork           = _provider.GetService <UnitOfWork>();
            var caseFolderRepository = _provider.GetService <ICaseFolderRepository>();
            var caseRepository       = _provider.GetService <ICaseRepository>();
            var caseTypeRepository   = _provider.GetService <ICaseTypeRepository>();

            Guid       caseId;
            CaseFolder caseFolder;

            using (unitOfWork.Begin())
            {
                caseFolder = new CaseFolder();
                caseFolder.AddCaseCard(caseTypeRepository.GetFirst(), Guid.NewGuid());
                await caseFolderRepository.Add(caseFolder);

                caseId = caseFolder.Cases.FirstOrDefault().Id;
                await unitOfWork.CommitAsync();
            }

            using (unitOfWork.Begin())
            {
                var caseFromDb = await caseRepository.GetById(caseId);

                caseFromDb.Status = "Closed";
                await unitOfWork.CommitAsync();
            }

            using (unitOfWork.Begin())
            {
                CaseFolder caseFolderFromDb = await caseFolderRepository.GetById(caseFolder.Id);

                var caseCard = caseFolderFromDb.Cases.First();

                Assert.Equal(4, caseFolderFromDb.TrackingEvents.Count);
                Assert.Equal(3, caseCard.Root.TrackingEvents.Count(t => t.OperationType == EventType.Inserted));
                Assert.Equal(1, caseCard.Root.TrackingEvents.Count(t => t.OperationType == EventType.Updated));
            }
        }
        // ReSharper disable once InconsistentNaming
        public async void Get_Case_ShouldExists()
        {
            //arrange
            _unitOfWork.Begin();

            var caseType   = _caseTypeRepository.GetFirst();
            var userId     = Guid.NewGuid();
            var caseFolder = new CaseFolder();

            caseFolder.AddCaseCard(caseType, userId);

            await _caseFolderRepository.Add(caseFolder);

            await _unitOfWork.CommitAsync();

            //action
            SetCurrentUserId(userId);

            demoResult <CaseDto> caseCardResult = await _caseController.GetCase(caseFolder.Id);

            //Assert
            caseCardResult.Result.ShouldBeOfType <demoOkResult <CaseDto> >();
        }
        public async Task TrackingEvents_UpdateBaseEntity_Success()
        {
            var unitOfWork         = _provider.GetService <UnitOfWork>();
            var caseRepository     = _provider.GetService <ICaseRepository>();
            var caseTypeRepository = _provider.GetService <ICaseTypeRepository>();

            Case caseCard;

            using (unitOfWork.Begin())
            {
                var caseTypeFromDb = caseTypeRepository.GetFirst();
                var caseFolder     = new CaseFolder();
                caseFolder.AddCaseCard(caseTypeFromDb, Guid.NewGuid());
                caseCard = caseFolder.Cases.FirstOrDefault();
                await caseRepository.Add(caseCard);

                await unitOfWork.CommitAsync();
            }

            using (unitOfWork.Begin())
            {
                caseCard = await caseRepository.GetById(caseCard.Id);

                caseCard.Updated = DateTime.Now;
                await unitOfWork.CommitAsync();
            }

            using (unitOfWork.Begin())
            {
                caseCard = await caseRepository.GetById(caseCard.Id);

                Assert.NotNull(caseCard.Root);
                Assert.Equal(1, caseCard.CaseFolder.TrackingEvents.Count(t => t.OperationType == EventType.Updated));
                Assert.Equal(3, caseCard.CaseFolder.TrackingEvents.Count(t => t.OperationType == EventType.Inserted));
            }
        }