el.xwx.moe/lib/api/controllers/migration/exportData.ts

51 lines
1.3 KiB
TypeScript
Raw Normal View History

import { prisma } from "@/lib/api/db";
export default async function exportData(userId: number) {
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,
},
});
if (!user) return { response: "User not found.", status: 404 };
2023-10-26 17:49:46 -05:00
const { password, id, ...userData } = user;
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);
return { response: userData, status: 200 };
}