protected async override Task <ClearanceRoot> Apply(OpenClearanceCommand command, CancellationToken cancellationToken)
        {
            var clearanceDto = await clearanceDtoRepository.GetClearanceIdForUserOnPeriod(command.UserId, command.StartDate, command.EndDate);

            if (clearanceDto == null)
            {
                var clearance = await ClearanceRoot.Open(
                    Domain.ClearanceId.FromGuid(command.ClearanceId),
                    Kernel.UserId.FromGuid(command.UserId),
                    Domain.AccountId.FromGuid(command.AccountId),
                    Kernel.Period.FromStartAndEndDate(command.StartDate, command.EndDate),
                    userSettingsService,
                    clearanceDtoRepository
                    );

                return(clearance);
            }
            else
            {
                AggregateRoot = await GetAggregateFromRepo();

                AggregateRoot.AddAccount(UserId.FromGuid(command.UserId), AccountId.FromGuid(command.AccountId));

                return(AggregateRoot);
            }
        }
Example #2
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);
        }