Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions src/app/api/events/[id]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,13 +196,29 @@ export async function PATCH(request: NextRequest, context: RouteContext) {
}
}

const updated = await eventQueries.update(eventId, session.user_id, {
// Pass description directly (including null) so it can be cleared.
// Only omit if the key was not present in the request body at all.
const updateData: {
title?: string;
description?: string | null;
event_date?: string;
event_time?: string;
location?: string;
} = {
title: title?.trim(),
description: description ?? undefined,
event_date: eventDate,
event_time: eventTime,
location: location?.trim(),
});
};
if ("description" in body) {
updateData.description = description ?? null;
}

const updated = await eventQueries.update(
eventId,
session.user_id,
updateData,
);

if (!updated) {
return NextResponse.json(
Expand Down
9 changes: 8 additions & 1 deletion src/lib/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -621,11 +621,18 @@ export const eventQueries = {
},
): Promise<Event | null> => {
try {
// For description, distinguish between "not provided" (undefined) and
// "explicitly cleared" (null). COALESCE would treat both as keeping
// the old value, so we use a CASE expression instead.
const isDescriptionProvided = data.description !== undefined;
const result = await sql`
UPDATE events
SET
title = COALESCE(${data.title ?? null}, title),
description = COALESCE(${data.description ?? null}, description),
description = CASE
WHEN ${isDescriptionProvided} THEN ${data.description ?? null}
ELSE description
END,
Comment thread
coderabbitai[bot] marked this conversation as resolved.
event_date = COALESCE(${data.event_date ?? null}, event_date),
event_time = COALESCE(${data.event_time ?? null}, event_time),
location = COALESCE(${data.location ?? null}, location)
Expand Down
Loading