Pages corrections pages missions

This commit is contained in:
alma 2026-01-16 15:20:40 +01:00
parent 808e1d93cd
commit ebe1f97acc
2 changed files with 46 additions and 22 deletions

View File

@ -252,31 +252,50 @@ export async function PUT(request: Request) {
// Check if this is using the direct id (key) or needs to construct one
let key: string;
// For Diary and Health folders, always reconstruct key from title to ensure new format
// This allows renaming from "2026-01-16.md" to "16_janvier_2026.md"
const normalizedFolder = folder?.toLowerCase() || '';
const isDiaryOrHealth = normalizedFolder === 'diary' || normalizedFolder === 'health';
if (id && !isDiaryOrHealth) {
// For non-Diary/Health folders, use the provided id if available
// Ensure the user can only access their own files
if (!id.includes(`user-${userId}/`)) {
// For Diary/Health, if id is provided and it's in the correct format, use it
// Otherwise, reconstruct from title (for migration from old format)
if (id && id.includes(`user-${userId}/`)) {
// Validate that the id is for the correct folder
if (id.includes(`/${normalizedFolder}/`)) {
// For Diary/Health, verify the id matches the expected format
if (isDiaryOrHealth) {
// Check if the id already matches the sanitized title format
const sanitizedTitle = title ? title.replace(/[^a-zA-Z0-9._-]/g, '_') : '';
const expectedKey = `user-${userId}/${normalizedFolder}/${sanitizedTitle}${sanitizedTitle.endsWith('.md') ? '' : '.md'}`;
// If id matches expected format, use it (this is an update to existing file)
if (id === expectedKey) {
key = id;
console.log(`[PUT] Using provided id for Diary/Health: ${key}`);
} else {
// Id doesn't match expected format, but it's a valid id for this folder
// Use it anyway (this handles updates to existing files with old format)
key = id;
console.log(`[PUT] Using provided id for Diary/Health (format mismatch, but valid): ${key}`);
}
} else {
// For non-Diary/Health folders, use the provided id
key = id;
}
} else {
return createErrorResponse(
StorageError.FORBIDDEN,
'Unauthorized access to file',
'File id does not match the specified folder',
403,
{ fileId: id }
{ fileId: id, folder: normalizedFolder }
);
}
key = id;
} else {
// If id is not provided, or for Diary/Health folders, construct it from folder and title
// If id is not provided or invalid, construct it from folder and title
if (!title || !folder) {
return createErrorResponse(
StorageError.VALIDATION_ERROR,
'Missing required fields: either id or (title and folder) must be provided',
400,
{ received: { title: !!title, folder: !!folder } }
{ received: { title: !!title, folder: !!folder, id: !!id } }
);
}
@ -295,13 +314,7 @@ export async function PUT(request: Request) {
const sanitizedTitle = title.replace(/[^a-zA-Z0-9._-]/g, '_');
key = `user-${userId}/${normalizedFolder}/${sanitizedTitle}${sanitizedTitle.endsWith('.md') ? '' : '.md'}`;
// If this is Diary/Health and we have an old id, we might need to delete the old file
// But for now, we'll just create/update with the new key
if (isDiaryOrHealth && id && id !== key) {
console.log(`[PUT] Renaming Diary/Health note from ${id} to ${key}`);
// The old file will remain, but the new one will be created/updated
// In a future enhancement, we could delete the old file here
}
console.log(`[PUT] Constructed key from title for ${normalizedFolder}: ${key}`);
}
// Validate content

View File

@ -509,29 +509,40 @@ export default function CarnetPage() {
// ALWAYS check if a note with this date already exists (even if note.id is set)
// This ensures we update the existing note instead of creating a duplicate
console.log(`[handleSaveNote] Checking for existing note. Date: ${dateTitle}, DateStr: ${dateStr}, Notes count: ${notes.length}`);
console.log(`[handleSaveNote] Current notes:`, notes.map(n => ({ title: n.title, id: n.id })));
const existingNote = notes.find(n => {
const title = n.title || '';
const noteId = n.id || '';
// Check multiple patterns to find the existing note
return title === dateTitle ||
const matches = title === dateTitle ||
title === dateStr ||
title.startsWith(dateStr) ||
title.startsWith(dateTitle) ||
noteId.includes(dateStr) ||
noteId.includes(dateTitle.replace(/\s/g, '_'));
noteId.includes(dateTitle.replace(/\s/g, '_')) ||
noteId.includes(dateTitle.replace(/\s/g, '-'));
if (matches) {
console.log(`[handleSaveNote] Found matching note:`, { title, id: noteId });
}
return matches;
});
if (existingNote) {
// Update the existing note instead of creating a new one
console.log(`[handleSaveNote] Found existing note for today, updating: ${existingNote.id}`);
console.log(`[handleSaveNote] Found existing note for today, updating: ${existingNote.id}, title: ${existingNote.title}`);
note.id = existingNote.id;
noteTitle = existingNote.title || dateTitle; // Keep the existing title format
// Use the existing note's id as the fileKey
// Use the existing note's id as the fileKey - this is critical!
fileKey = existingNote.id;
console.log(`[handleSaveNote] Using existing fileKey: ${fileKey}`);
} else {
// No existing note found, create new one with formatted date title
// The fileKey will be constructed from the formatted title
fileKey = `user-${session?.user?.id}/${selectedFolder.toLowerCase()}/${dateTitle}${dateTitle.endsWith('.md') ? '' : '.md'}`;
console.log(`[handleSaveNote] No existing note found, creating new with fileKey: ${fileKey}`);
}
} else {
// For other folders, use existing id or construct from title