Skip to content

Commit 41ec889

Browse files
committed
fix: big bugs
1 parent b56d1b0 commit 41ec889

File tree

5 files changed

+97
-56
lines changed

5 files changed

+97
-56
lines changed

models/requesttype.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
import mongoose, { Schema } from "mongoose";
22

33
const requesttypeSchema = new Schema({
4-
projectid: {
5-
type: String,
6-
ref: "Project"
7-
},
8-
status: { type: String },
4+
projectid: { type: String, required: true },
5+
status: { type: String, required: true },
96
});
107

118
const Requesttype =
12-
mongoose.models.Requesttype || mongoose.model("Requesttype", requesttypeSchema);
9+
mongoose.models.Requesttype ||
10+
mongoose.model("Requesttype", requesttypeSchema);
1311
export default Requesttype;

src/app/api/request/route.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { connectMongoDB } from "../../../../lib/mongodb";
2-
import Request from "../../../../models/requesttype";
2+
import Request from "../../../../models/request";
33
import { NextResponse } from "next/server";
44

55
export async function GET(req) {

src/app/api/requesttype/route.js

Lines changed: 45 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,33 @@ export async function GET(req) {
1717
}
1818

1919
export async function POST(req) {
20-
// Connect to MongoDB
21-
await connectMongoDB();
20+
try {
21+
await connectMongoDB();
2222

23-
const url = new URL(req.url);
24-
const projectid = url.searchParams.get("projectid");
25-
26-
const requesttype = await Requesttype({
27-
projectid,
28-
status: "created",
29-
}).save();
23+
// Parse the JSON body from the request
24+
const body = await req.json();
3025

31-
return NextResponse.json(
32-
{ message: "Successfully added requests", requests: requesttype },
33-
{ status: 201 }
34-
);
26+
// Validate required fields
27+
if (!body.projectid) throw new Error("Missing projectid");
28+
29+
// Set default status if not provided
30+
const requestData = {
31+
projectid: body.projectid,
32+
status: body.status,
33+
};
34+
35+
// Save to database
36+
const requesttype = new Requesttype(requestData);
37+
const saveRequestType = await requesttype.save();
38+
39+
return NextResponse.json({ saveRequestType }, { status: 200 });
40+
} catch (error) {
41+
console.error("Error creating request:", error);
42+
return NextResponse.json(
43+
{ message: "Error creating request", error: error.message },
44+
{ status: 500 }
45+
);
46+
}
3547
}
3648

3749
export async function PUT(req) {
@@ -43,25 +55,26 @@ export async function PUT(req) {
4355

4456
// Update requesttype or create if it doesn't exist
4557
const updatedRequestType = await Requesttype.findOneAndUpdate(
46-
{ projectid },
47-
{ status },
48-
{ new: true, upsert: true }
49-
);
50-
51-
return NextResponse.json(
52-
{ message: "Successfully updated request type", requesttype: updatedRequestType },
53-
{ status: 200 }
54-
);
55-
} catch (error) {
56-
console.error("Error updating request type:", error);
57-
return NextResponse.json(
58-
{ message: "Internal Server Error", error: error.message },
59-
{ status: 500 }
60-
);
61-
}
62-
}
58+
{ projectid },
59+
{ status },
60+
{ new: true, upsert: true }
61+
);
6362

64-
63+
return NextResponse.json(
64+
{
65+
message: "Successfully updated request type",
66+
requesttype: updatedRequestType,
67+
},
68+
{ status: 200 }
69+
);
70+
} catch (error) {
71+
console.error("Error updating request type:", error);
72+
return NextResponse.json(
73+
{ message: "Internal Server Error", error: error.message },
74+
{ status: 500 }
75+
);
76+
}
77+
}
6578

6679
export async function DELETE(req) {
6780
await connectMongoDB();
@@ -81,4 +94,4 @@ export async function DELETE(req) {
8194
{ message: "Request type deleted successfully" },
8295
{ status: 200 }
8396
);
84-
}
97+
}

