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,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2024-02-23 11:11:03 -06:00
|
|
|
pinnedLinks: true,
|
|
|
|
whitelistedUsers: true,
|
2023-08-10 11:16:44 -05:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
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
|
|
|
|
2024-07-26 16:41:19 -05:00
|
|
|
function redactIds(data: object | object[]): void {
|
|
|
|
if (Array.isArray(data)) {
|
|
|
|
data.forEach((item) => redactIds(item));
|
|
|
|
} else if (data !== null && typeof data === "object") {
|
|
|
|
const fieldsToRedact = ['id', 'parentId', 'collectionId', 'ownerId'];
|
|
|
|
|
|
|
|
fieldsToRedact.forEach((field) => {
|
|
|
|
if (field in data) {
|
|
|
|
delete (data as any)[field];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Recursively call redactIds for each property that is an object or an array
|
|
|
|
Object.keys(data).forEach((key) => {
|
|
|
|
const value = (data as any)[key];
|
|
|
|
if (value !== null && (typeof value === "object" || Array.isArray(value))) {
|
|
|
|
redactIds(value);
|
|
|
|
}
|
|
|
|
});
|
2023-10-16 17:27:04 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
redactIds(userData);
|
|
|
|
|
2023-08-10 11:16:44 -05:00
|
|
|
return { response: userData, status: 200 };
|
|
|
|
}
|