Exemple #1
0
        public static string ToJson(this LeverPostingApplyViewModel model)
        {
            var json = new Dictionary <string, object> {
                { "name", !string.IsNullOrEmpty(model.Name) ? model.Name : string.Empty },
                { "email", !string.IsNullOrEmpty(model.Email) ? model.Email : string.Empty },
                { "comments", !string.IsNullOrEmpty(model.Comments) ? model.Comments : string.Empty },
                { "org", !string.IsNullOrEmpty(model.Org) ? model.Org : string.Empty },
                { "phone", !string.IsNullOrEmpty(model.Phone) ? model.Phone : string.Empty },
            };

            if (model.Urls != null && model.Urls.Any())
            {
                json.Add("urls", model.Urls);
            }

            if (model.Consent != null && model.Consent.Any())
            {
                json.Add("consent", model.Consent);
            }

            if (model.CustomQuestions != null && model.CustomQuestions.Fields.Any())
            {
                json.Add("cards", new Dictionary <string, Dictionary <string, string> > {
                    {
                        model.CustomQuestions.Id,
                        model.CustomQuestions.Fields
                    }
                });
            }

            return(JsonConvert.SerializeObject(json));
        }
Exemple #2
0
        private static void SetSurveyResponsesToFromData(LeverPostingApplyViewModel model, MultipartFormDataContent httpContent)
        {
            if (model.CustomSurveyQuestions == null || !model.CustomSurveyQuestions.Fields.Any())
            {
                return;
            }

            foreach (var item in model.CustomSurveyQuestions.Fields.Where(x => !string.IsNullOrEmpty(x.Value)))
            {
                httpContent.Add(new StringContent(item.Value), FormatKeyForLever(string.Format("surveysResponses[{0}][responses][{1}]", model.CustomSurveyQuestions.Id, item.Key)));
            }
        }
Exemple #3
0
        private static void SetConsentToFromData(LeverPostingApplyViewModel model, MultipartFormDataContent httpContent)
        {
            if (model.Consent == null || !model.Consent.Any())
            {
                return;
            }

            foreach (var item in model.Consent.Where(x => !string.IsNullOrEmpty(x.Value)))
            {
                httpContent.Add(new StringContent(item.Value.ToLower() == "on" ? "true" : "false"), string.Format(FormatKeyForLever("consent[{0}]"), item.Key));
            }
        }
Exemple #4
0
        private static void SetUrlsToFromData(LeverPostingApplyViewModel model, MultipartFormDataContent httpContent)
        {
            if (model.Urls == null || !model.Urls.Any())
            {
                return;
            }

            foreach (var item in model.Urls.Where(x => !string.IsNullOrEmpty(x.Value)))
            {
                httpContent.Add(new StringContent(item.Value), FormatKeyForLever(string.Format("urls[{0}]", item.Key)));
            }
        }
Exemple #5
0
 private async Task TriggerNotificationEvent(LeverPostingApplyViewModel model, bool isSuccessful, string errorMessage)
 {
     await _workflowManager.TriggerEventAsync(
         nameof(LeverPostingNotificationEvent),
         input : new
     {
         LeverPostingNotificationViewModel = new LeverPostingNotificationViewModel
         {
             ErrorMessage = errorMessage,
             IsSuccessful = isSuccessful,
             LeverPostingApplyViewModel = model
         }
     },
         correlationId : model.PostingId
         );
 }
Exemple #6
0
        public async Task <IActionResult> Index(LeverPostingApplyViewModel model)
        {
            var settings = (await _siteService.GetSiteSettingsAsync()).As <LeverSettings>();
            var referer  = Request.Headers["Referer"].ToString();

            if (!ModelState.IsValid)
            {
                return(new RedirectResult(referer));
            }

            (var found, var contentItem) = await _autorouteEntries.TryGetEntryByPathAsync(GetReferrerRoute());

            if (!found)
            {
                return(new BadRequestResult());
            }

            var posting = await _leverPostingService.GetById(contentItem.ContentItemId);

            if (posting == null)
            {
                return(new BadRequestResult());
            }

            model.PostingId = JsonConvert.DeserializeObject <Posting>(posting.GetLeverPostingPart().Data).Id;
            model.UpdateCards(HttpContext.Request.Form);
            model.UpdateSurveysResponses(HttpContext.Request.Form);

            var result = await _postingApiService.Apply(settings, model);

            if (result == null || !result.Ok)
            {
                if (result != null)
                {
                    ModelState.AddModelError("error", result.Error);

                    // If there is a validation error from API
                    // we send the user back to the page
                    return(new RedirectResult(referer));
                }

                return(new RedirectResult($"{settings.SuccessUrl}"));
            }

            return(new RedirectResult($"{settings.SuccessUrl}?applicationId={result.ApplicationId}&contentItemId={contentItem.ContentItemId}" ?? "/"));
        }
Exemple #7
0
        private static void SetResumeToFromData(LeverPostingApplyViewModel model, MultipartFormDataContent httpContent)
        {
            if (model.Resume == null)
            {
                return;
            }

            var fileContent = new StreamContent(model.Resume.OpenReadStream())
            {
                Headers =
                {
                    ContentLength = model.Resume.Length,
                    ContentType   = new MediaTypeHeaderValue(model.Resume.ContentType)
                }
            };

            httpContent.Add(fileContent, FormatKeyForLever("resume"), model.Resume.FileName);
        }
Exemple #8
0
        public static MultipartFormDataContent ToFormData(this LeverPostingApplyViewModel model)
        {
            var httpContent = new MultipartFormDataContent
            {
                { new StringContent(!string.IsNullOrEmpty(model.Comments) ? model.Comments : string.Empty), FormatKeyForLever("comments") },
                { new StringContent(!string.IsNullOrEmpty(model.Email) ? model.Email : string.Empty), FormatKeyForLever("email") },
                { new StringContent(!string.IsNullOrEmpty(model.Name) ? model.Name : string.Empty), FormatKeyForLever("name") },
                { new StringContent(!string.IsNullOrEmpty(model.Org) ? model.Org : string.Empty), FormatKeyForLever("org") },
                { new StringContent(!string.IsNullOrEmpty(model.Phone) ? model.Phone : string.Empty), FormatKeyForLever("phone") }
            };

            SetUrlsToFromData(model, httpContent);
            SetConsentToFromData(model, httpContent);
            SetCardsToFromData(model, httpContent);
            SetResumeToFromData(model, httpContent);
            SetSurveyResponsesToFromData(model, httpContent);

            return(httpContent);
        }
Exemple #9
0
        public async Task <PostingResult> Apply(LeverSettings settings, LeverPostingApplyViewModel model)
        {
            if (settings == null || !settings.IsValid())
            {
                Logger.Error("Unable to submit application to API because `Site` or `ApiKey` is not defined in settings.");
                return(null);
            }

            if (string.IsNullOrEmpty(model.PostingId))
            {
                Logger.Error("Please provide postingId.");
                return(null);
            }

            try
            {
                var client   = _clientFactory.CreateClient();
                var response = await client.PostAsync($"{URL}{settings.Site}/{model.PostingId}?key={settings.ApiKey}", model.ToFormData());

                var json = JsonConvert.DeserializeObject <PostingResult>(await response.Content.ReadAsStringAsync());

                if (json.Ok)
                {
                    await TriggerNotificationEvent(model, true, null);
                }

                return(json);
            }
            catch (Exception e)
            {
                await TriggerNotificationEvent(model, false, e.Message);

                Logger.Error(string.Format("{0}, Error apply for posting id: {1}", e, model.PostingId));
            }

            return(null);
        }