public async Task<ServiceResponseAPI> InvokeCreateMatter(IAuthenticatedWho authenticatedWho, ServiceRequestAPI serviceRequest)
        {
            ServiceResponseAPI serviceResponse = null;
            DateTime whenDate = DateTime.Now;
            List<ObjectAPI> taskObjects = null;
            ObjectAPI taskObject = null;

            string authenticationUrl = null;
            string username = null;
            string password = null;

            string recordNumber = null;
            string description = null;

            string matterId = null;

            // Grab the configuration values from the service request
            authenticationUrl = SettingUtils.GetConfigurationValue(DocordoServiceSingleton.SERVICE_VALUE_DOCORDO_DOMAIN, serviceRequest.configurationValues, true);
            username = SettingUtils.GetConfigurationValue(DocordoServiceSingleton.SERVICE_VALUE_DOCORDO_USERNAME, serviceRequest.configurationValues, true);
            password = SettingUtils.GetConfigurationValue(DocordoServiceSingleton.SERVICE_VALUE_DOCORDO_PASSWORD, serviceRequest.configurationValues, true);            

            if (serviceRequest.authorization != null)
            {
                // Get the message from the inputs
                recordNumber = ValueUtils.GetContentValue(DocordoServiceSingleton.SERVICE_INPUT_RECORD_NUMBER, serviceRequest.inputs, true);
                description = ValueUtils.GetContentValue(DocordoServiceSingleton.SERVICE_INPUT_MATTER_DESCRIPTION, serviceRequest.inputs, true);

                // Create a task object to save back to the system
                taskObject = new ObjectAPI();
                taskObject.developerName = "Matter";
                taskObject.properties = new List<PropertyAPI>();
                taskObject.properties.Add(new PropertyAPI() { developerName = "RecordNumber", contentValue = description });                
                taskObject.properties.Add(new PropertyAPI() { developerName = "Description", contentValue = description });                

                // Add the object to the list of objects to save
                taskObjects = new List<ObjectAPI>();
                taskObjects.Add(taskObject);
                
                // Save the task object to docordo
                DocordoLoginResponse docordoLoginResponse = DocordoAPI.DocordoService.GetInstance().Login(authenticationUrl, username, password);

                taskObjects = DocordoDataSingleton.GetInstance().CreateMatter(authenticationUrl, docordoLoginResponse.EbikkoSessionId, docordoLoginResponse.CookieJSESSIONID, recordNumber, description);

                // Check to see if anything came back as part of the save - it should unless there was a fault
                if (taskObjects != null &&
                    taskObjects.Count > 0)
                {
                    // Grab the first object from the returned task objects
                    taskObject = taskObjects[0];

                    // Grab the id from that object - this needs to be returned in our outputs
                    matterId = taskObject.externalId;
                }
                else
                {
                    // If we didn't get any objects back, we need to throw an error
                    String errorMessage = "Task could not be created for an unknown reason.";

                    ErrorUtils.SendAlert(authenticatedWho, ErrorUtils.ALERT_TYPE_FAULT, "*****@*****.**", "DocordoPlugin", errorMessage);

                    throw ErrorUtils.GetWebException(HttpStatusCode.InternalServerError, errorMessage);
                }

            }
            else
            {
                // Alert the admin that no one is in the authorization context
                ErrorUtils.SendAlert(authenticatedWho, ErrorUtils.ALERT_TYPE_WARNING, "*****@*****.**", "DocordoPlugin", "The service request does not have an authorization context, so there's no one to notify.");
            }

            // Construct the service response
            serviceResponse = new ServiceResponseAPI();
            serviceResponse.invokeType = ManyWhoConstants.INVOKE_TYPE_FORWARD;
            serviceResponse.token = serviceRequest.token;
            serviceResponse.outputs = new List<EngineValueAPI>();
            serviceResponse.outputs.Add(new EngineValueAPI() { contentType = ManyWhoConstants.CONTENT_TYPE_STRING, contentValue = matterId, developerName = DocordoServiceSingleton.SERVICE_OUTPUT_ID });

            return serviceResponse;
        }
