private Dictionary <string, Deal> GetListOfDealsToBeUpdated(HashSet <string> setOfEmails) { Dictionary <string, Deal> dealsToUpdate = new Dictionary <string, Deal>(); //Using the email ids to get the deals to be updated foreach (var emailId in setOfEmails) { //get person ID from pipedrive using email id var PdPersonAccounts = ApiAccessHelper.GetResponseFromPipedriveApi(String.Format("/persons/find?term={0}", emailId), urlParameters: true); if (PdPersonAccounts != null) // Handling the possibility that no PD account exists for the email ID { foreach (var person in PdPersonAccounts) // Multiple persons possible with the same email { var pid = person["id"]; var allDealsForGivenPerson = ApiAccessHelper.GetResponseFromPipedriveApi(String.Format("/persons/{0}/deals", pid)); if (allDealsForGivenPerson != null) // ensuring that the person actually has deals associated with his account { foreach (var deal in allDealsForGivenPerson) { // Try getting the deal from the local list Deal tempDeal; if (!deal["active"]) { // Deal closed (won or lost), should not be updated by this run continue; } if (!dealsToUpdate.ContainsKey(deal["id"].ToString())) { // Deal object not present inside dealsToUpdate, but could be present inside AllDeals if (listOfAllDeals.ContainsKey(deal["id"].ToString())) { //We already have a deal inside allDeals. No need to create a new one. Just link the deal to dealsToUpdate tempDeal = listOfAllDeals[deal["id"] + ""]; } else { // Need to create a new deal tempDeal = new Deal(deal); // Add it to the allDeals listOfAllDeals.Add(tempDeal.id + "", tempDeal); } dealsToUpdate.Add(deal["id"] + "", tempDeal); } } } } } } return(dealsToUpdate); }
public Conversation(dynamic conv, dynamic events) { // Conversations extracted from "events" DO NOT contain full information (eg. list of messages) // Hence, we may need to get the entire conversation using the conversation ID this.id = conv["id"]; this.subject = conv["subject"]; this.primaryEmail = conv["recipient"]["handle"]; this.assignee = conv["assignee"]["first_name"]; this.createdAt = conv["created_at"]; this.lastMessage = conv["last_message"]["created_at"]; var fullConversationData = ApiAccessHelper.GetResponseFromFrontApi(String.Format("/conversations/{0}", id)); this.listOfMessages = new List <dynamic>(); // Returns an array of messages var response = ApiAccessHelper.GetResponseFromFrontApi(String.Format("/conversations/{0}/messages", id))["_results"]; foreach (var msg in response) { this.listOfMessages.Add(msg); // Convert to list } //todo integrate zapier emails for drift conversations string search = "[ZAPIER] Conversation with"; if (subject.Contains(search)) { //new drift conversation int start = search.Length; string email = null; if (start + 1 < subject.Length) { email = subject.Substring(start + 1); } setOfEmails = new HashSet <string>(); if (email != null) { setOfEmails.Add(email); } } else { this.setOfEmails = GetEmailsFromMessageList(listOfMessages); } this.PDDealsAffectedByConversation = GetListOfDealsToBeUpdated(setOfEmails); //! We do not directly add tags the conversation data because we also need the time when the tag was added. //! This is received from the 'tag' events foreach (var e in events) { if (e["type"] == "tag") { Tag t; if (!dictOfTags.TryGetValue(e["target"]["data"]["id"], out t)) { t = new Tag(e); //Add the tag to the list dictOfTags.Add(t.tagId, t); } } //if (e["type"] == "untag") { // ; //Untag event - removing the tag from the dictionary // if (dictOfTags.ContainsKey(e["target"]["data"]["id"])) { // dictOfTags.Remove(e["target"]["data"]["id"]); // } //} } var allTagIds = dictOfTags.Select(a => a.Key).ToList(); foreach (string key in allTagIds) { bool keep = false; foreach (var actualTag in fullConversationData["tags"]) { if (key == actualTag["id"]) { //Keep the tag keep = true; break; } } if (!keep) { //Remove the tag added from the tag event ; dictOfTags.Remove(key); } } }