2023-08-10 11:16:44 -05:00
|
|
|
import { prisma } from "@/lib/api/db";
|
|
|
|
|
2023-10-03 06:04:13 -05:00
|
|
|
export default async function exportData(userId: number) {
|
2023-08-10 11:16:44 -05:00
|
|
|
const user = await prisma.user.findUnique({
|
|
|
|
where: { id: userId },
|
|
|
|
include: {
|
|
|
|
collections: {
|
|
|
|
include: {
|
|
|
|
links: {
|
|
|
|
include: {
|
|
|
|
tags: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!user) return { response: "User not found.", status: 404 };
|
|
|
|
|
2023-10-26 17:49:46 -05:00
|
|
|
const { password, id, ...userData } = user;
|
2023-08-10 11:16:44 -05:00
|
|
|
|
2023-10-16 17:27:04 -05:00
|
|
|
function redactIds(obj: any) {
|
|
|
|
if (Array.isArray(obj)) {
|
|
|
|
obj.forEach((o) => redactIds(o));
|
|
|
|
} else if (obj !== null && typeof obj === "object") {
|
|
|
|
delete obj.id;
|
|
|
|
for (let key in obj) {
|
|
|
|
redactIds(obj[key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
redactIds(userData);
|
|
|
|
|
2023-08-10 11:16:44 -05:00
|
|
|
return { response: userData, status: 200 };
|
|
|
|
}
|