public async Task <ActionResult> UpdateOrganizationProfile([FromBody] OrganizationProfileSpecDto dto) { if (dto == null) { return(BadRequest()); } var userId = HttpContext.User.Claims.First(x => x.Type == JwtRegisteredClaimNames.Jti).Value; var roleType = HttpContext.User.Claims.First(x => x.Type == ClaimTypes.Role).Value; var hasAccessToOrganization = await _authService .HasAccessToOrganizationAsync(roleType, Guid.Parse(userId), dto.Id) .ConfigureAwait(false); if (!hasAccessToOrganization) { return(Unauthorized()); } await _organizationsService .UpdateOrganizationProfileAsync(dto, Guid.Parse(userId)) .ConfigureAwait(false); if (!_organizationsService.ValidationDictionary.IsValid()) { return(BadRequest(new { errors = _organizationsService.ValidationDictionary.GetErrorMessages() })); } return(NoContent()); }
public async Task UpdateOrganizationProfileAsync(OrganizationProfileSpecDto dto, Guid userId) { if (dto == null) { throw new ArgumentNullException(nameof(dto)); } var contactEmails = dto.Contacts.Select(u => u.Email); var contactUserIds = dto.Contacts.Select(u => u.Id); var existingEmails = await GetExistingContactEmailsAsync(contactEmails, dto.Id) .ConfigureAwait(false); if (existingEmails.Any()) { ValidationDictionary.AddModelError("Already Existing Emails", string.Join(", ", existingEmails)); return; } var isLoggedUserPartOfOrganization = await _organizationsRepository .IsUserPartOfOrganizationAsync(dto.Id, userId) .ConfigureAwait(false); if (isLoggedUserPartOfOrganization && !contactUserIds.Contains(userId)) { ValidationDictionary .AddModelError("Delete yourself", "You cannot delete yourself from the contact list of the organization"); return; } await _organizationsRepository .UpdateOrganizationProfileAsync(dto) .ConfigureAwait(false); var orgStatusCalculationDto = await _organizationsRepository .GetOrganizationStatusCalculationDataAsync(dto.Id) .ConfigureAwait(false); var orgStatus = CalculateOrganizationStatus(orgStatusCalculationDto, true); await _organizationsRepository .UpdateOrganizationStatusAsync(dto.Id, orgStatus) .ConfigureAwait(false); }
public async Task UpdateOrganizationProfileAsync(OrganizationProfileSpecDto organizationDto) { await using var context = ContextFactory.CreateDataContext(null); var organizations = context.Organizations; var entity = Mapper.Map <Organization>(organizationDto); var dbEntity = await organizations .Include(x => x.Contacts) .FirstOrDefaultAsync(t => t.Id == entity.Id) .ConfigureAwait(false); foreach (var dbContact in dbEntity.Contacts) { if (!entity.Contacts.Any(i => i.Id == dbContact.Id)) { context.Users.Remove(dbContact); } } foreach (var newContact in entity.Contacts) { var dbContact = dbEntity.Contacts.FirstOrDefault(i => i.Id == newContact.Id); if (dbContact == null) { dbEntity.Contacts.Add(newContact); } else { newContact.OrganizationId = dbEntity.Id; context.Entry(dbContact).CurrentValues.SetValues(newContact); context.Entry(dbContact).State = EntityState.Modified; } } OnBeforeUpdate(dbEntity); Mapper.Map(organizationDto, dbEntity); context.Entry(dbEntity).State = EntityState.Modified; organizations.Update(dbEntity); await context.SaveChangesAsync().ConfigureAwait(false); }