/// <summary> /// Adds a document to the queue. /// </summary> /// <param name="queueDocumentData">Queue document data.</param> /// <returns>Created document entity.</returns> public async Task <DocumentEntity> AddDocumentToQueue(QueueDocumentData queueDocumentData) { // Validate the document await _queueDocumentDataValidator.ValidateAndThrowAsync(queueDocumentData); if (Log.IsEnabled(LogEventLevel.Verbose)) { Log.Information("Queue document data {@queueDocumentData}", JsonConvert.SerializeObject(queueDocumentData)); } // Get the metadata from the package CdaPackageData cdaPackageData = _cdaPackageService.ExtractPackageData(queueDocumentData.CdaPackage); // Check if the document already exists with the same ID DocumentEntity documentEntity = await _dataStore.GetDocument(cdaPackageData.DocumentId); if (documentEntity != null) { throw new ArgumentException($"Document with ID '{cdaPackageData.DocumentId}' already exists in the queue with the '{documentEntity.Status.ToString()}' state"); } // Validate the replace if (!string.IsNullOrEmpty(queueDocumentData.DocumentIdToReplace)) { // Check if the document to replace exists var documentToReplace = await _dataStore.GetDocument(queueDocumentData.DocumentIdToReplace); if (documentToReplace == null) { throw new ArgumentException($"Document to replace with ID '{queueDocumentData.DocumentIdToReplace}' does not exist in the queue"); } // Check the replace document status if (documentToReplace.Status == DocumentStatus.RetryLimitReached || documentToReplace.Status == DocumentStatus.Removed) { throw new ArgumentException($"Document to replace cannot have a status of '{DocumentStatus.RetryLimitReached.ToString()}' or '{DocumentStatus.Removed.ToString()}' and is '{documentToReplace.Status.ToString()}'"); } } DateTime addedDateTime = DateTime.Now; // Create the document DocumentEntity documentToQueue = new DocumentEntity { QueueDate = addedDateTime, Status = DocumentStatus.Pending, DocumentId = cdaPackageData.DocumentId, DocumentIdToReplace = queueDocumentData.DocumentIdToReplace, FormatCodeName = queueDocumentData.FormatCodeName, FormatCode = queueDocumentData.FormatCode, DocumentData = queueDocumentData.CdaPackage, Ihi = cdaPackageData.Ihi }; // Add the create event documentToQueue.AddEvent(new EventEntity { Type = EventType.Created, EventDate = addedDateTime }); await _dataStore.AddDocument(documentToQueue); return(documentToQueue); }
public async Task <IHttpActionResult> AddDocument([FromBody] DocumentModel document) { if (document == null) { return(BadRequest("Document body required")); } if (document.Data == null) { return(BadRequest("Document data is missing")); } if (string.IsNullOrWhiteSpace(document.FormatCodeName)) { return(BadRequest("Format code name is null or empty")); } if (string.IsNullOrWhiteSpace(document.FormatCode)) { return(BadRequest("Format code is null or empty")); } if (document.ReplaceId != null && document.ReplaceId == string.Empty) { return(BadRequest("Replace ID cannot be empty")); } var queueDocumentData = new QueueDocumentData { CdaPackage = document.Data, DocumentIdToReplace = document.ReplaceId, FormatCode = document.FormatCode, FormatCodeName = document.FormatCodeName }; DocumentEntity documentEntity; try { documentEntity = await _queueManager.AddDocumentToQueue(queueDocumentData); } catch (ArgumentException e) // Thrown when the document to replace is invalid { return(BadRequest(e.Message)); } catch (ValidationException ve) // Thrown when the request is invalid { List <string> errors = ve.Errors.Select(e => e.ErrorMessage).ToList(); return(BadRequest(string.Join(" ", errors))); } catch (SignatureVerificationException sve) // Thrown when the package contents are invalid { return(BadRequest(sve.Message)); } catch (ZipException ze) // Thrown when the zip file is invalid { return(BadRequest(ze.Message)); } DocumentModel createdDocument = MapDocumentEntityToDocument(documentEntity); return(Created(new Uri(createdDocument.DocumentLink), createdDocument)); }