@@ -0,0 +1,119 @@
|
||||
import { prisma } from "@/lib/api/db";
|
||||
import { LinkRequestQuery, Sort } from "@/types/global";
|
||||
|
||||
type Response<D> =
|
||||
| {
|
||||
data: D;
|
||||
message: string;
|
||||
status: number;
|
||||
}
|
||||
| {
|
||||
data: D;
|
||||
message: string;
|
||||
status: number;
|
||||
};
|
||||
|
||||
export default async function getDashboardData(
|
||||
userId: number,
|
||||
query: LinkRequestQuery
|
||||
): Promise<Response<any>> {
|
||||
let order: any;
|
||||
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 numberOfPinnedLinks = await prisma.link.count({
|
||||
where: {
|
||||
AND: [
|
||||
{
|
||||
collection: {
|
||||
OR: [
|
||||
{ ownerId: userId },
|
||||
{
|
||||
members: {
|
||||
some: { userId },
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
{
|
||||
pinnedBy: { some: { id: userId } },
|
||||
},
|
||||
],
|
||||
},
|
||||
});
|
||||
|
||||
const pinnedLinks = await prisma.link.findMany({
|
||||
take: 10,
|
||||
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 },
|
||||
},
|
||||
},
|
||||
orderBy: order || { id: "desc" },
|
||||
});
|
||||
|
||||
const recentlyAddedLinks = await prisma.link.findMany({
|
||||
take: 10,
|
||||
where: {
|
||||
collection: {
|
||||
OR: [
|
||||
{ ownerId: userId },
|
||||
{
|
||||
members: {
|
||||
some: { userId },
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
include: {
|
||||
tags: true,
|
||||
collection: true,
|
||||
pinnedBy: {
|
||||
where: { id: userId },
|
||||
select: { id: true },
|
||||
},
|
||||
},
|
||||
orderBy: order || { id: "desc" },
|
||||
});
|
||||
|
||||
const links = [...recentlyAddedLinks, ...pinnedLinks].sort(
|
||||
(a, b) => (new Date(b.id) as any) - (new Date(a.id) as any)
|
||||
);
|
||||
|
||||
return {
|
||||
data: {
|
||||
links,
|
||||
numberOfPinnedLinks,
|
||||
},
|
||||
message: "Dashboard data fetched successfully.",
|
||||
status: 200,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
import { prisma } from "@/lib/api/db";
|
||||
import crypto from "crypto";
|
||||
import { decode, encode } from "next-auth/jwt";
|
||||
|
||||
export default async function createSession(
|
||||
userId: number,
|
||||
sessionName?: string
|
||||
) {
|
||||
const now = Date.now();
|
||||
const expiryDate = new Date();
|
||||
const oneDayInSeconds = 86400;
|
||||
|
||||
expiryDate.setDate(expiryDate.getDate() + 73000); // 200 years (not really never)
|
||||
const expiryDateSecond = 73050 * oneDayInSeconds;
|
||||
|
||||
const token = await encode({
|
||||
token: {
|
||||
id: userId,
|
||||
iat: now / 1000,
|
||||
exp: (expiryDate as any) / 1000,
|
||||
jti: crypto.randomUUID(),
|
||||
},
|
||||
maxAge: expiryDateSecond || 604800,
|
||||
secret: process.env.NEXTAUTH_SECRET,
|
||||
});
|
||||
|
||||
const tokenBody = await decode({
|
||||
token,
|
||||
secret: process.env.NEXTAUTH_SECRET,
|
||||
});
|
||||
|
||||
const createToken = await prisma.accessToken.create({
|
||||
data: {
|
||||
name: sessionName || "Unknown Device",
|
||||
userId,
|
||||
token: tokenBody?.jti as string,
|
||||
isSession: true,
|
||||
expires: expiryDate,
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
response: {
|
||||
token,
|
||||
},
|
||||
status: 200,
|
||||
};
|
||||
}
|
||||
@@ -9,6 +9,7 @@ export default async function getToken(userId: number) {
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
isSession: true,
|
||||
expires: true,
|
||||
createdAt: true,
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user