|
| 1 | +import type { PaginatedDocs, PayloadRequest, SanitizedCollectionConfig } from 'payload' |
| 2 | + |
| 3 | +/** |
| 4 | + * Enriches list view documents with correct draft status display. |
| 5 | + * When draft=true is used in the query, Payload returns the latest draft version if it exists. |
| 6 | + * This function checks if draft documents also have a published version to determine "changed" status. |
| 7 | + * |
| 8 | + * Performance: Uses a single query to find all documents with "changed" status instead of N queries. |
| 9 | + */ |
| 10 | +export async function enrichDocsWithVersionStatus({ |
| 11 | + collectionConfig, |
| 12 | + data, |
| 13 | + req, |
| 14 | +}: { |
| 15 | + collectionConfig: SanitizedCollectionConfig |
| 16 | + data: PaginatedDocs |
| 17 | + req: PayloadRequest |
| 18 | +}): Promise<PaginatedDocs> { |
| 19 | + const draftsEnabled = collectionConfig?.versions?.drafts |
| 20 | + |
| 21 | + if (!draftsEnabled || !data?.docs?.length) { |
| 22 | + return data |
| 23 | + } |
| 24 | + |
| 25 | + // Find all draft documents |
| 26 | + // When querying with draft:true, we get the latest draft if it exists |
| 27 | + // We need to check if these drafts have a published version |
| 28 | + const draftDocs = data.docs.filter((doc) => doc._status === 'draft') |
| 29 | + |
| 30 | + if (draftDocs.length === 0) { |
| 31 | + return data |
| 32 | + } |
| 33 | + |
| 34 | + const draftDocIds = draftDocs.map((doc) => doc.id).filter(Boolean) |
| 35 | + |
| 36 | + if (draftDocIds.length === 0) { |
| 37 | + return data |
| 38 | + } |
| 39 | + |
| 40 | + // OPTIMIZATION: Single query to find all document IDs that have BOTH: |
| 41 | + // 1. A draft version (latest=true, _status='draft') |
| 42 | + // 2. A published version (_status='published') |
| 43 | + // These are the documents with "changed" status |
| 44 | + try { |
| 45 | + // TODO: This could be more efficient with a findDistinctVersions() API: |
| 46 | + // const { values } = await req.payload.findDistinctVersions({ |
| 47 | + // collection: collectionConfig.slug, |
| 48 | + // field: 'parent', |
| 49 | + // where: { |
| 50 | + // and: [ |
| 51 | + // { parent: { in: draftDocIds } }, |
| 52 | + // { 'version._status': { equals: 'published' } }, |
| 53 | + // ], |
| 54 | + // }, |
| 55 | + // }) |
| 56 | + // const hasPublishedVersionSet = new Set(values) |
| 57 | + // |
| 58 | + // For now, we query all published versions but only select the 'parent' field |
| 59 | + // to minimize data transfer, then deduplicate with a Set |
| 60 | + const publishedVersions = await req.payload.findVersions({ |
| 61 | + collection: collectionConfig.slug, |
| 62 | + depth: 0, |
| 63 | + limit: 0, |
| 64 | + pagination: false, |
| 65 | + select: { |
| 66 | + parent: true, |
| 67 | + }, |
| 68 | + where: { |
| 69 | + and: [ |
| 70 | + { |
| 71 | + parent: { |
| 72 | + in: draftDocIds, |
| 73 | + }, |
| 74 | + }, |
| 75 | + { |
| 76 | + 'version._status': { |
| 77 | + equals: 'published', |
| 78 | + }, |
| 79 | + }, |
| 80 | + ], |
| 81 | + }, |
| 82 | + }) |
| 83 | + |
| 84 | + // Create a Set of document IDs that have published versions |
| 85 | + const hasPublishedVersionSet = new Set( |
| 86 | + publishedVersions.docs.map((version) => version.parent).filter(Boolean), |
| 87 | + ) |
| 88 | + |
| 89 | + // Enrich documents with display status |
| 90 | + const enrichedDocs = data.docs.map((doc) => { |
| 91 | + // If it's a draft and has a published version, show "changed" |
| 92 | + if (doc._status === 'draft' && hasPublishedVersionSet.has(doc.id)) { |
| 93 | + return { |
| 94 | + ...doc, |
| 95 | + _displayStatus: 'changed' as const, |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + return { |
| 100 | + ...doc, |
| 101 | + _displayStatus: doc._status as 'draft' | 'published', |
| 102 | + } |
| 103 | + }) |
| 104 | + |
| 105 | + return { |
| 106 | + ...data, |
| 107 | + docs: enrichedDocs, |
| 108 | + } |
| 109 | + } catch (error) { |
| 110 | + // If there's an error querying versions, just return the original data |
| 111 | + req.payload.logger.error({ |
| 112 | + err: error, |
| 113 | + msg: `Error checking version status for collection ${collectionConfig.slug}`, |
| 114 | + }) |
| 115 | + return data |
| 116 | + } |
| 117 | +} |
0 commit comments