Untitled
unknown
csharp
a year ago
11 kB
5
Indexable
Never
public override async Task<RegistrationLocationEditOverviewResponse> UpdateRegistrationLocations(UpdateRegistrationLocationsRequest request, ServerCallContext context) { SetRequestContext(context); var validationResult = _updateRegistrationLocationsValidator.Validate(request); if (!validationResult.IsValid) { throw new RpcValidationException($"{nameof(UpdateRegistrationLocationsRequest)} is not valid", validationResult.Errors); } var registration = await _context.Registrations .FirstOrDefaultAsync(e => e.Id == request.RegistrationId); if (registration == null) { throw new RpcValidationException($"Geen registratie gevonden met id: '{request.RegistrationId}'", nameof(UpdateRegistrationLocationsRequest.RegistrationId), request.RegistrationId, StatusCode.NotFound); } var registrationLocationChoices = await _context.RegistrationLocationChoices.Include(i => i.EducationLevelChoice) .Include(i => i.PriorityGroupChoices) .Include(i => i.Location) .ThenInclude(i => i!.Address) .Where(e => e.EducationLevelChoice.RegistrationId == request.RegistrationId && e.EducationLevelChoice.EducationLevel == request.EducationLevelId) .ToListAsync(); var existingLocationIds = registrationLocationChoices.Select(e => e.LocationId) .ToList(); using var trans = _context.Database.BeginTransaction(); var trackableUser = context.GetHttpContext().GetOrThrowTrackableUser(); // Find the registrationLocationChoices that are removed == the ones that aren't present in the request var locationIds = request.Requests.Select(e => e.LocationId); var registrationLocationChoicesToRemove = registrationLocationChoices.Where(e => !locationIds.Contains(e.LocationId)); // Make sure to first store the audit changes before performing any other action! var hasChanges = _context.ChangeTracker.HasChanges(); _context.RegistrationLocationChoices.RemoveRange(registrationLocationChoicesToRemove); await _context.SaveRegistrationRelatedChangesWithAuditAsync(trackableUser, registration.Id, AuditEntity.RegistrationLocationChoice, DefaultAuditActionType.Delete); // Add the new registrationLocationChoices var newRegistrationLocationChoices = new List<RegistrationLocationChoice>(); foreach (var updateRequest in request.Requests) { if (existingLocationIds.Contains(updateRequest.LocationId)) { // Update the preference index var registrationLocationChoiceToUpdate = registrationLocationChoices.First(e => e.LocationId == updateRequest.LocationId); Update_SchoolLocationChoice_PreferenceIndex(registration, registrationLocationChoiceToUpdate.EducationLevelChoice, registrationLocationChoiceToUpdate, updateRequest.PreferenceIndex); // Potentially update the national number for prio group brother sister var priorityGroupChoiceBrotherSister = registrationLocationChoiceToUpdate.PriorityGroupChoices!.FirstOrDefault(e => e.UnderRepresentedGroupId == (int)PriorityGroup.BrotherSister); // Prio choice brother sister doesn't exists, and needs to be created? if (priorityGroupChoiceBrotherSister == null && !string.IsNullOrEmpty(updateRequest.NationalNumberSiblings) && !string.IsNullOrWhiteSpace(updateRequest.NationalNumberSiblings)) { // Add a new prio choice for brother/sister since it doesn't yet exist and national number was provided var newPrioGroupChoiceSibiling = new RegistrationPriorityChoice() { UnderRepresentedGroupId = (int)PriorityGroup.BrotherSister, Rijksregisternummer = _encryptionService.Encrypt(updateRequest.NationalNumberSiblings), CheckPriorityStatus = CheckPriorityStatus.NotChecked, Answer1 = RegistrationPriorityGroupChoiceAnswer.Yes, HasPriority = true, }; registrationLocationChoiceToUpdate.PriorityGroupChoices!.Add(newPrioGroupChoiceSibiling); } else if (priorityGroupChoiceBrotherSister != null) { updateRequest.NationalNumberSiblings = new string(updateRequest.NationalNumberSiblings.Where(char.IsDigit).ToArray()); if (!string.IsNullOrEmpty(updateRequest.NationalNumberSiblings) && !string.IsNullOrWhiteSpace(updateRequest.NationalNumberSiblings)) { priorityGroupChoiceBrotherSister.Rijksregisternummer = _encryptionService.Encrypt(updateRequest.NationalNumberSiblings); priorityGroupChoiceBrotherSister.Answer1 = RegistrationPriorityGroupChoiceAnswer.Yes; priorityGroupChoiceBrotherSister.CheckPriorityStatus = CheckPriorityStatus.NotChecked; priorityGroupChoiceBrotherSister.HasPriority = true; } else { // Empty national number, so remove the prio registrationLocationChoiceToUpdate.PriorityGroupChoices!.Remove(priorityGroupChoiceBrotherSister); } } // Potentially update the national number for prio group kvp var priorityGroupChoiceKVP = registrationLocationChoiceToUpdate.PriorityGroupChoices!.FirstOrDefault(e => e.UnderRepresentedGroupId == (int)PriorityGroup.KVP); if (priorityGroupChoiceKVP == null && !string.IsNullOrEmpty(updateRequest.NationalNumberKVP) && !string.IsNullOrWhiteSpace(updateRequest.NationalNumberKVP)) { var newPrioGroupChoiceKVP = new RegistrationPriorityChoice() { UnderRepresentedGroupId = (int)PriorityGroup.KVP, Rijksregisternummer = _encryptionService.Encrypt(updateRequest.NationalNumberKVP), CheckPriorityStatus = CheckPriorityStatus.NotChecked, Answer1 = RegistrationPriorityGroupChoiceAnswer.Yes, HasPriority = true, }; registrationLocationChoiceToUpdate.PriorityGroupChoices!.Add(newPrioGroupChoiceKVP); } else if (priorityGroupChoiceKVP != null) { updateRequest.NationalNumberKVP = new string(updateRequest.NationalNumberKVP.Where(char.IsDigit).ToArray()); if (!string.IsNullOrEmpty(updateRequest.NationalNumberKVP) && !string.IsNullOrWhiteSpace(updateRequest.NationalNumberKVP)) { priorityGroupChoiceKVP.Rijksregisternummer = _encryptionService.Encrypt(updateRequest.NationalNumberKVP); priorityGroupChoiceKVP.Answer1 = RegistrationPriorityGroupChoiceAnswer.Yes; priorityGroupChoiceKVP.CheckPriorityStatus = CheckPriorityStatus.NotChecked; priorityGroupChoiceKVP.HasPriority = true; } else { // Empty national number, so remove the prio registrationLocationChoiceToUpdate.PriorityGroupChoices!.Remove(priorityGroupChoiceKVP); } } } else { // New var newRegistrationLocationChoice = new RegistrationLocationChoice() { LocationId = updateRequest.LocationId, }; // if RegistrationEducationLevelChoices exists use it, else create new one var redlc = _context.RegistrationEducationLevelChoices.FirstOrDefault(r => r.RegistrationId == request.RegistrationId && r.EducationLevel == request.EducationLevelId); if (redlc != null) { newRegistrationLocationChoice.EducationLevelChoice = redlc; } else { newRegistrationLocationChoice.EducationLevelChoice = new RegistrationEducationLevelChoice() { RegistrationId = request.RegistrationId, EducationLevel = request.EducationLevelId, }; } Update_SchoolLocationChoice_PreferenceIndex(registration, newRegistrationLocationChoice.EducationLevelChoice, newRegistrationLocationChoice, updateRequest.PreferenceIndex); var prioGroupChoices = new List<RegistrationPriorityChoice>(); if (!string.IsNullOrEmpty(updateRequest.NationalNumberSiblings) && !string.IsNullOrWhiteSpace(updateRequest.NationalNumberSiblings)) { updateRequest.NationalNumberSiblings = new string(updateRequest.NationalNumberSiblings.Where(char.IsDigit).ToArray()); var newPrioGroupChoiceBrotherSister = new RegistrationPriorityChoice() { UnderRepresentedGroupId = (int)PriorityGroup.BrotherSister, Rijksregisternummer = _encryptionService.Encrypt(updateRequest.NationalNumberSiblings), CheckPriorityStatus = CheckPriorityStatus.NotChecked, Answer1 = RegistrationPriorityGroupChoiceAnswer.Yes, HasPriority = true, }; prioGroupChoices.Add(newPrioGroupChoiceBrotherSister); } if (!string.IsNullOrEmpty(updateRequest.NationalNumberKVP) && !string.IsNullOrWhiteSpace(updateRequest.NationalNumberKVP)) { updateRequest.NationalNumberKVP = new string(updateRequest.NationalNumberKVP.Where(char.IsDigit).ToArray()); var newPrioGroupChoiceKVP = new RegistrationPriorityChoice() { UnderRepresentedGroupId = (int)PriorityGroup.KVP, Rijksregisternummer = _encryptionService.Encrypt(updateRequest.NationalNumberKVP), CheckPriorityStatus = CheckPriorityStatus.NotChecked, Answer1 = RegistrationPriorityGroupChoiceAnswer.Yes, HasPriority = true, }; prioGroupChoices.Add(newPrioGroupChoiceKVP); } newRegistrationLocationChoice.PriorityGroupChoices = prioGroupChoices; _context.RegistrationLocationChoices.Add(newRegistrationLocationChoice); } } // Make sure to first store the audit changes before performing any other action! var hasChanges2 = _context.ChangeTracker.HasChanges(); var auditComment = $"{registration.FirstName} {registration.Name}"; await _context.SaveRegistrationRelatedChangesWithAuditAsync(trackableUser, registration.Id, AuditEntity.RegistrationLocationChoice, DefaultAuditActionType.Update, comment: auditComment); await trans.CommitAsync(); if (hasChanges || hasChanges2) { await SendRegistrationMail_Update(registration, "Schoolkeuzes"); } return await GetRegistrationLocationEditOverview(new() { RegistrationId = request.RegistrationId, EducationLevelId = request.EducationLevelId }, context); }