el.xwx.moe/lib/api/controllers/links/getLinks.ts

146 lines
3.5 KiB
TypeScript
Raw Normal View History

2023-08-04 18:26:57 -05:00
import { prisma } from "@/lib/api/db";
import { LinkRequestQuery, Sort } from "@/types/global";
2023-06-15 07:39:30 -05:00
export default async function getLink(userId: number, body: string) {
2023-08-04 17:10:31 -05:00
const query: LinkRequestQuery = JSON.parse(decodeURIComponent(body));
2023-08-04 17:10:31 -05:00
const POSTGRES_IS_ENABLED = process.env.DATABASE_URL.startsWith("postgresql");
2023-08-04 17:10:31 -05:00
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" };
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,
2023-08-04 17:10:31 -05:00
},
});
}
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 },
},
},
},
},
},
],
2023-08-04 17:10:31 -05:00
},
},
});
}
}
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,
where: {
AND: [
2023-08-04 17:10:31 -05:00
{
collection: {
OR: [
{ ownerId: userId },
{
members: {
some: { userId },
},
},
],
2023-08-04 17:10:31 -05:00
},
},
...collectionCondition,
2023-08-04 17:10:31 -05:00
{
OR: [
...tagCondition,
{
[query.searchQuery ? "OR" : "AND"]: [
{
pinnedBy: query.pinnedOnly
? { some: { id: userId } }
2023-08-04 18:26:57 -05:00
: undefined,
},
...searchConditions,
],
},
],
2023-06-15 07:39:30 -05:00
},
2023-08-04 17:10:31 -05:00
],
},
include: {
tags: true,
collection: true,
pinnedBy: {
2023-08-04 18:26:57 -05:00
where: { id: userId },
select: { id: true },
2023-08-04 17:10:31 -05:00
},
},
orderBy: order || { createdAt: "desc" },
2023-08-04 17:10:31 -05:00
});
2023-03-05 15:03:20 -06:00
2023-08-04 18:26:57 -05:00
return { response: links, status: 200 };
2023-03-05 15:03:20 -06:00
}