public void DeallocateEntities(DeallocationExecutionRequest request) { var requests = new Collection <Entity>(); // bookings CreateUpdateRequests(requests, request.Bookings); // customer CreateUpdateRequests(requests, request.Customers); // cases CreateUpdateRequests(requests, request.Cases); // assign if (requests.Count > 0) { crmService.BulkUpdate(requests); } }
/// <summary> /// Covert CRM search results to collections of entities /// Implementation is based on assumption, that there cannot be one customer in 2 on-going bookings /// </summary> /// <param name="collection"></param> /// <returns></returns> public DeallocationExecutionRequest ConvertCrmResponse(EntityCollection collection, Collection <Guid> customerRelationUsers, Collection <Guid> customerRelationTeams) { if (collection == null || collection.Entities.Count == 0) { return(null); } var result = new DeallocationExecutionRequest { Bookings = new HashSet <Booking>(), Customers = new HashSet <Customer>(), Cases = new HashSet <Case>() }; var currentBookingId = Guid.Empty; var currentCustomerId = Guid.Empty; var fieldTeamId = AliasName.TeamAliasName + Attributes.Team.TeamId; var fieldContactId = AliasName.ContactAliasName + Attributes.Contact.ContactId; var fieldAccountId = AliasName.AccountAliasName + Attributes.Account.AccountId; var fieldContactCaseOwner = AliasName.ContactCaseAliasName + Attributes.CommonAttribute.Owner; var fieldAccountCaseOwner = AliasName.AccountCaseAliasName + Attributes.CommonAttribute.Owner; var fieldContactCaseId = AliasName.ContactCaseAliasName + Attributes.Case.CaseId; var fieldAccountCaseId = AliasName.AccountCaseAliasName + Attributes.Case.CaseId; var fieldBusinessUnitName = AliasName.BusinessUnitAliasName + Attributes.BusinessUnit.Name; foreach (var searchRecord in collection.Entities) { var owner = new Owner { Id = Guid.Parse(((AliasedValue)searchRecord[fieldTeamId]).Value.ToString()), OwnerType = OwnerType.Team }; var bookingId = Guid.Parse(searchRecord[Attributes.Booking.BookingId].ToString()); if (currentBookingId != bookingId) { // add booking entity var booking = new Booking { Id = bookingId, Owner = owner }; currentBookingId = bookingId; currentCustomerId = Guid.Empty; if (!result.Bookings.Contains(booking)) { result.Bookings.Add(booking); } } if (searchRecord.Contains(fieldContactId) || searchRecord.Contains(fieldAccountId)) { var isContact = searchRecord.Contains(fieldContactId); var customerId = Guid.Parse(((AliasedValue)searchRecord[isContact ? fieldContactId : fieldAccountId]).Value.ToString()); if (customerId != currentCustomerId) { // add customer entity var customer = new Customer { Id = customerId, Owner = owner, CustomerType = isContact ? CustomerType.Contact : CustomerType.Account }; currentCustomerId = customerId; if (!result.Customers.Contains(customer)) { result.Customers.Add(customer); } } } //Do this for UK only if (EntityRecords.BusinessUnit.GB.Equals(((AliasedValue)searchRecord[fieldBusinessUnitName]).Value.ToString(), StringComparison.InvariantCultureIgnoreCase) && (searchRecord.Contains(fieldContactCaseId) || searchRecord.Contains(fieldAccountCaseId))) { var isContact = searchRecord.Contains(fieldContactCaseId); var incidentId = Guid.Parse(((AliasedValue)searchRecord[isContact ? fieldContactCaseId : fieldAccountCaseId]).Value.ToString()); var caseOwner = (EntityReference)((AliasedValue)searchRecord[isContact ? fieldContactCaseOwner : fieldAccountCaseOwner]).Value; // add case entity var incident = new Case { Id = incidentId, StatusCode = CaseStatusCode.AssignedToLocalSourceMarket, State = CaseState.Active }; if (!customerRelationUsers.Contains(caseOwner.Id) && customerRelationTeams.Contains(owner.Id) && caseOwner.Id != owner.Id) { incident.Owner = owner; } if (!result.Cases.Contains(incident)) { result.Cases.Add(incident); } } } return(result); }