Example #2
0
        public string Response(IAuthenticatedWho authenticatedWho, string tenantId, string callbackUri, ServiceResponseAPI serviceResponse)
        {
            HttpClient          httpClient          = null;
            HttpContent         httpContent         = null;
            HttpResponseMessage httpResponseMessage = null;
            string invokeType = null;

            Policy.Handle <ServiceProblemException>().Retry(HttpUtils.MAXIMUM_RETRIES).Execute(() =>
            {
                using (httpClient = HttpUtils.CreateHttpClient(authenticatedWho, tenantId, null))
                {
                    // Use the JSON formatter to create the content of the request body
                    httpContent = new StringContent(JsonConvert.SerializeObject(serviceResponse));
                    httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");

                    // Post the engine invoke request over to ManyWho
                    httpResponseMessage = httpClient.PostAsync(callbackUri, httpContent).Result;

                    // Check the status of the response and respond appropriately
                    if (httpResponseMessage.IsSuccessStatusCode)
                    {
                        // Get the invoke type from the response message
                        invokeType = httpResponseMessage.Content.ReadAsStringAsync().Result;
                    }
                    else
                    {
                        throw new ServiceProblemException(new ServiceProblem(callbackUri, httpResponseMessage, string.Empty));
                    }
                }
            });

            return(invokeType);
        }
