feat(API, database): improve DELETE and GET property handling and enhance logging and error messages

This commit is contained in:
ryan 2025-07-04 17:35:47 +03:00
parent 23e9c4a621
commit f616d73826
3 changed files with 61 additions and 27 deletions

View file

@ -1,4 +1,3 @@
//Handles DELETE requests for specific properties
import { NextRequest, NextResponse } from 'next/server'; import { NextRequest, NextResponse } from 'next/server';
import { deletePropertyByUrl } from '@/lib/database'; import { deletePropertyByUrl } from '@/lib/database';
@ -7,36 +6,42 @@ export async function DELETE(
request: NextRequest, request: NextRequest,
{ params }: { params: { encodedUrl: string; property: string } } { params }: { params: { encodedUrl: string; property: string } }
) { ) {
try {
const url = decodeURIComponent(params.encodedUrl); const url = decodeURIComponent(params.encodedUrl);
const property = decodeURIComponent(params.property); const property = decodeURIComponent(params.property);
console.log('[API] Deleting property', property, 'from URL', url); console.log('[API] DELETE request - URL:', url, 'Property:', property);
try { if (!url || !property) {
if (!property) { console.log('[API] Missing required parameters');
return NextResponse.json( return NextResponse.json(
{ error: 'Property name is required' }, { error: 'URL and property name are required' },
{ status: 400 } { status: 400 }
); );
} }
console.log('[API] Calling deletePropertyByUrl...');
await deletePropertyByUrl(url, property); await deletePropertyByUrl(url, property);
console.log('[API] Successfully deleted property:', property); console.log('[API] Successfully deleted property:', property, 'from URL:', url);
return NextResponse.json( return NextResponse.json(
{ message: 'Property deleted', property: property }, {
message: 'Property deleted successfully',
property: property,
url: url
},
{ status: 200 } { status: 200 }
); );
} catch (error) { } catch (error) {
console.log('[API] Delete error:', error); console.error('[API] Delete error:', error);
return NextResponse.json( return NextResponse.json(
{ {
error: 'Failed to delete property', error: 'Failed to delete property',
details: error instanceof Error ? error.message : 'Unknown error', details: error instanceof Error ? error.message : 'Unknown error',
property: property, property: params.property,
url: url url: params.encodedUrl
}, },
{ status: 500 } { status: 500 }
); );

View file

@ -7,12 +7,12 @@ export async function GET(
request: NextRequest, request: NextRequest,
{ params }: { params: Promise<{ encodedUrl: string }> } { params }: { params: Promise<{ encodedUrl: string }> }
) { ) {
try {
// Await params before accessing properties // Await params before accessing properties
const resolvedParams = await params; const resolvedParams = await params;
const url = decodeURIComponent(resolvedParams.encodedUrl); const url = decodeURIComponent(resolvedParams.encodedUrl);
console.log('[API] Getting selected properties for URL:', url); console.log('[API] Getting selected properties for URL:', url);
try {
const properties = await getSelectedProperties(url); const properties = await getSelectedProperties(url);
console.log('[API] Returning', properties.length, 'properties for URL:', url); console.log('[API] Returning', properties.length, 'properties for URL:', url);
@ -23,6 +23,8 @@ export async function GET(
}, },
}); });
} catch (error) { } catch (error) {
const resolvedParams = await params;
const url = decodeURIComponent(resolvedParams.encodedUrl);
console.error('[API] Failed to fetch properties for URL:', url, error); console.error('[API] Failed to fetch properties for URL:', url, error);
return NextResponse.json( return NextResponse.json(
@ -41,11 +43,11 @@ export async function POST(
request: NextRequest, request: NextRequest,
{ params }: { params: Promise<{ encodedUrl: string }> } { params }: { params: Promise<{ encodedUrl: string }> }
) { ) {
try {
// Await params before accessing properties // Await params before accessing properties
const resolvedParams = await params; const resolvedParams = await params;
const url = decodeURIComponent(resolvedParams.encodedUrl); const url = decodeURIComponent(resolvedParams.encodedUrl);
try {
const property: string = await request.json(); const property: string = await request.json();
console.log('[API] Adding property', property, 'to URL', url); console.log('[API] Adding property', property, 'to URL', url);
@ -65,14 +67,20 @@ export async function POST(
// Add the selected property // Add the selected property
await addSelectedProperty(url, property); await addSelectedProperty(url, property);
console.log('[API] Successfully added property:', property); console.log('[API] Successfully added property:', property, 'to URL:', url);
return NextResponse.json( return NextResponse.json(
{ message: 'Property added', property: property }, {
message: 'Property added successfully',
property: property,
url: url
},
{ status: 200 } { status: 200 }
); );
} catch (error) { } catch (error) {
console.log('[API] Error adding property:', error); const resolvedParams = await params;
const url = decodeURIComponent(resolvedParams.encodedUrl);
console.error('[API] Error adding property:', error);
return NextResponse.json( return NextResponse.json(
{ {

View file

@ -250,7 +250,7 @@ export async function deleteItemByUrl(url: string, itemId: string): Promise<void
// Delete property by URL // Delete property by URL
export async function deletePropertyByUrl(url: string, propertyName: string): Promise<void> { export async function deletePropertyByUrl(url: string, propertyName: string): Promise<void> {
console.log(`[DB] Soft deleting property ${propertyName} for URL: ${url}`); console.log(`[DB] Starting soft delete of property ${propertyName} for URL: ${url}`);
await withTransaction(async (tx) => { await withTransaction(async (tx) => {
// Get URL ID // Get URL ID
@ -259,25 +259,44 @@ export async function deletePropertyByUrl(url: string, propertyName: string): Pr
}); });
if (!urlRecord) { if (!urlRecord) {
console.error(`[DB] URL not found: ${url}`);
throw new Error(`URL not found: ${url}`); throw new Error(`URL not found: ${url}`);
} }
console.log(`[DB] Found URL record with ID: ${urlRecord.id}`);
// Get property ID // Get property ID
const property = await tx.property.findUnique({ const property = await tx.property.findUnique({
where: { name: propertyName } where: { name: propertyName }
}); });
if (!property) { if (!property) {
console.error(`[DB] Property not found: ${propertyName}`);
throw new Error(`Property not found: ${propertyName}`); throw new Error(`Property not found: ${propertyName}`);
} }
console.log(`[DB] Found property record with ID: ${property.id}`);
// Remove from selected properties first
const deletedSelectedProperties = await tx.selectedProperty.deleteMany({
where: {
urlId: urlRecord.id,
propertyId: property.id
}
});
console.log(`[DB] Removed ${deletedSelectedProperties.count} selected property records`);
// Get all global item IDs for this URL // Get all global item IDs for this URL
const items = await tx.item.findMany({ const items = await tx.item.findMany({
where: { urlId: urlRecord.id }, where: { urlId: urlRecord.id },
select: { globalItemId: true } select: { globalItemId: true }
}); });
console.log(`[DB] Found ${items.length} items for URL`);
// Insert into deleted_properties for each global item // Insert into deleted_properties for each global item
let deletedPropertiesCount = 0;
for (const item of items) { for (const item of items) {
await tx.deletedProperty.upsert({ await tx.deletedProperty.upsert({
where: { where: {
@ -294,9 +313,11 @@ export async function deletePropertyByUrl(url: string, propertyName: string): Pr
propertyId: property.id propertyId: property.id
} }
}); });
deletedPropertiesCount++;
} }
console.log(`[DB] Property ${propertyName} soft deleted for URL: ${url}`); console.log(`[DB] Created/updated ${deletedPropertiesCount} deleted property records`);
console.log(`[DB] Successfully soft deleted property ${propertyName} for URL: ${url}`);
}); });
} }