HasMaximum() public method

public HasMaximum ( System.Guid notificationId ) : Task
notificationId System.Guid
return Task
コード例 #1
0
ファイル: MovementFactory.cs プロジェクト: DEFRA/prsd-iws
        public async Task <Movement> Create(Guid notificationId, DateTime actualMovementDate)
        {
            await dateValidator.EnsureDateValid(notificationId, actualMovementDate);

            var hasMaximumMovements = await numberOfMovements.HasMaximum(notificationId);

            if (hasMaximumMovements)
            {
                throw new InvalidOperationException(
                          string.Format("Cannot create new movement for notification {0} which has used all available movements",
                                        notificationId));
            }

            var hasMaximumActiveLoads = await numberOfActiveLoads.HasMaximum(notificationId, actualMovementDate);

            if (hasMaximumActiveLoads)
            {
                throw new InvalidOperationException(
                          string.Format("Cannot create new movement for notification {0} because permitted active loads would be exceeded",
                                        notificationId));
            }

            var quantityRemaining = await movementsQuantity.Remaining(notificationId);

            if (quantityRemaining <= new ShipmentQuantity(0, quantityRemaining.Units))
            {
                throw new InvalidOperationException(
                          string.Format("Cannot create new movement for notification {0} as the quantity has been reached or exceeded.",
                                        notificationId));
            }

            var notificationStatus = (await assessmentRepository.GetByNotificationId(notificationId)).Status;

            if (notificationStatus != NotificationStatus.Consented)
            {
                throw new InvalidOperationException(
                          string.Format("Cannot create a movement for notification {0} because its status is {1}",
                                        notificationId, notificationStatus));
            }

            var financialGuaranteeCollection = await financialGuaranteeRepository.GetByNotificationId(notificationId);

            if (!financialGuaranteeCollection.FinancialGuarantees.Any(fg => fg.Status == FinancialGuaranteeStatus.Approved))
            {
                throw new InvalidOperationException(
                          string.Format("Cannot create a movement for notification {0} because there are no approved financial guarantees",
                                        notificationId));
            }

            var consentPeriodExpired = await consentPeriod.HasExpired(notificationId);

            if (consentPeriodExpired)
            {
                throw new InvalidOperationException(
                          string.Format("Cannot create new movement for notification {0} because the consent period has passed",
                                        notificationId));
            }

            var newNumber = await numberGenerator.Generate(notificationId);

            return(new Movement(newNumber, notificationId, actualMovementDate, userContext.UserId));
        }