<commit_before>import * as Sentry from '@sentry/browser';
import { isEqual } from 'lodash';

import recordEvent from '../../../platform/monitoring/record-event';
import {
  apiRequest,
  stripEmpties,
  toGenericAddress,
  getStatus,
} from '../utils/helpers.jsx';
import {
  ADDRESS_TYPES,
  BACKEND_AUTHENTICATION_ERROR,
  BACKEND_SERVICE_ERROR,
  GET_ADDRESS_COUNTRIES_FAILURE,
  GET_ADDRESS_COUNTRIES_SUCCESS,
  GET_ADDRESS_FAILURE,
  GET_ADDRESS_SUCCESS,
  GET_ADDRESS_STATES_FAILURE,
  GET_ADDRESS_STATES_SUCCESS,
  GET_BENEFIT_SUMMARY_OPTIONS_FAILURE,
  GET_BENEFIT_SUMMARY_OPTIONS_SUCCESS,
  GET_LETTER_PDF_DOWNLOADING,
  GET_LETTER_PDF_FAILURE,
  GET_LETTER_PDF_SUCCESS,
  GET_LETTERS_FAILURE,
  GET_LETTERS_SUCCESS,
  LETTER_ELIGIBILITY_ERROR,
  LETTER_TYPES,
  UPDATE_BENFIT_SUMMARY_REQUEST_OPTION,
  SAVE_ADDRESS_PENDING,
  SAVE_ADDRESS_FAILURE,
  SAVE_ADDRESS_SUCCESS,
  INVALID_ADDRESS_PROPERTY,
  START_EDITING_ADDRESS,
  CANCEL_EDITING_ADDRESS,
} from '../utils/constants';

export function editAddress() {
  return dispatch => dispatch({ type: START_EDITING_ADDRESS });
}

export function cancelEditingAddress() {
  return dispatch => dispatch({ type: CANCEL_EDITING_ADDRESS });
}

export function getLetterList(dispatch) {
  return apiRequest(
    '/v0/letters',
    null,
    response => {
      recordEvent({ event: 'letter-list-success' });
      return dispatch({
        type: GET_LETTERS_SUCCESS,
        data: response,
      });
    },
    response => {
      recordEvent({ event: 'letter-list-failure' });
      const status = getStatus(response);
      if (status === '403') {
        // Backend authentication problem
        dispatch({ type: BACKEND_AUTHENTICATION_ERROR });
      } else if (status === '422') {
        // User has an invalid address for his or her letters
        dispatch({ type: INVALID_ADDRESS_PROPERTY });
      } else if (status === '502') {
        // Some of the partner services are down, so we cannot verify the
        // eligibility of some letters
        dispatch({ type: LETTER_ELIGIBILITY_ERROR });
      } else if (status === '503' || status === '504') {
        // Either EVSS or a partner service is down or EVSS times out
        dispatch({ type: BACKEND_SERVICE_ERROR });
      } else {
        dispatch({ type: GET_LETTERS_FAILURE });
      }

      Sentry.withScope(scope => {
        scope.setFingerprint(['{{ default }}', status]);
        Sentry.captureException(
          new Error(`vets_letters_error_getLetterList ${status}`),
        );
      });
      return Promise.reject();
    },
  );
}

export function getBenefitSummaryOptions(dispatch) {
  return apiRequest(
    '/v0/letters/beneficiary',
    null,
    response => {
      recordEvent({ event: 'letter-get-bsl-success' });
      return dispatch({
        type: GET_BENEFIT_SUMMARY_OPTIONS_SUCCESS,
        data: response,
      });
    },
    response => {
      recordEvent({ event: 'letter-get-bsl-failure' });
      dispatch({ type: GET_BENEFIT_SUMMARY_OPTIONS_FAILURE });
      const status = getStatus(response);
      throw new Error(`vets_letters_error_getBenefitSummaryOptions: ${status}`);
    },
  );
}

// Call getLetterList then getBenefitSummaryOptions
export function getLetterListAndBSLOptions() {
  return dispatch =>
    getLetterList(dispatch)
      .then(() => getBenefitSummaryOptions(dispatch))
      .catch(error => Sentry.captureException(error));
}

export function getAddressFailure() {
  recordEvent({ event: 'letter-get-address-failure' });
  return { type: GET_ADDRESS_FAILURE };
}

