Пример #1
0
        public virtual async Task <ApiResult> Insert(Classifier item, CancellationToken cancellationToken)
        {
            var type = await _classifierTypeService.Get(item.Type, cancellationToken);

            item.Uid ??= Guid.NewGuid();

            // todo: validate fields
            // todo: move to ClassifierValidator (?)
            var metadata = await _metadataService.GetMetadata(type, cancellationToken);

            var manageFieldDataRequest = new ManageFieldDataRequest
            {
                EntityTypeCode = Classifier.TypeCode,
                EntityUid      = item.Uid.Value,
                Metadata       = metadata,
                Item           = item
            };

            var result = await _fieldDataRepository.Validate(manageFieldDataRequest, cancellationToken);

            if (result.Success == false)
            {
                return(result);
            }

            ApiResult insertResult;

            using (var db = _dbContextFactory.Create())
            {
                insertResult = await InsertInternal(db, type, item, cancellationToken);

                if (insertResult.Success == false)
                {
                    return(insertResult);
                }
            }

            // insert fields
            // todo: exclude db fields and sections
            result = await _fieldDataRepository.Insert(manageFieldDataRequest, cancellationToken);

            if (result.Success == false)
            {
                return(result);
            }

            return(insertResult);
        }
Пример #2
0
        public async Task <ApiResult> Handle(CreateCompany request, CancellationToken cancellationToken)
        {
            var userUid = request.UserUid ?? throw new ArgumentNullException(nameof(request.UserUid));
            var company = request.Item ?? throw new ArgumentNullException(nameof(request.Item));

            var now = _dateTimeProvider.GetUtcNow();

            var companyUid  = Guid.NewGuid();
            var documentUid = Guid.NewGuid();

            var documentTypeRepository = _classifierRepositoryFactory.GetNamedOrDefaultService(Docs.ClassifierTypeCode.DocumentType);
            var documentType           = await documentTypeRepository.Get(Docs.ClassifierTypeCode.DocumentType, DocumentTypes.CompanyRegistrationRequest, cancellationToken);

            // todo: validate fields
            var metadata = await _fieldMetadataRepository.Search(new MetadataSearchRequest
            {
                EntityTypeCode = DocumentType.EntityTypeCode,
                EntityUid      = documentType.Uid.Value,
                // todo: check flags
                // IsSystem = false,
                IsActive   = true,
                SkipPaging = true
            }, cancellationToken);

            var manageFieldDataRequest = new ManageFieldDataRequest
            {
                EntityTypeCode = Document.TypeCode,
                EntityUid      = documentUid,
                Metadata       = metadata.Rows,
                Item           = company
            };

            // todo: move to Validator (?)
            var result = await _fieldDataRepository.Validate(manageFieldDataRequest, cancellationToken);

            if (result.Success == false)
            {
                return(result);
            }

            using (var scope = _unitOfWorkFactory.Create())
            {
                using (var db = _dbContextFactory.Create())
                {
                    // todo: валидация и ограничения

                    // company + todo: creation date
                    await db.GetTable <DbCompany>()
                    .Value(x => x.Uid, companyUid)
                    .Value(x => x.ConfigCode, company.ConfigCode ?? CompanyConfigCode.Company)
                    .Value(x => x.StatusCode, CompanyStatusCode.Draft)
                    .Value(x => x.Name, company.Name)
                    .InsertAsync(cancellationToken);

                    // user in company
                    await db.GetTable <DbCompanyUser>()
                    .Value(x => x.CompanyUid, companyUid)
                    .Value(x => x.UserUid, userUid)
                    .InsertAsync(cancellationToken);
                }

                // insert fields
                // todo: exclude db fields and sections
                await _fieldDataRepository.Insert(manageFieldDataRequest, cancellationToken);

                // todo: user roles

                // company registration request + todo: creation date
                var document = new Document
                {
                    Uid             = documentUid,
                    DocumentTypeUid = documentType.Uid.Value,
                    CompanyUid      = companyUid,
                    StatusCode      = DocumentStatusCode.Published,
                    Direction       = DocumentDirection.Outgoing,
                    DocumentDate    = now,
                    // Name = $"Company {company.Name} registration request"
                };

                await _documentRepository.Create(document, cancellationToken);

                // todo: audit log for company and for document
                await _auditLogService.Save(new AuditEvent
                {
                    EntityTypeCode = Company.EntityTypeCode,
                    EntityUid      = companyUid,
                    CompanyUid     = companyUid,
                    UserUid        = userUid,
                    CreatedAtUtc   = now,
                    MessageCode    = ExpressionHelper.GetFullName <CreateCompany.Resources>(x => x.CompanyCreated)
                }, cancellationToken);

                // todo: auto-approve request, notifications
                _jobManager.Enqueue <ISender>(x => x.Send(new RunAutomations
                {
                    EntityTypeCode = DocumentType.EntityTypeCode,
                    EntityTypeUid  = documentType.Uid.Value,
                    EntityUid      = documentUid
                }, cancellationToken));

                scope.Commit();

                return(new ApiResult {
                    Uid = companyUid
                });
            }
        }