mirror of
https://github.com/Memo-2023/mana-monorepo.git
synced 2026-05-14 19:21:10 +02:00
make auth working
This commit is contained in:
parent
7a1f1e9aef
commit
25824ed0ac
73 changed files with 9093 additions and 3877 deletions
|
|
@ -1,9 +1,9 @@
|
|||
/**
|
||||
* Document-Service für die Verwaltung von Dokumenten im Dokumentmodus
|
||||
* Document Service - CRUD operations via Backend API
|
||||
*/
|
||||
import { supabase } from '../utils/supabase';
|
||||
import { documentApi, type Document as ApiDocument } from './api';
|
||||
|
||||
// Typdefinition für ein Dokument
|
||||
// Re-export type with backwards-compatible naming (snake_case for mobile)
|
||||
export interface Document {
|
||||
id: string;
|
||||
conversation_id: string;
|
||||
|
|
@ -13,37 +13,39 @@ export interface Document {
|
|||
updated_at: string;
|
||||
}
|
||||
|
||||
// Helper to convert API response to local format
|
||||
function toLocalDocument(doc: ApiDocument): Document {
|
||||
return {
|
||||
id: doc.id,
|
||||
conversation_id: doc.conversationId,
|
||||
version: doc.version,
|
||||
content: doc.content,
|
||||
created_at: doc.createdAt,
|
||||
updated_at: doc.updatedAt,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Erstellt ein neues Dokument in einer Konversation
|
||||
* @param conversationId Die ID der Konversation
|
||||
* @param content Der Inhalt des Dokuments
|
||||
* @returns Das erstellte Dokument oder null bei Fehler
|
||||
*/
|
||||
export async function createDocument(
|
||||
conversationId: string,
|
||||
content: string
|
||||
): Promise<Document | null> {
|
||||
try {
|
||||
console.log(`Erstelle Dokument für Konversation ${conversationId} mit Inhalt: ${content.substring(0, 50)}...`);
|
||||
|
||||
const { data, error } = await supabase
|
||||
.from('documents')
|
||||
.insert({
|
||||
conversation_id: conversationId,
|
||||
version: 1, // Initiale Version ist immer 1
|
||||
content,
|
||||
})
|
||||
.select()
|
||||
.single();
|
||||
console.log(
|
||||
`Erstelle Dokument für Konversation ${conversationId} mit Inhalt: ${content.substring(0, 50)}...`
|
||||
);
|
||||
|
||||
if (error) {
|
||||
console.error('Fehler beim Erstellen des Dokuments:', error);
|
||||
console.error('Vollständiger Fehler:', JSON.stringify(error));
|
||||
const document = await documentApi.createDocument(conversationId, content);
|
||||
|
||||
if (!document) {
|
||||
console.error('Fehler beim Erstellen des Dokuments');
|
||||
return null;
|
||||
}
|
||||
|
||||
console.log('Dokument erfolgreich erstellt:', data);
|
||||
return data as Document;
|
||||
console.log('Dokument erfolgreich erstellt:', document);
|
||||
return toLocalDocument(document);
|
||||
} catch (error) {
|
||||
console.error('Fehler beim Erstellen des Dokuments:', error);
|
||||
if (error instanceof Error) {
|
||||
|
|
@ -55,48 +57,20 @@ export async function createDocument(
|
|||
|
||||
/**
|
||||
* Erstellt eine neue Version eines Dokuments
|
||||
* @param conversationId Die ID der Konversation
|
||||
* @param content Der neue Inhalt des Dokuments
|
||||
* @returns Das erstellte Dokument oder null bei Fehler
|
||||
*/
|
||||
export async function createDocumentVersion(
|
||||
conversationId: string,
|
||||
content: string
|
||||
): Promise<Document | null> {
|
||||
try {
|
||||
// Hole die aktuelle höchste Version
|
||||
const { data: latestVersionData, error: versionError } = await supabase
|
||||
.from('documents')
|
||||
.select('version')
|
||||
.eq('conversation_id', conversationId)
|
||||
.order('version', { ascending: false })
|
||||
.limit(1)
|
||||
.single();
|
||||
const document = await documentApi.createDocumentVersion(conversationId, content);
|
||||
|
||||
if (versionError) {
|
||||
console.error('Fehler beim Laden der letzten Dokumentversion:', versionError);
|
||||
if (!document) {
|
||||
console.error('Fehler beim Erstellen der neuen Dokumentversion');
|
||||
return null;
|
||||
}
|
||||
|
||||
const newVersion = (latestVersionData?.version || 0) + 1;
|
||||
|
||||
// Erstelle eine neue Dokumentversion
|
||||
const { data, error } = await supabase
|
||||
.from('documents')
|
||||
.insert({
|
||||
conversation_id: conversationId,
|
||||
version: newVersion,
|
||||
content,
|
||||
})
|
||||
.select()
|
||||
.single();
|
||||
|
||||
if (error) {
|
||||
console.error('Fehler beim Erstellen der neuen Dokumentversion:', error);
|
||||
return null;
|
||||
}
|
||||
|
||||
return data as Document;
|
||||
return toLocalDocument(document);
|
||||
} catch (error) {
|
||||
console.error('Fehler beim Erstellen der neuen Dokumentversion:', error);
|
||||
return null;
|
||||
|
|
@ -105,32 +79,20 @@ export async function createDocumentVersion(
|
|||
|
||||
/**
|
||||
* Holt die aktuellste Version eines Dokuments für eine Konversation
|
||||
* @param conversationId Die ID der Konversation
|
||||
* @returns Das aktuellste Dokument oder null, wenn nicht gefunden
|
||||
*/
|
||||
export async function getLatestDocument(conversationId: string): Promise<Document | null> {
|
||||
try {
|
||||
console.log(`Lade neuestes Dokument für Konversation ${conversationId}`);
|
||||
|
||||
// Einfache Abfrage ohne Cache-Busting
|
||||
const { data, error } = await supabase
|
||||
.from('documents')
|
||||
.select('*')
|
||||
.eq('conversation_id', conversationId)
|
||||
.order('version', { ascending: false })
|
||||
.limit(1)
|
||||
.single();
|
||||
|
||||
if (error) {
|
||||
console.error('Fehler beim Laden des aktuellen Dokuments:', error);
|
||||
if (error.code === 'PGRST116') {
|
||||
console.log('Kein Dokument gefunden (PGRST116)');
|
||||
}
|
||||
const document = await documentApi.getLatestDocument(conversationId);
|
||||
|
||||
if (!document) {
|
||||
console.log('Kein Dokument gefunden');
|
||||
return null;
|
||||
}
|
||||
|
||||
console.log(`Neuestes Dokument gefunden: Version ${data.version}, ID ${data.id}`);
|
||||
return data as Document;
|
||||
console.log(`Neuestes Dokument gefunden: Version ${document.version}, ID ${document.id}`);
|
||||
return toLocalDocument(document);
|
||||
} catch (error) {
|
||||
console.error('Fehler beim Laden des aktuellen Dokuments:', error);
|
||||
return null;
|
||||
|
|
@ -139,35 +101,22 @@ export async function getLatestDocument(conversationId: string): Promise<Documen
|
|||
|
||||
/**
|
||||
* Lädt alle Versionen eines Dokuments für eine Konversation
|
||||
* @param conversationId Die ID der Konversation
|
||||
* @returns Eine Liste aller Dokumentversionen
|
||||
*/
|
||||
export async function getAllDocumentVersions(conversationId: string): Promise<Document[]> {
|
||||
try {
|
||||
console.log(`Lade alle Dokumentversionen für Konversation ${conversationId}`);
|
||||
|
||||
// Einfache Abfrage ohne Cache-Busting (das verursacht Probleme)
|
||||
const { data, error } = await supabase
|
||||
.from('documents')
|
||||
.select('*')
|
||||
.eq('conversation_id', conversationId)
|
||||
.order('version', { ascending: false });
|
||||
|
||||
if (error) {
|
||||
console.error('Fehler beim Laden der Dokumentversionen:', error);
|
||||
return [];
|
||||
}
|
||||
const documents = await documentApi.getAllDocumentVersions(conversationId);
|
||||
|
||||
console.log(`${data?.length || 0} Dokumentversionen geladen`);
|
||||
|
||||
// Detaillierte Infos zur Fehlersuche
|
||||
if (data && data.length > 0) {
|
||||
console.log(`Erstes Dokument: ID=${data[0].id}, Version=${data[0].version}`);
|
||||
console.log(`${documents.length} Dokumentversionen geladen`);
|
||||
|
||||
if (documents.length > 0) {
|
||||
console.log(`Erstes Dokument: ID=${documents[0].id}, Version=${documents[0].version}`);
|
||||
} else {
|
||||
console.log('Keine Dokumente gefunden');
|
||||
}
|
||||
|
||||
return data as Document[];
|
||||
|
||||
return documents.map(toLocalDocument);
|
||||
} catch (error) {
|
||||
console.error('Fehler beim Laden der Dokumentversionen:', error);
|
||||
return [];
|
||||
|
|
@ -176,22 +125,10 @@ export async function getAllDocumentVersions(conversationId: string): Promise<Do
|
|||
|
||||
/**
|
||||
* Prüft, ob für eine Konversation ein Dokument existiert
|
||||
* @param conversationId Die ID der Konversation
|
||||
* @returns true, wenn ein Dokument existiert, sonst false
|
||||
*/
|
||||
export async function hasDocument(conversationId: string): Promise<boolean> {
|
||||
try {
|
||||
const { count, error } = await supabase
|
||||
.from('documents')
|
||||
.select('id', { count: 'exact', head: true })
|
||||
.eq('conversation_id', conversationId);
|
||||
|
||||
if (error) {
|
||||
console.error('Fehler beim Prüfen auf Dokument:', error);
|
||||
return false;
|
||||
}
|
||||
|
||||
return (count || 0) > 0;
|
||||
return await documentApi.hasDocument(conversationId);
|
||||
} catch (error) {
|
||||
console.error('Fehler beim Prüfen auf Dokument:', error);
|
||||
return false;
|
||||
|
|
@ -200,47 +137,20 @@ export async function hasDocument(conversationId: string): Promise<boolean> {
|
|||
|
||||
/**
|
||||
* Löscht eine spezifische Dokumentversion
|
||||
* @param documentId Die ID des zu löschenden Dokuments
|
||||
* @returns true, wenn erfolgreich gelöscht, sonst false
|
||||
*/
|
||||
export async function deleteDocumentVersion(documentId: string): Promise<boolean> {
|
||||
try {
|
||||
console.log(`=== LÖSCH-OPERATION GESTARTET FÜR DOKUMENT ID ${documentId} ===`);
|
||||
|
||||
// Prüfe zuerst, ob das Dokument existiert
|
||||
const { data: existingData, error: checkError } = await supabase
|
||||
.from('documents')
|
||||
.select('id')
|
||||
.eq('id', documentId)
|
||||
.single();
|
||||
|
||||
if (checkError) {
|
||||
console.error('Fehler beim Prüfen, ob das Dokument existiert:', checkError);
|
||||
console.error('Details:', JSON.stringify(checkError));
|
||||
return false;
|
||||
|
||||
const success = await documentApi.deleteDocumentVersion(documentId);
|
||||
|
||||
if (success) {
|
||||
console.log(`=== DOKUMENT ${documentId} ERFOLGREICH GELÖSCHT ===`);
|
||||
} else {
|
||||
console.error('Fehler beim Löschen der Dokumentversion');
|
||||
}
|
||||
|
||||
if (!existingData) {
|
||||
console.error('Dokument nicht gefunden:', documentId);
|
||||
return false;
|
||||
}
|
||||
|
||||
console.log('Dokument gefunden, führe Löschung durch...');
|
||||
|
||||
// Führe die eigentliche Löschung durch
|
||||
const { error } = await supabase
|
||||
.from('documents')
|
||||
.delete()
|
||||
.eq('id', documentId);
|
||||
|
||||
if (error) {
|
||||
console.error('Fehler beim Löschen der Dokumentversion:', error);
|
||||
console.error('Vollständige Fehlermeldung:', JSON.stringify(error));
|
||||
return false;
|
||||
}
|
||||
|
||||
console.log(`=== DOKUMENT ${documentId} ERFOLGREICH GELÖSCHT ===`);
|
||||
return true;
|
||||
|
||||
return success;
|
||||
} catch (error) {
|
||||
console.error('Unerwarteter Fehler beim Löschen der Dokumentversion:', error);
|
||||
if (error instanceof Error) {
|
||||
|
|
@ -248,4 +158,4 @@ export async function deleteDocumentVersion(documentId: string): Promise<boolean
|
|||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue