public async Task <ActionResult <Message> > GetMessageAsync(string organizationId, string msgId, [FromServices] IMessageMergeService messageMergeService, [FromServices] IDifiMessageRepository difiMessageRepository, [FromServices] IElementsMessageRepository elementsMessageRepository) { try { var difiMessage = await difiMessageRepository.GetDifiMessageAsync(organizationId, msgId); var elementsMessage = await elementsMessageRepository.GetElementsMessageAsync(organizationId, msgId); var combinedMessage = messageMergeService.MergeMessages(difiMessage, elementsMessage); if (difiMessage == null && elementsMessage == null || combinedMessage == null) { return(StatusCode(500)); } return(combinedMessage); } catch (DifiException) { var elementsMessagesList = await elementsMessageRepository.GetElementsMessageAsync(organizationId, msgId); var combinedMessage = messageMergeService.MergeMessages(null, elementsMessagesList); return(combinedMessage); } catch (ElementsException) { var difiMessagesList = await difiMessageRepository.GetDifiMessageAsync(organizationId, msgId); var combinedMessage = messageMergeService.MergeMessages(difiMessagesList, null); return(combinedMessage); } catch (Exception ex) { return(StatusCode(400, ex.Message)); } }
// Merging based on Elements, should be used to merge outgoing messages //Takes two lists (list of elements messages and a list of difi messages). //Match each Elements message with the corresponding difi message, //if there is no match it merges the Elements message with NULL. //Merging is based on DifiMessage.messageId and ElementsMessage.ConversationId //Returns a list of merged messages into Message-object public async Task <IEnumerable <Message> > MergeMessagesListsOutAsync(string organizationId, IEnumerable <ElementsMessage> elementsMessages, IEnumerable <DifiMessage> difiMessages) { //The final list of Message objects that the method returns List <Message> listOfMessages = new List <Message>(); //The foreach loop iterates the list of elementsMessages foreach (ElementsMessage emsg in elementsMessages) { //Ensures that the method can run if received difiMessages list is null if (difiMessages == null) { var oneMessage = MergeMessages(null, emsg); listOfMessages.Add(oneMessage); continue; } try { //difiMatch = a single difiMessages entry where its messageId matches the iterated ElementsMessages.ConversationId var difiMatch = difiMessages.Single(difMessage => difMessage.messageId.Equals(emsg.ConversationId)); var message = MergeMessages(difiMatch, emsg); //Append the merged message to the list of merged messages to be returned listOfMessages.Add(message); } //This is triggered when there is no difi match, the elementsmessage is merged with the result from the repository and added to the list. catch (InvalidOperationException) { //Specific matching when missing in other message-list var difiMsg = await _difiMessageRepository.GetDifiMessageAsync(organizationId, emsg.ConversationId); var message = MergeMessages(difiMsg, emsg); listOfMessages.Add(message); } catch (Exception) { throw; } } return(listOfMessages); }