Skip to content

Commit 703e0db

Browse files
committed
Cleaned up routing and access to API endpoints
1 parent 7632a3d commit 703e0db

File tree

9 files changed

+187
-190
lines changed

9 files changed

+187
-190
lines changed

assets/next.config.js

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,76 +4,76 @@ const nextConfig = {
44
output: 'standalone',
55
async rewrites() {
66
return [
7-
{
8-
source: '/booking/:path*',
9-
destination: `http://rbp-booking:3000/booking/:path*`,
7+
{
8+
source: '/api/booking/actuator/:path*',
9+
destination: `http://rbp-booking:3000/booking/actuator/:path*`,
1010
},
1111
{
12-
source: '/room/:path*',
13-
destination: `http://rbp-room:3001/room/:path*`,
12+
source: '/api/room/actuator/:path*',
13+
destination: `http://rbp-room:3001/room/actuator/:path*`,
1414
},
1515
{
16-
source: '/brand/:path*',
17-
destination: `http://rbp-branding:3002/brand/:path*`,
16+
source: '/api/branding/actuator/:path*',
17+
destination: `http://rbp-branding:3002/branding/actuator/:path*`,
1818
},
1919
{
20-
source: '/auth/:path*',
21-
destination: `http://rbp-auth:3004/auth/:path*`,
20+
source: '/api/auth/actuator/:path*',
21+
destination: `http://rbp-auth:3004/auth/actuator/:path*`,
2222
},
2323
{
24-
source: '/report/:path*',
25-
destination: `http://rbp-report:3005/report/:path*`,
24+
source: '/api/report/actuator/:path*',
25+
destination: `http://rbp-report:3005/report/actuator/:path*`,
2626
},
2727
{
28-
source: '/message/:path*',
29-
destination: `http://rbp-message:3006/message/:path*`,
28+
source: '/api/message/actuator/:path*',
29+
destination: `http://rbp-message:3006/message/actuator/:path*`,
3030
},
3131
{
32-
source: '/booking/swagger-ui/:path*',
32+
source: '/api/booking/swagger-ui/:path*',
3333
destination: `http://rbp-booking:3000/booking/swagger-ui/:path*`,
3434
},
3535
{
36-
source: '/booking/v3/:path*',
36+
source: '/api/booking/v3/:path*',
3737
destination: `http://rbp-booking:3000/booking/v3/:path*`,
3838
},
3939
{
40-
source: '/room/swagger-ui/:path*',
40+
source: '/api/room/swagger-ui/:path*',
4141
destination: `http://rbp-room:3001/room/swagger-ui/:path*`,
4242
},
4343
{
44-
source: '/room/v3/:path*',
44+
source: '/api/room/v3/:path*',
4545
destination: `http://rbp-room:3001/room/v3/:path*`,
4646
},
4747
{
48-
source: '/brand/swagger-ui/:path*',
49-
destination: `http://rbp-branding:3002/brand/swagger-ui/:path*`,
48+
source: '/api/branding/swagger-ui/:path*',
49+
destination: `http://rbp-branding:3002/branding/swagger-ui/:path*`,
5050
},
5151
{
52-
source: '/brand/v3/:path*',
53-
destination: `http://rbp-brand:3002/brand/v3/:path*`,
52+
source: '/api/branding/v3/:path*',
53+
destination: `http://rbp-branding:3002/branding/v3/:path*`,
5454
},
5555
{
56-
source: '/auth/swagger-ui/:path*',
56+
source: '/api/auth/swagger-ui/:path*',
5757
destination: `http://rbp-auth:3004/auth/swagger-ui/:path*`,
5858
},
5959
{
60-
source: '/auth/v3/:path*',
60+
source: '/api/auth/v3/:path*',
6161
destination: `http://rbp-auth:3004/auth/v3/:path*`,
6262
},
6363
{
64-
source: '/report/swagger-ui/:path*',
64+
source: '/api/report/swagger-ui/:path*',
6565
destination: `http://rbp-report:3005/report/swagger-ui/:path*`,
6666
},
6767
{
68-
source: '/report/v3/:path*',
68+
source: '/api/report/v3/:path*',
6969
destination: `http://rbp-report:3005/report/v3/:path*`,
7070
},
7171
{
72-
source: '/message/swagger-ui/:path*',
72+
source: '/api/message/swagger-ui/:path*',
7373
destination: `http://rbp-message:3006/message/swagger-ui/:path*`,
7474
},
7575
{
76-
source: '/message/v3/:path*',
76+
source: '/api/message/v3/:path*',
7777
destination: `http://rbp-message:3006/message/v3/:path*`,
7878
}
7979
];
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { NextResponse } from 'next/server';
2+
3+
export async function POST(request: Request) {
4+
try {
5+
// Parse the request body to get the token
6+
const { token } = await request.json();
7+
8+
if (!token) {
9+
return NextResponse.json({ message: 'Token is required' }, { status: 400 });
10+
}
11+
12+
// Here you would typically handle the actual logout logic
13+
// For example, invalidate the token on the server, clear sessions, etc.
14+
15+
// Return a success response
16+
return NextResponse.json({ success: true });
17+
} catch (error) {
18+
console.error('Logout error:', error);
19+
return NextResponse.json({ message: 'Failed to logout' }, { status: 500 });
20+
}
21+
}

assets/src/app/api/booking/[id]/route.ts

Lines changed: 61 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,18 @@ export async function PUT(
3232
});
3333

3434
if (!response.ok) {
35-
const errorData = await response.json();
36-
return NextResponse.json(
37-
errorData,
38-
{ status: response.status }
39-
);
35+
try {
36+
const errorData = await response.json();
37+
return NextResponse.json(
38+
{ errors: errorData.fieldErrors || ['Failed to update booking'] },
39+
{ status: response.status }
40+
);
41+
} catch (e) {
42+
return NextResponse.json(
43+
{ error: 'Failed to update booking' },
44+
{ status: response.status }
45+
);
46+
}
4047
}
4148

4249
return NextResponse.json({ success: true });
@@ -87,4 +94,52 @@ export async function DELETE(
8794
{ status: 500 }
8895
);
8996
}
90-
}
97+
}
98+
99+
export async function GET(
100+
request: Request,
101+
{ params }: { params: Promise<{ id: string }> }
102+
) {
103+
try {
104+
const { id } = await params;
105+
const bookingApi = process.env.BOOKING_API || 'http://localhost:3000';
106+
107+
// Get the token from cookies
108+
const cookieStore = await cookies();
109+
const token = cookieStore.get('token');
110+
111+
if (!token) {
112+
return NextResponse.json(
113+
{ error: 'Authentication required' },
114+
{ status: 401 }
115+
);
116+
}
117+
118+
const response = await fetch(`${bookingApi}/booking/${id}`, {
119+
method: 'GET',
120+
headers: {
121+
'Accept': '*/*',
122+
'Cookie': `token=${token.value}`
123+
}
124+
});
125+
126+
if (!response.ok) {
127+
const errorData = await response.json().catch(() => ({
128+
error: `Failed to fetch booking: ${response.status}`
129+
}));
130+
return NextResponse.json(
131+
errorData,
132+
{ status: response.status }
133+
);
134+
}
135+
136+
const data = await response.json();
137+
return NextResponse.json(data);
138+
} catch (error) {
139+
console.error('Error fetching booking:', error);
140+
return NextResponse.json(
141+
{ error: 'Failed to fetch booking' },
142+
{ status: 500 }
143+
);
144+
}
145+
}

