Skip to content

Commit b1a4362

Browse files
committed
Merge remote-tracking branch 'upstream/main'
2 parents ab9d671 + 3cda816 commit b1a4362

File tree

14 files changed

+875
-451
lines changed

14 files changed

+875
-451
lines changed

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

Lines changed: 82 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,93 @@
1-
import { NextRequest, NextResponse } from "next/server";
21
import axios from "axios";
2+
import { NextRequest, NextResponse } from "next/server";
3+
4+
export async function PUT(
5+
req: NextRequest,
6+
context: { params: Promise<{ id: string }> },
7+
) {
8+
try {
9+
const token = req.cookies.get("token")?.value;
10+
if (!token) {
11+
return NextResponse.json(
12+
{ message: "Unauthorized. Please login to update a referral." },
13+
{ status: 401 },
14+
);
15+
}
16+
17+
const data = await req.json();
18+
const { id } = await context.params;
19+
20+
// Validate fields only if they are present
21+
if (data.jobDetails) {
22+
if (data.jobDetails.link) {
23+
const urlPattern = /^https?:\/\/\S+$/;
24+
if (!urlPattern.test(data.jobDetails.link)) {
25+
return NextResponse.json(
26+
{ message: "Please provide a valid URL for the job link" },
27+
{ status: 400 },
28+
);
29+
}
30+
}
31+
}
32+
33+
if (data.lastApplyDate) {
34+
const applyDate = new Date(data.lastApplyDate);
35+
const currentDate = new Date();
36+
if (applyDate <= currentDate) {
37+
return NextResponse.json(
38+
{ message: "Last apply date must be in the future" },
39+
{ status: 400 },
40+
);
41+
}
42+
}
43+
44+
if (data.numberOfReferrals !== undefined) {
45+
if (
46+
typeof data.numberOfReferrals !== "number" ||
47+
data.numberOfReferrals < 0
48+
) {
49+
return NextResponse.json(
50+
{ message: "Number of referrals must be a non-negative number" },
51+
{ status: 400 },
52+
);
53+
}
54+
}
55+
56+
const response = await axios.put(
57+
`${process.env.NEXT_PUBLIC_API_URL}/api/referrals/${id}`,
58+
data,
59+
{
60+
headers: {
61+
Authorization: `Bearer ${token}`,
62+
"Content-Type": "application/json",
63+
},
64+
},
65+
);
66+
67+
return NextResponse.json(response.data, { status: response.status });
68+
} catch (error: any) {
69+
console.error(
70+
"Error updating referral:",
71+
error?.response?.data || error.message,
72+
);
73+
74+
return NextResponse.json(
75+
{
76+
message: error.response?.data?.message || "Failed to update referral",
77+
error: error.message,
78+
},
79+
{ status: error.response?.status || 500 },
80+
);
81+
}
82+
}
383

