public async Task Execute() { var documents = await _localDatabaseGateway.GetDocumentsThatAreReadyForGovNotify(); LambdaLogger.Log("Document ids retrieved: " + JsonConvert.SerializeObject(documents)); documents.ForEach(async document => { LambdaLogger.Log($"Sending document. ID: {document.Id}"); try{ LambdaLogger.Log("Fetching PDF"); var pdfBytesResponse = _s3Gateway.GetPdfDocumentAsByteArray(document.Id); LambdaLogger.Log("Fetched from S3"); var sentStatus = _cominoGateway.GetDocumentSentStatus(document.CominoDocumentNumber); LambdaLogger.Log($"Got send status from comino {JsonConvert.SerializeObject(sentStatus)}"); if (sentStatus.Printed) { LambdaLogger.Log($"Document already printed. ID: {document.Id}"); await _localDatabaseGateway.UpdateStatus(document.Id, LetterStatusEnum.PrintedManually); await _logger.LogMessage(document.Id, $"Not sent to GovNotify. Document already printed, printed at {sentStatus.PrintedAt}"); return; } var govNotifyResponse = _govNotifyGateway.SendPdfDocumentForPostage(pdfBytesResponse, document.Id); if (govNotifyResponse.Success) { LambdaLogger.Log($"Document sent to notify. ID: {document.Id}"); _localDatabaseGateway.UpdateStatus(document.Id, LetterStatusEnum.SentToGovNotify).Wait(); _localDatabaseGateway.SaveSendNotificationId(document.Id, govNotifyResponse.NotificationId).Wait(); _logger.LogMessage(document.Id, $"Sent to Gov Notify. Gov Notify Notification Id {govNotifyResponse.NotificationId}").Wait(); _cominoGateway.MarkDocumentAsSent(document.CominoDocumentNumber); _logger.LogMessage(document.Id, "Removed from batch print queue and print date set in comino").Wait(); } else { LambdaLogger.Log($"Error sending to notify. ID: {document.Id}"); _localDatabaseGateway.UpdateStatus(document.Id, LetterStatusEnum.FailedToSend).Wait(); _logger.LogMessage(document.Id, $"Error Sending to GovNotify: {govNotifyResponse.Error}").Wait(); } }catch (Exception e) { LambdaLogger.Log(JsonConvert.SerializeObject(e)); throw; } }); }
private async Task AddLogMessageForEachDocument(List <DocumentDetails> docsWithTimestamps, string message) { foreach (var doc in docsWithTimestamps) { await _logger.LogMessage(doc.Id, message); } }
public async Task Execute(SQSEvent sqsEvent) { Console.WriteLine("Received message from SQS"); // expected Records count = 1, per batchSize configured in serverless.yml // Messages will be removed from the queue upon successful response of this lambda. // If no successful response within 30sec then they will be available to pick up from the queue again. Console.WriteLine("Getting document configuration"); var documentConfig = ParsingHelpers.GetDocumentConfig(); var automaticApprovals = documentConfig.AutomaticApprovals; var record = sqsEvent.Records.First(); var timestamp = record.Body; Console.WriteLine($"Received from queue [{record.EventSourceArn}] document timestamp = {timestamp}"); var document = await _getDocumentDetails.Execute(timestamp); Console.WriteLine($"Received Document {JsonConvert.SerializeObject(document)}"); if (document == null) { Console.WriteLine($"Could not find document for ID {timestamp} waiting to be processed in Dynamo"); return; } await _logger.LogMessage(timestamp, "Picked up document from queue - Processing"); Console.WriteLine($"Retrieved from dynamo, getting Html for documentId = {document.CominoDocumentNumber}"); var html = await TryGetDocumentAsHtml(document, timestamp); Console.WriteLine($"Received HTML: {(html == null ? "" : (html.Length < 100 ? html : html.Substring(0, 100)))}"); await TryConvertToPdf(html, document, timestamp); await TryStoreInS3(document, timestamp); if (automaticApprovals != null && automaticApprovals.Contains(document.LetterType)) { await _localDatabaseGateway.UpdateStatus(document.Id, LetterStatusEnum.ReadyForGovNotify); } else { await _localDatabaseGateway.UpdateStatus(document.Id, LetterStatusEnum.WaitingForApproval); } }
public async Task Execute() { var lettersToCheck = await _localDatabaseGateway.GetLettersWaitingForGovNotify(); lettersToCheck.ForEach(async letter => { try{ LambdaLogger.Log($"Checking status for letter {letter.Id}, doc no {letter.CominoDocumentNumber} Notify ID: {letter.GovNotifyNotificationId}"); var govNotifyResponse = _govNotifyGateway.GetStatusForLetter(letter.Id, letter.GovNotifyNotificationId); var updateStatusTask = _localDatabaseGateway.UpdateStatus(letter.Id, govNotifyResponse.Status); updateStatusTask.Wait(); var updateResponse = updateStatusTask.Result; LambdaLogger.Log($"Updated status in Local DB to {govNotifyResponse.Status}"); if (updateResponse.StatusUpdated) { _logger.LogMessage(letter.Id, $"Gov Notify status: {govNotifyResponse.Status.PrettierStatusName()}").Wait(); } }catch (NotifyClientException e) { LambdaLogger.Log($"Error checking status of document. ({e.Message})"); } }); }