Example #3
0
        public ServiceResponseAPI InvokeCreateEvent(INotifier notifier, IAuthenticatedWho authenticatedWho, ServiceRequestAPI serviceRequest)
        {
            List <ObjectDataTypePropertyAPI> objectDataTypeProperties = null;
            ServiceResponseAPI serviceResponse   = null;
            DateTime           whenDate          = DateTime.Now;
            List <ObjectAPI>   eventObjects      = null;
            ObjectAPI          eventObject       = null;
            String             authenticationUrl = null;
            String             username          = null;
            String             password          = null;
            String             securityToken     = null;
            String             adminEmail        = null;
            String             when        = null;
            String             duration    = null;
            String             description = null;
            String             subject     = null;
            String             eventId     = null;

            // Grab the configuration values from the service request
            authenticationUrl = ValueUtils.GetContentValue(SalesforceServiceSingleton.SERVICE_VALUE_AUTHENTICATION_URL, serviceRequest.configurationValues, true);
            username          = ValueUtils.GetContentValue(SalesforceServiceSingleton.SERVICE_VALUE_USERNAME, serviceRequest.configurationValues, true);
            password          = ValueUtils.GetContentValue(SalesforceServiceSingleton.SERVICE_VALUE_PASSWORD, serviceRequest.configurationValues, true);
            securityToken     = ValueUtils.GetContentValue(SalesforceServiceSingleton.SERVICE_VALUE_SECURITY_TOKEN, serviceRequest.configurationValues, false);
            adminEmail        = ValueUtils.GetContentValue(SalesforceServiceSingleton.SERVICE_VALUE_ADMIN_EMAIL, serviceRequest.configurationValues, true);

            if (serviceRequest.authorization != null)
            {
                // Get the message from the inputs
                when        = ValueUtils.GetContentValue(SalesforceServiceSingleton.SERVICE_INPUT_WHEN, serviceRequest.inputs, true);
                duration    = ValueUtils.GetContentValue(SalesforceServiceSingleton.SERVICE_INPUT_DURATION, serviceRequest.inputs, true);
                description = ValueUtils.GetContentValue(SalesforceServiceSingleton.SERVICE_INPUT_DESCRIPTION, serviceRequest.inputs, true);
                subject     = ValueUtils.GetContentValue(SalesforceServiceSingleton.SERVICE_INPUT_SUBJECT, serviceRequest.inputs, true);

                // Get the when date for the provided command
                whenDate = DateUtils.CreateDateFromWhenCommand(notifier, authenticatedWho, when, adminEmail);
                // Set the calendar event for a day in the week at 10am
                whenDate = DateUtils.GetDayInWeek(whenDate, 10);

                // Add the link to the flow in the description
                description += "  Link to Flow: " + serviceRequest.joinPlayerUri;

                // Create a event object to save back to the system
                eventObject = new ObjectAPI();
                eventObject.developerName = "Event";
                eventObject.properties    = new List <PropertyAPI>();
                eventObject.properties.Add(new PropertyAPI()
                {
                    developerName = "ActivityDateTime", contentValue = whenDate.ToUniversalTime().ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffZ")
                });
                eventObject.properties.Add(new PropertyAPI()
                {
                    developerName = "Description", contentValue = description
                });
                eventObject.properties.Add(new PropertyAPI()
                {
                    developerName = "DurationInMinutes", contentValue = duration
                });
                eventObject.properties.Add(new PropertyAPI()
                {
                    developerName = "Subject", contentValue = subject
                });
                eventObject.properties.Add(new PropertyAPI()
                {
                    developerName = "IsAllDayEvent", contentValue = "false"
                });
                eventObject.properties.Add(new PropertyAPI()
                {
                    developerName = "IsArchived", contentValue = "false"
                });
                eventObject.properties.Add(new PropertyAPI()
                {
                    developerName = "IsChild", contentValue = "false"
                });
                eventObject.properties.Add(new PropertyAPI()
                {
                    developerName = "IsGroupEvent", contentValue = "false"
                });
                eventObject.properties.Add(new PropertyAPI()
                {
                    developerName = "IsPrivate", contentValue = "false"
                });
                eventObject.properties.Add(new PropertyAPI()
                {
                    developerName = "IsRecurrence", contentValue = "false"
                });
                eventObject.properties.Add(new PropertyAPI()
                {
                    developerName = "IsReminderSet", contentValue = "false"
                });
                eventObject.properties.Add(new PropertyAPI()
                {
                    developerName = "IsVisibleInSelfService", contentValue = "false"
                });

                // Add the object to the list of objects to save
                eventObjects = new List <ObjectAPI>();
                eventObjects.Add(eventObject);

                // Create the object data type properties for this object so the system knows what we're selecting
                objectDataTypeProperties = new List <ObjectDataTypePropertyAPI>();
                objectDataTypeProperties.Add(new ObjectDataTypePropertyAPI()
                {
                    developerName = "ActivityDateTime"
                });
                objectDataTypeProperties.Add(new ObjectDataTypePropertyAPI()
                {
                    developerName = "Description"
                });
                objectDataTypeProperties.Add(new ObjectDataTypePropertyAPI()
                {
                    developerName = "DurationInMinutes"
                });
                objectDataTypeProperties.Add(new ObjectDataTypePropertyAPI()
                {
                    developerName = "Subject"
                });
                objectDataTypeProperties.Add(new ObjectDataTypePropertyAPI()
                {
                    developerName = "IsAllDayEvent"
                });
                objectDataTypeProperties.Add(new ObjectDataTypePropertyAPI()
                {
                    developerName = "IsArchived"
                });
                objectDataTypeProperties.Add(new ObjectDataTypePropertyAPI()
                {
                    developerName = "IsChild"
                });
                objectDataTypeProperties.Add(new ObjectDataTypePropertyAPI()
                {
                    developerName = "IsGroupEvent"
                });
                objectDataTypeProperties.Add(new ObjectDataTypePropertyAPI()
                {
                    developerName = "IsPrivate"
                });
                objectDataTypeProperties.Add(new ObjectDataTypePropertyAPI()
                {
                    developerName = "IsRecurrence"
                });
                objectDataTypeProperties.Add(new ObjectDataTypePropertyAPI()
                {
                    developerName = "IsReminderSet"
                });
                objectDataTypeProperties.Add(new ObjectDataTypePropertyAPI()
                {
                    developerName = "IsVisibleInSelfService"
                });

                // Save the event object to salesforce
                eventObjects = SalesforceDataSingleton.GetInstance().Save(notifier, authenticatedWho, serviceRequest.configurationValues, objectDataTypeProperties, eventObjects);

                // Check to see if anything came back as part of the save - it should unless there was a fault
                if (eventObjects != null &&
                    eventObjects.Count > 0)
                {
                    // Grab the first object from the returned event objects
                    eventObject = eventObjects[0];

                    // Grab the id from that object - this needs to be returned in our outputs
                    eventId = eventObject.externalId;
                }
                else
                {
                    // If we didn't get any objects back, we need to throw an error
                    String errorMessage = "Event could not be created for an unknown reason.";

                    ErrorUtils.SendAlert(notifier, authenticatedWho, ErrorUtils.ALERT_TYPE_FAULT, errorMessage);

                    throw new ArgumentNullException("BadRequest", errorMessage);
                }
            }
            else
            {
                // Alert the admin that no one is in the authorization context
                ErrorUtils.SendAlert(notifier, authenticatedWho, ErrorUtils.ALERT_TYPE_WARNING, "The service request does not have an authorization context, so there's no one to notify.");
            }

            // Construct the service response
            serviceResponse            = new ServiceResponseAPI();
            serviceResponse.invokeType = ManyWhoConstants.INVOKE_TYPE_FORWARD;
            serviceResponse.token      = serviceRequest.token;
            serviceResponse.outputs    = new List <EngineValueAPI>();
            serviceResponse.outputs.Add(new EngineValueAPI()
            {
                contentType = ManyWhoConstants.CONTENT_TYPE_STRING, contentValue = eventId, developerName = SalesforceServiceSingleton.SERVICE_OUTPUT_ID
            });

            return(serviceResponse);
        }
