Beispiel #1
0
        public async Task UpdateOrganizationAsync(OrganizationDto dto, Guid loggedUserId)
        {
            if (dto == null)
            {
                throw new ArgumentNullException(nameof(dto));
            }

            var organizationInDb = await _organizationsRepository
                                   .GetByIdAsync(dto.Id)
                                   .ConfigureAwait(false);

            if (organizationInDb.RegisteredEmployees > 0 && organizationInDb.OrganizationShortcutName != dto.OrganizationShortcutName)
            {
                ValidationDictionary.AddModelError("Organization shortcut name cannot be changed",
                                                   "Organization shortcut name cannot be changed when registered employees are greater than 0.");

                return;
            }

            if ((dto.ExclusionStartDate.HasValue && !dto.ExclusionEndDate.HasValue) ||
                (!dto.ExclusionStartDate.HasValue && dto.ExclusionEndDate.HasValue))
            {
                ValidationDictionary.AddModelError("Both exclusion dates must have value",
                                                   "One of the exclusion dates has value and the other does not.");

                return;
            }

            var existinOrganizationType = await _organizationTypesRepository
                                          .AnyOrganizationTypeAsync(x => x.Id == dto.OrganizationTypeId)
                                          .ConfigureAwait(false);

            if (!existinOrganizationType)
            {
                ValidationDictionary
                .AddModelError("Organization type not found", $"Organization type with that id doesn't exist.");

                return;
            }

            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, loggedUserId)
                                                 .ConfigureAwait(false);

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

                return;
            }

            dto.LastUpdatedOn = DateTime.UtcNow;

            await UpdateAsync(dto).ConfigureAwait(false);

            var orgStatusCalculationDto = await _organizationsRepository
                                          .GetOrganizationStatusCalculationDataAsync(dto.Id)
                                          .ConfigureAwait(false);

            dto.Status = CalculateOrganizationStatus(orgStatusCalculationDto, true);

            await _organizationsRepository
            .UpdateOrganizationStatusAsync(dto.Id, dto.Status)
            .ConfigureAwait(false);

            /*var organizationEpaadId = await _organizationsRepository
             *  .GetOrganizationEpaadIdAsync(dto.Id)
             *  .ConfigureAwait(false);
             *
             * if (organizationEpaadId.HasValue)
             * {
             *  await UpdateOrganizationinEpaadAsync(organizationEpaadId.Value, dto)
             *      .ConfigureAwait(false);
             * }*/
        }
 public async Task <bool> AnyOrganizationTypeAsync(Expression <Func <OrganizationTypeDto, bool> > predicate)
 {
     return(await _organizationTypesRepository
            .AnyOrganizationTypeAsync(predicate)
            .ConfigureAwait(false));
 }