public DopplerSubscriberDto TypeFormToSubscriberDTO(TypeformDTO rawSubscriber, ItemsDto allowedFields) { var rawSubscriberEmail = rawSubscriber.form_response.answers.FirstOrDefault(x => !string.IsNullOrEmpty(x.email)); if ((rawSubscriberEmail == null) && string.IsNullOrEmpty(rawSubscriber.form_response.hidden.dplrid)) { var responseText = string.Format("The response event: {0} to the form: {1} with ID: {2} has not included an email", rawSubscriber.event_id, rawSubscriber.form_response.definition.title, rawSubscriber.form_response.definition.id); _log.LogWarning(responseText); throw new ArgumentNullException(responseText); } var emailSuscriber = rawSubscriberEmail == null ? DecodeDPLRID(rawSubscriber.form_response.hidden.dplrid) : rawSubscriberEmail.email; DopplerSubscriberDto dopplerSubscriber = new DopplerSubscriberDto { Email = emailSuscriber }; var answersById = rawSubscriber.form_response.answers .Where(x => String.IsNullOrEmpty(x.email)) .ToDictionary(y => y.field.id); dopplerSubscriber.Fields = rawSubscriber.form_response.definition.fields .Where(x => x.type != "email") .Select(f => { var name = GENDER_FIELD_NAMES.Contains(f.@ref) ? "GENDER" : COUNTRY_FIELD_NAMES.Contains(f.@ref) ? "COUNTRY" : BASIC_FIELD_NAMES.Contains(f.@ref) ? [email protected]() : f.@ref; var questionType = GENDER_FIELD_NAMES.Contains(f.@ref) ? "gender" : COUNTRY_FIELD_NAMES.Contains(f.@ref) ? "country" : f.@ref == "consent" ? "consent" : Dictionaries.CustomFieldTypes.TryGetValue(f.type, out string convertedQuestionType) ? convertedQuestionType : null; var answerType = Dictionaries.CustomFieldTypes.TryGetValue(answersById[f.id].field.type, out string convertedAnswerType) ? convertedAnswerType : null; var answerValue = GetAnswerValue(answersById[f.id]); var value = GENDER_FIELD_NAMES.Contains(f.@ref) ? GetGenderValue(answerValue.ToString()) : COUNTRY_FIELD_NAMES.Contains(f.@ref) ? GetCountryValue(answerValue.ToString()) : answerValue; return(new { name, questionType, f.id, answersById, value }); }) .Where(y => allowedFields.Items .Any(z => z.Name == y.name && y.questionType == z.Type)) .Select(h => new CustomFieldDto { Name = h.name, Value = h.value }) .ToList(); return(dopplerSubscriber); }
public async Task <IActionResult> AddSubscriber(string accountName, long idList, string apiKey, [FromBody] TypeformDTO subscriberDto) { const string HELP_LINK = "https://help.fromdoppler.com/en/how-integrate-doppler-typeform"; if (string.IsNullOrWhiteSpace(accountName)) { _log.LogError("Account Name should not be Null or empty"); return(BadRequest(new { ErrorMessage = "An account name must be provided", HelpLink = HELP_LINK })); } if (string.IsNullOrWhiteSpace(apiKey)) { _log.LogError("API Key should not be Null or empty"); return(BadRequest(new { ErrorMessage = "An API key must be provided", HelpLink = HELP_LINK })); } try { var itemList = await _dopplerService.GetFields(apiKey, accountName); //we get the user's custom fields var subscriber = _mapper.TypeFormToSubscriberDTO(subscriberDto, itemList); var requestOrigin = "Typeform"; var result = await _dopplerService.CreateNewSubscriberAsync(apiKey, accountName, idList, subscriber, requestOrigin); return(result); } catch (Exception ex) { _log.LogError(new EventId(), ex, string.Format("AccountName: {0} | ID_List: {1} | Status: Add subscriber has failed", accountName, idList)); return(new BadRequestResult()); } }