Example #4
0
        public ServiceResponseAPI InvokeNotifyUsers(INotifier notifier, IAuthenticatedWho authenticatedWho, ServiceRequestAPI serviceRequest)
        {
            ChatterPostedMessage     chatterPostedMessage     = null;
            ChatterNewMessageSegment chatterNewMessageSegment = null;
            ServiceResponseAPI       serviceResponse          = null;
            SforceService            sforceService            = null;
            String groupAuthenticationToken = null;
            String authenticationUrl        = null;
            String username       = null;
            String password       = null;
            String securityToken  = null;
            String chatterBaseUrl = null;
            String adminEmail     = null;
            String endpointUrl    = null;
            String message        = null;

            // Grab the configuration values from the service request
            authenticationUrl = ValueUtils.GetContentValue(SalesforceServiceSingleton.SERVICE_VALUE_AUTHENTICATION_URL, serviceRequest.configurationValues, true);
            username          = ValueUtils.GetContentValue(SalesforceServiceSingleton.SERVICE_VALUE_USERNAME, serviceRequest.configurationValues, true);
            password          = ValueUtils.GetContentValue(SalesforceServiceSingleton.SERVICE_VALUE_PASSWORD, serviceRequest.configurationValues, true);
            securityToken     = ValueUtils.GetContentValue(SalesforceServiceSingleton.SERVICE_VALUE_SECURITY_TOKEN, serviceRequest.configurationValues, false);
            chatterBaseUrl    = ValueUtils.GetContentValue(SalesforceServiceSingleton.SERVICE_VALUE_CHATTER_BASE_URL, serviceRequest.configurationValues, true);
            adminEmail        = ValueUtils.GetContentValue(SalesforceServiceSingleton.SERVICE_VALUE_ADMIN_EMAIL, serviceRequest.configurationValues, true);

            if (serviceRequest.authorization != null)
            {
                Boolean postMade = false;

                // Get the message from the inputs
                message = ValueUtils.GetContentValue("Post", serviceRequest.inputs, true);

                // Check to see if we have any group authorization - if so, we post to those groups
                if (serviceRequest.authorization.groups != null &&
                    serviceRequest.authorization.groups.Count > 0)
                {
                    foreach (GroupAuthorizationGroupAPI groupAuthorization in serviceRequest.authorization.groups)
                    {
                        // For group posts, we post as the admin, not as the user - as it's very likely the user does not have permissions to post
                        if (groupAuthenticationToken == null ||
                            groupAuthenticationToken.Trim().Length == 0)
                        {
                            // Login as the API user
                            sforceService = SalesforceDataSingleton.GetInstance().Login(authenticatedWho, serviceRequest.configurationValues, true, false);

                            if (sforceService == null)
                            {
                                throw new ArgumentNullException("SalesforceService", "Unable to log into Salesforce.");
                            }

                            // Get the session id out as we'll use that for the oauth login
                            groupAuthenticationToken = sforceService.SessionHeaderValue.sessionId;
                        }

                        // Create the endpoint url for the group
                        endpointUrl = chatterBaseUrl + SalesforceServiceSingleton.CHATTER_URI_PART_API_VERSION + String.Format(SalesforceServiceSingleton.CHATTER_URI_PART_POSTS, groupAuthorization.authenticationId);

                        // Create a new chatter post
                        chatterPostedMessage      = new ChatterPostedMessage();
                        chatterPostedMessage.Body = new ChatterNewMessageBody();
                        chatterPostedMessage.Body.MessageSegments = new List <ChatterSegment>();

                        // Create a message segment for the actual post
                        chatterNewMessageSegment      = new ChatterNewMessageSegment();
                        chatterNewMessageSegment.Type = ChatterMessageSegmentType.Text.ToString();
                        chatterNewMessageSegment.Text = message;

                        // Add the segment to the post
                        chatterPostedMessage.Body.MessageSegments.Add(chatterNewMessageSegment);

                        // Post the message synchronously
                        SalesforceSocialSingleton.GetInstance().PostNotification(notifier, authenticatedWho, groupAuthenticationToken, serviceRequest, endpointUrl, serviceRequest.joinPlayerUri, chatterPostedMessage);

                        // Set the flag that we did in fact make a post
                        postMade = true;
                    }
                }

                // Check to see if we have any user authorization - if so, we post to those users
                if (serviceRequest.authorization.users != null &&
                    serviceRequest.authorization.users.Count > 0)
                {
                    // Create the endpoint url
                    endpointUrl = chatterBaseUrl + SalesforceServiceSingleton.CHATTER_URI_PART_API_VERSION + String.Format(SalesforceServiceSingleton.CHATTER_URI_PART_MY_FEED, authenticatedWho.UserId);

                    // Create a new chatter post
                    chatterPostedMessage      = new ChatterPostedMessage();
                    chatterPostedMessage.Body = new ChatterNewMessageBody();
                    chatterPostedMessage.Body.MessageSegments = new List <ChatterSegment>();

                    // Create a message segment for the actual post
                    chatterNewMessageSegment      = new ChatterNewMessageSegment();
                    chatterNewMessageSegment.Type = ChatterMessageSegmentType.Text.ToString();
                    chatterNewMessageSegment.Text = message;

                    // Add the segment to the post
                    chatterPostedMessage.Body.MessageSegments.Add(chatterNewMessageSegment);

                    // Rather than posting to each user, we do a joint post to all of the users that need to be notified
                    foreach (GroupAuthorizationUserAPI userAuthorization in serviceRequest.authorization.users)
                    {
                        ChatterMentionsSegment chatterMentionsSegment = null;

                        chatterMentionsSegment      = new ChatterMentionsSegment();
                        chatterMentionsSegment.Id   = userAuthorization.authenticationId;
                        chatterMentionsSegment.Type = ChatterMessageSegmentType.Mention.ToString();

                        // Add the user to the @mention
                        chatterPostedMessage.Body.MessageSegments.Add(chatterMentionsSegment);
                    }

                    // Post the message synchronously
                    SalesforceSocialSingleton.GetInstance().PostNotification(notifier, authenticatedWho, serviceRequest, endpointUrl, serviceRequest.joinPlayerUri, chatterPostedMessage);

                    // Set the flag that we did in fact make a post
                    postMade = true;
                }

                if (postMade == false)
                {
                    // Alert the admin that no message was sent
                    ErrorUtils.SendAlert(notifier, authenticatedWho, ErrorUtils.ALERT_TYPE_WARNING, "The service request does not have anything in the authorization context, so there's no one to notify.");
                }
            }
            else
            {
                // Alert the admin that no one is in the authorization context
                ErrorUtils.SendAlert(notifier, authenticatedWho, ErrorUtils.ALERT_TYPE_WARNING, "The service request does not have an authorization context, so there's no one to notify.");
            }

            // Construct the service response
            serviceResponse            = new ServiceResponseAPI();
            serviceResponse.invokeType = ManyWhoConstants.INVOKE_TYPE_FORWARD;
            serviceResponse.token      = serviceRequest.token;
            serviceResponse.outputs    = null;

            return(serviceResponse);
        }
        public HttpResponseMessage TaskEmailOutcomeResponse(String token, String selectedOutcomeId, String redirectUri = null)
        {
            IAuthenticatedWho   authenticatedWho = null;
            INotifier           notifier         = null;
            HttpResponseMessage response         = null;
            ServiceResponseAPI  serviceResponse  = null;
            ServiceRequestAPI   serviceRequest   = null;
            String invokeType      = null;
            String responseContent = null;

            try
            {
                if (String.IsNullOrWhiteSpace(token) == true)
                {
                    throw new ArgumentNullException("Token", "The token for the request is null or blank.");
                }

                if (String.IsNullOrWhiteSpace(selectedOutcomeId) == true)
                {
                    throw new ArgumentNullException("SelectedOutcomeId", "The selected outcome for the request is null or blank.");
                }

                // Get the email verification for this tracking code
                serviceRequest = JsonConvert.DeserializeObject <ServiceRequestAPI>(StorageUtils.GetStoredJson(token.ToLower()));

                if (serviceRequest == null)
                {
                    throw new ArgumentNullException("ServiceRequest", "The request has already been processed.");
                }

                // Get the notifier email
                authenticatedWho       = new AuthenticatedWho();
                authenticatedWho.Email = ValueUtils.GetContentValue(SalesforceServiceSingleton.SERVICE_VALUE_ADMIN_EMAIL, serviceRequest.configurationValues, true);

                // Create the notifier
                notifier = EmailNotifier.GetInstance(serviceRequest.tenantId, authenticatedWho, null, "TaskEmailOutcomeResponse");

                // Create the service response to send back to ManyWho based on this outcome click
                serviceResponse                   = new ServiceResponseAPI();
                serviceResponse.invokeType        = ManyWhoConstants.INVOKE_TYPE_FORWARD;
                serviceResponse.tenantId          = serviceRequest.tenantId;
                serviceResponse.token             = serviceRequest.token;
                serviceResponse.selectedOutcomeId = selectedOutcomeId;

                // Invoke the response on the manywho service
                invokeType = RunSingleton.GetInstance().Response(notifier, null, serviceRequest.tenantId, serviceRequest.callbackUri, serviceResponse);

                if (invokeType == null ||
                    invokeType.Trim().Length == 0)
                {
                    throw new ArgumentNullException("ServiceRequest", "The invokeType coming back from ManyWho cannot be null or blank.");
                }

                if (invokeType.IndexOf(ManyWhoConstants.INVOKE_TYPE_SUCCESS, StringComparison.InvariantCultureIgnoreCase) >= 0)
                {
                    // The system has accepted our task email response so the token is now dead - we remove it from storage
                    StorageUtils.RemoveStoredJson(token.ToLower());
                }
                else
                {
                    // The system has not accepted our task email response, so we should simply keep waiting and responding to emails with this task token
                }

                // Tell the user the outcome selection was successful
                responseContent = "Your request has been successfully completed. Please close this window.";

                if (String.IsNullOrWhiteSpace(redirectUri) == false)
                {
                    // Redirect the user as specified
                    response = Request.CreateResponse(HttpStatusCode.RedirectMethod, redirectUri);
                    response.Headers.Add("Location", redirectUri);
                }
                else
                {
                    // Send the user a response page
                    response         = Request.CreateResponse(HttpStatusCode.OK);
                    response.Content = new StringContent(responseContent);
                }
            }
            catch (Exception exception)
            {
                throw BaseHttpUtils.GetWebException(HttpStatusCode.BadRequest, BaseHttpUtils.GetExceptionMessage(exception));
            }

            return(response);
        }