484
export async function DELETE(
585
req: NextRequest,
6-
context: { params: Promise<{ id: string }> }, // Ensure params is awaited
86+
context: { params: Promise<{ id: string }> },
787
) {
888
try {
9-
// Await params before extracting id
1089
const { id } = await context.params;
1190

12-
// Extract token from cookies
1391
const token = req.cookies.get("token")?.value;
1492
if (!token) {
1593
return NextResponse.json(
@@ -18,7 +96,6 @@ export async function DELETE(
1896
);
1997
}
2098

21-
// Send request to backend API
2299
const response = await axios.delete(
23100
`${process.env.NEXT_PUBLIC_API_URL}/api/referrals/${id}`,
24101
{

app/api/referrals/filter/route.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@ import { NextRequest, NextResponse } from "next/server";
33

44
export async function GET(req: NextRequest) {
55
try {
6-
// Extract search parameters from request URL
76
const { searchParams } = new URL(req.url);
87
let month = searchParams.get("month");
98
let year = searchParams.get("year");
109

11-
// Validate month (should be between 1-12)
1210
if (
1311
month &&
1412
(!/^\d+$/.test(month) || Number(month) < 1 || Number(month) > 12)
@@ -19,15 +17,13 @@ export async function GET(req: NextRequest) {
1917
);
2018
}
2119

22-
// Validate year (should be a valid 4-digit number)
23-
if (year && (!/^\d{4}$/.test(year) || Number(year) < 2000)) {
20+
if (year && !/^\d{4}$/.test(year)) {
2421
return NextResponse.json(
2522
{ message: "Invalid year value." },
2623
{ status: 400 },
2724
);
2825
}
2926

30-
// Extract token from cookies
3127
const token = req.cookies.get("token")?.value;
3228
if (!token) {
3329
return NextResponse.json(
@@ -36,7 +32,6 @@ export async function GET(req: NextRequest) {
3632
);
3733
}
3834

39-
// Construct API URL
4035
const baseUrl = `${process.env.NEXT_PUBLIC_API_URL}/api/referrals/filter`;
4136
const queryParams = new URLSearchParams();
4237

@@ -45,7 +40,6 @@ export async function GET(req: NextRequest) {
4540

4641
const apiUrl = `${baseUrl}?${queryParams.toString()}`;
4742

48-
// Fetch referrals from backend
4943
const response = await axios.get(apiUrl, {
5044
headers: { Authorization: `Bearer ${token}` },
5145
});

app/api/referrals/route.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
1-
import { NextRequest, NextResponse } from "next/server";
21
import axios from "axios";
2+
import { NextRequest, NextResponse } from "next/server";
33

44
export async function POST(req: NextRequest) {
55
try {
6-
// Extract token from cookies
76
const token = req.cookies.get("token")?.value;
87
if (!token) {
98
return NextResponse.json(
109
{ message: "Unauthorized. Please login to create a referral." },
1110
{ status: 401 },
1211
);
1312
}
14-
// Parse request body
13+
1514
const data = await req.json();
1615

17-
// Validate required fields
1816
const { jobDetails, lastApplyDate, numberOfReferrals } = data;
1917

2018
if (
@@ -32,13 +30,14 @@ export async function POST(req: NextRequest) {
3230
{ status: 400 },
3331
);
3432
}
33+
3534
if (typeof numberOfReferrals !== "number" || numberOfReferrals < 0) {
3635
return NextResponse.json(
3736
{ message: "Number of referrals must be a non-negative number" },
3837
{ status: 400 },
3938
);
4039
}
41-
// URL validation
40+
4241
const urlPattern = /^https?:\/\/\S+$/;
4342
if (!urlPattern.test(jobDetails.link)) {
4443
return NextResponse.json(
@@ -47,7 +46,6 @@ export async function POST(req: NextRequest) {
4746
);
4847
}
4948

50-
// Date validation
5149
const applyDate = new Date(lastApplyDate);
5250
const currentDate = new Date();
5351
if (applyDate <= currentDate) {
@@ -57,7 +55,6 @@ export async function POST(req: NextRequest) {
5755
);
5856
}
5957

60-
// Send data to backend API
6158
const response = await axios.post(
6259
`${process.env.NEXT_PUBLIC_API_URL}/api/referrals`,
6360
data,

app/api/referrals/user/[userId]/route.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
import { NextRequest, NextResponse } from "next/server";
21
import axios from "axios";
2+
import { NextRequest, NextResponse } from "next/server";
33

44
export async function GET(
55
req: NextRequest,
6-
context: { params: Promise<{ userId: string }> }, // Ensure params is awaited
6+
context: { params: Promise<{ userId: string }> },
77
) {
88
try {
9-
// Await params before extracting userId
109
const { userId } = await context.params;
1110

12-
// Extract token from cookies
1311
const token = req.cookies.get("token")?.value;
1412
if (!token) {
1513
return NextResponse.json(
@@ -18,7 +16,6 @@ export async function GET(
1816
);
1917
}
2018

21-
// Send request to backend API
2219
const response = await axios.get(
2320
`${process.env.NEXT_PUBLIC_API_URL}/api/referrals/user/${userId}`,
2421
{

components/JobsPage/JobsCard.tsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,15 +94,12 @@ export default function JobsCard({ job }: { job: Job }) {
9494
<strong>Posted By:</strong>{" "}
9595
<Link
9696
href={`/profile/${job.postedBy.id}`}
97-
className="flex items-center"
97+
className="flex items-center ml-2 text-blue-500 hover:text-blue-600"
9898
>
9999
<span>
100100
{job.postedBy.name} ({job.postedBy.collegeEmail})
101101
</span>
102-
<ExternalLink
103-
size={20}
104-
className="inline-block ml-2 text-blue-500"
105-
/>
102+
<ExternalLink size={20} className="ml-1" />
106103
</Link>
107104
</div>
108105
</div>

0 commit comments

Comments
 (0)