Example #1
0
        public async Task <ActionResult> UpdateLamaCompanyProfile([FromBody] LamaCompanyProfileSpecDto dto)
        {
            if (dto == null)
            {
                return(BadRequest());
            }

            var userId = HttpContext.User.Claims.First(x => x.Type == JwtRegisteredClaimNames.Jti).Value;

            var hasAccessToOrganization = await _authService
                                          .HasAccessToLamaCompanyAsync(Guid.Parse(userId), dto.Id)
                                          .ConfigureAwait(false);

            if (!hasAccessToOrganization)
            {
                return(Unauthorized());
            }

            await _lamaCompaniesService
            .UpdateLamaCompanyProfileAsync(dto, Guid.Parse(userId))
            .ConfigureAwait(false);

            if (!_lamaCompaniesService.ValidationDictionary.IsValid())
            {
                return(BadRequest(new { errors = _lamaCompaniesService.ValidationDictionary.GetErrorMessages() }));
            }

            return(NoContent());
        }
        public async Task UpdateLamaCompanyProfileAsync(LamaCompanyProfileSpecDto lamaCompanyProfileDto, Guid loggedUserId)
        {
            if (lamaCompanyProfileDto == null)
            {
                throw new ArgumentNullException(nameof(lamaCompanyProfileDto));
            }

            var userEmails     = lamaCompanyProfileDto.Users.Select(u => u.Email);
            var userIds        = lamaCompanyProfileDto.Users.Select(u => u.Id);
            var existingEmails = await GetExistingContactEmailsAsync(userEmails, lamaCompanyProfileDto.Id)
                                 .ConfigureAwait(false);

            if (existingEmails.Any())
            {
                ValidationDictionary.AddModelError("Already existing emails", string.Join(", ", existingEmails));

                return;
            }

            if (!userIds.Contains(loggedUserId))
            {
                ValidationDictionary.AddModelError("Delete yourself",
                                                   "You cannot delete yourself from the contact list of the organization");

                return;
            }

            foreach (var lamaUser in lamaCompanyProfileDto.Users)
            {
                if (lamaUser.SupportDefaultOrganizationTypes != null)
                {
                    var organizationTypeIds = lamaUser.SupportDefaultOrganizationTypes.Select(x => x.OrganizationTypeId);
                    if (!CheckForDuplicates(organizationTypeIds))
                    {
                        ValidationDictionary
                        .AddModelError($"Cannot set duplicate organization types for one user", lamaUser.Id.ToString());
                    }
                }
            }

            var deletedUsersAssignedToOrganizationEmails = await GetDeletedUsersEmailsThatHaveAssignedOrganizationAsync(userIds, lamaCompanyProfileDto.Id)
                                                           .ConfigureAwait(false);

            if (deletedUsersAssignedToOrganizationEmails.Any())
            {
                ValidationDictionary
                .AddModelError("User that you want to delete is assigned to an Organization", string.Join(", ", deletedUsersAssignedToOrganizationEmails));
            }

            if (!ValidationDictionary.IsValid())
            {
                return;
            }

            await _lamaCompaniesRepository
            .UpdateLamaCompanyProfileAsync(lamaCompanyProfileDto)
            .ConfigureAwait(false);
        }
        public async Task UpdateLamaCompanyProfileAsync(LamaCompanyProfileSpecDto lamaCompanyProfileDto)
        {
            await using var context = ContextFactory.CreateDataContext(null);
            var organizations = context.LamaCompanies;

            var entity   = Mapper.Map <LamaCompany>(lamaCompanyProfileDto);
            var dbEntity = await organizations
                           .Include(x => x.Users)
                           .ThenInclude(x => x.SupportPersonOrgTypeDefaults)
                           .FirstOrDefaultAsync(t => t.Id == entity.Id)
                           .ConfigureAwait(false);

            foreach (var user in dbEntity.Users)
            {
                if (!entity.Users.Any(i => i.Id == user.Id))
                {
                    context.Users.Remove(user);
                }
            }

            var dbEntityUsers = new List <User>(dbEntity.Users);

            foreach (var newContact in entity.Users)
            {
                var dbContact = dbEntityUsers.FirstOrDefault(i => i.Id == newContact.Id);
                if (dbContact == null)
                {
                    dbEntity.Users.Add(newContact);
                }
                else
                {
                    newContact.LamaCompanyId = dbEntity.Id;
                    context.Entry(dbContact).CurrentValues.SetValues(newContact);
                    context.Entry(dbContact).State = EntityState.Modified;
                }
            }

            OnBeforeUpdate(dbEntity);

            Mapper.Map(lamaCompanyProfileDto, dbEntity);

            context.Entry(dbEntity).State = EntityState.Modified;
            organizations.Update(dbEntity);

            await context.SaveChangesAsync().ConfigureAwait(false);
        }