def post(self, request): """ Given a minimal set of fields that may be necessary for DNB investigation, create a Company record in DataHub. """ formatted_company_data = format_dnb_company_investigation(request.data) company_serializer = DNBCompanyInvestigationSerializer( data=formatted_company_data) try: company_serializer.is_valid(raise_exception=True) except serializers.ValidationError: message = 'Company investigation payload failed serializer validation' extra_data = { 'formatted_dnb_company_data': formatted_company_data, 'dh_company_serializer_errors': company_serializer.errors, } logger.error(message, extra=extra_data) raise datahub_company = company_serializer.save( created_by=request.user, modified_by=request.user, pending_dnb_investigation=True, ) statsd.incr(f'dnb.create.investigation') return Response( company_serializer.to_representation(datahub_company), )
def send_estimated_land_date_reminder(project, adviser, days_left): """ Sends approaching estimated land date reminder by email. """ statsd.incr(f'send_investment_notification.{days_left}') notify_adviser_by_email( adviser, settings.INVESTMENT_NOTIFICATION_ESTIMATED_LAND_DATE_TEMPLATE_ID, get_project_item(project), NotifyServiceName.investment, )
def search_dnb(query_params): """ Queries the dnb-service with the given query_params. E.g.: {"duns_number": "29393217", "page_size": 1} {"search_term": "brompton", "page_size": 10} """ if not settings.DNB_SERVICE_BASE_URL: raise ImproperlyConfigured('The setting DNB_SERVICE_BASE_URL has not been set') response = api_client.request( 'POST', 'companies/search/', json=query_params, timeout=3.0, ) statsd.incr(f'dnb.search.{response.status_code}') return response
def send_estimated_land_date_summary(projects, adviser, current_date): """ Sends approaching estimated land date summary reminder by email. """ statsd.incr('send_estimated_land_date_summary') notifications = get_projects_summary_list(projects) notify_adviser_by_email( adviser, settings.INVESTMENT_NOTIFICATION_ESTIMATED_LAND_DATE_SUMMARY_TEMPLATE_ID, { 'month': current_date.strftime('%B'), 'reminders_number': len(notifications), 'summary': ''.join(notifications), 'settings_url': settings.DATAHUB_FRONTEND_REMINDER_SETTINGS_URL, }, NotifyServiceName.investment, )
def post(self, request): """ Given a duns_number, get the data for the company from dnb-service and create a record in DataHub. """ duns_serializer = DUNSNumberSerializer(data=request.data) duns_serializer.is_valid(raise_exception=True) duns_number = duns_serializer.validated_data['duns_number'] try: dnb_company = get_company(duns_number) except (DNBServiceConnectionError, DNBServiceError, DNBServiceInvalidResponse) as exc: raise APIUpstreamException(str(exc)) except DNBServiceInvalidRequest as exc: raise APIBadRequestException(str(exc)) company_serializer = DNBCompanySerializer(data=dnb_company, ) try: company_serializer.is_valid(raise_exception=True) except serializers.ValidationError: message = 'Company data from DNB failed DH serializer validation' extra_data = { 'formatted_dnb_company_data': dnb_company, 'dh_company_serializer_errors': company_serializer.errors, } logger.error(message, extra=extra_data) raise datahub_company = company_serializer.save( created_by=request.user, modified_by=request.user, dnb_modified_on=now(), ) statsd.incr(f'dnb.create.company') return Response( company_serializer.to_representation(datahub_company), )
def notify_meeting_ingest_success(adviser, interaction, recipients): """ Notify an adviser that a meeting ingest has succeeeded - including a link to the interaction and intended recipients. """ domain_label = get_domain_label(adviser.get_email_domain()) statsd.incr(f'celery.calendar-invite-ingest.success.{domain_label}') if not is_feature_flag_active( INTERACTION_EMAIL_NOTIFICATION_FEATURE_FLAG_NAME): logger.info( f'Feature flag "{INTERACTION_EMAIL_NOTIFICATION_FEATURE_FLAG_NAME}" is not active, ' 'exiting.', ) return flat_recipients = ', '.join(recipients) notify_adviser_by_email( adviser, Template.meeting_ingest_success.value, context={ 'interaction_url': interaction.get_absolute_url(), 'recipients': flat_recipients, 'support_team_email': settings.DATAHUB_SUPPORT_EMAIL_ADDRESS, }, )
def notify_meeting_ingest_failure(adviser, errors, recipients): """ Notify an adviser that a meeting ingest has failed - including error details and intended recipients. """ domain_label = get_domain_label(adviser.get_email_domain()) statsd.incr(f'celery.calendar-invite-ingest.failure.{domain_label}') if not is_feature_flag_active( INTERACTION_EMAIL_NOTIFICATION_FEATURE_FLAG_NAME): logger.info( f'Feature flag "{INTERACTION_EMAIL_NOTIFICATION_FEATURE_FLAG_NAME}" is not active, ' 'exiting.', ) return flat_recipients = ', '.join(recipients) notify_adviser_by_email( adviser, Template.meeting_ingest_failure.value, context={ 'errors': errors, 'recipients': flat_recipients, 'support_team_email': settings.DATAHUB_SUPPORT_EMAIL_ADDRESS, }, )