private void Handle(V1.ClearanceCreated @event) { Id = ClearanceId.FromGuid(@event.ClearanceId); UserAccounts = @event.UserAccounts.Select(x => UserAccount.FromUserGuidAndAccountGuid(x.Key, x.Value)).ToList(); Period = Period.FromStartAndEndDate(@event.StartDate, @event.EndDate); State = ClearanceState.CreateOpen(); }
} //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); }