fixed filter by tags + refactored search + bug fixed + settings page [WIP]

This commit is contained in:
daniel31x13
2023-10-16 13:10:52 -04:00
parent 36a1ed209e
commit f98500ec4e
22 changed files with 561 additions and 184 deletions
+111 -121
View File
@@ -3,138 +3,130 @@ import { LinkRequestQuery, Sort } from "@/types/global";
export default async function getLink(userId: number, body: string) {
const query: LinkRequestQuery = JSON.parse(decodeURIComponent(body));
console.log(query);
const POSTGRES_IS_ENABLED = process.env.DATABASE_URL.startsWith("postgresql");
// Sorting logic
let order: any;
if (query.sort === Sort.DateNewestFirst)
order = {
createdAt: "desc",
};
else if (query.sort === Sort.DateOldestFirst)
order = {
createdAt: "asc",
};
else if (query.sort === Sort.NameAZ)
order = {
name: "asc",
};
else if (query.sort === Sort.NameZA)
order = {
name: "desc",
};
else if (query.sort === Sort.DescriptionAZ)
order = {
description: "asc",
};
else if (query.sort === Sort.DescriptionZA)
order = {
description: "desc",
};
if (query.sort === Sort.DateNewestFirst) order = { createdAt: "desc" };
else if (query.sort === Sort.DateOldestFirst) order = { createdAt: "asc" };
else if (query.sort === Sort.NameAZ) order = { name: "asc" };
else if (query.sort === Sort.NameZA) order = { name: "desc" };
else if (query.sort === Sort.DescriptionAZ) order = { description: "asc" };
else if (query.sort === Sort.DescriptionZA) order = { description: "desc" };
const searchConditions = [];
if (query.searchQuery) {
if (query.searchFilter?.name) {
searchConditions.push({
name: {
contains: query.searchQuery,
mode: POSTGRES_IS_ENABLED ? "insensitive" : undefined,
},
});
}
if (query.searchFilter?.url) {
searchConditions.push({
url: {
contains: query.searchQuery,
mode: POSTGRES_IS_ENABLED ? "insensitive" : undefined,
},
});
}
if (query.searchFilter?.description) {
searchConditions.push({
description: {
contains: query.searchQuery,
mode: POSTGRES_IS_ENABLED ? "insensitive" : undefined,
},
});
}
if (query.searchFilter?.tags) {
searchConditions.push({
tags: {
some: {
name: {
contains: query.searchQuery,
mode: POSTGRES_IS_ENABLED ? "insensitive" : undefined,
},
OR: [
{ ownerId: userId },
{
links: {
some: {
collection: {
members: {
some: { userId },
},
},
},
},
},
],
},
},
});
}
}
const tagCondition = [];
if (query.tagId) {
tagCondition.push({
tags: {
some: {
id: query.tagId,
},
},
});
}
const collectionCondition = [];
if (query.collectionId) {
collectionCondition.push({
collection: {
id: query.collectionId,
},
});
}
const links = await prisma.link.findMany({
take: Number(process.env.PAGINATION_TAKE_COUNT) || 20,
skip: query.cursor ? 1 : undefined,
cursor: query.cursor
? {
id: query.cursor,
}
: undefined,
cursor: query.cursor ? { id: query.cursor } : undefined,
where: {
collection: {
id: query.collectionId ? query.collectionId : undefined, // If collectionId was defined, filter by collection
OR: [
{
ownerId: userId,
},
{
members: {
some: {
userId,
AND: [
{
collection: {
OR: [
{ ownerId: userId },
{
members: {
some: { userId },
},
},
},
},
],
},
[query.searchQuery ? "OR" : "AND"]: [
{
pinnedBy: query.pinnedOnly ? { some: { id: userId } } : undefined,
},
{
name: {
contains:
query.searchQuery && query.searchFilter?.name
? query.searchQuery
: undefined,
mode: POSTGRES_IS_ENABLED ? "insensitive" : undefined,
],
},
},
...collectionCondition,
{
url: {
contains:
query.searchQuery && query.searchFilter?.url
? query.searchQuery
: undefined,
mode: POSTGRES_IS_ENABLED ? "insensitive" : undefined,
},
},
{
description: {
contains:
query.searchQuery && query.searchFilter?.description
? query.searchQuery
: undefined,
mode: POSTGRES_IS_ENABLED ? "insensitive" : undefined,
},
},
{
tags:
query.searchQuery && !query.searchFilter?.tags
? undefined
: {
some: query.tagId
? {
// If tagId was defined, filter by tag
id: query.tagId,
name:
query.searchQuery && query.searchFilter?.tags
? {
contains: query.searchQuery,
mode: POSTGRES_IS_ENABLED
? "insensitive"
: undefined,
}
: undefined,
OR: [
{ ownerId: userId }, // Tags owned by the user
{
links: {
some: {
name: {
contains:
query.searchQuery &&
query.searchFilter?.tags
? query.searchQuery
: undefined,
mode: POSTGRES_IS_ENABLED
? "insensitive"
: undefined,
},
collection: {
members: {
some: {
userId, // Tags from collections where the user is a member
},
},
},
},
},
},
],
}
OR: [
...tagCondition,
{
[query.searchQuery ? "OR" : "AND"]: [
{
pinnedBy: query.pinnedOnly
? { some: { id: userId } }
: undefined,
},
...searchConditions,
],
},
],
},
],
},
@@ -146,9 +138,7 @@ export default async function getLink(userId: number, body: string) {
select: { id: true },
},
},
orderBy: order || {
createdAt: "desc",
},
orderBy: order || { createdAt: "desc" },
});
return { response: links, status: 200 };