export function getMailingAddress() {
  return dispatch =>
    apiRequest(
      '/v0/address',
      null,
      response => {
        recordEvent({ event: 'letter-get-address-success' });
        const responseCopy = Object.assign({}, response);<commit_msg>Remove duplicative Sentry log in letters (#10258)<commit_after>import * as Sentry from '@sentry/browser';
import { isEqual } from 'lodash';

import recordEvent from '../../../platform/monitoring/record-event';
import {
  apiRequest,
  stripEmpties,
  toGenericAddress,
  getStatus,
} from '../utils/helpers.jsx';
import {
  ADDRESS_TYPES,
  BACKEND_AUTHENTICATION_ERROR,
  BACKEND_SERVICE_ERROR,
  GET_ADDRESS_COUNTRIES_FAILURE,
  GET_ADDRESS_COUNTRIES_SUCCESS,
  GET_ADDRESS_FAILURE,
  GET_ADDRESS_SUCCESS,
  GET_ADDRESS_STATES_FAILURE,
  GET_ADDRESS_STATES_SUCCESS,
  GET_BENEFIT_SUMMARY_OPTIONS_FAILURE,
  GET_BENEFIT_SUMMARY_OPTIONS_SUCCESS,
  GET_LETTER_PDF_DOWNLOADING,
  GET_LETTER_PDF_FAILURE,
  GET_LETTER_PDF_SUCCESS,
  GET_LETTERS_FAILURE,
  GET_LETTERS_SUCCESS,
  LETTER_ELIGIBILITY_ERROR,
  LETTER_TYPES,
  UPDATE_BENFIT_SUMMARY_REQUEST_OPTION,
  SAVE_ADDRESS_PENDING,
  SAVE_ADDRESS_FAILURE,
  SAVE_ADDRESS_SUCCESS,
  INVALID_ADDRESS_PROPERTY,
  START_EDITING_ADDRESS,
  CANCEL_EDITING_ADDRESS,
} from '../utils/constants';

export function editAddress() {
  return dispatch => dispatch({ type: START_EDITING_ADDRESS });
}

export function cancelEditingAddress() {
  return dispatch => dispatch({ type: CANCEL_EDITING_ADDRESS });
}

export function getLetterList(dispatch) {
  return apiRequest(
    '/v0/letters',
    null,
    response => {
      recordEvent({ event: 'letter-list-success' });
      return dispatch({
        type: GET_LETTERS_SUCCESS,
        data: response,
      });
    },
    response => {
      recordEvent({ event: 'letter-list-failure' });
      const status = getStatus(response);
      if (status === '403') {
        // Backend authentication problem
        dispatch({ type: BACKEND_AUTHENTICATION_ERROR });
      } else if (status === '422') {
        // User has an invalid address for his or her letters
        dispatch({ type: INVALID_ADDRESS_PROPERTY });
      } else if (status === '502') {
        // Some of the partner services are down, so we cannot verify the
        // eligibility of some letters
        dispatch({ type: LETTER_ELIGIBILITY_ERROR });
      } else if (status === '503' || status === '504') {
        // Either EVSS or a partner service is down or EVSS times out
        dispatch({ type: BACKEND_SERVICE_ERROR });
      } else {
        dispatch({ type: GET_LETTERS_FAILURE });
      }

      Sentry.withScope(scope => {
        scope.setFingerprint(['{{ default }}', status]);
        Sentry.captureException(
          new Error(`vets_letters_error_getLetterList ${status}`),
        );
      });
      return Promise.reject();
    },
  );
}

export function getBenefitSummaryOptions(dispatch) {
  return apiRequest(
    '/v0/letters/beneficiary',
    null,
    response => {
      recordEvent({ event: 'letter-get-bsl-success' });
      return dispatch({
        type: GET_BENEFIT_SUMMARY_OPTIONS_SUCCESS,
        data: response,
      });
    },
    response => {
      recordEvent({ event: 'letter-get-bsl-failure' });
      dispatch({ type: GET_BENEFIT_SUMMARY_OPTIONS_FAILURE });
      const status = getStatus(response);
      throw new Error(`vets_letters_error_getBenefitSummaryOptions: ${status}`);
    },
  );
}

// Call getLetterList then getBenefitSummaryOptions
export function getLetterListAndBSLOptions() {
  return dispatch =>
    getLetterList(dispatch)
      .then(() => getBenefitSummaryOptions(dispatch))
      .catch(() => {});
}

export function getAddressFailure() {
  recordEvent({ event: 'letter-get-address-failure' });
  return { type: GET_ADDRESS_FAILURE };
}

export function getMailingAddress() {
  return dispatch =>
    apiRequest(
      '/v0/address',
      null,
      response => {
        recordEvent({ event: 'letter-get-address-success' });
        const responseCopy = Object.assign({}, response);
{'fraction_non_alphanumeric': 0.11471321695760599, 'fraction_numerical': 0.005381283633022706, 'mean_word_length': 3.4302325581395348, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5618573', 'n_tokens_mistral': 2416, 'n_tokens_neox': 2363, 'n_words': 490}