Esempio n. 1
0
        /*private static async Task LinkToDefaultRoot(DbContext db, ClassifierType type, Guid itemUid, CancellationToken cancellationToken)
         * {
         *      var defaultRoot = await GetDefaultRoot(db, type, cancellationToken);
         *
         *      // todo: insert default root group?
         *      if (defaultRoot != null)
         *      {
         *              await db.GetTable<DbClassifierLink>()
         *                      .Value(x => x.GroupUid, defaultRoot.Uid)
         *                      .Value(x => x.ItemUid, itemUid)
         *                      .InsertAsync(cancellationToken);
         *      }
         * }
         *
         * // todo: move to ClassifierGroupService.GetDefaultRoot()
         * private static async Task<DbClassifierGroup> GetDefaultRoot(DbContext db, ClassifierType type, CancellationToken cancellationToken)
         * {
         *      return await (
         *              from @group in db.GetTable<DbClassifierGroup>()
         *              where @group.TypeUid == type.Uid && @group.Code == ClassifierTree.DefaultCode
         *              select @group
         *      ).SingleOrDefaultAsync(cancellationToken);
         * }*/

        public virtual async Task <ApiResult> Update(Classifier item, CancellationToken cancellationToken)
        {
            var type = await _classifierTypeService.Get(item.Type, cancellationToken);

            var tree = type.HierarchyType == HierarchyType.Groups
                                ? await _classifierTreeService.GetClassifierTree(/*request.CompanyUid,*/ type.Code, ClassifierTree.DefaultCode, cancellationToken)
                                : null;

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

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

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

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

            ApiResult updateResult;

            using (var db = _dbContextFactory.Create())
            {
                updateResult = await UpdateInternal(db, type, tree, item, cancellationToken);

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

            // update fields
            result = await _fieldDataRepository.Update(manageFieldDataRequest, cancellationToken);

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

            return(updateResult);
        }
Esempio n. 2
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);
        }
Esempio n. 3
0
        public async Task <ApiResult> Validate(ManageFieldDataRequest request, CancellationToken cancellationToken)
        {
            var item     = request.Item ?? throw new ArgumentNullException(nameof(request.Metadata));
            var metadata = request.Metadata ?? throw new ArgumentNullException(nameof(request.Metadata));

            var data = item.Fields ?? (item.Fields = new FieldData());

            var errors = new List <ApiResultError>();

            // todo: validate/insert/update system fields stored in FieldData
            foreach (var field in metadata /*.Where(x => x.System == false)*/)
            {
                data.TryGetValue(field.Key, out var value);

                var fieldProvider = _fieldProviderRegistry.GetFieldTypeProvider(field.Type);

                if (fieldProvider.Validate(value, out var parsed, out var fieldErrors))
                {
                    data[field.Key] = parsed;
                }
Esempio n. 4
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
                });
            }
        }