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