public async Task HandleAsync(UpdateEntryOrder message, IRequestInfo requestInfo) { var entries = await _repository.GetEntriesAsync(message.BusinessId); var specifications = entries.OrderBy(s => s.Order).ToList(); var updating = specifications.FirstOrDefault(d => d.Id == message.EntryId); if (updating is null) { _logger.LogWarning($"No entry found with ID: {message.EntryId}"); _publisher.PublishEvent(new EntryOrderUpdateRejected(Codes.InvalidId, $"Entry with the id {message.EntryId} could not be found."), requestInfo); return; } var replacing = specifications.FirstOrDefault(s => s.Order == message.Order); int oldOrder = updating.Order; _specificationAggregate.UpdateOrder(updating, message.Order); _specificationAggregate.UpdateOrder(replacing, oldOrder); await _repository.UpdateAsync(updating); await _repository.UpdateAsync(replacing); _publisher.PublishEvent(new EntryOrderUpdated(), requestInfo); }
public async Task HandleAsync(DeprecateDataEntry message, IRequestInfo requestInfo) { var entries = await _repository.GetEntriesAsync(message.BusinessId); var dataSpecifications = entries.ToList(); if (!dataSpecifications.Any()) { _logger.LogWarning($"No entries found with business ID: {message.BusinessId}"); _publisher.PublishEvent(new DataEntryDeprecationRejected(Codes.InvalidBusinessId, $"Entries with the id {message.BusinessId} could not be found."), requestInfo); return; } var spec = dataSpecifications.FirstOrDefault(d => d.Id == message.Id); if (spec is null) { _logger.LogWarning($"No entry found with ID: {message.Id}"); _publisher.PublishEvent(new DataEntryDeprecationRejected(Codes.InvalidId, $"Entry with the id {message.Id} could not be found."), requestInfo); return; } if (spec.IsMandatory) { _logger.LogInformation($"{spec.Label} is set as mandatory and cannot be deprecated."); _publisher.PublishEvent(new DataEntryDeprecationRejected(Codes.InvalidId, $"{spec.Label} is set as mandatory and cannot be deprecated."), requestInfo); return; } _specificationAggregate.Deprecate(spec); await _repository.UpdateAsync(spec); dataSpecifications.Remove(spec); dataSpecifications = dataSpecifications.Where(s => s.IsMandatory == false).ToList(); var ordered = dataSpecifications.OrderBy(d => d.Order).ToList(); for (int i = 0; i < ordered.Count; i++) { _specificationAggregate.UpdateOrder(ordered[i], i + 1); await _repository.UpdateAsync(ordered[i]); } _publisher.PublishEvent(new DataEntryDeprecated(), requestInfo); _logger.LogInformation($"Entry deprecated with id: {message.Id} and other entries re-ordered."); }
public async Task <IEnumerable <DataSpecificationDto> > HandleAsync(GetBusinessDataSpecifications query) { var specifications = await _repository.GetEntriesAsync(query.BusinessId); var ret = new List <DataSpecificationDto>(); foreach (var dataSpecification in specifications) { ret.Add(new DataSpecificationDto { Id = dataSpecification.Id, Order = dataSpecification.Order, ValidationCode = dataSpecification.ValidationCode, ValidationMessage = dataSpecification.ValidationMessage, Label = dataSpecification.Label, IsMandatory = dataSpecification.IsMandatory }); } return(ret.OrderBy(d => d.Order)); }
public async Task Validate(Guid businessId, IEnumerable <VisitorDataEntry> data) { var dataSpecs = await _specificationRepository.GetEntriesAsync(businessId); var dataSpecificationDocuments = dataSpecs.ToList(); var visitorDataEntries = data.ToList(); if (!dataSpecificationDocuments.Any()) { _logger.LogError($"No data specs for business with id {businessId}"); throw new VmsException(Codes.InvalidBusinessId, "There are no data fields for the business."); } foreach (var dataSpecificationDocument in dataSpecificationDocuments) { if (visitorDataEntries.FirstOrDefault(d => d.FieldId == dataSpecificationDocument.Id) == null) { throw new VmsException(Codes.InvalidFieldCount, "All the data fields have not been specified."); } } foreach (var entry in visitorDataEntries) { var spec = dataSpecificationDocuments.FirstOrDefault(s => s.Id == entry.FieldId); if (spec is null) { _logger.LogError($"No data specification found with id: {entry.FieldId}"); throw new VmsException(Codes.InvalidId, "The data presented does not match our specification"); } if (!_specificationValidator.IsDataValid(entry.Value, spec.ValidationCode)) { throw new VmsException(Codes.ValidationError, spec.ValidationMessage); } } }