Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pull the latest version of a letter template #593

Open
3 tasks
Tracked by #49
paramsiddharth opened this issue Sep 22, 2023 · 4 comments
Open
3 tasks
Tracked by #49

Pull the latest version of a letter template #593

paramsiddharth opened this issue Sep 22, 2023 · 4 comments
Assignees

Comments

@paramsiddharth
Copy link
Member

paramsiddharth commented Sep 22, 2023

User story: letters are updating by campaigns depending on which stage an action is in the middle of. Today we call in the letter template but if there is a new version

  • it isn’t updated and the old template shows up.

Directions

To update the Letterversion.js (amplify/server/routes/api/letter_versions.js) component to include the latest version of a letter, you can modify the campaignID function to retrieve the latest version of the letter from the Lob API. Here are the steps you can follow:

  • Line 5: Modify router method to retrieve the latest version of the letter from the Lob API:
    This code retrieves the latest version of the letter from the Lob API using the getLatestVersion action, and merges it with the existing letter data using the spread operator.
try {
    const letterId = this.$route.params.id
    const letter = await this.$store.dispatch('getLetter', letterId)

    // Retrieve the latest version of the letter from the Lob API
    const latestVersion = await this.$store.dispatch('getLatestVersion', letter.id)

    this.letter = {
      ...letter,
      ...latestVersion
    }
  } catch (error) {
    console.error(error)
  }
}
  • Modify the campaignId after try in line 6 action to retrieve the latest version of the letter from the Lob API and nest in the try from earlier :
async getLatestVersion({ commit }, letterId) {
  try {
    const response = await axios.get(`/api/letters/${letterId}/versions/latest`)
    const { data } = response

    return data
  } catch (error) {
    console.error(error)
  }
}

This code sends a GET request to the /api/letters/${letterId}/versions/latest endpoint to retrieve the latest version of the letter from the Lob API.

  • Line 235: Modify the server-side route handler for the /api/letters/:id/versions/latest endpoint to retrieve the latest version of the letter from the Lob API:
    This code sends a GET request to the Lob API to retrieve the list of versions for the specified letter ID, and returns the latest version in the response.
router.get('/api/letters/:id/versions/latest', async (req, res) => {
  try {
    const { id } = req.params
    const versions = await lob.letters.listVersions(id)
    const latestVersion = versions.data[0]

    res.json(latestVersion)
  } catch (error) {
    console.error(error)
    res.status(500).send(error.message)
  }
})

That's it! With these changes, the LetterLoad component should now retrieve the latest version of the letter from the Lob API. Note that you will need to modify the getLatestVersion action and server-side route handler to match your specific implementation.

@nancy-luu
Copy link
Collaborator

Hi @paramsiddharth may I work on this?

@manishapriya94
Copy link
Contributor

@nancy-luu realized that we will be updating CampaignID within letterversion.js and that will be called in lob.js in createLetter - updated instructions to reflect this

@nancy-luu
Copy link
Collaborator

Hi @manishapriya94 and @Alex-is-Gonzalez , please see my notes below for this issue.

When trying to implement the suggested solution above, there are a few points that I feel needs clarity.

  • Line 5: Modify router method to retrieve the latest version of the letter from the Lob API:
  • Assuming this step should be implemented in the letter_version.js file, the suggested solution above is not clear on how the latestVersion would be sent to the lob.js or LetterLoad.vue.
  • Currently in letter_version.js, we find letterVersions by querying with campaign_id. (see code below)
  • In the code snippet provided above, is const letterId = this.$route.params.id the same as what is happening in our current code below using campaignId? Also, in the current code we send the response as letterVersions. In the suggested solution, there is no way for the response to be sent.
router.get('/:campaignId', async (req, res) => {
  const campaignId = req.params.campaignId
  try {
    const letterVersions = await LetterVersion.query().where(
      'campaign_id',
      campaignId
    )
    res.send(letterVersions)
  } catch (error) {
    console.log(error)
    res.status(500).send({ error: 'Whoops' })
  }
})
  • Modify the campaignId after try in line 6 action to retrieve the latest version of the letter from the Lob API and nest in the try from earlier :
  • Line 235: Modify the server-side route handler for the /api/letters/:id/versions/latest endpoint to retrieve the latest version of the letter from the Lob API:
  • In a previous meeting we had discussed that the getLatestVersion function could be implemented inside the /createLetter route in Lob.js but the solution above also suggests that another route needs to be defined for this function to get the latest letter version.
  • Does is make more sense to abstract the getLatestVersion function outside of the /createLetter route?
  • In a broader perspective, how do letter_versions.js, LetterLoad.vue, and lob.js work together to ensure the latest letter template is effectively accessed? What is the source of the data, which file initiates its transmission, and is there a need to declare any specific action or method within the Vue component?

@nancy-luu
Copy link
Collaborator

For reference from Devs + QA

Data Structures

Campaigns
This is the major data table we use to onboard every advocacy
153418163-80588b94-6408-4fb8-9ce1-b52237fd1adf

id is the key identifier as there can be multiple orgs to one campaign
organization is an array of strings of the orgs behind a particular advocacy
name the name of the advocacy
cause the area the cause focuses on
page_url refers to the main site hosting the information to learn more about said cause
letters_counter how many letters have been sent to date

  • need to add a picture_url for campaign card
  • a description column
  • letters_goal column

letter_versions
This data is sent to Lob, primarily template_id which is specific to each campaign by office division.
This data structure triages the letter object that's being displayed and sent, specific to the template_id of the region that's being picked by the office.
​​
153047864-e5a493b2-2530-454a-bd9e-9e30e1a591ff

keyID: use to create join or belong relationships for Users, Campaigns, and Letters sent
template_id: lob html template
office_division: each campaign has a different letter dependent on filter. Federal is the default.
state: Custom versions per state
county: Custom versions per county
CampaignID: maps to campaigns table

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants