Ejemplo n.º 1
0
        public async Task <IEnumerable <LockDto> > Handle(LockContractCommand request, CancellationToken cancellationToken)
        {
            _unitOfWork.BeginTransaction();

            try
            {
                var userName = _identityService.GetUserName();
                var locks    = new List <LockDto>();
                var lockDto  = await GenerateContractLockRowIfNotLocked(request.CompanyId, request.ResourceId, request.FunctionalContext, userName, request.ApplicationSessionId);

                locks.Add(lockDto);
                if (RequiresLockWholeFamily(request.FunctionalContext))
                {
                    LockFunctionalContext relativeFunctionalContext = GetRelativeFunctionalContext(request.FunctionalContext);
                    var relatives = await _lockRepository.GetContractRelativesAsync(request.CompanyId, request.ResourceId);

                    foreach (var relative in relatives)
                    {
                        var childLockDto = await GenerateContractLockRowIfNotLocked(request.CompanyId, relative, relativeFunctionalContext, userName, request.ApplicationSessionId);

                        locks.Add(childLockDto);
                    }
                }

                if (RequiresLockAllocatedContractWholeFamily(request.FunctionalContext))
                {
                    var information = await _lockRepository.GetSectionInformationAsync(request.CompanyId, request.ResourceId);

                    if (information.AllocatedTo != null)
                    {
                        var allocatedLockDto = await GenerateContractLockRowIfNotLocked(request.CompanyId, information.AllocatedTo.Value, LockFunctionalContext.Deallocation, userName, request.ApplicationSessionId);

                        locks.Add(allocatedLockDto);
                        var allocatedRelatives = await _lockRepository.GetContractRelativesAsync(request.CompanyId, information.AllocatedTo.Value);

                        foreach (var allocatedRelative in allocatedRelatives)
                        {
                            var childLockDto = await GenerateContractLockRowIfNotLocked(request.CompanyId, allocatedRelative, LockFunctionalContext.RelativeDeallocation, userName, request.ApplicationSessionId);

                            locks.Add(childLockDto);
                        }
                    }
                }

                var lockCreationResult = await _lockRepository.CreateLocksAsync(locks, request.CompanyId, userName);

                _unitOfWork.Commit();

                return(lockCreationResult);
            }
            catch
            {
                _unitOfWork.Rollback();
                throw;
            }
        }
Ejemplo n.º 2
0
 private static bool RequiresLockWholeFamily(LockFunctionalContext lockFunctionalContext)
 {
     return(lockFunctionalContext == LockFunctionalContext.TradeEdit ||
            lockFunctionalContext == LockFunctionalContext.Allocation ||
            lockFunctionalContext == LockFunctionalContext.Deallocation ||
            lockFunctionalContext == LockFunctionalContext.BulkAllocation ||
            lockFunctionalContext == LockFunctionalContext.BulkDeallocation ||
            lockFunctionalContext == LockFunctionalContext.TradeSplit ||
            lockFunctionalContext == LockFunctionalContext.TradeTranche);
 }
Ejemplo n.º 3
0
        private async Task <LockDto> GenerateContractLockRowIfNotLocked(string companyId, long resourceId, LockFunctionalContext lockFunctionalContext, string userName, string applicationSessionId)
        {
            var sectionInformation = await _lockRepository.GetSectionInformationAsync(companyId, resourceId);

            var resourceCode = sectionInformation != null ? sectionInformation.SectionCode : ResourceCodeNotFound;

            return(await GenerateLockRowIfNotLocked(companyId, resourceId, resourceCode, ResourceType.Contract, lockFunctionalContext, userName, applicationSessionId));
        }
Ejemplo n.º 4
0
        private async Task <List <LockDto> > GenerateInvoiceLockRowIfNotLocked(string companyId, long resourceId, LockFunctionalContext lockFunctionalContext, string userName, string applicationSessionId)
        {
            var locks = new List <LockDto>();
            var invoiceLinesInformation = await _lockRepository.GetInvoiceInformation(resourceId, companyId, null);

            var resourceCode = invoiceLinesInformation != null && invoiceLinesInformation.Any() ? invoiceLinesInformation.First().DocumentReference : ResourceCodeNotFound;
            var lockState    = await _lockQueries.GetLockStateAsync(companyId, resourceId, applicationSessionId, ResourceType.Invoice);

            if (lockState.IsLocked)
            {
                throw new AtlasBusinessException(LockMessage.GenerateLockMessage(lockState));
            }
            else
            {
                locks.Add(new LockDto()
                {
                    ApplicationSessionId    = applicationSessionId,
                    CompanyId               = companyId,
                    FunctionalContext       = lockFunctionalContext,
                    LockAcquisitionDateTime = DateTime.UtcNow,
                    LockOwner               = userName,
                    ResourceId              = resourceId,
                    ResourceCode            = resourceCode,
                    ResourceType            = ResourceType.Invoice
                });
            }

            if (invoiceLinesInformation != null)
            {
                foreach (var invoiceLineInformation in invoiceLinesInformation)
                {
                    var contractLockState = await _lockQueries.GetLockStateAsync(companyId, invoiceLineInformation.SectionId, applicationSessionId, ResourceType.Contract);

                    if (contractLockState.IsLocked)
                    {
                        throw new AtlasBusinessException(LockMessage.GenerateLockMessage(contractLockState));
                    }
                    else
                    {
                        locks.Add(new LockDto()
                        {
                            ApplicationSessionId    = applicationSessionId,
                            CompanyId               = companyId,
                            FunctionalContext       = lockFunctionalContext,
                            LockAcquisitionDateTime = DateTime.UtcNow,
                            LockOwner               = userName,
                            ResourceId              = invoiceLineInformation.SectionId,
                            ResourceCode            = invoiceLineInformation.SectionCode != null ? invoiceLineInformation.SectionCode : ResourceCodeNotFound,
                            ResourceType            = ResourceType.Contract
                        });
                    }
                }
            }

            return(locks);
        }
