fix: Drafts appear in document insert search (#1148)

* fix: Drafts appear in document insert search

* test
This commit is contained in:
Tom Moor
2020-01-05 17:24:57 -08:00
committed by GitHub
parent 9214be5645
commit 0ccbc6126b
5 changed files with 49 additions and 5 deletions

View File

@ -188,6 +188,7 @@ class Search extends React.Component<Props> {
limit: DEFAULT_PAGINATION_LIMIT, limit: DEFAULT_PAGINATION_LIMIT,
dateFilter: this.dateFilter, dateFilter: this.dateFilter,
includeArchived: this.includeArchived, includeArchived: this.includeArchived,
includeDrafts: true,
collectionId: this.collectionId, collectionId: this.collectionId,
userId: this.userId, userId: this.userId,
}); });

View File

@ -521,7 +521,14 @@ router.post('documents.restore', auth(), async ctx => {
}); });
router.post('documents.search', auth(), pagination(), 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 { offset, limit } = ctx.state.pagination;
const user = ctx.state.user; const user = ctx.state.user;
ctx.assertPresent(query, 'query is required'); 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, { const results = await Document.searchForUser(user, query, {
includeArchived: includeArchived === 'true', includeArchived: includeArchived === 'true',
includeDrafts: includeDrafts === 'true',
collaboratorIds, collaboratorIds,
collectionId, collectionId,
dateFilter, dateFilter,

View File

@ -556,7 +556,28 @@ describe('#documents.search', async () => {
expect(body.data[0].document.id).toEqual(firstResult.id); 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 { user } = await seed();
const document = await buildDocument({ const document = await buildDocument({
title: 'search term', title: 'search term',
@ -566,7 +587,11 @@ describe('#documents.search', async () => {
teamId: user.teamId, teamId: user.teamId,
}); });
const res = await server.post('/api/documents.search', { 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(); const body = await res.json();
@ -584,7 +609,11 @@ describe('#documents.search', async () => {
teamId: user.teamId, teamId: user.teamId,
}); });
const res = await server.post('/api/documents.search', { 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(); const body = await res.json();

View File

@ -244,6 +244,7 @@ type SearchOptions = {
dateFilter?: 'day' | 'week' | 'month' | 'year', dateFilter?: 'day' | 'week' | 'month' | 'year',
collaboratorIds?: string[], collaboratorIds?: string[],
includeArchived?: boolean, includeArchived?: boolean,
includeDrafts?: boolean,
}; };
Document.searchForTeam = async ( Document.searchForTeam = async (
@ -351,7 +352,11 @@ Document.searchForUser = async (
} }
${options.includeArchived ? '' : '"archivedAt" IS NULL AND'} ${options.includeArchived ? '' : '"archivedAt" IS NULL AND'}
"deletedAt" 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 ORDER BY
"searchRanking" DESC, "searchRanking" DESC,
"updatedAt" DESC "updatedAt" DESC

View File

@ -313,6 +313,7 @@ export default function Api() {
<Argument id="userId" description="User ID" /> <Argument id="userId" description="User ID" />
<Argument id="collectionId" description="Collection ID" /> <Argument id="collectionId" description="Collection ID" />
<Argument id="includeArchived" description="Boolean" /> <Argument id="includeArchived" description="Boolean" />
<Argument id="includeDrafts" description="Boolean" />
<Argument <Argument
id="dateFilter" id="dateFilter"
description="Date range to consider (day, week, month or year)" description="Date range to consider (day, week, month or year)"