assets/src/app/api/booking/route.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,22 @@ export async function POST(request: Request) {
5555
});
5656

5757
if (!response.ok) {
58-
const errorData = await response.json();
59-
return NextResponse.json(
60-
{ errors: errorData.fieldErrors || ['Failed to create booking'] },
61-
{ status: response.status }
62-
);
58+
try {
59+
const errorData = await response.json();
60+
return NextResponse.json(
61+
{ errors: errorData.fieldErrors || ['Failed to create booking'] },
62+
{ status: response.status }
63+
);
64+
} catch (e) {
65+
return NextResponse.json(
66+
{ error: 'Failed to create booking' },
67+
{ status: response.status }
68+
);
69+
}
6370
}
6471

6572
const data = await response.json();
66-
return NextResponse.json(data.bookings || []);
73+
return NextResponse.json(data.booking || [], { status: response.status});
6774
} catch (error) {
6875
console.error('Error fetching bookings:', error);
6976
return NextResponse.json([], { status: 500 });
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { NextResponse } from 'next/server';
2+
import { cookies } from 'next/headers';
3+
4+
export async function GET(request: Request) {
5+
try {
6+
const { searchParams } = new URL(request.url);
7+
const roomid = searchParams.get('roomid');
8+
9+
const cookieStore = await cookies();
10+
const token = cookieStore.get('token');
11+
12+
if (!token) {
13+
return NextResponse.json(
14+
{ error: 'Authentication required' },
15+
{ status: 401 }
16+
);
17+
}
18+
19+
if (!roomid) {
20+
return NextResponse.json(
21+
{ error: 'Room ID is required' },
22+
{ status: 400 }
23+
);
24+
}
25+
26+
const bookingApi = process.env.BOOKING_API || 'http://localhost:3000';
27+
const response = await fetch(`${bookingApi}/booking/summary?roomid=${roomid}`, {
28+
headers: {
29+
'Cookie': `token=${token.value}`
30+
}
31+
});
32+
33+
if (!response.ok) {
34+
throw new Error(`Failed to fetch booking summary: ${response.status}`);
35+
}
36+
37+
const data = await response.json();
38+
return NextResponse.json(data || {});
39+
} catch (error) {
40+
console.error('Error fetching booking summary:', error);
41+
return NextResponse.json(
42+
{ error: 'Failed to fetch booking summary' },
43+
{ status: 500 }
44+
);
45+
}
46+
}

assets/src/app/api/message/count/route.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,7 @@ export async function GET(request: Request) {
2626
try {
2727
const data = JSON.parse(rawText);
2828
return NextResponse.json({
29-
count: data.count,
30-
debug: {
31-
rawResponse: rawText,
32-
headers: Object.fromEntries(response.headers),
33-
url: messageApi
34-
}
29+
count: data.count
3530
});
3631
} catch (e) {
3732
return NextResponse.json({

assets/src/components/admin/Nav.tsx

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,24 @@ const Nav: React.FC<NavProps> = ({ setAuthenticate, isAuthenticated }) => {
4343
}
4444
}, []);
4545

46-
const doLogout = () => {
47-
const cookies = new Cookies();
48-
cookies.remove('token', { path: '/' });
49-
setAuthenticate(false);
50-
window.location.href = '/';
46+
const doLogout = async () => {
47+
try {
48+
// Call the auth/logout endpoint
49+
await fetch('/api/auth/logout', {
50+
method: 'POST',
51+
headers: {
52+
'Content-Type': 'application/json'
53+
}
54+
});
55+
} catch (error) {
56+
console.error('Error logging out:', error);
57+
} finally {
58+
// Continue with local logout regardless of API success
59+
const cookies = new Cookies();
60+
cookies.remove('token', { path: '/' });
61+
setAuthenticate(false);
62+
window.location.href = '/';
63+
}
5164
};
5265

5366
const getNavLinkClass = (path: string) => {

0 commit comments

Comments
 (0)