Ejemplo n.º 5
0
        private async Task <List <LockDto> > GenerateAccountingDocumentLockRowIfNotLocked(string companyId, long resourceId, LockFunctionalContext lockFunctionalContext, string userName, string applicationSessionId)
        {
            var locks = new List <LockDto>();
            var accountingDocumentInformation = await _lockRepository.GetAccountingDocumentInformation(resourceId, companyId, null);

            var resourceCode           = accountingDocumentInformation != null ? accountingDocumentInformation.DocumentReference : ResourceCodeNotFound;
            var lockAccountingDocument = await GenerateLockRowIfNotLocked(companyId, resourceId, resourceCode, ResourceType.AccountingDocument, lockFunctionalContext, userName, applicationSessionId);

            locks.Add(lockAccountingDocument);
            if (accountingDocumentInformation != null && accountingDocumentInformation.InvoiceId != null)
            {
                var locksInvoice = await GenerateInvoiceLockRowIfNotLocked(companyId, accountingDocumentInformation.InvoiceId.Value, LockFunctionalContext.AccountingDocumentEdition, userName, applicationSessionId);

                locks.AddRange(locksInvoice);
            }

            return(locks);
        }
Ejemplo n.º 6
0
        private async Task <LockDto> GenerateCashDocumentLockRowIfNotLocked(string companyId, long resourceId, LockFunctionalContext lockFunctionalContext, string userName, string applicationSessionId)
        {
            var cashDocumentInformation = await _lockRepository.GetCashDocumentInformation(resourceId);

            var resourceCode = cashDocumentInformation != null ? cashDocumentInformation.DocumentReference : ResourceCodeNotFound;

            return(await GenerateLockRowIfNotLocked(companyId, resourceId, resourceCode, ResourceType.CashDocument, lockFunctionalContext, userName, applicationSessionId));
        }
Ejemplo n.º 7
0
        private async Task <LockDto> GenerateUserProfileLockRowIfNotLocked(string companyId, long resourceId, LockFunctionalContext lockFunctionalContext, string userName, string applicationSessionId)
        {
            var userProfileInformation = await _lockRepository.GetUserProfileInformation(resourceId);

            var resourceCode = userProfileInformation != null ? userProfileInformation.Name : ResourceCodeNotFound;

            return(await GenerateLockRowIfNotLocked(companyId, resourceId, resourceCode, ResourceType.UserProfile, lockFunctionalContext, userName, applicationSessionId));
        }
Ejemplo n.º 8
0
        private async Task <LockDto> GenerateFxDealLockRowIfNotLocked(string companyId, long resourceId, LockFunctionalContext lockFunctionalContext, string userName, string applicationSessionId)
        {
            var fxDealInformation = await _lockRepository.GetFxDealInformationAsync(companyId, resourceId);

            var resourceCode = fxDealInformation != null ? fxDealInformation.Reference : ResourceCodeNotFound;

            return(await GenerateLockRowIfNotLocked(companyId, resourceId, resourceCode, ResourceType.FxDeal, lockFunctionalContext, userName, applicationSessionId));
        }
Ejemplo n.º 9
0
        private async Task <LockDto> GenerateLockRowIfNotLocked(string companyId, long resourceId, string resourceCode, string resouceType, LockFunctionalContext lockFunctionalContext, string userName, string applicationSessionId)
        {
            var lockState = await _lockQueries.GetLockStateAsync(companyId, resourceId, applicationSessionId, resouceType);

            if (lockState.IsLocked)
            {
                throw new AtlasBusinessException(LockMessage.GenerateLockMessage(lockState));
            }

            var lockRow = new LockDto()
            {
                ApplicationSessionId    = applicationSessionId,
                CompanyId               = companyId,
                FunctionalContext       = lockFunctionalContext,
                LockAcquisitionDateTime = DateTime.UtcNow,
                LockOwner               = userName,
                ResourceId              = resourceId,
                ResourceCode            = resourceCode != null ? resourceCode : ResourceCodeNotFound,
                ResourceType            = resouceType
            };

            return(lockRow);
        }
Ejemplo n.º 10
0
 private static bool RequiresLockAllocatedContractWholeFamily(LockFunctionalContext lockFunctionalContext)
 {
     return(lockFunctionalContext == LockFunctionalContext.Deallocation);
 }
Ejemplo n.º 11
0
 private static LockFunctionalContext GetRelativeFunctionalContext(LockFunctionalContext lockFunctionalContext)
 {
     return(relativeFunctionalContextMapping.ContainsKey(lockFunctionalContext)
         ? relativeFunctionalContextMapping[lockFunctionalContext]
         : LockFunctionalContext.RelativeTradeEdit);
 }