Add a function to get all photo albums

This commit is contained in:
Quinn Hegeman
2026-03-08 17:24:39 +01:00
parent 44437c766b
commit ad73ab5672
6 changed files with 166 additions and 2 deletions

View File

@@ -0,0 +1,78 @@
import { createDirectusConnection } from "@/lib/directus";
import { print } from "graphql";
import getAlbums from '@/graphql/photos/getAlbums.graphql';
import { formatDate } from "@/lib/dates";
export async function getAllAlbums(settings: GlobalSettings): Promise<PhotoAlbum[]> {
const client = await createDirectusConnection();
const result = await client.query(print(getAlbums), {
date: formatDate(new Date(), "%Y-%M-%D")
});
let albums: PhotoAlbum[] = [];
result["Photo_Albums"].forEach((albumRecord: any) => {
let dates: string[] = [
settings.website.lastModified.toISOString(),
settings.photo.lastModified.toISOString(),
albumRecord["date_created"],
albumRecord["date_updated"],
albumRecord["thumbnail"]["created_on"],
];
const album: PhotoAlbum = {
title: albumRecord["title"],
description: albumRecord["description"],
url: albumRecord["url"],
startDate: albumRecord["start_date"],
endDate: albumRecord["end_date"],
location: albumRecord["location"],
category: {
title: albumRecord["category"][0]["Photo_Categories_id"]["title"],
url: albumRecord["category"][0]["Photo_Categories_id"]["url"],
thumbnail: {
url: albumRecord["category"][0]["Photo_Categories_id"]["thumbnail"]["filename_disk"],
height: albumRecord["category"][0]["Photo_Categories_id"]["thumbnail"]["height"],
width: albumRecord["category"][0]["Photo_Categories_id"]["thumbnail"]["width"]
}
},
thumbnail: {
url: albumRecord["thumbnail"]["filename_download"],
height: albumRecord["thumbnail"]["height"],
width: albumRecord["thumbnail"]["width"]
},
photos: [],
lastModified: new Date()
};
albumRecord["photos"].forEach((photoRecord: any) => {
album.photos.push({
photo: {
url: photoRecord["photo"]["filename_download"],
height: photoRecord["photo"]["filename_download"]["height"],
width: photoRecord["photo"]["filename_download"]["width"]
},
text: photoRecord["text"]
});
dates.push(photoRecord["date_created"]);
dates.push(photoRecord["date_updated"]);
dates.push(photoRecord["photo"]["created_on"]);
});
if (dates.filter(e => e !== null).length === 0) {
album.lastModified = new Date();
}
else {
const sortedDates: string[] = dates.sort((a: string, b: string) => {
return new Date(b).getTime() - new Date(a).getTime();
});
album.lastModified = new Date(sortedDates[0]);
}
albums.push(album);
});
return albums;
}