private ServiceConnectionViewModel GenerateServiceConnectionVM(IEnumerable <UserChannel> userChannels) { //get channels' Ids var channelMagento = userChannels.SingleOrDefault(channel => channel.ChannelType == Shared.Enums.ChannelType.Magento); var channelEbay = userChannels.SingleOrDefault(channel => channel.ChannelType == Shared.Enums.ChannelType.Ebay); var channelEmail = userChannels.SingleOrDefault(channel => channel.ChannelType == Shared.Enums.ChannelType.Email); //assign channels into viewmodel ServiceConnectionViewModel model = new ServiceConnectionViewModel(); if (channelMagento != null) { model.channelMagento = mapper.Map <ChannelMagentoViewModel>( channelMagentoService.GetChannel(channelMagento.ChannelId) ); model.channelMagento.IsConnected = magentoService.IsConnected(model.channelMagento.Host, model.channelMagento.Username, model.channelMagento.Password); } if (channelEbay != null) { model.channelEbay = mapper.Map <ChannelEbayViewModel>( channelEbayService.GetChannel(channelEbay.ChannelId) ); var validateToken = ebayService.IsValidToken(model.channelEbay.Token); if (!validateToken.HasError && validateToken.Result) { model.channelEbay.IsConnected = true; } } if (channelEmail != null) { var channelEmailModel = channelEmailService.GetChannel(channelEmail.ChannelId); model.channelEmail = mapper.Map <ChannelEmailViewModel>(channelEmailModel); var token = mapper.Map <SmartHub.Channels.Gmail.Models.Token>(channelEmailModel); model.channelEmail.IsConnected = gmailService.IsConnected(token); } return(model); }
public void Proceed(Services.Models.Connection connection) { TextBuffer.WriteLine("EbayMagentoProcessor - Start"); TextBuffer.WriteLine("EbayMagentoProcessor - Proceed Connection " + connection.Id); var syncTime = DateTime.UtcNow; connection.Status = ConnectionStatus.Synchronizing; connectionService.Update(connection); var userChannelEbay = userChannelService.GetUserChannel(connection.UserChannelSource); var userChannelMagento = userChannelService.GetUserChannel(connection.UserChannelTarget); var channelEbay = channelEbayService.GetChannel(userChannelEbay.ChannelId); var channelMagento = channelMagentoService.GetChannel(userChannelMagento.ChannelId); var channelMagento_TimeZoneOffset = TimeZoneInfo.GetSystemTimeZones() .FirstOrDefault(x => x.DisplayName == channelMagento.TimeZoneDisplayName) .BaseUtcOffset; if (!ebayService.IsValidToken(channelEbay.Token).Result) { connection.Status = ConnectionStatus.Error; connection.Message = "Invalid Ebay Token"; connection.LastSyncedDate = syncTime; connection.NextSyncedDate = connection.LastSyncedDate.Value.AddMinutes(ebayMagentoTaskIntervalInMinutes); connection.Counter++; connectionService.Update(connection); return; } var magentoTokenCall = magentoService.GetToken(channelMagento.Host, channelMagento.Username, channelMagento.Password); if (magentoTokenCall.HasError) { connection.Status = ConnectionStatus.Error; connection.Message = "Invalid Magento Token"; connection.LastSyncedDate = syncTime; connection.NextSyncedDate = connection.LastSyncedDate.Value.AddMinutes(ebayMagentoTaskIntervalInMinutes); connection.Counter++; connectionService.Update(connection); return; } var magentoToken = magentoTokenCall.Result; #region Ebay to Magento //1. Pull Ebay MemberMessage and persist to local DB #region Pull MemberMessage from Ebay var lastSyncDate_Ebay_MemberMessage = channelEbay.LastSyncedDateMemberMessage ?? DateTime.UtcNow; var ebayMessageApi = ebayService.GetMemberMessage(channelEbay.Token, lastSyncDate_Ebay_MemberMessage, DateTime.UtcNow, MessageTypeCodeType.All, MessageStatusTypeCodeType.Unanswered); var ebayTickets = new List <TicketEbay>(); var ebayMessages = new List <MessageEbay>(); if (ebayMessageApi.HasError) { channelEbay.Message += ebayMessageApi.Error; } else { var memberMessages = ebayMessageApi.Result; foreach (var memberMessage in memberMessages) { var ticketEbay = GetOrCreateTicketEbay(memberMessage, connection, syncTime); ebayTickets.Add(ticketEbay); var messageEbay = CreateMessageEbay(memberMessage, ticketEbay, syncTime); ebayMessages.Add(messageEbay); } } channelEbay.LastSyncedDateMemberMessage = syncTime; channelEbayService.Update(channelEbay); #endregion //2. Transfer Ebay MemberMessage to Magento local DB #region Transfer Ebay MemberMessage to Magento local DB foreach (var ebayTicket in ebayTickets) { var magentoTicket = mapper.Map <TicketMagento>(ebayTicket); magentoTicket.Id = ticketMagentoService.CreateTicket(magentoTicket); ticketEbayMagentoService.Create(ebayTicket.Id, magentoTicket.Id); var ebayMessagesFilterByTicketId = ebayMessages.Where(x => x.TicketId == ebayTicket.Id); foreach (var ebayMessage in ebayMessagesFilterByTicketId) { var magentoMessage = mapper.Map <MessageMagento>(ebayMessage); magentoMessage.TicketId = magentoTicket.Id; magentoMessage.Id = messageMagentoService.CreateMessage(magentoMessage); messageEbayMagentoService.Create(ebayMessage.Id, magentoMessage.Id); } } #endregion //3. Push Magento Ticket and Message from local DB to remote site #region Push Magento Ticket and Message to Remote Site var magentoTickets = ticketMagentoService.GetMagentoTicketsForSyncByConnectionId(connection.Id); foreach (var magentoTicket in magentoTickets) { var createTicketRequest = CreateTicketMagentoRequest(magentoTicket, connection, channelMagento); var result = magentoService.CreateTicket(channelMagento.Host, magentoToken, createTicketRequest); if (result.HasError) { magentoTicket.SyncStatus = SyncStatus.Error; magentoTicket.SyncErrorMessage = result.Error; } else { magentoTicket.SyncStatus = SyncStatus.Ok; magentoTicket.SyncErrorMessage = string.Empty; magentoTicket.MagentoId = result.Result.ToString(); var magentoMessages = messageMagentoService.GetMessagesForSyncByTicketId(magentoTicket.Id); foreach (var magentoMessage in magentoMessages) { var sendMessageRequest = SendMessageMagentoRequest(magentoMessage, magentoTicket); var sendMsgResult = magentoService.SendMessage(channelMagento.Host, magentoToken, sendMessageRequest); if (sendMsgResult.HasError) { magentoMessage.SyncStatus = SyncStatus.Error; magentoMessage.SyncErrorMessage = sendMsgResult.Error; } else { magentoMessage.SyncStatus = SyncStatus.Ok; magentoMessage.SyncErrorMessage = string.Empty; magentoMessage.MagentoId = sendMsgResult.Result.ToString(); } magentoMessage.LastSynchronizedDate = syncTime; messageMagentoService.UpdateMessage(magentoMessage); } } magentoTicket.LastSynchronizedDate = syncTime; ticketMagentoService.UpdateTicket(magentoTicket); } #endregion #endregion #region Magento to Ebay //4. Pull Ticket and Message from Magento #region Pull Ticket/Message from Magento var lastSyncDate_Magento_Ticket = channelMagento.LastSyncedDateTicket ?? DateTime.UtcNow; var getTicketsMagentoRequest = new GetTicketsRequest() { connection_id = connection.Id.ToString(), from = lastSyncDate_Magento_Ticket.ToString(Shared.Constants.DateTimeFormat_Magento), to = syncTime.ToString(Shared.Constants.DateTimeFormat_Magento), }; var getTicketsResult = magentoService.GetTickets(channelMagento.Host, magentoToken, getTicketsMagentoRequest); if (getTicketsResult.HasError) { channelMagento.Message += getTicketsResult.Error; } else { channelMagento.LastSyncedDateTicket = syncTime; } var lastSyncDate_Magento_Message = channelMagento.LastSyncedDateMessage ?? DateTime.UtcNow; var getMessagesMagentoRequest = new GetMessagesRequest() { connection_id = connection.Id.ToString(), from = lastSyncDate_Magento_Message.ToString(Shared.Constants.DateTimeFormat_Magento), to = syncTime.ToString(Shared.Constants.DateTimeFormat_Magento), }; var getMessagesResult = magentoService.GetMessages(channelMagento.Host, magentoToken, getMessagesMagentoRequest); if (getMessagesResult.HasError) { channelMagento.Message += ". " + getTicketsResult.Error; } else { channelMagento.LastSyncedDateMessage = syncTime; } channelMagentoService.Update(channelMagento); #endregion //5. Persist Message reply by admin to local db #region Persist Message reply by admin to local db var pulledMagentMessages = new List <MessageMagento>(); if (!getMessagesResult.HasError) { foreach (var magentoMessage in getMessagesResult.Result) { var ticket = ticketMagentoService.Get(magentoMessage.ticket_id); if (ticket != null && !messageMagentoService.IsExisting(magentoMessage.message_id)) { var message = new MessageMagento() { MagentoId = magentoMessage.message_id, SenderId = ticket.RecipientId, SenderEmail = string.Empty, TicketId = ticket.Id, Message = magentoMessage.message, CreatedDate = DateTime.Parse(magentoMessage.created_date), LastModifiedDate = DateTime.Parse(magentoMessage.last_modified_date), Note = string.Empty, LastSynchronizedDate = syncTime, SyncStatus = SyncStatus.Ok, SyncErrorMessage = string.Empty, }; message.Id = messageMagentoService.CreateMessage(message); pulledMagentMessages.Add(message); } } } #endregion //6. Transfer Magento message to Ebay message #region Transfer Magento message to Ebay message foreach (var message in pulledMagentMessages) { var ebayMessage = mapper.Map <MessageEbay>(message); var ebayTicketId = ticketEbayMagentoService.GetByTicketMagentoId(message.TicketId).IdEbay; ebayMessage.TicketId = ebayTicketId; ebayMessage.Id = messageEbayService.CreateMessage(ebayMessage); } #endregion //7. Push Transfered ebay message to remote site #region Push Tranfered ebay message to remote site var transferEbayMessages = messageEbayService.GetMessagesForSyncByConnectionId(connection.Id); foreach (var message in transferEbayMessages) { var ticket = ticketEbayService.GetTicketById(message.TicketId); var ebayMessage = new Channels.Ebay.Models.MemberMessage() { ItemId = ticket.ItemId, ParentMessageId = ticket.EbayId, SenderId = message.SenderId, SenderEmail = message.SenderEmail, RecipientId = ticket.CreatorId, Body = message.Message, CreatedDate = message.CreatedDate, }; var result = ebayService.SendMessage(channelEbay.Token, ticket.Subject, ticket.EbayId, ebayMessage); if (result.HasError) { message.SyncStatus = SyncStatus.Error; message.Message = result.Error; } else { message.SyncStatus = SyncStatus.Ok; message.Message = string.Empty; } message.LastSynchronizedDate = syncTime; messageEbayService.UpdateMessage(message); } #endregion #endregion connection.Status = ConnectionStatus.Connected; connection.LastSyncedDate = syncTime; connection.NextSyncedDate = connection.LastSyncedDate.Value.AddMinutes(ebayMagentoTaskIntervalInMinutes); connection.Counter++; connectionService.Update(connection); TextBuffer.WriteLine("EbayMagentoProcessor - End Connection " + connection.Id); }