Skip to main content
Skip table of contents

Controller Route Serializer

Introduction

Creating a service backend endpoint for http://va.gov is a common task for developers. Due to the complexity of integrating with legacy VA REST or SOAP applications we've developed a pattern for making these connections. This document showcases examples pertaining to the setup of an endpoint, including controllers, routes, and serializers.

vets-api/app/controllers/v0/profile/email_addresses_controller.rb

CODE
module V0
  module Profile
    class EmailAddressesController < ApplicationController
      include Vet360::Writeable
      service_tag 'profile'

      before_action { authorize :vet360, :access? }
      after_action :invalidate_cache

      def create
        write_to_vet360_and_render_transaction!(
          'email',
          email_address_params
        )
        Rails.logger.warn('EmailAddressesController#create request completed', sso_logging_info)
      end

      def create_or_update
        write_to_vet360_and_render_transaction!(
          'email',
          email_address_params,
          http_verb: 'update'
        )
      end

      def update
        write_to_vet360_and_render_transaction!(
          'email',
          email_address_params,
          http_verb: 'put'
        )
        Rails.logger.warn('EmailAddressesController#update request completed', sso_logging_info)
      end

      def destroy
        write_to_vet360_and_render_transaction!(
          'email',
          add_effective_end_date(email_address_params),
          http_verb: 'put'
        )
        Rails.logger.warn('EmailAddressesController#destroy request completed', sso_logging_info)
      end

      private

      def email_address_params
        params.permit(
          :email_address,
          :id,
          :transaction_id
        )
      end
    end
  end
end

vets-api/modules/mobile/app/serializers/mobile/v0/contact_info_serializer.rb

CODE
# frozen_string_literal: true

module Mobile
  module V0
    class ContactInfoSerializer
      include JSONAPI::Serializer

      ADDRESS_KEYS = %i[
        id
        address_line1
        address_line2
        address_line3
        address_pou
        address_type
        city
        country_name
        country_code_iso3
        international_postal_code
        province
        state_code
        zip_code
        zip_code_suffix
      ].freeze

      EMAIL_KEYS = %i[
        id
        email_address
      ].freeze

      PHONE_KEYS = %i[
        id
        area_code
        country_code
        extension
        phone_number
        phone_type
      ].freeze

      set_type :contact_info
      attributes :residential_address, :mailing_address, :home_phone, :mobile_phone, :work_phone, :contact_email

      def initialize(user_id, contact_info)
        resource = ContactInfoStruct.new(id: user_id,
                                         residential_address: filter_keys(contact_info&.residential_address,
                                                                          ADDRESS_KEYS),
                                         mailing_address: filter_keys(contact_info&.mailing_address, ADDRESS_KEYS),
                                         home_phone: filter_keys(contact_info&.home_phone, PHONE_KEYS),
                                         mobile_phone: filter_keys(contact_info&.mobile_phone, PHONE_KEYS),
                                         work_phone: filter_keys(contact_info&.work_phone, PHONE_KEYS),
                                         contact_email: filter_keys(contact_info&.email, EMAIL_KEYS))

        super(resource)
      end

      def filter_keys(value, keys)
        value&.to_h&.slice(*keys)
      end
    end

    ContactInfoStruct = Struct.new(:id, :residential_address, :mailing_address, :home_phone, :mobile_phone,
                                   :work_phone, :contact_email)
  end
end

vets-api/config/routes.rb

CODE
...
  namespace :v0, defaults: { format: 'json' } do
...
    namespace :profile do
...
       resource :email_addresses, only: %i[create update destroy] do
        collection do
          post :create_or_update
        end
      end
    end
  end
...

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.