2023-10-23 09:45:48 -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 getDashboardData(
|
|
|
|
userId: number,
|
|
|
|
query: LinkRequestQuery
|
|
|
|
) {
|
2024-07-25 18:58:52 -05:00
|
|
|
let order: Order = { id: "desc" };
|
2023-12-25 19:43:31 -06:00
|
|
|
if (query.sort === Sort.DateNewestFirst) order = { id: "desc" };
|
|
|
|
else if (query.sort === Sort.DateOldestFirst) order = { id: "asc" };
|
2023-10-23 09:45:48 -05:00
|
|
|
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 pinnedLinks = await prisma.link.findMany({
|
2024-05-24 18:13:04 -05:00
|
|
|
take: 10,
|
2023-10-23 09:45:48 -05:00
|
|
|
where: {
|
|
|
|
AND: [
|
|
|
|
{
|
|
|
|
collection: {
|
|
|
|
OR: [
|
|
|
|
{ ownerId: userId },
|
|
|
|
{
|
|
|
|
members: {
|
|
|
|
some: { userId },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
pinnedBy: { some: { id: userId } },
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
include: {
|
|
|
|
tags: true,
|
|
|
|
collection: true,
|
|
|
|
pinnedBy: {
|
|
|
|
where: { id: userId },
|
|
|
|
select: { id: true },
|
|
|
|
},
|
|
|
|
},
|
2024-08-12 23:08:57 -05:00
|
|
|
orderBy: order,
|
2023-10-23 09:45:48 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
const recentlyAddedLinks = await prisma.link.findMany({
|
2024-05-24 18:13:04 -05:00
|
|
|
take: 10,
|
2023-10-23 09:45:48 -05:00
|
|
|
where: {
|
|
|
|
collection: {
|
|
|
|
OR: [
|
|
|
|
{ ownerId: userId },
|
|
|
|
{
|
|
|
|
members: {
|
|
|
|
some: { userId },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
include: {
|
|
|
|
tags: true,
|
|
|
|
collection: true,
|
|
|
|
pinnedBy: {
|
|
|
|
where: { id: userId },
|
|
|
|
select: { id: true },
|
|
|
|
},
|
|
|
|
},
|
2024-08-12 23:08:57 -05:00
|
|
|
orderBy: order,
|
2023-10-23 09:45:48 -05:00
|
|
|
});
|
|
|
|
|
2024-08-12 23:08:57 -05:00
|
|
|
const combinedLinks = [...recentlyAddedLinks, ...pinnedLinks];
|
|
|
|
|
|
|
|
const uniqueLinks = Array.from(
|
|
|
|
combinedLinks
|
|
|
|
.reduce((map, item) => map.set(item.id, item), new Map())
|
|
|
|
.values()
|
|
|
|
);
|
|
|
|
|
|
|
|
const links = uniqueLinks.sort(
|
2023-12-25 19:43:31 -06:00
|
|
|
(a, b) => (new Date(b.id) as any) - (new Date(a.id) as any)
|
2023-10-23 09:45:48 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
return { response: links, status: 200 };
|
|
|
|
}
|