src/app/page.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export default function HomePage() {
2727

2828
const data = await res.json();
2929
setNotificationList(data);
30-
// console.log("Notifications:", data);
30+
console.log("Notifications:", data);
3131
return data;
3232
} catch (error) {
3333
console.error("Error fetching notifications:", error);
@@ -128,7 +128,12 @@ export default function HomePage() {
128128

129129
{/* Footer Section */}
130130
<div className="flex justify-between items-center mt-2 text-xs text-gray-500">
131-
<p>Date: {new Date().toLocaleDateString()}</p>
131+
<p>
132+
Date:{" "}
133+
{new Date(notification.date).toLocaleDateString(
134+
"en-US"
135+
)}
136+
</p>
132137
<div className="flex items-center gap-2">
133138
<Image
134139
src={bell}

src/app/requestresource/page.js

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ export default function RequestResource() {
2929
throw new Error(`Project fetch failed: ${projectRes.statusText}`);
3030

3131
const projectData = await projectRes.json();
32-
console.log("Fetched project:", projectData);
32+
// console.log("Fetched project:", projectData);
3333

34-
console.log("project length", projectData.length);
34+
// console.log("project length", projectData.length);
3535
if (projectData.length > 0) {
3636
const projectRequest = projectData[0]._id;
37-
console.log("Project ID:", projectRequest);
37+
// console.log("Project ID:", projectRequest);
3838

3939
const resourceRes = await fetch(
4040
`/api/resource?projectRequest=${projectRequest}`
@@ -43,7 +43,7 @@ export default function RequestResource() {
4343
throw new Error(`Resource fetch failed: ${resourceRes.statusText}`);
4444

4545
const resourceData = await resourceRes.json();
46-
console.log("Fetched resources:", resourceData);
46+
// console.log("Fetched resources:", resourceData);
4747

4848
setTableRowsCR(
4949
resourceData.map((element) => ({
@@ -72,6 +72,13 @@ export default function RequestResource() {
7272
toast.success("Request sent successfully");
7373

7474
try {
75+
console.log("TABLE_ROWS_CR before request:", TABLE_ROWS_CR);
76+
77+
if (!TABLE_ROWS_CR || TABLE_ROWS_CR.length === 0) {
78+
throw new Error("TABLE_ROWS_CR is empty or undefined.");
79+
}
80+
81+
// Send request to /api/request
7582
const resRequest = await fetch("/api/request", {
7683
method: "POST",
7784
headers: {
@@ -80,27 +87,45 @@ export default function RequestResource() {
8087
body: JSON.stringify(TABLE_ROWS_CR),
8188
});
8289

83-
const resRequesttype = await fetch(`/api/requesttype?projectid=${projectid}&status=created`, {
90+
if (!resRequest.ok) {
91+
const errorMessage = await resRequest.text();
92+
throw new Error(
93+
`Request API failed: ${resRequest.status} - ${errorMessage}`
94+
);
95+
}
96+
} catch (error) {
97+
console.error("Error while saving request:", error.message);
98+
}
99+
100+
try {
101+
// Prepare JSON data for /api/requesttype
102+
const requestTypeData = {
103+
projectid: TABLE_ROWS_CR[0].projectid,
104+
status: "created",
105+
};
106+
107+
// Send request to /api/requesttype with JSON body
108+
const resRequesttype = await fetch("/api/requesttype", {
84109
method: "POST",
85110
headers: {
86111
"Content-Type": "application/json",
87112
},
88-
113+
body: JSON.stringify(requestTypeData),
89114
});
90-
91-
92-
if (!resRequest.ok) {
93-
throw new Error(`Error: ${resRequest.status} - ${resRequest.statusText}`);
94-
} else {
95-
await fetchResources(); // Refetch resources after request
96115

97-
router.push("/projectlist");
116+
if (!resRequesttype.ok) {
117+
const errorMessage = await resRequesttype.text();
118+
throw new Error(
119+
`RequestType API failed: ${resRequesttype.status} - ${errorMessage}`
120+
);
98121
}
122+
123+
await fetchResources(); // Refetch resources after request
124+
router.push("/projectlist");
99125
} catch (error) {
100-
console.log("Error while saving request:", error.message);
126+
console.error("Error while saving request:", error.message);
101127
}
102128
};
103-
104129
return (
105130
<div>
106131
<h1 className="text-5xl font-bold mx-16 my-5">{projectName}</h1>

0 commit comments

Comments
 (0)