Pull the latest version of a letter template
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.
Hi @paramsiddharth may I work on this?
@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
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.idthe 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?
For reference from Devs + QA
Data Structures
Campaigns
This is the major data table we use to onboard every advocacy
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.
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