Exemplo n.º 1
0
        }                                                                        //only for 2 persons, normally this should be a list

        public async static Task <ClearanceRoot> Open(
            ClearanceId clearanceId,
            UserId userId,
            AccountId accountId,
            Period period,
            IUserSettingsService userSettingsService,
            IClearanceDtoRepository clearanceDtoRepository)
        {
            //Does this accountId already have a clearance?
            var clearanceIdAccountAlreadyExist = await clearanceDtoRepository.GetClearanceIdByAccountId(accountId.Value);

            if (clearanceIdAccountAlreadyExist != null)
            {
                throw new InvalidOperationException("Only one clearance can be created per account");
            }

            //Check if for the user is already a clearance on this period
            var clearanceDto = await clearanceDtoRepository.GetClearanceIdForUserOnPeriod(userId.Value, period.StartDate, period.EndDate);

            if (clearanceDto != null)
            {
                throw new InvalidOperationException("A clearance for this group already created. Try adding user accountId to existing clearance.");
            }

            //Get all the users of the group where the user is belonging to, remove this user
            var users = (await userSettingsService.GetUserGroupByUserId(userId)).Except(new List <Guid> {
                userId
            });

            //Build the dictionary for each user an account
            var userAccounts = new Dictionary <Guid, Guid?>();

            userAccounts.Add(userId.Value, accountId);

            foreach (var user in users)
            {
                userAccounts.Add(user, null);
            }

            var clearance = new ClearanceRoot();

            clearance.Apply(new V1.ClearanceCreated(clearanceId.Value, userAccounts, period.StartDate, period.EndDate));

            return(clearance);
        }
 protected async override void PreHandle(CloseAccountCommand command)
 {
     AggregateId = ClearanceId.FromGuid((await clearanceDtoRepository.GetClearanceIdByAccountId(command.AccountId)).Value);
 }