Example #1
0
        public static string GetCRMLinkTableItem(RestCommand command, int cRMLinkID)
        {
            CRMLinkTableItem cRMLinkTableItem = CRMLinkTable.GetCRMLinkTableItem(command.LoginUser, cRMLinkID);

            if (cRMLinkTableItem.OrganizationID != command.Organization.OrganizationID)
            {
                throw new RestException(HttpStatusCode.Unauthorized);
            }
            return(cRMLinkTableItem.GetXml("CRMLinkTableItem", true));
        }
        private bool ValidateIntegrationRequest(IntegrationApps app, HttpContext context, ref CRMLinkTableItem crmLink)
        {
            const int     orgIdSegment        = 4;
            const int     webhookTokenSegment = 5;
            List <string> errors = new List <string>();

            //As of right now all webhook calls for integration apps should have orgid and the token in the 3rd and 4th segments respectively. And the token should be valid.
            int    orgId        = 0;
            string webhookToken = string.Empty;

            if (!int.TryParse(context.Request.Url.Segments[orgIdSegment].TrimEnd('/'), out orgId))
            {
                errors.Add("OrganizationId is missing or invalid in the URL request");
            }

            if (context.Request.Url.Segments.Count() < 6 || string.IsNullOrEmpty(context.Request.Url.Segments[webhookTokenSegment].TrimEnd('/')))
            {
                errors.Add("WebHook token is missing in the URL request");
            }
            else
            {
                webhookToken = context.Request.Url.Segments[webhookTokenSegment].TrimEnd('/');
            }

            //Specific validation, if any.
            switch (app)
            {
            case IntegrationApps.ServiceNow:
                break;
            }

            Log(context, errors, LogType.Both, isError: true, httpStatusCode: HttpStatusCode.BadRequest);

            return(errors.Count == 0);
        }
        private void ProcessIntegration(HttpContext context)
        {
            const int       orgIdSegment        = 4;
            const int       webhookTokenSegment = 5;
            IntegrationApps processApp;
            string          app = string.Empty;

            if (context.Request.Url.Segments.Count() > 3)
            {
                app = context.Request.Url.Segments[3].TrimEnd('/');

                if (Enum.TryParse(app, true, out processApp))
                {
                    CRMLinkTableItem crmLink = null;

                    switch (processApp)
                    {
                    case IntegrationApps.ServiceNow:
                        if (ValidateIntegrationRequest(processApp, context, ref crmLink))
                        {
                            int    organizationId = -1;
                            string webhookToken   = string.Empty;
                            int.TryParse(context.Request.Url.Segments[orgIdSegment].TrimEnd('/'), out organizationId);
                            webhookToken = context.Request.Url.Segments[webhookTokenSegment].TrimEnd('/');

                            string jsonData = ReadJsonData(context);

                            if (!string.IsNullOrEmpty(jsonData))
                            {
                                log.InfoFormat("Body: {0}", jsonData);

                                WebHooksPendingItem newPendingWebHook = (new WebHooksPending(LoginUser.Anonymous)).AddNewWebHooksPendingItem();
                                newPendingWebHook.OrganizationId = organizationId;
                                newPendingWebHook.RefType        = (int)ReferenceType.Tickets;
                                newPendingWebHook.Type           = (short)WebHookType.Integration;
                                newPendingWebHook.Url            = context.Request.Url.AbsolutePath;
                                newPendingWebHook.BodyData       = jsonData;
                                newPendingWebHook.Token          = webhookToken;
                                newPendingWebHook.Inbound        = true;
                                newPendingWebHook.IsProcessing   = false;
                                newPendingWebHook.DateCreated    = DateTime.UtcNow;
                                newPendingWebHook.Collection.Save();

                                Log(context, "Queued", LogType.Client);
                            }
                            else
                            {
                                Log(context, "This integration needs data to process and it was not found.", isError: true);
                                Log(context, "Body data is expected", LogType.Client, isError: true, httpStatusCode: HttpStatusCode.BadRequest);
                            }
                        }
                        break;

                    case IntegrationApps.Unknown:
                    default:
                        break;
                    }
                }
                else
                {
                    Log(context, string.Format("Integration webhook for \"{0}\" not found", app), isError: true);
                    Log(context, "Webhook requested not implemented", LogType.Client, isError: true, httpStatusCode: HttpStatusCode.NotImplemented);
                }
            }
            else
            {
                Log(context, "Integration type not found", LogType.Both, isError: true, httpStatusCode: HttpStatusCode.NotImplemented);
            }
        }