public async Task <Organisation> RegisterAsync(OrganisationRegistration organisationContract) { if (await repository.ExistsByAsync(o => o.Id == organisationContract.Id)) { throw new NameAlreadyUsedException(organisationContract.Name); } var organisationEntity = organisationContract.ToEntity(); organisationEntity = await repository.AddAsync(organisationEntity); var user = GeneratePasswordAndAttachAdminRole(organisationContract.AdminUser, organisationContract.Id); user.CreatedAt = DateTime.Now; user = await userRepository.AddAsync(user); var administrationTeam = new Domain.Entities.Team { Name = "org-admin", TeamCode = "org-admin", OrganisationId = organisationContract.Id, CreatedAt = DateTime.Now }; administrationTeam.Users.Add(new Domain.Entities.TeamUser(user)); administrationTeam = await teamRepository.AddAsync(administrationTeam); user.TeamsPermissions.Add(new Domain.Entities.TeamPermission { TeamCode = administrationTeam.TeamCode, IsOwner = true }); user = await userRepository.UpdateAsync(user); return(organisationEntity.ToContract()); }