fix: include in documents.search_titles

This commit is contained in:
Apoorv Mishra committed 2023-12-30 19:07:41 +05:30
1 parent 6cb0118747
commit 284ce18001
2 files changed
+114 -18

No files matched your search

+35 -18
View File
@@ -188,28 +188,37 @@ export default class SearchHelper {
// Ensure we're filtering by the users accessible collections. If
// collectionId is passed as an option it is assumed that the authorization
// has already been done in the router
let collectionIds;
if (options.collectionId) {
where[Op.and].push({
collectionId: options.collectionId,
});
collectionIds = [options.collectionId];
} else {
where[Op.and].push({
[Op.or]: [
{
collectionId: {
[Op.in]: await user.collectionIds(),
},
},
{
collectionId: {
[Op.is]: null,
},
createdById: user.id,
},
],
});
collectionIds = await user.collectionIds();
}
const documentIds = await user.documentIds();
where[Op.and].push({
[Op.or]: [
{
collectionId: {
[Op.in]: collectionIds,
},
},
{
collectionId: {
[Op.is]: null,
},
createdById: user.id,
},
{
id: {
[Op.in]: documentIds,
},
},
],
});
if (options.dateFilter) {
where[Op.and].push({
updatedAt: {
@@ -239,6 +248,11 @@ export default class SearchHelper {
{
createdById: user.id,
},
{
id: {
[Op.in]: documentIds,
},
},
],
});
} else {
@@ -266,6 +280,9 @@ export default class SearchHelper {
{
method: ["withCollectionPermissions", user.id],
},
{
method: ["withMembership", user.id],
},
]).findAll({
where,
order: [["updatedAt", "DESC"]],
@@ -1067,6 +1067,85 @@ describe("#documents.drafts", () => {
});
describe("#documents.search_titles", () => {
it("should include individually shared drafts with a user in search results", async () => {
const user = await buildUser();
// create a private collection
const collection = await buildCollection({
createdById: user.id,
teamId: user.teamId,
permission: null,
});
// create a draft in collection
const document = await buildDocument({
collectionId: collection.id,
teamId: user.teamId,
createdById: user.id,
title: "Some title",
});
document.publishedAt = null;
await document.save();
const member = await buildUser({
teamId: user.teamId,
});
// add member to the document
await server.post("/api/documents.add_user", {
body: {
token: user.getJwtToken(),
id: document.id,
userId: member.id,
},
});
const res = await server.post("/api/documents.search_titles", {
body: {
token: member.getJwtToken(),
query: "title",
includeDrafts: true,
},
});
const body = await res.json();
expect(body.data).toHaveLength(1);
expect(body.data[0].id).toEqual(document.id);
expect(res.status).toEqual(200);
});
it("should include individually shared docs with a user in search results", async () => {
const user = await buildUser();
// create a private collection
const collection = await buildCollection({
createdById: user.id,
teamId: user.teamId,
permission: null,
});
// create document in that private collection
const document = await buildDocument({
collectionId: collection.id,
teamId: user.teamId,
createdById: user.id,
title: "Some title",
});
const member = await buildUser({
teamId: user.teamId,
});
// add member to the document
await server.post("/api/documents.add_user", {
body: {
token: user.getJwtToken(),
id: document.id,
userId: member.id,
},
});
const res = await server.post("/api/documents.search_titles", {
body: {
token: member.getJwtToken(),
query: "title",
},
});
const body = await res.json();
expect(body.data).toHaveLength(1);
expect(body.data[0].id).toEqual(document.id);
expect(res.status).toEqual(200);
});
it("should fail without query", async () => {
const user = await buildUser();
const res = await server.post("/api/documents.search_titles", {