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

154 lines
3.7 KiB
TypeScript
Raw Normal View History

2023-08-04 18:26:57 -05:00
import { prisma } from "@/lib/api/db";
2024-07-25 18:58:52 -05:00
import { LinkRequestQuery, Order, Sort } from "@/types/global";
2023-10-23 09:45:48 -05:00
export default async function getLink(userId: number, query: LinkRequestQuery) {
2024-06-27 17:19:07 -05:00
const POSTGRES_IS_ENABLED =
process.env.DATABASE_URL?.startsWith("postgresql");
2024-07-25 18:58:52 -05:00
let order: Order = { id: "desc" };
2023-12-23 11:11:47 -06:00
if (query.sort === Sort.DateNewestFirst) order = { id: "desc" };
else if (query.sort === Sort.DateOldestFirst) order = { id: "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 = [];
2023-10-23 09:45:48 -05:00
if (query.searchQueryString) {
if (query.searchByName) {
searchConditions.push({
name: {
2023-10-23 09:45:48 -05:00
contains: query.searchQueryString,
mode: POSTGRES_IS_ENABLED ? "insensitive" : undefined,
},
});
}
2023-10-23 09:45:48 -05:00
if (query.searchByUrl) {
searchConditions.push({
url: {
2023-10-23 09:45:48 -05:00
contains: query.searchQueryString,
mode: POSTGRES_IS_ENABLED ? "insensitive" : undefined,
2023-08-04 17:10:31 -05:00
},
});
}
2023-10-23 09:45:48 -05:00
if (query.searchByDescription) {
searchConditions.push({
description: {
2023-10-23 09:45:48 -05:00
contains: query.searchQueryString,
mode: POSTGRES_IS_ENABLED ? "insensitive" : undefined,
},
});
}
2023-11-01 05:01:26 -05:00
if (query.searchByTextContent) {
searchConditions.push({
textContent: {
contains: query.searchQueryString,
mode: POSTGRES_IS_ENABLED ? "insensitive" : undefined,
},
});
}
2023-10-23 09:45:48 -05:00
if (query.searchByTags) {
searchConditions.push({
tags: {
some: {
name: {
2023-10-23 09:45:48 -05:00
contains: query.searchQueryString,
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({
2024-07-18 09:27:32 -05:00
take: Number(process.env.PAGINATION_TAKE_COUNT) || 50,
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,
{
2023-10-23 09:45:48 -05:00
[query.searchQueryString ? "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,
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
}