OpenSourceFellows / amplify-back-end

The API backend for ProgramEquity
https://www.programequity.com/
MIT License
11 stars 0 forks source link

Combining addressVerification and CreateAddress api in lob.js #194

Closed mahrsbar closed 2 years ago

mahrsbar commented 2 years ago

Hello, I was wondering if it would be better to combine the addressVerification and createAddress into one so the front-end only needs to make one API call.

Currently, front-end has to send a POST request with the address to addressVerification, receive a successful response, then send another POST request with the verified address to createAddress, then receive an address_id which then can be used to make a POST request to createLetter.

I can write this logic in the front-end but I think best practices would be having the addressVerification and createAddress as one call in which it would return the address_id to front-end which can then be used to createLetter.

If there is a reason we're two doing two calls, please let me know because I might have missed something. I hope my ask makes sense.

I'm referencing the following code in this file:

CreateAddress

router.post('/createAddress', async (req, res) => {
  // Get description, to, and template_id from request body
  const address = req.body || {}
  const lobApiKey = getLobApiKey()
  const lob = new Lob({ apiKey: lobApiKey })

  try {
    // Create Lob address using variables passed into route via post body
    const addressResponse = await lob.addresses.create({
      description: address.description,
      name: address.name,
      address_line1: address.line1,
      address_line2: address.line2,
      address_city: address.city,
      address_state: address.state,
      address_zip: address.zip,
      address_country: 'US'
    })

    res.status(200).send({ address_id: addressResponse.id })
  } catch (error) {
    res.status(500).send({ error: 'Something failed!' })
  }
})

addressVerification

router.post('/addressVerification', async (req, res) => {
  ...
  try {
    const lob = new Lob({ apiKey: getLobApiKey() })
    const response = await lob.usVerifications.verify({
      primary_line: line1,
      secondary_line: line2,
      city,
      state,
      zip_code: zipCode
    })

    const {
      deliverability,
      primary_line: revisedLine1,
      secondary_line: revisedLine2,
      components: {
        city: revisedCity,
        state: revisedState,
        zip_code: revisedZip,
        zip_code_plus_4: revisedZipPlus4,
        address_type: addressType,
        record_type: recordType
      }
    } = response

    const isUndeliverable =
      !deliverability || deliverability === 'undeliverable'
    const isResidential = addressType === 'residential'
    const isPostOfficeBox = recordType === 'po_box'
    const isPuertoRico = revisedState === 'PR'

    const deliverable =
      !isUndeliverable && isResidential && !isPostOfficeBox && !isPuertoRico
    const warning = DELIVERABILITY_WARNINGS[deliverability] || null

    if (!deliverable) {
      let errorMessage = 'Address is undeliverable'
      if (!isUndeliverable) {
        if (!isResidential) {
          errorMessage = 'Non-residential addresses are not currently supported'
        } else if (isPostOfficeBox) {
          errorMessage = 'Post office boxes are not currently supported'
        } else if (isPuertoRico) {
          errorMessage = 'Puerto Rico addresses are not currently supported'
        }
      }

      return res.status(400).send({ error: errorMessage })
    }

    return res.status(200).send({
      deliverable,
      warning,
      revisedAddress: {
        line1: revisedLine1,
        line2: revisedLine2 || null,
        city: revisedCity,
        state: revisedState,
        zip: revisedZip + (revisedZipPlus4 ? '-' + revisedZipPlus4 : '')
      }
    })
  } catch (error) {
    // This endpoint should not return anything other than `200` status
    // codes, even for undeliverable addresses
    return handleLobError(error, res)
  }
})
JamesMGreene commented 2 years ago

@teakopp and I discussed this earlier this week and saw no reason why we shouldn't do this. 👍🏻

anishasthana commented 2 years ago

I'd like to work on this one!

anishasthana commented 2 years ago

@mahrsbar There needs to be a corresponding PR that needs to be made to the front end repo for this -- do we have an issue there for it?

mahrsbar commented 2 years ago

This is complete, https://github.com/ProgramEquity/amplify-back-end/pull/210. Sorry should be closed.