public override async Task DoWork(CancellationToken cancellationToken)
        {
            Log.Information($"{nameof(AuditLogThumbprintJob)} {DateTime.Now:HH:mm:ss} is running the Job.");

            var transactionHistoryDate = DateTime.Now.AddDays(-1);

            try
            {
                var pdfModel = await _transformTransactionHistory.ToPdfModel(transactionHistoryDate);

                var dailyFingerPrintPdf = await _pdfService.GenerateTransactionHistory(pdfModel);

                if (_signerConfiguration.Base != null || _signerConfiguration.Url != null)
                {
                    dailyFingerPrintPdf = (await _signerClient.Seal(dailyFingerPrintPdf)).Output;
                }

                await _alfrescoHttpClient.CreateNode(AlfrescoNames.Aliases.Root,
                                                     new FormDataParam(dailyFingerPrintPdf, string.Format(JobsNames.DailyFingerPrintPdfName, transactionHistoryDate.ToString("yyyy-MM-dd"), _transactionHistoryConfiguration.Originator)),
                                                     ImmutableList <Parameter> .Empty
                                                     .Add(new Parameter(AlfrescoNames.Headers.RelativePath, JobsNames.DailyFingerPrintPath, ParameterType.GetOrPost)));
            }
            catch (Exception ex) when(ex is HttpClientException httpClientException && httpClientException.HttpStatusCode == HttpStatusCode.Conflict)
            {
                Log.Warning($"Fingerprint for {transactionHistoryDate:dd.MM.yyyy} already exists.");
            }
            catch (Exception ex)
            {
                Log.Error($"Couldn't create daily fingerprint. Message:{ex.Message}, StackTrace:{ex.StackTrace}");
            }
        }
        private async Task <NodeEntry> CreateComponentByPath(string documentId, string componentPath, IFormFile component, string ownerId)
        {
            var componentPID = await GenerateComponentPID(documentId, "/", GeneratePIDComponentType.Component);

            return(await _alfrescoHttpClient.CreateNode(AlfrescoNames.Aliases.Root, new FormDataParam(
                                                            await component.GetBytes(),
                                                            $"{IdGenerator.GenerateId()}{ System.IO.Path.GetExtension(component.FileName) }"
                                                            ), ImmutableList <Parameter> .Empty
                                                        .Add(new Parameter(SpisumNames.Properties.Pid, componentPID, ParameterType.GetOrPost))
                                                        .Add(new Parameter(SpisumNames.Properties.Ref, documentId, ParameterType.GetOrPost))
                                                        .Add(new Parameter(AlfrescoNames.Headers.NodeType, SpisumNames.NodeTypes.Component, ParameterType.GetOrPost))
                                                        .Add(new Parameter(AlfrescoNames.Headers.RelativePath, componentPath ?? SpisumNames.Paths.MailRoomUnfinished, ParameterType.GetOrPost))
                                                        .Add(new Parameter(SpisumNames.Properties.FileName, component.FileName, ParameterType.GetOrPost))
                                                        .Add(new Parameter(AlfrescoNames.ContentModel.Owner, ownerId, ParameterType.GetOrPost))
                                                        .Add(new Parameter(SpisumNames.Properties.Group, _identityUser.RequestGroup, ParameterType.GetOrPost))));
        }
        public async Task CheckSiteChilds <T, U>(bool isRecordManagement, List <ChildrenARM <T, U> > childs, bool groupStructure, string guid, List <GroupModel> configGroups)
        {
            var documentLibrary = await _alfrescoHttpClient.GetNodeInfo(guid, ImmutableList <Parameter> .Empty
                                                                        .Add(new Parameter(AlfrescoNames.Headers.RelativePath, AlfrescoNames.DocumentLibrary, ParameterType.QueryString)));

            var documentLibraryId = documentLibrary?.Entry?.Id;

            if (documentLibraryId == null)
            {
                Log.Error($"{guid} documentlibrary doesn't exist");
                return;
            }

            if (!groupStructure)
            {
                await CreateChilds(childs, documentLibraryId);

                return;
            }

            foreach (var group in configGroups.Where(x => x?.Body?.Id != null))
            {
                var permissions = Enum.GetValues(typeof(GroupPermissionTypes)).Cast <GroupPermissionTypes>().Select(x => new Permission {
                    Id = $"{group.Body.Id}_{x}", Role = x.ToString()
                }).ToList();
                permissions.Insert(0, new Permission {
                    Id = group.Body.Id, Role = "Consumer"
                });
                permissions.Insert(0, new Permission {
                    Id = $"{group.Body.Id}_Sign", Role = "Collaborator"
                });

                string nodeId = null;

                try
                {
                    var groupFolder = await _alfrescoHttpClient.GetNodeInfo(documentLibraryId, ImmutableList <Parameter> .Empty
                                                                            .Add(new Parameter(AlfrescoNames.Headers.RelativePath, group.Body.Id, ParameterType.QueryString)));

                    nodeId = groupFolder?.Entry?.Id;
                }
                catch
                {
                }

                if (nodeId == null)
                {
                    try
                    {
                        if (isRecordManagement)
                        {
                            var result = await _alfrescoHttpClient.CreateRecordCategory(documentLibraryId, new NodeBodyCreate
                            {
                                Name       = group.Body.Id,
                                NodeType   = "rma:recordFolder",
                                Properties = new Dictionary <string, object> {
                                    { "cm:title", group.Body.DisplayName },
                                    { "cm:description", group.Body.DisplayName }
                                }
                            });

                            nodeId = result?.Entry?.Id;

                            // update permissions
                            await SetPermissionInheritence(nodeId);
                        }
                        else
                        {
                            var result = await _alfrescoHttpClient.CreateNode(documentLibraryId, new NodeBodyCreate
                            {
                                Name       = group.Body.Id,
                                NodeType   = "cm:folder",
                                Properties = new Dictionary <string, object> {
                                    { "cm:title", group.Body.DisplayName },
                                    { "cm:description", group.Body.DisplayName }
                                }
                            });

                            nodeId = result?.Entry?.Id;
                        }
                    }
                    catch (Exception ex)
                    {
                        Log.Error(ex, "CheckSiteChilds Fail");
                    }
                }

                await CheckCreatePermissions(nodeId, permissions);
                await CreateChilds(childs, nodeId);
            }
        }
        public async Task <NodeEntry> ShreddingProposalCreate(string name, List <string> ids)
        {
            var nodes = new List <NodeEntry>();

            await ids.ForEachAsync(async x =>
            {
                #region Validations

                var nodeInfo = await _alfrescoHttpClient.GetNodeInfo(x);
                nodes.Add(nodeInfo);

                var properties    = nodeInfo.Entry.Properties.As <JObject>().ToDictionary();
                var discardReason = properties.GetNestedValueOrDefault(SpisumNames.Properties.DiscardReason)?.ToString();
                var discardTo     = properties.GetNestedValueOrDefault(SpisumNames.Properties.DiscardTo)?.ToString();
                var discardDate   = properties.GetNestedValueOrDefault(SpisumNames.Properties.DiscardDate)?.ToString();
                var borrower      = properties.GetNestedValueOrDefault(SpisumNames.Properties.Borrower)?.ToString();
                var proposalName  = properties.GetNestedValueOrDefault(SpisumNames.Properties.ProposalName)?.ToString();

                if (!string.IsNullOrWhiteSpace(proposalName))
                {
                    throw new BadRequestException("", "One or more of the ids cannot be used because they were used previously.");
                }

                var parentRm = await _nodesService.GetParentsByAssociation(x, new List <string>
                {
                    SpisumNames.Associations.DocumentInRepository,
                    SpisumNames.Associations.FileInRepository
                });

                var rmRef = parentRm.FirstOrDefault()?.Entry?.Id;

                if (string.IsNullOrWhiteSpace(rmRef))
                {
                    throw new BadRequestException("", "One or more of the ids are not in record management.");
                }

                var rmNodeInfo = await _alfrescoHttpClient.GetNodeInfo(rmRef);

                var propertiesRm = rmNodeInfo.Entry.Properties.As <JObject>().ToDictionary();
                var cutOffDate   = propertiesRm.GetNestedValueOrDefault(AlfrescoNames.ContentModel.CutOffDate)?.ToString();

                if (string.IsNullOrWhiteSpace(cutOffDate))
                {
                    throw new BadRequestException("", "One or more of the ids has not been cut off yet.");
                }

                if (!string.IsNullOrWhiteSpace(discardReason) ||
                    !string.IsNullOrWhiteSpace(discardTo) ||
                    !string.IsNullOrWhiteSpace(discardDate) ||
                    !string.IsNullOrWhiteSpace(borrower))
                {
                    throw new BadRequestException("", "One or more of ids is borrowed or removed of shredding plan");
                }

                #endregion
            });

            var shreddingPlanCreate = await _alfrescoHttpClient.CreateNode(AlfrescoNames.Aliases.Root, new FormDataParam(new byte[] { 01 }),
                                                                           ImmutableList <Parameter> .Empty
                                                                           .Add(new Parameter(AlfrescoNames.Headers.NodeType, SpisumNames.NodeTypes.ShreddingProposal, ParameterType.GetOrPost))
                                                                           .Add(new Parameter(AlfrescoNames.Headers.Name, IdGenerator.GenerateId(), ParameterType.GetOrPost))
                                                                           .Add(new Parameter(AlfrescoNames.Headers.RelativePath, SpisumNames.Paths.RepositoryShreddingProposal, ParameterType.GetOrPost))
                                                                           .Add(new Parameter(AlfrescoNames.Headers.Include, AlfrescoNames.Includes.Properties, ParameterType.QueryString))
                                                                           .Add(new Parameter(SpisumNames.Properties.Name, name, ParameterType.GetOrPost))
                                                                           .Add(new Parameter(SpisumNames.Properties.AssociationCount, nodes.Count, ParameterType.GetOrPost))
                                                                           .Add(new Parameter(SpisumNames.Properties.CreatedDate, DateTime.UtcNow.ToAlfrescoDateTimeString(), ParameterType.GetOrPost))
                                                                           .Add(new Parameter(SpisumNames.Properties.Author, _identityUser.Id, ParameterType.GetOrPost))
                                                                           );

            var shreddingPlan = await _alfrescoHttpClient.GetNodeInfo(shreddingPlanCreate?.Entry?.Id);

            var shredding = shreddingPlan?.Entry?.Properties.As <JObject>().ToDictionary();
            var pid       = shredding.GetNestedValueOrDefault(SpisumNames.Properties.Pid)?.ToString();

            await nodes.ForEachAsync(async x =>
            {
                await _nodesService.TryUnlockNode(x?.Entry?.Id);

                await _alfrescoHttpClient.CreateNodeSecondaryChildren(shreddingPlan?.Entry?.Id, new ChildAssociationBody
                {
                    AssocType = SpisumNames.Associations.ShreddingObjects,
                    ChildId   = x?.Entry?.Id
                });

                await _alfrescoHttpClient.UpdateNode(x?.Entry?.Id, new NodeBodyUpdate()
                                                     .AddProperty(SpisumNames.Properties.ProposalName, pid)
                                                     );

                await _alfrescoHttpClient.NodeLock(x?.Entry?.Id, new NodeBodyLock
                {
                    Type = NodeBodyLockType.FULL
                });
            });

            return(shreddingPlan);
        }