Sitemap: Fix updated location date from latest photo

- Sorting was wrong
- Simplify requests
This commit is contained in:
2020-05-09 18:36:14 +02:00
parent 71ad577e6a
commit c4e70b8777

View File

@@ -45,20 +45,26 @@ const render = (pages, locations) => {
// Get function // Get function
export async function get (req, res, next) { export async function get (req, res, next) {
let locations
// Define base url from request // Define base url from request
baseURL = `https://${req.headers.host}` baseURL = `https://${req.headers.host}`
// Get locations // Get locations
const locationsReq = await fetch(`${apiEndpoints.rest}/items/locations?fields=slug,country.slug,modified_on&status=published`) await fetch(`${apiEndpoints.rest}/items/locations?fields=slug,country.slug,modified_on&status=published&sort=created_on`)
const locationsData = await locationsReq.json() .then(res => res.json())
const locations = locationsData.data .then(res => {
locations = res.data
})
// Add last modified date to each location from its last photo // Add last modified date to each location from its last photo
const updatedLocations = locations.map(async (location, i) => { const updatedLocations = locations.map(async (location, i) => {
const latestPhotoReq = await fetch(`${apiEndpoints.rest}/items/photos?fields=created_on&limit=1&sort=created_on&status=published&filter[location.slug][rlike]=%${location.slug}`) await fetch(`${apiEndpoints.rest}/items/photos?fields=created_on&limit=1&sort=-created_on&status=published&filter[location.slug][rlike]=%${location.slug}`)
const latestPhotoData = await latestPhotoReq.json() .then(res => res.json())
const latestPhoto = latestPhotoData.data[0] .then(res => {
const latestPhoto = res.data[0]
location.updated = latestPhoto ? latestPhoto.created_on : location.modified_on location.updated = latestPhoto ? latestPhoto.created_on : location.modified_on
})
return location return location
}) })
await Promise.all(updatedLocations) await Promise.all(updatedLocations)