carnet api nc

This commit is contained in:
alma 2025-04-20 14:52:45 +02:00
parent d2366a8c74
commit 61f603d002

View File

@ -5,6 +5,15 @@ import { DOMParser } from '@xmldom/xmldom';
import { Buffer } from 'buffer'; import { Buffer } from 'buffer';
import { PrismaClient } from '@prisma/client'; import { PrismaClient } from '@prisma/client';
interface WebDAVCredentials {
id: string;
userId: string;
username: string;
password: string;
createdAt: Date;
updatedAt: Date;
}
const prisma = new PrismaClient(); const prisma = new PrismaClient();
async function sleep(ms: number) { async function sleep(ms: number) {
@ -60,9 +69,9 @@ async function createFolder(nextcloudUrl: string, username: string, password: st
async function getWebDAVCredentials(nextcloudUrl: string, username: string, adminUsername: string, adminPassword: string, userId: string) { async function getWebDAVCredentials(nextcloudUrl: string, username: string, adminUsername: string, adminPassword: string, userId: string) {
try { try {
// Check if credentials exist in database // Check if credentials exist in database
let credentials = await prisma.webDAVCredentials.findUnique({ let credentials = await (prisma as any).webDAVCredentials.findUnique({
where: { userId }, where: { userId },
}); }) as WebDAVCredentials | null;
if (credentials) { if (credentials) {
// Verify existing credentials still work // Verify existing credentials still work
@ -81,7 +90,7 @@ async function getWebDAVCredentials(nextcloudUrl: string, username: string, admi
} }
// If verification failed, delete the old credentials // If verification failed, delete the old credentials
await prisma.webDAVCredentials.delete({ await (prisma as any).webDAVCredentials.delete({
where: { userId }, where: { userId },
}); });
} }
@ -121,13 +130,13 @@ async function getWebDAVCredentials(nextcloudUrl: string, username: string, admi
} }
// Store the new credentials // Store the new credentials
credentials = await prisma.webDAVCredentials.create({ credentials = await (prisma as any).webDAVCredentials.create({
data: { data: {
userId, userId,
username, username,
password: newPassword, password: newPassword,
}, },
}); }) as WebDAVCredentials;
// Create required folder structure // Create required folder structure
await createFolder(nextcloudUrl, username, newPassword, 'Private'); await createFolder(nextcloudUrl, username, newPassword, 'Private');