bug fix + cleaner code/logic
This commit is contained in:
@@ -1,25 +1,9 @@
|
||||
import { prisma } from "@/lib/api/db";
|
||||
import { LinkRequestQuery, LinkSearchFilter, Sort } from "@/types/global";
|
||||
import { LinkRequestQuery, Sort } from "@/types/global";
|
||||
|
||||
export default async function getLink(userId: number, query: LinkRequestQuery) {
|
||||
query.sort = Number(query.sort) || 0;
|
||||
query.pinnedOnly = query.pinnedOnly
|
||||
? JSON.parse(query.pinnedOnly as unknown as string)
|
||||
: undefined;
|
||||
|
||||
if (query.searchFilter) {
|
||||
const filterParams = (query.searchFilter as unknown as string).split("-");
|
||||
|
||||
query.searchFilter = {} as LinkSearchFilter;
|
||||
|
||||
query.searchFilter.name = JSON.parse(filterParams[0]);
|
||||
query.searchFilter.url = JSON.parse(filterParams[1]);
|
||||
query.searchFilter.description = JSON.parse(filterParams[2]);
|
||||
query.searchFilter.collection = JSON.parse(filterParams[3]);
|
||||
query.searchFilter.tags = JSON.parse(filterParams[4]);
|
||||
}
|
||||
|
||||
console.log(query.searchFilter);
|
||||
export default async function getLink(userId: number, body: string) {
|
||||
const query: LinkRequestQuery = JSON.parse(decodeURIComponent(body));
|
||||
console.log(query);
|
||||
|
||||
// Sorting logic
|
||||
let order: any;
|
||||
@@ -48,76 +32,77 @@ export default async function getLink(userId: number, query: LinkRequestQuery) {
|
||||
name: "desc",
|
||||
};
|
||||
|
||||
const links =
|
||||
// Searching logic
|
||||
query.searchFilter && query.searchQuery
|
||||
? await prisma.link.findMany({
|
||||
take: Number(process.env.PAGINATION_TAKE_COUNT),
|
||||
skip: query.cursor !== "undefined" ? 1 : undefined,
|
||||
cursor:
|
||||
query.cursor !== "undefined"
|
||||
const links = await prisma.link.findMany({
|
||||
take: Number(process.env.PAGINATION_TAKE_COUNT),
|
||||
skip: query.cursor ? 1 : undefined,
|
||||
cursor: query.cursor
|
||||
? {
|
||||
id: query.cursor,
|
||||
}
|
||||
: undefined,
|
||||
where: {
|
||||
[query.searchQuery ? "OR" : "AND"]: [
|
||||
{
|
||||
pinnedBy: query.pinnedOnly ? { some: { id: userId } } : undefined,
|
||||
},
|
||||
{
|
||||
name: {
|
||||
contains: query.searchFilter?.name ? query.searchQuery : undefined,
|
||||
mode: "insensitive",
|
||||
},
|
||||
},
|
||||
{
|
||||
url: {
|
||||
contains: query.searchFilter?.url ? query.searchQuery : undefined,
|
||||
mode: "insensitive",
|
||||
},
|
||||
},
|
||||
{
|
||||
description: {
|
||||
contains: query.searchFilter?.description
|
||||
? query.searchQuery
|
||||
: undefined,
|
||||
mode: "insensitive",
|
||||
},
|
||||
},
|
||||
{
|
||||
collection: {
|
||||
id: query.collectionId ? query.collectionId : undefined, // If collectionId was defined, filter by collection
|
||||
name: query.searchFilter?.collection
|
||||
? {
|
||||
id: Number(query.cursor),
|
||||
contains: query.searchQuery,
|
||||
mode: "insensitive",
|
||||
}
|
||||
: undefined,
|
||||
where: {
|
||||
OR: [
|
||||
{
|
||||
name: {
|
||||
contains: query.searchFilter?.name
|
||||
? query.searchQuery
|
||||
: undefined,
|
||||
mode: "insensitive",
|
||||
},
|
||||
},
|
||||
{
|
||||
url: {
|
||||
contains: query.searchFilter?.url
|
||||
? query.searchQuery
|
||||
: undefined,
|
||||
mode: "insensitive",
|
||||
},
|
||||
},
|
||||
{
|
||||
description: {
|
||||
contains: query.searchFilter?.description
|
||||
? query.searchQuery
|
||||
: undefined,
|
||||
mode: "insensitive",
|
||||
},
|
||||
},
|
||||
{
|
||||
collection: {
|
||||
name: {
|
||||
contains: query.searchFilter?.collection
|
||||
? query.searchQuery
|
||||
: undefined,
|
||||
mode: "insensitive",
|
||||
OR: query.searchQuery
|
||||
? undefined
|
||||
: [
|
||||
{
|
||||
ownerId: userId,
|
||||
},
|
||||
OR: [
|
||||
{
|
||||
ownerId: userId,
|
||||
},
|
||||
{
|
||||
members: {
|
||||
some: {
|
||||
userId,
|
||||
},
|
||||
{
|
||||
members: {
|
||||
some: {
|
||||
userId,
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
{
|
||||
tags: {
|
||||
// If tagId was defined, search by tag
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
{
|
||||
tags:
|
||||
query.searchQuery && !query.searchFilter?.tags
|
||||
? undefined
|
||||
: {
|
||||
some: {
|
||||
name: {
|
||||
contains: query.searchFilter?.tags
|
||||
? query.searchQuery
|
||||
: undefined,
|
||||
mode: "insensitive",
|
||||
},
|
||||
id: query.tagId ? query.tagId : undefined, // If tagId was defined, filter by tag
|
||||
name: query.searchFilter?.tags
|
||||
? {
|
||||
contains: query.searchQuery,
|
||||
mode: "insensitive",
|
||||
}
|
||||
: undefined,
|
||||
OR: [
|
||||
{ ownerId: userId }, // Tags owned by the user
|
||||
{
|
||||
@@ -142,88 +127,21 @@ export default async function getLink(userId: number, query: LinkRequestQuery) {
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
include: {
|
||||
tags: true,
|
||||
collection: true,
|
||||
pinnedBy: {
|
||||
where: { id: userId },
|
||||
select: { id: true },
|
||||
},
|
||||
},
|
||||
orderBy: order || undefined,
|
||||
})
|
||||
: // If not searching
|
||||
await prisma.link.findMany({
|
||||
take: Number(process.env.PAGINATION_TAKE_COUNT),
|
||||
skip: query.cursor !== "undefined" ? 1 : undefined,
|
||||
cursor:
|
||||
query.cursor !== "undefined"
|
||||
? {
|
||||
id: Number(query.cursor),
|
||||
}
|
||||
: undefined,
|
||||
where: {
|
||||
pinnedBy: query.pinnedOnly ? { some: { id: userId } } : undefined,
|
||||
collection: {
|
||||
id: query.collectionId && Number(query.collectionId), // If collectionId was defined, search by collection
|
||||
|
||||
OR: [
|
||||
{
|
||||
ownerId: userId,
|
||||
},
|
||||
{
|
||||
members: {
|
||||
some: {
|
||||
userId,
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
tags: {
|
||||
some: query.tagId // If tagId was defined, search by tag
|
||||
? {
|
||||
id: Number(query.tagId),
|
||||
OR: [
|
||||
{ ownerId: userId }, // Tags owned by the user
|
||||
{
|
||||
links: {
|
||||
some: {
|
||||
collection: {
|
||||
members: {
|
||||
some: {
|
||||
userId, // Tags from collections where the user is a member
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
}
|
||||
: undefined,
|
||||
},
|
||||
},
|
||||
include: {
|
||||
tags: true,
|
||||
collection: {
|
||||
select: {
|
||||
id: true,
|
||||
ownerId: true,
|
||||
name: true,
|
||||
color: true,
|
||||
},
|
||||
},
|
||||
pinnedBy: {
|
||||
where: { id: userId },
|
||||
select: { id: true },
|
||||
},
|
||||
},
|
||||
orderBy: order || undefined,
|
||||
});
|
||||
},
|
||||
],
|
||||
},
|
||||
include: {
|
||||
tags: true,
|
||||
collection: true,
|
||||
pinnedBy: {
|
||||
where: { id: userId },
|
||||
select: { id: true },
|
||||
},
|
||||
},
|
||||
orderBy: order || {
|
||||
createdAt: "desc",
|
||||
},
|
||||
});
|
||||
|
||||
return { response: links, status: 200 };
|
||||
}
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
import { prisma } from "@/lib/api/db";
|
||||
import { PublicLinkRequestQuery } from "@/types/global";
|
||||
|
||||
export default async function getCollection(query: PublicLinkRequestQuery) {
|
||||
export default async function getCollection(body: string) {
|
||||
const query: PublicLinkRequestQuery = JSON.parse(decodeURIComponent(body));
|
||||
console.log(query);
|
||||
|
||||
let data;
|
||||
|
||||
const collection = await prisma.collection.findFirst({
|
||||
@@ -14,13 +17,12 @@ export default async function getCollection(query: PublicLinkRequestQuery) {
|
||||
if (collection) {
|
||||
const links = await prisma.link.findMany({
|
||||
take: Number(process.env.PAGINATION_TAKE_COUNT),
|
||||
skip: query.cursor !== "undefined" ? 1 : undefined,
|
||||
cursor:
|
||||
query.cursor !== "undefined"
|
||||
? {
|
||||
id: Number(query.cursor),
|
||||
}
|
||||
: undefined,
|
||||
skip: query.cursor ? 1 : undefined,
|
||||
cursor: query.cursor
|
||||
? {
|
||||
id: Number(query.cursor),
|
||||
}
|
||||
: undefined,
|
||||
where: {
|
||||
collection: {
|
||||
id: Number(query.collectionId),
|
||||
|
||||
Reference in New Issue
Block a user