fix: Server error in hooks.slack endpoint if team has no public collections
This commit is contained in:
@@ -258,6 +258,11 @@ Document.searchForTeam = async (
|
||||
const wildcardQuery = `${sequelize.escape(query)}:*`;
|
||||
const collectionIds = await team.collectionIds();
|
||||
|
||||
// If the team has access no public collections then shortcircuit the rest of this
|
||||
if (!collectionIds.length) {
|
||||
return [];
|
||||
}
|
||||
|
||||
// Build the SQL query to get documentIds, ranking, and search term context
|
||||
const sql = `
|
||||
SELECT
|
||||
@@ -328,6 +333,11 @@ Document.searchForUser = async (
|
||||
collectionIds = await user.collectionIds();
|
||||
}
|
||||
|
||||
// If the user has access to no collections then shortcircuit the rest of this
|
||||
if (!collectionIds.length) {
|
||||
return [];
|
||||
}
|
||||
|
||||
let dateFilter;
|
||||
if (options.dateFilter) {
|
||||
dateFilter = `1 ${options.dateFilter}`;
|
||||
|
||||
45
server/models/Document.test.js
Normal file
45
server/models/Document.test.js
Normal file
@@ -0,0 +1,45 @@
|
||||
/* eslint-disable flowtype/require-valid-file-annotation */
|
||||
import { flushdb } from '../test/support';
|
||||
import { Document } from '../models';
|
||||
import { buildDocument, buildCollection, buildTeam } from '../test/factories';
|
||||
|
||||
beforeEach(flushdb);
|
||||
beforeEach(jest.resetAllMocks);
|
||||
|
||||
describe('#searchForTeam', () => {
|
||||
test('should return search results from public collections', async () => {
|
||||
const team = await buildTeam();
|
||||
const collection = await buildCollection({ teamId: team.id });
|
||||
const document = await buildDocument({
|
||||
teamId: team.id,
|
||||
collectionId: collection.id,
|
||||
title: 'test',
|
||||
});
|
||||
|
||||
const results = await Document.searchForTeam(team, 'test');
|
||||
expect(results.length).toBe(1);
|
||||
expect(results[0].document.id).toBe(document.id);
|
||||
});
|
||||
|
||||
test('should not return search results from private collections', async () => {
|
||||
const team = await buildTeam();
|
||||
const collection = await buildCollection({
|
||||
private: true,
|
||||
teamId: team.id,
|
||||
});
|
||||
await buildDocument({
|
||||
teamId: team.id,
|
||||
collectionId: collection.id,
|
||||
title: 'test',
|
||||
});
|
||||
|
||||
const results = await Document.searchForTeam(team, 'test');
|
||||
expect(results.length).toBe(0);
|
||||
});
|
||||
|
||||
test('should handle no collections', async () => {
|
||||
const team = await buildTeam();
|
||||
const results = await Document.searchForTeam(team, 'test');
|
||||
expect(results.length).toBe(0);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user