Skip to content

Commit

Permalink
🐛 Improve sub-sub-folder hero Image finding
Browse files Browse the repository at this point in the history
  • Loading branch information
IsaacInsoll committed Dec 1, 2024
1 parent 12764ac commit 2ee99bf
Showing 1 changed file with 23 additions and 13 deletions.
36 changes: 23 additions & 13 deletions backend/graphql/helpers/heroImageForFolder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,30 @@ export const heroImageForFolder = async (f: Folder) => {
});
if (first) return first;

// 3. Hero image in any subfolder
// 3. Hero image in subfolder
const subFolder = await heroImageForSubFolder(f.id);
if (subFolder) return subFolder;

// 4. First image in any subfolder
const allSubFolders = await allSubFoldersRecursive(f.id);
const subFolderIds = allSubFolders.map((f) => f.id);
const s = await heroImageForSubFolder(subFolderIds);
if (s) return s;

const allImages = await File.findOne({
where: {
folderId: subFolderIds,
type: 'Image',
exists: true,
},
order: [['name', 'ASC']],
});
return allImages;
};

const heroImageForSubFolder = async (parentIds: string[]) => {
const subFolder = await Folder.findOne({
where: { parentId: f.id, exists: true, [Op.not]: { heroImageId: 0 } },
where: { parentId: parentIds, exists: true, [Op.not]: { heroImageId: 0 } },
order: [['name', 'ASC']],
});

Expand All @@ -34,15 +55,4 @@ export const heroImageForFolder = async (f: Folder) => {
) {
return subHeroImage;
}
// 4. First image in any subfolder
const allSubFolders = await allSubFoldersRecursive(f.id);
const allImages = await File.findOne({
where: {
folderId: allSubFolders.map((f) => f.id),
type: 'Image',
exists: true,
},
order: [['name', 'ASC']],
});
return allImages;
};

0 comments on commit 2ee99bf

Please sign in to comment.