2023-08-04 18:26:57 -05:00
|
|
|
import { prisma } from "@/lib/api/db";
|
|
|
|
import { LinkRequestQuery, Sort } from "@/types/global";
|
2023-06-14 17:34:54 -05:00
|
|
|
|
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));
|
|
|
|
console.log(query);
|
2023-06-14 17:34:54 -05:00
|
|
|
|
2023-08-04 17:10:31 -05:00
|
|
|
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 = {
|
|
|
|
name: "asc",
|
|
|
|
};
|
|
|
|
else if (query.sort === Sort.DescriptionZA)
|
|
|
|
order = {
|
|
|
|
name: "desc",
|
|
|
|
};
|
2023-06-14 17:34:54 -05:00
|
|
|
|
2023-08-04 17:10:31 -05:00
|
|
|
const links = await prisma.link.findMany({
|
|
|
|
take: Number(process.env.PAGINATION_TAKE_COUNT) || 20,
|
|
|
|
skip: query.cursor ? 1 : undefined,
|
|
|
|
cursor: query.cursor
|
|
|
|
? {
|
2023-08-04 18:26:57 -05:00
|
|
|
id: query.cursor,
|
|
|
|
}
|
2023-08-04 17:10:31 -05:00
|
|
|
: undefined,
|
|
|
|
where: {
|
|
|
|
collection: {
|
|
|
|
id: query.collectionId ? query.collectionId : undefined, // If collectionId was defined, filter by collection
|
|
|
|
OR: [
|
|
|
|
{
|
|
|
|
ownerId: userId,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
members: {
|
|
|
|
some: {
|
|
|
|
userId,
|
|
|
|
},
|
2023-07-05 02:13:37 -05:00
|
|
|
},
|
2023-08-04 17:10:31 -05:00
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
[query.searchQuery ? "OR" : "AND"]: [
|
|
|
|
{
|
2023-08-04 18:26:57 -05:00
|
|
|
pinnedBy: query.pinnedOnly ? { some: { id: userId } } : undefined,
|
2023-08-04 12:08:04 -05:00
|
|
|
},
|
2023-08-04 17:10:31 -05:00
|
|
|
{
|
|
|
|
name: {
|
|
|
|
contains:
|
|
|
|
query.searchQuery && query.searchFilter?.name
|
|
|
|
? query.searchQuery
|
|
|
|
: undefined,
|
2023-08-04 18:26:57 -05:00
|
|
|
mode: POSTGRES_IS_ENABLED ? "insensitive" : undefined,
|
2023-08-04 17:10:31 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
url: {
|
|
|
|
contains:
|
|
|
|
query.searchQuery && query.searchFilter?.url
|
|
|
|
? query.searchQuery
|
|
|
|
: undefined,
|
2023-08-04 18:26:57 -05:00
|
|
|
mode: POSTGRES_IS_ENABLED ? "insensitive" : undefined,
|
2023-08-04 17:10:31 -05:00
|
|
|
},
|
2023-08-04 12:08:04 -05:00
|
|
|
},
|
2023-08-04 17:10:31 -05:00
|
|
|
{
|
|
|
|
description: {
|
|
|
|
contains:
|
|
|
|
query.searchQuery && query.searchFilter?.description
|
|
|
|
? query.searchQuery
|
|
|
|
: undefined,
|
2023-08-04 18:26:57 -05:00
|
|
|
mode: POSTGRES_IS_ENABLED ? "insensitive" : undefined,
|
2023-08-04 17:10:31 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
tags:
|
|
|
|
query.searchQuery && !query.searchFilter?.tags
|
|
|
|
? undefined
|
|
|
|
: {
|
2023-08-04 18:26:57 -05:00
|
|
|
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"
|
2023-08-04 17:10:31 -05:00
|
|
|
: undefined,
|
2023-08-04 18:26:57 -05:00
|
|
|
}
|
|
|
|
: 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
|
|
|
|
},
|
|
|
|
},
|
2023-08-04 17:10:31 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2023-08-04 18:26:57 -05:00
|
|
|
],
|
|
|
|
}
|
|
|
|
: undefined,
|
|
|
|
},
|
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-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
|
|
|
}
|