diff --git a/labman/.gitignore b/labman/.gitignore
index 16bda08..cec7877 100644
--- a/labman/.gitignore
+++ b/labman/.gitignore
@@ -43,6 +43,8 @@ next-env.d.ts
/src/generated/prisma
+.idea
+
# Playwright
node_modules/
/test-results/
diff --git a/labman/.idea/.gitignore b/labman/.idea/.gitignore
deleted file mode 100644
index 7e5b7d7..0000000
--- a/labman/.idea/.gitignore
+++ /dev/null
@@ -1,8 +0,0 @@
-# Default ignored files
-/shelf/
-/workspace.xml
-# Editor-based HTTP Client requests
-/httpRequests/
-# Datasource local storage ignored files
-/dataSources/
-/dataSources.local.xml
diff --git a/labman/.idea/encodings.xml b/labman/.idea/encodings.xml
deleted file mode 100644
index df87cf9..0000000
--- a/labman/.idea/encodings.xml
+++ /dev/null
@@ -1,4 +0,0 @@
-
-
-
-
\ No newline at end of file
diff --git a/labman/.idea/labman.iml b/labman/.idea/labman.iml
deleted file mode 100644
index 24643cc..0000000
--- a/labman/.idea/labman.iml
+++ /dev/null
@@ -1,12 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/labman/.idea/material_theme_project_new.xml b/labman/.idea/material_theme_project_new.xml
deleted file mode 100644
index 52e0d89..0000000
--- a/labman/.idea/material_theme_project_new.xml
+++ /dev/null
@@ -1,12 +0,0 @@
-
-
-
-
-
-
\ No newline at end of file
diff --git a/labman/.idea/modules.xml b/labman/.idea/modules.xml
deleted file mode 100644
index 4c3f2bc..0000000
--- a/labman/.idea/modules.xml
+++ /dev/null
@@ -1,8 +0,0 @@
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/labman/.idea/vcs.xml b/labman/.idea/vcs.xml
deleted file mode 100644
index efccd08..0000000
--- a/labman/.idea/vcs.xml
+++ /dev/null
@@ -1,12 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/labman/src/app/api/equipment/route.ts b/labman/src/app/api/equipment/route.ts
index f69d561..f4e4948 100644
--- a/labman/src/app/api/equipment/route.ts
+++ b/labman/src/app/api/equipment/route.ts
@@ -1,53 +1,73 @@
import {NextResponse} from "next/server";
import prisma from "@/lib/prisma";
-export async function POST(req: Request) {
- const body = await req.json();
- const { name, category, image } = body;
+//TODO: Generally post reguests like this should be done in actions.ts. But there is little point in changing this right now as the backend might be moved later anyways.
- let categoryId = 0;
+export async function POST(req: Request) : Promise {
+ try {
+ const body = await req.json();
+ const {name, category, image} = body;
- let equipmentCategory = await prisma.equipmentCategory.findUnique({
- where: {
- name: category
- }
- })
-
- // Add equipment category if it doesn't exist'
- if (equipmentCategory) {
- console.log("Category exists");
- categoryId = equipmentCategory.id;
- } else {
- console.log("Category does not exist");
- equipmentCategory = await prisma.equipmentCategory.create({
- data: {
+ let categoryId = 0;
+
+ let equipmentCategory = await prisma.equipmentCategory.findUnique({
+ where: {
name: category
}
})
- categoryId = equipmentCategory.id;
- }
- // Add equipment to the database
- const newEquipment = await prisma.equipment.create({
- data: {
- name,
- image,
- categoryId,
- status: "Active",
- items: {
- create: {
- status: "Available"
+ // Add equipment category if it doesn't exist'
+ if (equipmentCategory) {
+ console.log("Category exists");
+ categoryId = equipmentCategory.id;
+ } else {
+ console.log("Category does not exist");
+ equipmentCategory = await prisma.equipmentCategory.create({
+ data: {
+ name: category
}
- }
- },
- include: {
- category: true,
- items: true
+ })
+ categoryId = equipmentCategory.id;
+ }
+
+ const existingEquipment = await prisma.equipment.findUnique({where: {name: name}});
+
+ if (existingEquipment) {
+ return NextResponse.json(
+ { type: "error", message: "Equipment already exists" },
+ { status: 409 }
+ );
}
- });
+ // Add equipment to the database
+ const newEquipment = await prisma.equipment.create({
+ data: {
+ name,
+ image,
+ categoryId,
+ status: "Active",
+ items: {
+ create: {
+ status: "Available"
+ }
+ }
+ },
+ include: {
+ category: true,
+ items: true
+ }
+ });
- return NextResponse.json(newEquipment);
+ return NextResponse.json(
+ {type: "success", data: newEquipment},
+ {status: 201}
+ )
+ } catch (error) {
+ return NextResponse.json(
+ {type: "error", message: "An error occurred while adding the equipment"},
+ {status: 500}
+ )
+ }
}
\ No newline at end of file
diff --git a/labman/src/components/inventory/EquipmentClient.tsx b/labman/src/components/inventory/EquipmentClient.tsx
index ab671fc..1a7eaad 100644
--- a/labman/src/components/inventory/EquipmentClient.tsx
+++ b/labman/src/components/inventory/EquipmentClient.tsx
@@ -80,14 +80,19 @@ export default function EquipmentClient({equipmentList}: EquipmentClientProps) {
})
})
// Adding the new equipment to the state
- const newEquipment = await res.json();
- console.log(newEquipment)
+ const result = await res.json();
- setAllEquipment(prev => [...prev, newEquipment]);
- setName("")
- setCategory("")
- setImage("")
+ if (result.type === "success") {
+ const newEquipment = result.data
+ setAllEquipment(prev => [...prev, newEquipment]);
+ setName("")
+ setCategory("")
+ setImage("")
+
+ } else {
+ alert(result.message || "Failed to add equipment")
+ }
}
async function handleDeleteEquipment(name: string) {
diff --git a/labman/src/components/inventory/EquipmentInfo.tsx b/labman/src/components/inventory/EquipmentInfo.tsx
index 6357405..2e3ee0d 100644
--- a/labman/src/components/inventory/EquipmentInfo.tsx
+++ b/labman/src/components/inventory/EquipmentInfo.tsx
@@ -89,7 +89,13 @@ export default function EquipmentInfo({equipmentData, setAllEquipment, setSelect
if (JSON.stringify(formData) === JSON.stringify(initialFormData)) return;
- const updatedEq = await updateEquipment(equipmentData!.id, formData.name!, formData.category!, formData.image!)
+ const res = await updateEquipment(equipmentData.id, formData.name, formData.category, formData.image!)
+ if (res.type !== "success") {
+ alert(res.message);
+ return;
+ }
+
+ const updatedEq = res.data;
const updatedEquipment = {
...equipmentData,
@@ -99,7 +105,7 @@ export default function EquipmentInfo({equipmentData, setAllEquipment, setSelect
name: updatedEq.category.name
},
image: updatedEq.image,
- categoryId: updatedEq.categoryId
+ categoryId: updatedEq.category.id
}
diff --git a/labman/src/lib/actions.ts b/labman/src/lib/actions.ts
index 9744946..82e6a12 100644
--- a/labman/src/lib/actions.ts
+++ b/labman/src/lib/actions.ts
@@ -4,6 +4,7 @@ import {revalidatePath} from "next/cache";
import {deleteSession, validateSessionToken} from "@/auth/session"
import {cookies} from "next/headers";
import {Borrower} from "@/generated/prisma";
+import {Equipment, EquipmentWithCategoryAndItems} from "@/types/inventory"
import {Loan as ExtendedLoan} from "@/types/Loan";
import {Loan} from "@/generated/prisma";
import {redirect} from "next/navigation";
@@ -90,7 +91,7 @@ export async function addUnit(equipmentName: string) {
return newUnit;
}
-export async function updateEquipment (equipmentId: number, name: string, category: string, image: string) {
+export async function updateEquipment (equipmentId: number, name: string, category: string, image: string) : Promise> {
let categoryId = 0;
let equipmentCategory = await prisma.equipmentCategory.findUnique({where: {name: category}})
@@ -102,13 +103,22 @@ export async function updateEquipment (equipmentId: number, name: string, catego
categoryId = equipmentCategory.id
}
+ const existingEquipment = await prisma.equipment.findUnique({where: {name: name}})
+
+ if (existingEquipment) {
+ return {type: "error", message: "Equipment already exists"}
+ }
+
const equipment = await prisma.equipment.update({
where: {id: equipmentId},
data : {name: name, categoryId: categoryId, image: image},
- include: {category: true}
+ include: {
+ category: true,
+ items: true
+ }
})
revalidatePath("/");
- return equipment;
+ return {type: "success", data: equipment};
}
export async function addBorrower(name: string, phone?: string | null, email?: string | null, borrowerId?: number) : Promise> {
diff --git a/labman/src/types/inventory.ts b/labman/src/types/inventory.ts
index a9619a7..cd90895 100644
--- a/labman/src/types/inventory.ts
+++ b/labman/src/types/inventory.ts
@@ -23,6 +23,27 @@ export type Equipment = {
}[]
}
+export type EquipmentWithCategoryAndItems = {
+ id: number;
+ name: string;
+ image: string | null;
+ category: {
+ id: number;
+ name: string;
+ }
+ createdAt: Date;
+ items: {
+ id: number;
+ equipmentId: number;
+ status: string;
+ createdAt: Date;
+ notes: string[];
+ errors: string[];
+ activeLoanId: number | null;
+
+ }[]
+}
+
export type Unit = {
id: number;
equipmentId: number;