diff --git a/app/scenes/Search/Search.js b/app/scenes/Search/Search.js index 10170b5d..cc9cbb38 100644 --- a/app/scenes/Search/Search.js +++ b/app/scenes/Search/Search.js @@ -188,6 +188,7 @@ class Search extends React.Component { limit: DEFAULT_PAGINATION_LIMIT, dateFilter: this.dateFilter, includeArchived: this.includeArchived, + includeDrafts: true, collectionId: this.collectionId, userId: this.userId, }); diff --git a/server/api/documents.js b/server/api/documents.js index d1d1d3f0..cae59faa 100644 --- a/server/api/documents.js +++ b/server/api/documents.js @@ -521,7 +521,14 @@ router.post('documents.restore', auth(), async ctx => { }); router.post('documents.search', auth(), pagination(), async ctx => { - const { query, includeArchived, collectionId, userId, dateFilter } = ctx.body; + const { + query, + includeArchived, + includeDrafts, + collectionId, + userId, + dateFilter, + } = ctx.body; const { offset, limit } = ctx.state.pagination; const user = ctx.state.user; ctx.assertPresent(query, 'query is required'); @@ -551,6 +558,7 @@ router.post('documents.search', auth(), pagination(), async ctx => { const results = await Document.searchForUser(user, query, { includeArchived: includeArchived === 'true', + includeDrafts: includeDrafts === 'true', collaboratorIds, collectionId, dateFilter, diff --git a/server/api/documents.test.js b/server/api/documents.test.js index ff77336b..3000dba1 100644 --- a/server/api/documents.test.js +++ b/server/api/documents.test.js @@ -556,7 +556,28 @@ describe('#documents.search', async () => { expect(body.data[0].document.id).toEqual(firstResult.id); }); - it('should return draft documents created by user', async () => { + it('should not return draft documents', async () => { + const { user } = await seed(); + await buildDocument({ + title: 'search term', + text: 'search term', + publishedAt: null, + userId: user.id, + teamId: user.teamId, + }); + const res = await server.post('/api/documents.search', { + body: { + token: user.getJwtToken(), + query: 'search term', + }, + }); + const body = await res.json(); + + expect(res.status).toEqual(200); + expect(body.data.length).toEqual(0); + }); + + it('should return draft documents created by user if chosen', async () => { const { user } = await seed(); const document = await buildDocument({ title: 'search term', @@ -566,7 +587,11 @@ describe('#documents.search', async () => { teamId: user.teamId, }); const res = await server.post('/api/documents.search', { - body: { token: user.getJwtToken(), query: 'search term' }, + body: { + token: user.getJwtToken(), + query: 'search term', + includeDrafts: 'true', + }, }); const body = await res.json(); @@ -584,7 +609,11 @@ describe('#documents.search', async () => { teamId: user.teamId, }); const res = await server.post('/api/documents.search', { - body: { token: user.getJwtToken(), query: 'search term' }, + body: { + token: user.getJwtToken(), + query: 'search term', + includeDrafts: 'true', + }, }); const body = await res.json(); diff --git a/server/models/Document.js b/server/models/Document.js index c52752d1..4cd42c5f 100644 --- a/server/models/Document.js +++ b/server/models/Document.js @@ -244,6 +244,7 @@ type SearchOptions = { dateFilter?: 'day' | 'week' | 'month' | 'year', collaboratorIds?: string[], includeArchived?: boolean, + includeDrafts?: boolean, }; Document.searchForTeam = async ( @@ -351,7 +352,11 @@ Document.searchForUser = async ( } ${options.includeArchived ? '' : '"archivedAt" IS NULL AND'} "deletedAt" IS NULL AND - ("publishedAt" IS NOT NULL OR "createdById" = :userId) + ${ + options.includeDrafts + ? '("publishedAt" IS NOT NULL OR "createdById" = :userId)' + : '"publishedAt" IS NOT NULL' + } ORDER BY "searchRanking" DESC, "updatedAt" DESC diff --git a/server/pages/developers/Api.js b/server/pages/developers/Api.js index 8a61a419..87b43b9a 100644 --- a/server/pages/developers/Api.js +++ b/server/pages/developers/Api.js @@ -313,6 +313,7 @@ export default function Api() { +