Skip to content

Commit 770cd9b

Browse files
committed
Refactor MongoDB connection and enhance error handling in comments API
- Updated MONGODB_URI to use environment variable with fallback. - Changed error response handling in POST request for comments to return a 500 status on failure. - Updated logging messages in VeltCommentDataProvider for clarity and consistency. - Improved error handling in save and delete comment functions to return accurate success status. - Added logging for JWT token retrieval in VeltInitializeUser component.
1 parent ed941ed commit 770cd9b

5 files changed

Lines changed: 20 additions & 19 deletions

File tree

‎apps/react/comments/dashboard/self-hosting/dashboard-mongo-db-demo/app/api/velt/comments/get/route.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ export async function POST(request: NextRequest) {
1717
return NextResponse.json({ result, success: true });
1818
} catch (error) {
1919
console.error('[Velt API] Error getting comments:', error);
20-
return NextResponse.json({ result: {}, success: true }, { status: 200 });
20+
return NextResponse.json({ result: {}, success: false }, { status: 500 });
2121
}
2222
}

‎apps/react/comments/dashboard/self-hosting/dashboard-mongo-db-demo/app/api/velt/comments/store.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export type CommentAnnotation = {
1212
};
1313

1414
// MongoDB Atlas connection string
15-
const MONGODB_URI = 'mongodb+srv://eng_db_user:pAS6b4RCSkLZI7Wf@cluster0.8belzzg.mongodb.net/?appName=Cluster0';
15+
const MONGODB_URI = process.env.MONGODB_URI || 'mongodb+srv://eng_db_user:pAS6b4RCSkLZI7Wf@cluster0.8belzzg.mongodb.net/?appName=Cluster0&retryWrites=true&w=majority';
1616
const DB_NAME = 'velt_comments';
1717
const COLLECTION_NAME = 'comment_annotations';
1818

‎apps/react/comments/dashboard/self-hosting/dashboard-mongo-db-demo/app/userAuth/AppUserContext.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ function generateRandomUser(): User {
8888
userId: userId,
8989
name: fullName,
9090
email: `${firstName.toLowerCase()}.${lastName.toLowerCase()}@example.com`,
91-
organizationId: "sample-apps-demo-org",
91+
organizationId: "sample-apps-inline-comments-demo-org",
9292
photoUrl: `https://ui-avatars.com/api/?name=${encodeURIComponent(fullName)}&background=${avatarColor.substring(1)}&color=fff&size=128`,
9393
};
9494
}

‎apps/react/comments/dashboard/self-hosting/dashboard-mongo-db-demo/components/velt/VeltCommentDataProvider.ts‎

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,22 @@ const fetchCommentsFromDB = async (request: {
2323
folderId?: string;
2424
allDocuments?: boolean;
2525
}) => {
26-
console.log('[Velt Self-Host] GET called:', request);
26+
console.log('Info: Comments GET called:', request);
2727
try {
2828
const response = await fetch(`${DATA_PROVIDER_URL}/get`, {
2929
method: 'POST',
3030
headers: { 'Content-Type': 'application/json' },
3131
body: JSON.stringify(request)
3232
});
3333
if (!response.ok) {
34-
console.warn('[Velt Self-Host] GET endpoint returned', response.status);
35-
return { data: {}, success: true, statusCode: 200 }; // Return empty data on 404
34+
console.warn('Error: Comments GET endpoint returned', response.status);
35+
return { data: {}, success: false, statusCode: response.status };
3636
}
3737
const data = await response.json();
38-
return { data: data.result || {}, success: true, statusCode: 200 };
38+
return { data: data.result || {}, success: data.success, statusCode: response.status };
3939
} catch (error) {
40-
console.error('[Velt Self-Host] Error fetching comments:', error);
41-
return { data: {}, success: true, statusCode: 200 }; // Graceful fallback
40+
console.warn('Error: Comments GET endpoint returned', error);
41+
return { data: {}, success: false, statusCode: 500 };
4242
}
4343
};
4444

@@ -50,21 +50,21 @@ const saveCommentsToDB = async (request: {
5050
comments: Record<string, { commentId: string | number; commentHtml?: string; commentText?: string }>;
5151
}>;
5252
}) => {
53-
console.log('[Velt Self-Host] SAVE called:', request);
53+
console.log('Info: Comments SAVE called:', request);
5454
try {
5555
const response = await fetch(`${DATA_PROVIDER_URL}/save`, {
5656
method: 'POST',
5757
headers: { 'Content-Type': 'application/json' },
5858
body: JSON.stringify(request)
5959
});
6060
if (!response.ok) {
61-
console.error('[Velt Self-Host] SAVE endpoint returned', response.status);
61+
console.warn('Error: Comments SAVE endpoint returned', response.status);
6262
return { success: false, statusCode: response.status };
6363
}
64-
await response.json();
65-
return { success: true, statusCode: 200 };
64+
const data = await response.json();
65+
return { success: data.success ?? true, statusCode: response.status };
6666
} catch (error) {
67-
console.error('[Velt Self-Host] Error saving comments:', error);
67+
console.warn('Error: Comments SAVE endpoint returned', error);
6868
return { success: false, statusCode: 500 };
6969
}
7070
};
@@ -74,21 +74,21 @@ const deleteCommentsFromDB = async (request: {
7474
commentAnnotationId: string;
7575
metadata?: unknown;
7676
}) => {
77-
console.log('[Velt Self-Host] DELETE called:', request);
77+
console.log('Info: Comments DELETE called:', request);
7878
try {
7979
const response = await fetch(`${DATA_PROVIDER_URL}/delete`, {
8080
method: 'POST',
8181
headers: { 'Content-Type': 'application/json' },
8282
body: JSON.stringify(request)
8383
});
8484
if (!response.ok) {
85-
console.error('[Velt Self-Host] DELETE endpoint returned', response.status);
85+
console.warn('Error: Comments DELETE endpoint returned', response.status);
8686
return { success: false, statusCode: response.status };
8787
}
88-
await response.json();
89-
return { success: true, statusCode: 200 };
88+
const data = await response.json();
89+
return { success: data.success ?? true, statusCode: response.status };
9090
} catch (error) {
91-
console.error('[Velt Self-Host] Error deleting comments:', error);
91+
console.warn('Error: Comments DELETE endpoint returned', error);
9292
return { success: false, statusCode: 500 };
9393
}
9494
};

‎apps/react/comments/dashboard/self-hosting/dashboard-mongo-db-demo/components/velt/VeltInitializeUser.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ async function getVeltJwtFromBackend(user: {
2626
throw new Error(`Token API failed: ${err?.error || resp.statusText}`);
2727
}
2828
const { token } = await resp.json();
29+
console.log('Info: Velt JWT token:', token);
2930
if (!token) throw new Error("No token in response");
3031
return token as string;
3132
}

0 commit comments

Comments
 (0)