Skip to content

Commit ea05074

Browse files
authored
Merge pull request #99 from design-sparx/feat/integrate-with-invoices-api
feat: added invoices apis
2 parents 070e221 + 0c08221 commit ea05074

File tree

13 files changed

+2332
-120
lines changed

13 files changed

+2332
-120
lines changed

.changeset/clean-dryers-enjoy.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"mantine-analytics-dashboard": patch
3+
---
4+
5+
feat: added invoices apis

app/api/invoices/[id]/route.ts

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
import { NextResponse } from 'next/server';
2+
3+
export async function GET(
4+
request: Request,
5+
{ params }: { params: { id: string } },
6+
) {
7+
try {
8+
// Get the authorization header from the incoming request
9+
const authHeader = request.headers.get('authorization');
10+
11+
const headers: HeadersInit = {
12+
'Content-Type': 'application/json',
13+
};
14+
15+
// Add the authorization header if it exists
16+
if (authHeader) {
17+
headers['Authorization'] = authHeader;
18+
}
19+
20+
const response = await fetch(
21+
`${process.env.NEXT_PUBLIC_API_URL}/api/invoices/${params.id}`,
22+
{
23+
method: 'GET',
24+
headers,
25+
},
26+
);
27+
28+
const data = await response.json();
29+
return NextResponse.json(data);
30+
} catch (error) {
31+
return NextResponse.json(
32+
{ error: 'Failed to fetch invoice' },
33+
{ status: 500 },
34+
);
35+
}
36+
}
37+
38+
export async function PUT(
39+
request: Request,
40+
{ params }: { params: { id: string } },
41+
) {
42+
try {
43+
const body = await request.json();
44+
45+
// Get the authorization header from the incoming request
46+
const authHeader = request.headers.get('authorization');
47+
48+
const headers: HeadersInit = {
49+
'Content-Type': 'application/json',
50+
};
51+
52+
// Add the authorization header if it exists
53+
if (authHeader) {
54+
headers['Authorization'] = authHeader;
55+
}
56+
57+
const response = await fetch(
58+
`${process.env.NEXT_PUBLIC_API_URL}/api/invoices/${params.id}`,
59+
{
60+
method: 'PUT',
61+
headers,
62+
body: JSON.stringify(body),
63+
},
64+
);
65+
66+
const data = await response.json();
67+
68+
if (!response.ok) {
69+
return NextResponse.json(
70+
{ error: 'Failed to update invoice', details: data },
71+
{ status: response.status },
72+
);
73+
}
74+
75+
return NextResponse.json(data);
76+
} catch (error) {
77+
return NextResponse.json(
78+
{ error: 'Failed to update invoice' },
79+
{ status: 500 },
80+
);
81+
}
82+
}
83+
84+
export async function DELETE(
85+
request: Request,
86+
{ params }: { params: { id: string } },
87+
) {
88+
try {
89+
// Get the authorization header from the incoming request
90+
const authHeader = request.headers.get('authorization');
91+
92+
const headers: HeadersInit = {
93+
'Content-Type': 'application/json',
94+
};
95+
96+
// Add the authorization header if it exists
97+
if (authHeader) {
98+
headers['Authorization'] = authHeader;
99+
}
100+
101+
const response = await fetch(
102+
`${process.env.NEXT_PUBLIC_API_URL}/api/invoices/${params.id}`,
103+
{
104+
method: 'DELETE',
105+
headers,
106+
},
107+
);
108+
109+
if (!response.ok) {
110+
const data = await response.json();
111+
return NextResponse.json(
112+
{ error: 'Failed to delete invoice', details: data },
113+
{ status: response.status },
114+
);
115+
}
116+
117+
return NextResponse.json(
118+
{ message: 'Invoice deleted successfully' },
119+
{ status: 200 },
120+
);
121+
} catch (error) {
122+
return NextResponse.json(
123+
{ error: 'Failed to delete invoice' },
124+
{ status: 500 },
125+
);
126+
}
127+
}

app/api/invoices/route.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import { NextResponse } from 'next/server';
2+
3+
export async function GET(request: Request) {
4+
try {
5+
// Get the authorization header from the incoming request
6+
const authHeader = request.headers.get('authorization');
7+
8+
const headers: HeadersInit = {
9+
'Content-Type': 'application/json',
10+
};
11+
12+
// Add the authorization header if it exists
13+
if (authHeader) {
14+
headers['Authorization'] = authHeader;
15+
}
16+
17+
const response = await fetch(
18+
process.env.NEXT_PUBLIC_API_URL + '/api/invoices',
19+
{
20+
method: 'GET',
21+
headers,
22+
},
23+
);
24+
25+
const data = await response.json();
26+
return NextResponse.json(data);
27+
} catch (error) {
28+
return NextResponse.json(
29+
{ error: 'Failed to fetch invoices' },
30+
{ status: 500 },
31+
);
32+
}
33+
}
34+
35+
export async function POST(request: Request) {
36+
try {
37+
const body = await request.json();
38+
39+
// Get the authorization header from the incoming request
40+
const authHeader = request.headers.get('authorization');
41+
42+
const headers: HeadersInit = {
43+
'Content-Type': 'application/json',
44+
};
45+
46+
// Add the authorization header if it exists
47+
if (authHeader) {
48+
headers['Authorization'] = authHeader;
49+
}
50+
51+
const response = await fetch(
52+
process.env.NEXT_PUBLIC_API_URL + '/api/invoices',
53+
{
54+
method: 'POST',
55+
headers,
56+
body: JSON.stringify(body),
57+
},
58+
);
59+
60+
const data = await response.json();
61+
62+
if (!response.ok) {
63+
return NextResponse.json(
64+
{ error: 'Failed to create invoice', details: data },
65+
{ status: response.status },
66+
);
67+
}
68+
69+
return NextResponse.json(data);
70+
} catch (error) {
71+
return NextResponse.json(
72+
{ error: 'Failed to create invoice' },
73+
{ status: 500 },
74+
);
75+
}
76+
}

0 commit comments

Comments
 (0)